Skip to content

Commit eeb97cd

Browse files
committed
chore(isthmus): handle nullability and EnumArgument in SimplExtensionToSqlOperator
1 parent 857fe2c commit eeb97cd

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

isthmus/src/main/java/io/substrait/isthmus/SqlToSubstrait.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
package io.substrait.isthmus;
22

3-
import io.substrait.isthmus.sql.SubstraitSqlToCalcite;
4-
import com.google.common.annotations.VisibleForTesting;
53
import io.substrait.extension.SimpleExtension;
64
import io.substrait.isthmus.calcite.SubstraitOperatorTable;
7-
import io.substrait.isthmus.sql.SubstraitSqlValidator;
5+
import io.substrait.isthmus.sql.SubstraitSqlToCalcite;
86
import io.substrait.plan.ImmutablePlan.Builder;
97
import io.substrait.plan.Plan;
108
import io.substrait.plan.Plan.Version;
119
import io.substrait.plan.PlanProtoConverter;
10+
import java.util.List;
1211
import org.apache.calcite.prepare.Prepare;
13-
import org.apache.calcite.rel.RelRoot;
14-
import org.apache.calcite.rel.rules.CoreRules;
15-
import org.apache.calcite.sql.SqlNode;
16-
import org.apache.calcite.sql.SqlNodeList;
1712
import org.apache.calcite.sql.SqlOperator;
1813
import org.apache.calcite.sql.SqlOperatorTable;
1914
import org.apache.calcite.sql.parser.SqlParseException;
20-
import org.apache.calcite.sql.parser.SqlParser;
2115
import org.apache.calcite.sql.util.SqlOperatorTables;
22-
import org.apache.calcite.sql.validate.SqlValidator;
23-
import org.apache.calcite.sql2rel.SqlToRelConverter;
24-
import org.apache.calcite.sql2rel.StandardConvertletTable;
25-
26-
import java.util.List;
2716

2817
/** Take a SQL statement and a set of table definitions and return a substrait plan. */
2918
public class SqlToSubstrait extends SqlConverterBase {
@@ -84,9 +73,9 @@ public Plan convert(String sqlStatements, Prepare.CatalogReader catalogReader)
8473
builder.version(Version.builder().from(Version.DEFAULT_VERSION).producer("isthmus").build());
8574

8675
// TODO: consider case in which one sql passes conversion while others don't
87-
SubstraitSqlToCalcite.convertQueries(sqlStatements, catalogReader).stream()
88-
.map(root -> SubstraitRelVisitor.convert(root, extensionCollection, featureBoard))
89-
.forEach(root -> builder.addRoots(root));
76+
SubstraitSqlToCalcite.convertQueries(sqlStatements, catalogReader, operatorTable).stream()
77+
.map(root -> SubstraitRelVisitor.convert(root, extensionCollection, featureBoard))
78+
.forEach(root -> builder.addRoots(root));
9079

9180
return builder.build();
9281
}

isthmus/src/main/java/io/substrait/isthmus/sql/SubstraitSqlToCalcite.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.calcite.rel.rules.CoreRules;
1616
import org.apache.calcite.rex.RexBuilder;
1717
import org.apache.calcite.sql.SqlNode;
18+
import org.apache.calcite.sql.SqlOperatorTable;
1819
import org.apache.calcite.sql.parser.SqlParseException;
1920
import org.apache.calcite.sql.validate.SqlValidator;
2021
import org.apache.calcite.sql2rel.SqlToRelConverter;
@@ -41,6 +42,23 @@ public static RelRoot convertQuery(String sqlStatement, Prepare.CatalogReader ca
4142
return convertQuery(sqlStatement, catalogReader, validator, createDefaultRelOptCluster());
4243
}
4344

45+
/**
46+
* Converts a SQL statement to a Calcite {@link RelRoot}.
47+
*
48+
* @param sqlStatement a SQL statement string
49+
* @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in
50+
* the SQL statement
51+
* @param operatorTable the {@link SqlOperatorTable} for dynamic operators
52+
* @return a {@link RelRoot} corresponding to the given SQL statement
53+
* @throws SqlParseException if there is an error while parsing the SQL statement
54+
*/
55+
public static RelRoot convertQuery(
56+
String sqlStatement, Prepare.CatalogReader catalogReader, SqlOperatorTable operatorTable)
57+
throws SqlParseException {
58+
SqlValidator validator = new SubstraitSqlValidator(catalogReader, operatorTable);
59+
return convertQuery(sqlStatement, catalogReader, validator, createDefaultRelOptCluster());
60+
}
61+
4462
/**
4563
* Converts a SQL statement to a Calcite {@link RelRoot}.
4664
*
@@ -72,6 +90,24 @@ public static RelRoot convertQuery(
7290
return relRoots.get(0);
7391
}
7492

93+
/**
94+
* Converts one or more SQL statements to a List of {@link RelRoot}, with one {@link RelRoot} per
95+
* statement.
96+
*
97+
* @param sqlStatements a string containing one or more SQL statements
98+
* @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in
99+
* the SQL statements
100+
* @param operatorTable the {@link SqlOperatorTable} for dynamic operators
101+
* @return a list of {@link RelRoot}s corresponding to the given SQL statements
102+
* @throws SqlParseException if there is an error while parsing the SQL statements
103+
*/
104+
public static List<RelRoot> convertQueries(
105+
String sqlStatements, Prepare.CatalogReader catalogReader, SqlOperatorTable operatorTable)
106+
throws SqlParseException {
107+
SqlValidator validator = new SubstraitSqlValidator(catalogReader, operatorTable);
108+
return convertQueries(sqlStatements, catalogReader, validator, createDefaultRelOptCluster());
109+
}
110+
75111
/**
76112
* Converts one or more SQL statements to a List of {@link RelRoot}, with one {@link RelRoot} per
77113
* statement.

isthmus/src/test/java/io/substrait/isthmus/OptimizerIntegrationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.substrait.extension.SimpleExtension;
66
import io.substrait.isthmus.sql.SubstraitSqlToCalcite;
77
import java.io.IOException;
8-
import java.util.List;
98
import org.apache.calcite.plan.hep.HepPlanner;
109
import org.apache.calcite.plan.hep.HepProgram;
1110
import org.apache.calcite.plan.hep.HepProgramBuilder;

isthmus/src/test/java/io/substrait/isthmus/PlanTestBase.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,10 @@ protected RelRoot assertSqlSubstraitRelRoundTripWorkaroundOptimizer(
167167
SqlToSubstrait s = new SqlToSubstrait(extensions, null);
168168

169169
// 1. SQL -> Calcite RelRoot
170-
List<RelRoot> relRoots = s.sqlToRelNode(query, catalogReader);
171-
assertEquals(1, relRoots.size());
172-
RelRoot relRoot1 = relRoots.get(0);
170+
Plan plan1 = s.convert(query, catalogReader);
173171

174172
// 2. Calcite RelRoot -> Substrait Rel
175-
Plan.Root pojo1 = SubstraitRelVisitor.convert(relRoot1, extensions);
173+
Plan.Root pojo1 = plan1.getRoots().get(0);
176174

177175
// 3. Substrait Rel -> Calcite RelNode
178176
RelRoot relRoot2 = substraitToCalcite.convert(pojo1);

isthmus/src/test/resources/extensions/functions_string_custom.yaml

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)