Skip to content

Simplify expressions passed to table functions #16388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions datafusion/core/src/execution/session_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,13 @@ impl ContextProvider for SessionContextProvider<'_> {
.get(name)
.cloned()
.ok_or_else(|| plan_datafusion_err!("table function '{name}' not found"))?;
let dummy_schema = DFSchema::empty();
Copy link
Contributor

@jonathanc-n jonathanc-n Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we eventually want to reference existing columns

For example:
SELECT * FROM t, range(t.id, t.id + 5);

I think the solution to this instead would be to have the a ExprSimplifier optimizer rule where it can reference the column values from the LATERAL statement. @Lordworms WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree long term it would be great to have table functions support referencing other relations.

However, we would need to update the API more than just this -- right now TableFunctions have no way of getting inputs (they return TableProviders)

We would have to work out what the API would look like if they got an input (perhaps they get an input ExecutonPlan)

As you mention, we would also probably have to figure out planning for LATERALs, etc

let simplifier =
ExprSimplifier::new(SessionSimplifyProvider::new(self.state, &dummy_schema));
let args = args
.into_iter()
.map(|arg| simplifier.simplify(arg))
.collect::<datafusion_common::Result<Vec<_>>>()?;
let provider = tbl_func.create_table_provider(&args)?;

Ok(provider_as_source(provider))
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sqllogictest/test_files/table_functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ SELECT * FROM range(3, 6)
4
5

query I rowsort
SELECT * FROM range(1, 1+2)
----
1
2

# #generated_data > batch_size
query I
SELECT count(v1) FROM range(-66666,66666) t1(v1)
Expand Down