Skip to content

Commit 649a36f

Browse files
authored
Allow usage of table functions in relations (#16571)
* Allow usage of table funstions in relations * Rebase
1 parent ebf49b4 commit 649a36f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

datafusion/sql/src/relation/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,35 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
154154
"UNNEST table factor with offset is not supported yet"
155155
);
156156
}
157+
TableFactor::Function {
158+
name, args, alias, ..
159+
} => {
160+
let tbl_func_ref = self.object_name_to_table_reference(name)?;
161+
let schema = planner_context
162+
.outer_query_schema()
163+
.cloned()
164+
.unwrap_or_else(DFSchema::empty);
165+
let func_args = args
166+
.into_iter()
167+
.map(|arg| match arg {
168+
FunctionArg::Unnamed(FunctionArgExpr::Expr(expr))
169+
| FunctionArg::Named {
170+
arg: FunctionArgExpr::Expr(expr),
171+
..
172+
} => {
173+
self.sql_expr_to_logical_expr(expr, &schema, planner_context)
174+
}
175+
_ => plan_err!("Unsupported function argument: {arg:?}"),
176+
})
177+
.collect::<Result<Vec<Expr>>>()?;
178+
let provider = self
179+
.context_provider
180+
.get_table_function_source(tbl_func_ref.table(), func_args)?;
181+
let plan =
182+
LogicalPlanBuilder::scan(tbl_func_ref.table(), provider, None)?
183+
.build()?;
184+
(plan, alias)
185+
}
157186
// @todo Support TableFactory::TableFunction?
158187
_ => {
159188
return not_impl_err!(

datafusion/sqllogictest/test_files/table_functions.slt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,15 @@ SELECT * FROM range(DATE '2023-01-01', DATE '2023-01-02', INTERVAL '-1' DAY)
482482

483483
query error DataFusion error: Error during planning: range function with dates requires exactly 3 arguments
484484
SELECT * FROM range(DATE '2023-01-01', DATE '2023-01-03')
485+
486+
# Table function as relation
487+
statement ok
488+
CREATE OR REPLACE TABLE json_table (c INT) AS VALUES (1), (2);
489+
490+
query II
491+
SELECT c, f.* FROM json_table, LATERAL generate_series(1,2) f;
492+
----
493+
1 1
494+
1 2
495+
2 1
496+
2 2

0 commit comments

Comments
 (0)