Skip to content

Commit 99ea57d

Browse files
committed
feat: introduce SubstraitSqlValidator
1 parent 42dfc3a commit 99ea57d

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.substrait.isthmus;
22

33
import io.substrait.extension.SimpleExtension;
4-
import io.substrait.isthmus.calcite.SubstraitOperatorTable;
54
import io.substrait.isthmus.calcite.SubstraitTable;
5+
import io.substrait.isthmus.sql.SubstraitSqlValidator;
66
import java.util.ArrayList;
77
import java.util.List;
88
import org.apache.calcite.config.CalciteConnectionConfig;
@@ -23,7 +23,6 @@
2323
import org.apache.calcite.schema.Schema;
2424
import org.apache.calcite.sql.SqlNode;
2525
import org.apache.calcite.sql.SqlNodeList;
26-
import org.apache.calcite.sql.SqlOperatorTable;
2726
import org.apache.calcite.sql.ddl.SqlColumnDeclaration;
2827
import org.apache.calcite.sql.ddl.SqlCreateTable;
2928
import org.apache.calcite.sql.ddl.SqlKeyConstraint;
@@ -33,8 +32,6 @@
3332
import org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl;
3433
import org.apache.calcite.sql.validate.SqlConformanceEnum;
3534
import org.apache.calcite.sql.validate.SqlValidator;
36-
import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
37-
import org.apache.calcite.sql.validate.SqlValidatorImpl;
3835
import org.apache.calcite.sql2rel.SqlToRelConverter;
3936

