|
| 1 | +package io.substrait.isthmus.sql; |
| 2 | + |
| 3 | +import io.substrait.isthmus.SubstraitTypeSystem; |
| 4 | +import io.substrait.isthmus.calcite.SubstraitTable; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import org.apache.calcite.avatica.util.Casing; |
| 9 | +import org.apache.calcite.config.CalciteConnectionConfig; |
| 10 | +import org.apache.calcite.config.CalciteConnectionProperty; |
| 11 | +import org.apache.calcite.jdbc.CalciteSchema; |
| 12 | +import org.apache.calcite.jdbc.JavaTypeFactoryImpl; |
| 13 | +import org.apache.calcite.prepare.CalciteCatalogReader; |
| 14 | +import org.apache.calcite.rel.type.RelDataType; |
| 15 | +import org.apache.calcite.rel.type.RelDataTypeFactory; |
| 16 | +import org.apache.calcite.sql.SqlNode; |
| 17 | +import org.apache.calcite.sql.SqlNodeList; |
| 18 | +import org.apache.calcite.sql.ddl.SqlColumnDeclaration; |
| 19 | +import org.apache.calcite.sql.ddl.SqlCreateTable; |
| 20 | +import org.apache.calcite.sql.ddl.SqlKeyConstraint; |
| 21 | +import org.apache.calcite.sql.parser.SqlParseException; |
| 22 | +import org.apache.calcite.sql.parser.SqlParser; |
| 23 | +import org.apache.calcite.sql.parser.SqlParserPos; |
| 24 | +import org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl; |
| 25 | +import org.apache.calcite.sql.validate.SqlConformanceEnum; |
| 26 | +import org.apache.calcite.sql.validate.SqlValidator; |
| 27 | + |
| 28 | +/** Utility class for parsing CREATE statements into a {@link CalciteSchema} */ |
| 29 | +public class SubstraitCreateStatementParser { |
| 30 | + |
| 31 | + private static final RelDataTypeFactory TYPE_FACTORY = |
| 32 | + new JavaTypeFactoryImpl(SubstraitTypeSystem.TYPE_SYSTEM); |
| 33 | + |
| 34 | + private static final CalciteConnectionConfig CONNECTION_CONFIG = |
| 35 | + CalciteConnectionConfig.DEFAULT.set( |
| 36 | + CalciteConnectionProperty.CASE_SENSITIVE, Boolean.FALSE.toString()); |
| 37 | + |
| 38 | + private static final SqlParser.Config PARSER_CONFIG = |
| 39 | + SqlParser.config() |
| 40 | + // To process CREATE statements we must use the SqlDdlParserImpl, as the default |
| 41 | + // parser does not handle them |
| 42 | + .withParserFactory(SqlDdlParserImpl.FACTORY) |
| 43 | + .withUnquotedCasing(Casing.TO_UPPER) |
| 44 | + .withConformance(SqlConformanceEnum.LENIENT); |
| 45 | + |
| 46 | + private static final CalciteCatalogReader EMPTY_CATALOG = |
| 47 | + new CalciteCatalogReader( |
| 48 | + CalciteSchema.createRootSchema(false), List.of(), TYPE_FACTORY, CONNECTION_CONFIG); |
| 49 | + |
| 50 | + // A validator is needed to convert the types in column declarations to Calcite types |
| 51 | + private static final SqlValidator VALIDATOR = |
| 52 | + new SubstraitSqlValidator( |
| 53 | + // as we are validating CREATE statements, an empty catalog suffices |
| 54 | + EMPTY_CATALOG); |
| 55 | + |
| 56 | + /** |
| 57 | + * @param createStatements a SQL string containing only CREATE statements |
| 58 | + * @return a list of {@link SubstraitTable}s generated from the CREATE statements |
| 59 | + * @throws SqlParseException |
| 60 | + */ |
| 61 | + public static List<SubstraitTable> processCreateStatements(String createStatements) |
| 62 | + throws SqlParseException { |
| 63 | + SqlParser parser = SqlParser.create(createStatements, PARSER_CONFIG); |
| 64 | + List<SubstraitTable> tableList = new ArrayList<>(); |
| 65 | + |
| 66 | + SqlNodeList sqlNode = parser.parseStmtList(); |
| 67 | + for (SqlNode parsed : sqlNode) { |
| 68 | + if (!(parsed instanceof SqlCreateTable create)) { |
| 69 | + throw fail("Not a valid CREATE TABLE statement."); |
| 70 | + } |
| 71 | + |
| 72 | + if (create.name.names.size() > 1) { |
| 73 | + throw fail("Only simple table names are allowed.", create.name.getParserPosition()); |
| 74 | + } |
| 75 | + |
| 76 | + if (create.query != null) { |
| 77 | + throw fail("CTAS not supported.", create.name.getParserPosition()); |
| 78 | + } |
| 79 | + |
| 80 | + List<String> names = new ArrayList<>(); |
| 81 | + List<RelDataType> columnTypes = new ArrayList<>(); |
| 82 | + |
| 83 | + for (SqlNode node : create.columnList) { |
| 84 | + if (!(node instanceof SqlColumnDeclaration col)) { |
| 85 | + if (node instanceof SqlKeyConstraint) { |
| 86 | + // key constraints declarations, like primary key declaration, are valid and should not |
| 87 | + // result in parse exceptions. Ignore the constraint declaration. |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + throw fail("Unexpected column list construction.", node.getParserPosition()); |
| 92 | + } |
| 93 | + |
| 94 | + if (col.name.names.size() != 1) { |
| 95 | + throw fail("Expected simple column names.", col.name.getParserPosition()); |
| 96 | + } |
| 97 | + |
| 98 | + names.add(col.name.names.get(0)); |
| 99 | + columnTypes.add(col.dataType.deriveType(VALIDATOR)); |
| 100 | + } |
| 101 | + |
| 102 | + tableList.add( |
| 103 | + new SubstraitTable( |
| 104 | + create.name.names.get(0), TYPE_FACTORY.createStructType(columnTypes, names))); |
| 105 | + } |
| 106 | + |
| 107 | + return tableList; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param createStatements a SQL string containing only CREATE statements |
| 112 | + * @return a {@link CalciteCatalogReader} generated from the CREATE statements |
| 113 | + * @throws SqlParseException |
| 114 | + */ |
| 115 | + public static CalciteCatalogReader processCreateStatementsToCatalog(List<String> createStatements) |
| 116 | + throws SqlParseException { |
| 117 | + List<SubstraitTable> tables = new ArrayList<>(); |
| 118 | + for (String statement : createStatements) { |
| 119 | + tables.addAll(processCreateStatements(statement)); |
| 120 | + } |
| 121 | + CalciteSchema rootSchema = CalciteSchema.createRootSchema(false); |
| 122 | + for (SubstraitTable table : tables) { |
| 123 | + rootSchema.add(table.getName(), table); |
| 124 | + } |
| 125 | + List<String> defaultSchema = Collections.emptyList(); |
| 126 | + return new CalciteCatalogReader(rootSchema, defaultSchema, TYPE_FACTORY, CONNECTION_CONFIG); |
| 127 | + } |
| 128 | + |
| 129 | + private static SqlParseException fail(String text, SqlParserPos pos) { |
| 130 | + return new SqlParseException(text, pos, null, null, new RuntimeException("fake lineage")); |
| 131 | + } |
| 132 | + |
| 133 | + private static SqlParseException fail(String text) { |
| 134 | + return fail(text, SqlParserPos.ZERO); |
| 135 | + } |
| 136 | +} |
0 commit comments