Skip to content

Commit a6989d5

Browse files
committed
move setting of recursive ctes to slt file and add test to ensure multiple record batches are produced each iteration
1 parent eb1b181 commit a6989d5

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

datafusion/sqllogictest/src/test_context.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ impl TestContext {
7070
// hardcode target partitions so plans are deterministic
7171
.with_target_partitions(4);
7272

73-
// for all tests except information_schema.slt, enable recursive CTEs
74-
config.options_mut().execution.enable_recursive_ctes = relative_path
75-
.file_name()
76-
.map(|s| s == "cte.slt")
77-
.unwrap_or(false);
78-
7973
let mut test_ctx = TestContext::new(SessionContext::new_with_config(config));
8074

8175
let file_name = relative_path.file_name().unwrap().to_str().unwrap();

datafusion/sqllogictest/test_files/cte.slt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ select * from (WITH source AS (select 1 as e) SELECT * FROM source) t1, (WITH
2020
----
2121
1 1
2222

23+
# enable recursive CTEs
24+
statement ok
25+
set datafusion.execution.enable_recursive_ctes = true;
26+
2327
# trivial recursive CTE works
2428
query I rowsort
2529
WITH RECURSIVE nodes AS (
@@ -81,7 +85,48 @@ CREATE EXTERNAL TABLE balance STORED as CSV WITH HEADER ROW LOCATION '../../test
8185
statement ok
8286
CREATE EXTERNAL TABLE growth STORED as CSV WITH HEADER ROW LOCATION '../../testing/data/csv/r_cte_growth.csv'
8387

88+
# setup
89+
statement ok
90+
set datafusion.execution.batch_size = 2;
91+
92+
# recursive CTE with static term derived from table works.
93+
# use explain to ensure that batch size is set to 2. This should produce multiple batches per iteration since the input
94+
# table 'balances' has 4 rows
95+
query TT
96+
EXPLAIN WITH RECURSIVE balances AS (
97+
SELECT * from balance
98+
UNION ALL
99+
SELECT time + 1 as time, name, account_balance + 10 as account_balance
100+
FROM balances
101+
WHERE time < 10
102+
)
103+
SELECT * FROM balances
104+
ORDER BY time, name, account_balance
105+
----
106+
logical_plan
107+
Sort: balances.time ASC NULLS LAST, balances.name ASC NULLS LAST, balances.account_balance ASC NULLS LAST
108+
--Projection: balances.time, balances.name, balances.account_balance
109+
----SubqueryAlias: balances
110+
------RecursiveQuery: is_distinct=false
111+
--------Projection: balance.time, balance.name, balance.account_balance
112+
----------TableScan: balance
113+
--------Projection: balances.time + Int64(1) AS time, balances.name, balances.account_balance + Int64(10) AS account_balance
114+
----------Filter: balances.time < Int64(10)
115+
------------TableScan: balances
116+
physical_plan
117+
SortExec: expr=[time@0 ASC NULLS LAST,name@1 ASC NULLS LAST,account_balance@2 ASC NULLS LAST]
118+
--RecursiveQueryExec: is_distinct=false
119+
----CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/r_cte_balance.csv]]}, projection=[time, name, account_balance], has_header=true
120+
----CoalescePartitionsExec
121+
------ProjectionExec: expr=[time@0 + 1 as time, name@1 as name, account_balance@2 + 10 as account_balance]
122+
--------CoalesceBatchesExec: target_batch_size=2
123+
----------FilterExec: time@0 < 10
124+
------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
125+
--------------WorkTableExec: name=balances
126+
84127
# recursive CTE with static term derived from table works
128+
# note that this is run with batch size set to 2. This should produce multiple batches per iteration since the input
129+
# table 'balances' has 4 rows
85130
query ITI
86131
WITH RECURSIVE balances AS (
87132
SELECT * from balance
@@ -132,6 +177,9 @@ ORDER BY time, name, account_balance
132177
10 Tim 290
133178
10 Tim 480
134179

180+
# reset batch size to default
181+
statement ok
182+
set datafusion.execution.batch_size = 8182;
135183

136184
# recursive CTE with recursive join works
137185
query ITI

0 commit comments

Comments
 (0)