4037
class SqlConverterBase {
@@ -76,7 +73,7 @@ CalciteCatalogReader registerCreateTables(List<String> tables) throws SqlParseEx
7673
CalciteSchema rootSchema = CalciteSchema.createRootSchema(false);
7774
CalciteCatalogReader catalogReader =
7875
new CalciteCatalogReader(rootSchema, List.of(), factory, config);
79-
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
76+
SqlValidator validator = new SubstraitSqlValidator(catalogReader);
8077
if (tables != null) {
8178
for (String tableDef : tables) {
8279
List<SubstraitTable> tList = parseCreateTable(factory, validator, tableDef);
@@ -155,22 +152,4 @@ protected static SqlParseException fail(String text, SqlParserPos pos) {
155152
protected static SqlParseException fail(String text) {
156153
return fail(text, SqlParserPos.ZERO);
157154
}
158-
159-
protected static final class Validator extends SqlValidatorImpl {
160-
161-
private Validator(
162-
SqlOperatorTable opTab,
163-
SqlValidatorCatalogReader catalogReader,
164-
RelDataTypeFactory typeFactory,
165-
Config config) {
166-
super(opTab, catalogReader, typeFactory, config);
167-
}
168-
169-
public static Validator create(
170-
RelDataTypeFactory factory,
171-
SqlValidatorCatalogReader validatorCatalog,
172-
SqlValidator.Config config) {
173-
return new Validator(SubstraitOperatorTable.INSTANCE, validatorCatalog, factory, config);
174-
}
175-
}
176155
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.substrait.isthmus.calcite.SubstraitTable;
99
import io.substrait.isthmus.expression.RexExpressionConverter;
1010
import io.substrait.isthmus.expression.ScalarFunctionConverter;
11+
import io.substrait.isthmus.sql.SubstraitSqlValidator;
1112
import io.substrait.proto.ExtendedExpression;
1213
import io.substrait.type.NamedStruct;
1314
import io.substrait.type.Type;
@@ -143,7 +144,7 @@ private Result registerCreateTablesForExtendedExpression(List<String> tables)
143144
CalciteSchema rootSchema = CalciteSchema.createRootSchema(false);
144145
CalciteCatalogReader catalogReader =
145146
new CalciteCatalogReader(rootSchema, List.of(), factory, config);
146-
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
147+
SqlValidator validator = new SubstraitSqlValidator(catalogReader);
147148
if (tables != null) {
148149
for (String tableDef : tables) {
149150
List<SubstraitTable> tList = parseCreateTable(factory, validator, tableDef);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.annotations.VisibleForTesting;
44
import io.substrait.extension.ExtensionCollector;
5+
import io.substrait.isthmus.sql.SubstraitSqlValidator;
56
import io.substrait.proto.Plan;
67
import io.substrait.proto.PlanRel;
78
import io.substrait.relation.RelProtoConverter;
@@ -32,25 +33,25 @@ public SqlToSubstrait(FeatureBoard features) {
3233

3334
public Plan execute(String sql, List<String> tables) throws SqlParseException {
3435
CalciteCatalogReader catalogReader = registerCreateTables(tables);
35-
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
36+
SqlValidator validator = new SubstraitSqlValidator(catalogReader);
3637
return executeInner(sql, validator, catalogReader);
3738
}
3839

3940
public Plan execute(String sql, String name, Schema schema) throws SqlParseException {
4041
CalciteCatalogReader catalogReader = registerSchema(name, schema);
41-
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
42+
SqlValidator validator = new SubstraitSqlValidator(catalogReader);
4243
return executeInner(sql, validator, catalogReader);
4344
}
4445

4546
public Plan execute(String sql, Prepare.CatalogReader catalogReader) throws SqlParseException {
46-
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
47+
SqlValidator validator = new SubstraitSqlValidator(catalogReader);
4748
return executeInner(sql, validator, catalogReader);
4849
}
4950

5051
// Package protected for testing
5152
List<RelRoot> sqlToRelNode(String sql, List<String> tables) throws SqlParseException {
5253
Prepare.CatalogReader catalogReader = registerCreateTables(tables);
53-
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
54+
SqlValidator validator = new SubstraitSqlValidator(catalogReader);
5455
return sqlToRelNode(sql, validator, catalogReader);
5556
}
5657

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.substrait.isthmus.sql;
2+
3+
import io.substrait.isthmus.calcite.SubstraitOperatorTable;
4+
import org.apache.calcite.prepare.Prepare;
5+
import org.apache.calcite.sql.validate.SqlValidator;
6+
import org.apache.calcite.sql.validate.SqlValidatorImpl;
7+
8+
public class SubstraitSqlValidator extends SqlValidatorImpl {
9+
10+
static SqlValidator.Config CONFIG = Config.DEFAULT;
11+
12+
public SubstraitSqlValidator(Prepare.CatalogReader catalogReader) {
13+
super(SubstraitOperatorTable.INSTANCE, catalogReader, catalogReader.getTypeFactory(), CONFIG);
14+
}
15+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.substrait.isthmus;
22

3+
import io.substrait.isthmus.sql.SubstraitSqlValidator;
34
import java.util.Map;
45
import org.apache.calcite.adapter.tpcds.TpcdsSchema;
56
import org.apache.calcite.prepare.CalciteCatalogReader;
67
import org.apache.calcite.rel.RelRoot;
78
import org.apache.calcite.rex.RexFieldAccess;
89
import org.apache.calcite.sql.parser.SqlParseException;
910
import org.apache.calcite.sql.parser.SqlParser;
10-
import org.apache.calcite.sql.validate.SqlValidator;
1111
import org.apache.calcite.sql2rel.SqlToRelConverter;
1212
import org.junit.jupiter.api.Assertions;
1313
import org.junit.jupiter.api.Test;
@@ -17,9 +17,7 @@ public class ApplyJoinPlanTest {
1717
private static RelRoot getCalcitePlan(SqlToSubstrait s, TpcdsSchema schema, String sql)
1818
throws SqlParseException {
1919
CalciteCatalogReader catalogReader = s.registerSchema("tpcds", schema);
20-
SqlConverterBase.Validator validator =
21-
SqlConverterBase.Validator.create(
22-
catalogReader.getTypeFactory(), catalogReader, SqlValidator.Config.DEFAULT);
20+
SubstraitSqlValidator validator = new SubstraitSqlValidator(catalogReader);
2321
SqlToRelConverter converter = s.createSqlToRelConverter(validator, catalogReader);
2422
SqlParser parser = SqlParser.create(sql, s.parserConfig);
2523
return s.getBestExpRelRoot(converter, parser.parseQuery());

0 commit comments

Comments
 (0)