forked from substrait-io/substrait-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(isthmus): allow for conversion of plans containing Calcite SqlAgg…
…Functions (substrait-io#230) Calcite planning rules can introduce the Calcite variants of SqlAggFunctions In substrait-io#180, Substrait specific variants for these function were introduced which better matched the type inference for these functions as defined in Substrait Those changes cause failures when converting the Calcite variants to Substrait, which is what these changes address --------- Co-authored-by: Victor Barua <victor.barua@datadoghq.com>
- Loading branch information
Showing
6 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
isthmus/src/test/java/io/substrait/isthmus/OptimizerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.substrait.isthmus; | ||
|
||
import static io.substrait.isthmus.SqlConverterBase.EXTENSION_COLLECTION; | ||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import org.apache.calcite.plan.hep.HepPlanner; | ||
import org.apache.calcite.plan.hep.HepProgram; | ||
import org.apache.calcite.plan.hep.HepProgramBuilder; | ||
import org.apache.calcite.rel.RelNode; | ||
import org.apache.calcite.rel.RelRoot; | ||
import org.apache.calcite.rel.rules.CoreRules; | ||
import org.apache.calcite.sql.parser.SqlParseException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class OptimizerIntegrationTest extends PlanTestBase { | ||
|
||
@Test | ||
void conversionHandlesBuiltInSum0CallAddedByRule() throws SqlParseException, IOException { | ||
var query = | ||
"select O_CUSTKEY, count(distinct O_ORDERKEY), count(*) from orders group by O_CUSTKEY"; | ||
// verify that the query works generally | ||
assertFullRoundTrip(query); | ||
|
||
SqlToSubstrait sqlConverter = new SqlToSubstrait(); | ||
List<RelRoot> relRoots = sqlConverter.sqlToRelNode(query, tpchSchemaCreateStatements()); | ||
assertEquals(1, relRoots.size()); | ||
RelRoot planRoot = relRoots.get(0); | ||
RelNode originalPlan = planRoot.rel; | ||
|
||
// Create a program to apply the AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN rule. | ||
// This will introduce a SqlSumEmptyIsZeroAggFunction to the plan. | ||
// This function does not have a mapping to Substrait. | ||
// SubstraitSumEmptyIsZeroAggFunction is the variant which has a mapping. | ||
// See io.substrait.isthmus.AggregateFunctions for details | ||
HepProgram program = | ||
new HepProgramBuilder() | ||
.addRuleInstance(CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN) | ||
.build(); | ||
HepPlanner planner = new HepPlanner(program); | ||
planner.setRoot(originalPlan); | ||
var newPlan = planner.findBestExp(); | ||
|
||
assertDoesNotThrow( | ||
() -> | ||
// Conversion of the new plan should succeed | ||
SubstraitRelVisitor.convert(RelRoot.of(newPlan, planRoot.kind), EXTENSION_COLLECTION)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
isthmus/src/test/java/io/substrait/isthmus/expression/AggregateFunctionConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.substrait.isthmus.expression; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import io.substrait.isthmus.AggregateFunctions; | ||
import io.substrait.isthmus.PlanTestBase; | ||
import io.substrait.isthmus.TypeConverter; | ||
import java.util.List; | ||
import org.apache.calcite.rel.core.AggregateCall; | ||
import org.apache.calcite.sql.fun.SqlSumEmptyIsZeroAggFunction; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AggregateFunctionConverterTest extends PlanTestBase { | ||
|
||
@Test | ||
void testFunctionFinderMatch() { | ||
AggregateFunctionConverter converter = | ||
new AggregateFunctionConverter( | ||
extensions.aggregateFunctions(), List.of(), typeFactory, TypeConverter.DEFAULT); | ||
|
||
var functionFinder = | ||
converter.getFunctionFinder( | ||
AggregateCall.create( | ||
new SqlSumEmptyIsZeroAggFunction(), | ||
true, | ||
List.of(1), | ||
0, | ||
typeFactory.createSqlType(SqlTypeName.VARCHAR), | ||
null)); | ||
assertNotNull(functionFinder); | ||
assertEquals("sum0", functionFinder.getName()); | ||
assertEquals(AggregateFunctions.SUM0, functionFinder.getOperator()); | ||
} | ||
} |