Skip to content

Commit 38aa6a7

Browse files
committed
feat: introduce SubstraitTable
1 parent b7abddd commit 38aa6a7

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

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

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

33
import io.substrait.extension.SimpleExtension;
44
import io.substrait.isthmus.calcite.SubstraitOperatorTable;
5+
import io.substrait.isthmus.calcite.SubstraitTable;
56
import java.util.ArrayList;
67
import java.util.List;
78
import org.apache.calcite.config.CalciteConnectionConfig;
@@ -20,7 +21,6 @@
2021
import org.apache.calcite.rel.type.RelDataTypeFactory;
2122
import org.apache.calcite.rex.RexBuilder;
2223
import org.apache.calcite.schema.Schema;
23-
import org.apache.calcite.schema.impl.AbstractTable;
2424
import org.apache.calcite.sql.SqlNode;
2525
import org.apache.calcite.sql.SqlNodeList;
2626
import org.apache.calcite.sql.SqlOperatorTable;
@@ -79,8 +79,8 @@ CalciteCatalogReader registerCreateTables(List<String> tables) throws SqlParseEx
7979
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
8080
if (tables != null) {
8181
for (String tableDef : tables) {
82-
List<DefinedTable> tList = parseCreateTable(factory, validator, tableDef);
83-
for (DefinedTable t : tList) {
82+
List<SubstraitTable> tList = parseCreateTable(factory, validator, tableDef);
83+
for (SubstraitTable t : tList) {
8484
rootSchema.add(t.getName(), t);
8585
}
8686
}
@@ -97,10 +97,10 @@ CalciteCatalogReader registerSchema(String name, Schema schema) {
9797
return new CalciteCatalogReader(rootSchema, List.of(), factory, config);
9898
}
9999

100-
protected List<DefinedTable> parseCreateTable(
100+
protected List<SubstraitTable> parseCreateTable(
101101
RelDataTypeFactory factory, SqlValidator validator, String sql) throws SqlParseException {
102102
SqlParser parser = SqlParser.create(sql, parserConfig);
103-
List<DefinedTable> definedTableList = new ArrayList<>();
103+
List<SubstraitTable> tableList = new ArrayList<>();
104104

105105
SqlNodeList nodeList = parser.parseStmtList();
106106
for (SqlNode parsed : nodeList) {
@@ -140,12 +140,12 @@ protected List<DefinedTable> parseCreateTable(
140140
columnTypes.add(col.dataType.deriveType(validator));
141141
}
142142

143-
definedTableList.add(
144-
new DefinedTable(
145-
create.name.names.get(0), factory, factory.createStructType(columnTypes, names)));
143+
tableList.add(
144+
new SubstraitTable(
145+
create.name.names.get(0), factory.createStructType(columnTypes, names)));
146146
}
147147

148-
return definedTableList;
148+
return tableList;
149149
}
150150

151151
protected static SqlParseException fail(String text, SqlParserPos pos) {
@@ -173,30 +173,4 @@ public static Validator create(
173173
return new Validator(SubstraitOperatorTable.INSTANCE, validatorCatalog, factory, config);
174174
}
175175
}
176-
177-
/** A fully defined pre-specified table. */
178-
protected static final class DefinedTable extends AbstractTable {
179-
180-
private final String name;
181-
private final RelDataTypeFactory factory;
182-
private final RelDataType type;
183-
184-
public DefinedTable(String name, RelDataTypeFactory factory, RelDataType type) {
185-
this.name = name;
186-
this.factory = factory;
187-
this.type = type;
188-
}
189-
190-
@Override
191-
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
192-
// if (factory != typeFactory) {
193-
// throw new IllegalStateException("Different type factory than previously used.");
194-
// }
195-
return type;
196-
}
197-
198-
public String getName() {
199-
return name;
200-
}
201-
}
202176
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.substrait.extendedexpression.ImmutableExpressionReference;
66
import io.substrait.extendedexpression.ImmutableExtendedExpression;
77
import io.substrait.extension.SimpleExtension;
8+
import io.substrait.isthmus.calcite.SubstraitTable;
89
import io.substrait.isthmus.expression.RexExpressionConverter;
910
import io.substrait.isthmus.expression.ScalarFunctionConverter;
1011
import io.substrait.proto.ExtendedExpression;
@@ -145,8 +146,8 @@ private Result registerCreateTablesForExtendedExpression(List<String> tables)
145146
SqlValidator validator = Validator.create(factory, catalogReader, SqlValidator.Config.DEFAULT);
146147
if (tables != null) {
147148
for (String tableDef : tables) {
148-
List<DefinedTable> tList = parseCreateTable(factory, validator, tableDef);
149-
for (DefinedTable t : tList) {
149+
List<SubstraitTable> tList = parseCreateTable(factory, validator, tableDef);
150+
for (SubstraitTable t : tList) {
150151
rootSchema.add(t.getName(), t);
151152
for (RelDataTypeField field : t.getRowType(factory).getFieldList()) {
152153
nameToTypeMap.merge( // to validate the sql expression tree

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

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

33
import io.substrait.extension.SimpleExtension;
4+
import io.substrait.isthmus.calcite.SubstraitTable;
45
import io.substrait.plan.Plan;
56
import io.substrait.relation.NamedScan;
67
import io.substrait.relation.Rel;
@@ -64,9 +65,8 @@ protected CalciteSchema toSchema(Rel rel) {
6465
if (table == null) {
6566
return null;
6667
}
67-
return new SqlConverterBase.DefinedTable(
68+
return new SubstraitTable(
6869
id.get(id.size() - 1),
69-
typeFactory,
7070
typeConverter.toCalcite(typeFactory, table.struct(), table.names()));
7171
};
7272
return LookupCalciteSchema.createRootSchema(lookup);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.substrait.isthmus.calcite;
2+
3+
import org.apache.calcite.rel.type.RelDataType;
4+
import org.apache.calcite.rel.type.RelDataTypeFactory;
5+
import org.apache.calcite.schema.impl.AbstractTable;
6+
7+
/** Basic {@link AbstractTable} implementation */
8+
public class SubstraitTable extends AbstractTable {
9+
10+
private final RelDataType rowType;
11+
private final String tableName;
12+
13+
public SubstraitTable(String tableName, RelDataType rowType) {
14+
this.tableName = tableName;
15+
this.rowType = rowType;
16+
}
17+
18+
public String getName() {
19+
return tableName;
20+
}
21+
22+
@Override
23+
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
24+
return rowType;
25+
}
26+
}

0 commit comments

Comments
 (0)