|
1 | 1 | package io.substrait.isthmus; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
3 | 4 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
4 | 5 |
|
| 6 | +import io.substrait.expression.Expression; |
5 | 7 | import io.substrait.extension.SimpleExtension; |
6 | 8 | import io.substrait.isthmus.expression.AggregateFunctionConverter; |
7 | 9 | import io.substrait.isthmus.expression.ScalarFunctionConverter; |
8 | 10 | import io.substrait.isthmus.expression.WindowFunctionConverter; |
9 | 11 | import java.io.IOException; |
10 | 12 | import java.io.UncheckedIOException; |
11 | 13 | import java.util.List; |
| 14 | +import java.util.Optional; |
| 15 | +import org.apache.calcite.rex.RexBuilder; |
| 16 | +import org.apache.calcite.rex.RexCall; |
| 17 | +import org.apache.calcite.rex.RexNode; |
| 18 | +import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
12 | 19 | import org.junit.jupiter.api.Test; |
13 | 20 |
|
14 | 21 | /** Tests to reproduce #562 */ |
15 | 22 | public class DuplicateFunctionUrnTest extends PlanTestBase { |
16 | 23 |
|
| 24 | + static final SimpleExtension.ExtensionCollection collection1; |
| 25 | + static final SimpleExtension.ExtensionCollection collection2; |
17 | 26 | static final SimpleExtension.ExtensionCollection collection; |
18 | 27 |
|
19 | 28 | static { |
20 | 29 | try { |
21 | 30 | String extensions1 = asString("extensions/functions_duplicate_urn1.yaml"); |
22 | 31 | String extensions2 = asString("extensions/functions_duplicate_urn2.yaml"); |
23 | | - SimpleExtension.ExtensionCollection collection1 = |
24 | | - SimpleExtension.load("urn1://functions", extensions1); |
25 | | - SimpleExtension.ExtensionCollection collection2 = |
26 | | - SimpleExtension.load("urn2://functions", extensions2); |
| 32 | + collection1 = SimpleExtension.load("urn1://functions", extensions1); |
| 33 | + collection2 = SimpleExtension.load("urn2://functions", extensions2); |
27 | 34 | collection = collection1.merge(collection2); |
28 | 35 |
|
29 | 36 | // Verify that the merged collection contains duplicate functions with different URNs |
@@ -70,4 +77,52 @@ void testDuplicateWindowFunctionWithDifferentUrns() { |
70 | 77 |
|
71 | 78 | assertNotNull(converter); |
72 | 79 | } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testMergeOrderDeterminesFunctionPrecedence() { |
| 83 | + // This test verifies that when multiple extension collections contain functions with |
| 84 | + // the same name and signature but different URNs, the merge order determines precedence. |
| 85 | + // The FunctionConverter uses a "last-wins" strategy: the last function added to the |
| 86 | + // extension collection will be matched when converting from Calcite to Substrait. |
| 87 | + |
| 88 | + SimpleExtension.ExtensionCollection reverseCollection = collection2.merge(collection1); |
| 89 | + ScalarFunctionConverter converterA = |
| 90 | + new ScalarFunctionConverter(collection.scalarFunctions(), typeFactory); |
| 91 | + ScalarFunctionConverter converterB = |
| 92 | + new ScalarFunctionConverter(reverseCollection.scalarFunctions(), typeFactory); |
| 93 | + |
| 94 | + RexBuilder rexBuilder = new RexBuilder(typeFactory); |
| 95 | + RexNode arg1 = rexBuilder.makeLiteral("hello"); |
| 96 | + RexNode arg2 = rexBuilder.makeLiteral("world"); |
| 97 | + RexCall concatCall = (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.CONCAT, arg1, arg2); |
| 98 | + |
| 99 | + // Create a simple topLevelConverter that converts literals to Substrait expressions |
| 100 | + java.util.function.Function<RexNode, Expression> topLevelConverter = |
| 101 | + rexNode -> { |
| 102 | + if (rexNode instanceof org.apache.calcite.rex.RexLiteral) { |
| 103 | + org.apache.calcite.rex.RexLiteral lit = (org.apache.calcite.rex.RexLiteral) rexNode; |
| 104 | + return Expression.StrLiteral.builder() |
| 105 | + .value(lit.getValueAs(String.class)) |
| 106 | + .nullable(false) |
| 107 | + .build(); |
| 108 | + } |
| 109 | + throw new UnsupportedOperationException("Only literals supported in test"); |
| 110 | + }; |
| 111 | + |
| 112 | + Optional<Expression> exprA = converterA.convert(concatCall, topLevelConverter); |
| 113 | + Optional<Expression> exprB = converterB.convert(concatCall, topLevelConverter); |
| 114 | + |
| 115 | + Expression.ScalarFunctionInvocation funcA = (Expression.ScalarFunctionInvocation) exprA.get(); |
| 116 | + Expression.ScalarFunctionInvocation funcB = (Expression.ScalarFunctionInvocation) exprB.get(); |
| 117 | + |
| 118 | + assertEquals( |
| 119 | + "extension:com.domain:string", |
| 120 | + funcA.declaration().getAnchor().urn(), |
| 121 | + "converterA should use last concat function (from collection2)"); |
| 122 | + |
| 123 | + assertEquals( |
| 124 | + "extension:io.substrait:functions_string", |
| 125 | + funcB.declaration().getAnchor().urn(), |
| 126 | + "converterB should use last concat function (from collection1)"); |
| 127 | + } |
73 | 128 | } |
0 commit comments