fix: Add Substrait roundtrip support for EXISTS and correlated OuterReferenceColumn - #18987
fix: Add Substrait roundtrip support for EXISTS and correlated OuterReferenceColumn#18987Nithurshen wants to merge 5 commits into
Conversation
|
I'm requesting some backup for reviewing this one, thanks for the contribution! |
vbarua
left a comment
There was a problem hiding this comment.
I don't think this is the best approach to fix this, primarily because you've added a non-standard mechanism to handle outer references. The plans you produce will only work on DataFusion, and not other systems.
If you want to see what this would look like in more standard Substrait, you can use the isthmus tool with the following input:
isthmus \
--create "CREATE TABLE lsaj_t1(t1_id INT, t1_name VARCHAR, t1_int INT)" \
--create "CREATE TABLE lsaj_t2(t2_id INT, t2_name VARCHAR, t2_int INT)" \
"SELECT t1_id, t1_name
FROM lsaj_t1
WHERE NOT EXISTS (SELECT 1 FROM lsaj_t2 WHERE t1_id = t2_id)
ORDER BY t1_id"
to generate a Substrait plan that captures the outer reference using standard Substrait.
| }; | ||
|
|
||
| let fn_name = substrait_fun_name(fn_signature); | ||
| if fn_name == "outer_reference" { |
There was a problem hiding this comment.
Substrait already has a mechanism for handling outer references via the OuterReference root_type inside of a FieldReference.
Defining a custom function like you have here means that other systems won't be able to understand these plans, and DataFusion won't understands plans from other systems that use the standard mechanism for outer references.
Which issue does this PR close?
Rationale for this change
Substrait roundtrip tests were failing for queries involving
EXISTSand correlated subqueries (specifically those using outer references). The DataFusion Substrait producer did not support serializingExpr::Exists, and it threw a "feature not implemented" error forOuterReferenceColumn.Supporting these features is essential for improving the reliability of DataFusion's Substrait integration and enabling complex join scenarios in distributed environments.
What changes are included in this PR?
Producer Support for
EXISTS:from_existsin the producer to mapExpr::Existsto the SubstraitSetPredicate(usingPredicateOp::Existence).NOT EXISTSby wrapping the predicate in a "not" scalar function.Producer Support for
OuterReferenceColumn:OuterReferenceColumnas a custom scalar function named"outer_reference". This avoids schema validation errors during serialization since outer columns often cannot be resolved against the local subquery schema.Consumer Support for
OuterReferenceColumn:"outer_reference"scalar function and deserialize it back into a DataFusionOuterReferenceColumn, ensuring a successful roundtrip.Are these changes tested?
Yes.
joins.slt.cargo test --test sqllogictests -- --substrait-round-trip joins.slt:1233Are there any user-facing changes?
No breaking API changes. This PR purely expands the coverage of supported Logical Plans that can be converted to/from Substrait.