You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "lorenarosati (via GitHub)" <gi...@apache.org> on 2025/07/22 19:15:25 UTC
[PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
lorenarosati opened a new pull request, #16857:
URL: https://github.com/apache/datafusion/pull/16857
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- Closes #16363.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
Virtual tables in Substrait can have either literal values or expressions, and currently, the Substrait consumer only handles the case when values are provided ([values is a deprecated field](https://github.com/substrait-io/substrait/blob/793c64ba26e337c22f5e91b658be58b1eea7efd3/proto/substrait/algebra.proto#L152)). If there are no values provided, an empty relation is returned, even if the virtual table has expressions specified.
If expressions are provided as elements in a virtual table (rather than literal values), from_read_rel() should return a LogicalPlan with the virtual table elements rather than an empty relation.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
- Only returns an empty relation if there are no values _and_ no expressions specified.
- If expressions is non-empty, we iterate through the records and consume each expression; if expressions is empty, we fallback to iterate through the records and consume each literal.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
These changes will allow users to specify expressions in Substrait virtual tables, and get a result from their query.
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#issuecomment-3113855760
I will plan to merge tomorrow so there is some additional time for other reviewers if they want
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "lorenarosati (via GitHub)" <gi...@apache.org>.
lorenarosati commented on code in PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#discussion_r2225787214
##########
datafusion/substrait/src/logical_plan/consumer/rel/read_rel.rs:
##########
@@ -114,14 +114,37 @@ pub async fn from_read_rel(
.await
}
Some(ReadType::VirtualTable(vt)) => {
- if vt.values.is_empty() {
+ if vt.values.is_empty() && vt.expressions.is_empty() {
return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: DFSchemaRef::new(substrait_schema),
}));
}
- let values = vt
+ let values = if vt.values.is_empty() {
Review Comment:
Made this change!
##########
datafusion/substrait/tests/testdata/test_plans/select_count_from_select_1_virtual_table_expressions.substrait.json:
##########
@@ -0,0 +1,94 @@
+{
Review Comment:
Made this change!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #16857:
URL: https://github.com/apache/datafusion/pull/16857
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#issuecomment-3120083386
Thanks @lorenarosati and @vbarua
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "vbarua (via GitHub)" <gi...@apache.org>.
vbarua commented on PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#issuecomment-3113949635
> I will plan to merge tomorrow so there is some additional time for other reviewers if they want
Sounds good. I actually just though of something to update in this PR as well 😅
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "lorenarosati (via GitHub)" <gi...@apache.org>.
lorenarosati commented on code in PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#discussion_r2225788037
##########
datafusion/substrait/tests/testdata/test_plans/select_count_from_select_1_virtual_table_expressions.substrait.json:
##########
@@ -0,0 +1,94 @@
+{
+ "extensionUris": [
+ {
+ "uri": "https://github.com/substrait-io/substrait/blob/main/extensions/functions_aggregate_generic.yaml"
+ }
+ ],
+ "extensions": [
+ {
+ "extensionFunction": {
+ "functionAnchor": 185,
+ "name": "count:any"
+ }
+ }
+ ],
+ "relations": [
+ {
+ "root": {
+ "input": {
+ "aggregate": {
+ "common": {
+ "direct": {
+ }
+ },
+ "input": {
+ "read": {
+ "common": {
+ "direct": {
+ }
+ },
+ "baseSchema": {
+ "names": [
+ "dummy"
+ ],
+ "struct": {
+ "types": [
+ {
+ "i64": {
+ "nullability": "NULLABILITY_REQUIRED"
+ }
+ }
+ ],
+ "nullability": "NULLABILITY_REQUIRED"
+ }
+ },
+ "virtualTable": {
+ "expressions": [
+ {
+ "fields": [
+ {
+ "literal": {
+ "i64": "0",
+ "nullable": false
+ }
+ }
+ ]
+ }
Review Comment:
Made this change!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "lorenarosati (via GitHub)" <gi...@apache.org>.
lorenarosati commented on code in PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#discussion_r2229221353
##########
datafusion/substrait/src/logical_plan/consumer/rel/read_rel.rs:
##########
@@ -114,14 +114,37 @@ pub async fn from_read_rel(
.await
}
Some(ReadType::VirtualTable(vt)) => {
- if vt.values.is_empty() {
+ if vt.values.is_empty() && vt.expressions.is_empty() {
return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: DFSchemaRef::new(substrait_schema),
}));
}
- let values = vt
+ let values = if !vt.expressions.is_empty() {
+ let mut exprs = vec![];
+ for row in &vt.expressions {
+ let mut name_idx = 0;
+ let mut row_exprs = vec![];
+ for expression in &row.fields {
+ name_idx += 1;
+ let expr = consumer
+ .consume_expression(expression, &substrait_schema)
Review Comment:
Made this change!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "vbarua (via GitHub)" <gi...@apache.org>.
vbarua commented on code in PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#discussion_r2228878495
##########
datafusion/substrait/src/logical_plan/consumer/rel/read_rel.rs:
##########
@@ -114,14 +114,37 @@ pub async fn from_read_rel(
.await
}
Some(ReadType::VirtualTable(vt)) => {
- if vt.values.is_empty() {
+ if vt.values.is_empty() && vt.expressions.is_empty() {
return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: DFSchemaRef::new(substrait_schema),
}));
}
- let values = vt
+ let values = if !vt.expressions.is_empty() {
+ let mut exprs = vec![];
+ for row in &vt.expressions {
+ let mut name_idx = 0;
+ let mut row_exprs = vec![];
+ for expression in &row.fields {
+ name_idx += 1;
+ let expr = consumer
+ .consume_expression(expression, &substrait_schema)
Review Comment:
Actually, we should be using an empty schema here. The `substrait_schema` we have access to here is the schema of the ReadRel. The expressions in the VirtualTable cannot reference that schema, or any schema at all.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Ensure Substrait consumer can handle expressions in VirtualTable [datafusion]
Posted by "vbarua (via GitHub)" <gi...@apache.org>.
vbarua commented on code in PR #16857:
URL: https://github.com/apache/datafusion/pull/16857#discussion_r2223862112
##########
datafusion/substrait/tests/testdata/test_plans/select_count_from_select_1_virtual_table_expressions.substrait.json:
##########
@@ -0,0 +1,94 @@
+{
Review Comment:
minor: I would suggest we update the name to something like `virtual_table_with_expressions.substrait.json` which more accurately reflects what this plan is intended to verify.
##########
datafusion/substrait/src/logical_plan/consumer/rel/read_rel.rs:
##########
@@ -114,14 +114,37 @@ pub async fn from_read_rel(
.await
}
Some(ReadType::VirtualTable(vt)) => {
- if vt.values.is_empty() {
+ if vt.values.is_empty() && vt.expressions.is_empty() {
return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: DFSchemaRef::new(substrait_schema),
}));
}
- let values = vt
+ let values = if vt.values.is_empty() {
+ let mut exprs = vec![];
+ for row in &vt.expressions {
+ let mut name_idx = 0;
+ let mut row_exprs = vec![];
+ for expression in &row.fields {
+ name_idx += 1;
+ let expr = consumer
+ .consume_expression(expression, &substrait_schema)
+ .await?;
+ row_exprs.push(expr);
+ }
+ if name_idx != named_struct.names.len() {
+ return substrait_err!(
+ "Names list must match exactly to nested schema, but found {} uses for {} names",
+ name_idx,
+ named_struct.names.len()
+ );
+ }
+ exprs.push(row_exprs);
+ }
+ exprs
Review Comment:
This does introduce a bit of duplication with the block below, but given that `consume_expression` is async and `from_substrait_literal` is not it would be a bit tricky to unify the processing. In the long-term Substrait drop support for the `values` field which is deprecated so we'll be able to remove the lower block entirely.
##########
datafusion/substrait/tests/testdata/test_plans/select_count_from_select_1_virtual_table_expressions.substrait.json:
##########
@@ -0,0 +1,94 @@
+{
+ "extensionUris": [
+ {
+ "uri": "https://github.com/substrait-io/substrait/blob/main/extensions/functions_aggregate_generic.yaml"
+ }
+ ],
+ "extensions": [
+ {
+ "extensionFunction": {
+ "functionAnchor": 185,
+ "name": "count:any"
+ }
+ }
+ ],
+ "relations": [
+ {
+ "root": {
+ "input": {
+ "aggregate": {
+ "common": {
+ "direct": {
+ }
+ },
+ "input": {
+ "read": {
+ "common": {
+ "direct": {
+ }
+ },
+ "baseSchema": {
+ "names": [
+ "dummy"
+ ],
+ "struct": {
+ "types": [
+ {
+ "i64": {
+ "nullability": "NULLABILITY_REQUIRED"
+ }
+ }
+ ],
+ "nullability": "NULLABILITY_REQUIRED"
+ }
+ },
+ "virtualTable": {
+ "expressions": [
+ {
+ "fields": [
+ {
+ "literal": {
+ "i64": "0",
+ "nullable": false
+ }
+ }
+ ]
+ }
Review Comment:
To make this a slighly more robust test, I would suggest:
* Using a struct with more than 1 field to verify the name processing is applied correctly.
* Having more than 1 row to check that multiple rows can be processed.
##########
datafusion/substrait/src/logical_plan/consumer/rel/read_rel.rs:
##########
@@ -114,14 +114,37 @@ pub async fn from_read_rel(
.await
}
Some(ReadType::VirtualTable(vt)) => {
- if vt.values.is_empty() {
+ if vt.values.is_empty() && vt.expressions.is_empty() {
return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: DFSchemaRef::new(substrait_schema),
}));
}
- let values = vt
+ let values = if vt.values.is_empty() {
Review Comment:
minor: I would suggest checking `!vt.expressions.is_empty()` to make it clearer that `expressions` are preferred over values. Effectively, it's the difference between "We are processing expressions because there are no values" as opposed to "We are processing expressions because they are present".
##########
datafusion/substrait/tests/testdata/test_plans/select_count_from_select_1_virtual_table_expressions.substrait.json:
##########
@@ -0,0 +1,94 @@
+{
+ "extensionUris": [
+ {
+ "uri": "https://github.com/substrait-io/substrait/blob/main/extensions/functions_aggregate_generic.yaml"
+ }
+ ],
+ "extensions": [
+ {
+ "extensionFunction": {
+ "functionAnchor": 185,
+ "name": "count:any"
+ }
+ }
+ ],
+ "relations": [
+ {
+ "root": {
+ "input": {
+ "aggregate": {
+ "common": {
+ "direct": {
+ }
+ },
+ "input": {
+ "read": {
Review Comment:
minor: I would suggest of getting rid of everything other than the `read` in this plan, to make it clearer that this is a test of virtual table functionality. The aggregation in this plan is effectively noise.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org