|
1 | 1 | package io.substrait.isthmus;
|
2 | 2 |
|
3 | 3 | import io.substrait.extension.SimpleExtension;
|
4 |
| -import io.substrait.isthmus.calcite.SubstraitOperatorTable; |
5 |
| -import io.substrait.isthmus.calcite.SubstraitTable; |
6 |
| -import java.util.ArrayList; |
7 |
| -import java.util.List; |
8 | 4 | import org.apache.calcite.config.CalciteConnectionConfig;
|
9 | 5 | import org.apache.calcite.config.CalciteConnectionProperty;
|
10 |
| -import org.apache.calcite.jdbc.CalciteSchema; |
11 | 6 | import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
|
12 | 7 | import org.apache.calcite.plan.Contexts;
|
13 | 8 | import org.apache.calcite.plan.RelOptCluster;
|
14 | 9 | import org.apache.calcite.plan.RelOptCostImpl;
|
15 | 10 | import org.apache.calcite.plan.volcano.VolcanoPlanner;
|
16 |
| -import org.apache.calcite.prepare.CalciteCatalogReader; |
17 | 11 | import org.apache.calcite.rel.metadata.DefaultRelMetadataProvider;
|
18 | 12 | import org.apache.calcite.rel.metadata.ProxyingMetadataHandlerProvider;
|
19 | 13 | import org.apache.calcite.rel.metadata.RelMetadataQuery;
|
20 |
| -import org.apache.calcite.rel.type.RelDataType; |
21 | 14 | import org.apache.calcite.rel.type.RelDataTypeFactory;
|
22 | 15 | import org.apache.calcite.rex.RexBuilder;
|
23 |
| -import org.apache.calcite.schema.Schema; |
24 |
| -import org.apache.calcite.sql.SqlNode; |
25 |
| -import org.apache.calcite.sql.SqlNodeList; |
26 |
| -import org.apache.calcite.sql.SqlOperatorTable; |
27 |
| -import org.apache.calcite.sql.ddl.SqlColumnDeclaration; |
28 |
| -import org.apache.calcite.sql.ddl.SqlCreateTable; |
29 |
| -import org.apache.calcite.sql.ddl.SqlKeyConstraint; |
30 |
| -import org.apache.calcite.sql.parser.SqlParseException; |
31 | 16 | import org.apache.calcite.sql.parser.SqlParser;
|
32 |
| -import org.apache.calcite.sql.parser.SqlParserPos; |
33 | 17 | import org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl;
|
34 | 18 | import org.apache.calcite.sql.validate.SqlConformanceEnum;
|
35 |
| -import org.apache.calcite.sql.validate.SqlValidator; |
36 |
| -import org.apache.calcite.sql.validate.SqlValidatorCatalogReader; |
37 |
| -import org.apache.calcite.sql.validate.SqlValidatorImpl; |
38 | 19 | import org.apache.calcite.sql2rel.SqlToRelConverter;
|
39 | 20 |
|
40 | 21 | class SqlConverterBase {
|
@@ -71,106 +52,4 @@ protected SqlConverterBase(FeatureBoard features) {
|
71 | 52 |
|
72 | 53 | protected static final SimpleExtension.ExtensionCollection EXTENSION_COLLECTION =
|
73 | 54 | SimpleExtension.loadDefaults();
|
74 |
| - |
75 |
| - CalciteCatalogReader registerCreateTables(List<String> tables) throws SqlParseException { |
76 |
| - CalciteSchema rootSchema = CalciteSchema.createRootSchema(false); |
77 |
| - CalciteCatalogReader catalogReader = |
78 |
| - new CalciteCatalogReader(rootSchema, List.of(), factory, config); |
79 |
| - SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT); |
80 |
| - if (tables != null) { |
81 |
| - for (String tableDef : tables) { |
82 |
| - List<SubstraitTable> tList = parseCreateTable(factory, validator, tableDef); |
83 |
| - for (SubstraitTable t : tList) { |
84 |
| - rootSchema.add(t.getName(), t); |
85 |
| - } |
86 |
| - } |
87 |
| - } |
88 |
| - return catalogReader; |
89 |
| - } |
90 |
| - |
91 |
| - CalciteCatalogReader registerSchema(String name, Schema schema) { |
92 |
| - CalciteSchema rootSchema = CalciteSchema.createRootSchema(false); |
93 |
| - if (schema != null) { |
94 |
| - rootSchema.add(name, schema); |
95 |
| - rootSchema = rootSchema.getSubSchema(name, false); |
96 |
| - } |
97 |
| - return new CalciteCatalogReader(rootSchema, List.of(), factory, config); |
98 |
| - } |
99 |
| - |
100 |
| - protected List<SubstraitTable> parseCreateTable( |
101 |
| - RelDataTypeFactory factory, SqlValidator validator, String sql) throws SqlParseException { |
102 |
| - SqlParser parser = SqlParser.create(sql, parserConfig); |
103 |
| - List<SubstraitTable> tableList = new ArrayList<>(); |
104 |
| - |
105 |
| - SqlNodeList nodeList = parser.parseStmtList(); |
106 |
| - for (SqlNode parsed : nodeList) { |
107 |
| - if (!(parsed instanceof SqlCreateTable)) { |
108 |
| - throw fail("Not a valid CREATE TABLE statement."); |
109 |
| - } |
110 |
| - |
111 |
| - SqlCreateTable create = (SqlCreateTable) parsed; |
112 |
| - if (create.name.names.size() > 1) { |
113 |
| - throw fail("Only simple table names are allowed.", create.name.getParserPosition()); |
114 |
| - } |
115 |
| - |
116 |
| - if (create.query != null) { |
117 |
| - throw fail("CTAS not supported.", create.name.getParserPosition()); |
118 |
| - } |
119 |
| - |
120 |
| - List<String> names = new ArrayList<>(); |
121 |
| - List<RelDataType> columnTypes = new ArrayList<>(); |
122 |
| - |
123 |
| - for (SqlNode node : create.columnList) { |
124 |
| - if (!(node instanceof SqlColumnDeclaration)) { |
125 |
| - if (node instanceof SqlKeyConstraint) { |
126 |
| - // key constraints declarations, like primary key declaration, are valid and should not |
127 |
| - // result in parse exceptions. Ignore the constraint declaration. |
128 |
| - continue; |
129 |
| - } |
130 |
| - |
131 |
| - throw fail("Unexpected column list construction.", node.getParserPosition()); |
132 |
| - } |
133 |
| - |
134 |
| - SqlColumnDeclaration col = (SqlColumnDeclaration) node; |
135 |
| - if (col.name.names.size() != 1) { |
136 |
| - throw fail("Expected simple column names.", col.name.getParserPosition()); |
137 |
| - } |
138 |
| - |
139 |
| - names.add(col.name.names.get(0)); |
140 |
| - columnTypes.add(col.dataType.deriveType(validator)); |
141 |
| - } |
142 |
| - |
143 |
| - tableList.add( |
144 |
| - new SubstraitTable( |
145 |
| - create.name.names.get(0), factory.createStructType(columnTypes, names))); |
146 |
| - } |
147 |
| - |
148 |
| - return tableList; |
149 |
| - } |
150 |
| - |
151 |
| - protected static SqlParseException fail(String text, SqlParserPos pos) { |
152 |
| - return new SqlParseException(text, pos, null, null, new RuntimeException("fake lineage")); |
153 |
| - } |
154 |
| - |
155 |
| - protected static SqlParseException fail(String text) { |
156 |
| - return fail(text, SqlParserPos.ZERO); |
157 |
| - } |
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 |
| - } |
176 | 55 | }
|
0 commit comments