Skip to content

Commit

Permalink
more recursive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgapp committed Sep 25, 2023
1 parent d5b50c0 commit f5d7eba
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions datafusion/sqllogictest/test_files/cte.slt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ select * from (WITH source AS (select 1 as e) SELECT * FROM source) t1, (WITH
----
1 1

# trivial recursive CTE works
query I rowsort
WITH RECURSIVE nodes AS (
SELECT 1 as id
Expand All @@ -40,3 +41,80 @@ SELECT * FROM nodes
7
8
9

# setup
statement ok
CREATE EXTERNAL TABLE beg_account_balance STORED as CSV WITH HEADER ROW LOCATION '../../testing/data/csv/recursive_query_account_beg_2.csv'

# setup
statement ok
CREATE EXTERNAL TABLE account_balance_growth STORED as CSV WITH HEADER ROW LOCATION '../../testing/data/csv/recursive_query_account_growth_2.csv'

# recursive CTE with static term derived from table works
query ITI rowsort
WITH RECURSIVE balances AS (
SELECT * from beg_account_balance
UNION ALL
SELECT time + 1 as time, name, account_balance + 10 as account_balance
FROM balances
WHERE time < 10
)
SELECT * FROM balances
----
1 John 100
1 Tim 200
10 John 190
10 Tim 290
2 John 110
2 Tim 210
3 John 120
3 Tim 220
4 John 130
4 Tim 230
5 John 140
5 Tim 240
6 John 150
6 Tim 250
7 John 160
7 Tim 260
8 John 170
8 Tim 270
9 John 180
9 Tim 280


# recursive CTE with recursive join works
query ITI
WITH RECURSIVE balances AS (
SELECT time as time, name as name, account_balance as account_balance
FROM beg_account_balance
UNION ALL
SELECT time + 1 as time, balances.name, account_balance + account_balance_growth.account_growth as account_balance
FROM balances
JOIN account_balance_growth
ON balances.name = account_balance_growth.name
WHERE time < 10
)
SELECT * FROM balances
ORDER BY time, name
----
1 John 100
1 Tim 200
2 John 103
2 Tim 220
3 John 106
3 Tim 240
4 John 109
4 Tim 260
5 John 112
5 Tim 280
6 John 115
6 Tim 300
7 John 118
7 Tim 320
8 John 121
8 Tim 340
9 John 124
9 Tim 360
10 John 127
10 Tim 380

0 comments on commit f5d7eba

Please sign in to comment.