11package io .substrait .isthmus .sql ;
22
3+ import io .substrait .isthmus .ConverterProvider ;
34import io .substrait .isthmus .SqlConverterBase ;
45import io .substrait .isthmus .SubstraitTypeSystem ;
56import io .substrait .isthmus .Utils ;
1617import org .apache .calcite .sql .ddl .SqlCreateTable ;
1718import org .apache .calcite .sql .ddl .SqlKeyConstraint ;
1819import org .apache .calcite .sql .parser .SqlParseException ;
20+ import org .apache .calcite .sql .parser .SqlParser ;
1921import org .apache .calcite .sql .parser .SqlParserPos ;
2022import org .apache .calcite .sql .validate .SqlValidator ;
2123import org .jspecify .annotations .NonNull ;
@@ -50,28 +52,26 @@ public class SubstraitCreateStatementParser {
5052 */
5153 public static List <SubstraitTable > processCreateStatements (@ NonNull final String createStatements )
5254 throws SqlParseException {
53- final List <SubstraitTable > tableList = new ArrayList <>();
54-
55- final List <SqlNode > sqlNode = SubstraitSqlStatementParser .parseStatements (createStatements );
56- for (final SqlNode parsed : sqlNode ) {
57- if (!(parsed instanceof SqlCreateTable )) {
58- throw fail ("Not a valid CREATE TABLE statement." );
59- }
60-
61- final SqlCreateTable create = (SqlCreateTable ) parsed ;
62-
63- if (create .name .names .size () > 1 ) {
64- throw fail ("Only simple table names are allowed." , create .name .getParserPosition ());
65- }
66-
67- if (create .query != null ) {
68- throw fail ("CTAS not supported." , create .name .getParserPosition ());
69- }
70-
71- tableList .add (createSubstraitTable (create .name .names .get (0 ), create .columnList ));
72- }
55+ return processCreateStatements ((SqlParser .Config ) null , createStatements );
56+ }
7357
74- return tableList ;
58+ /**
59+ * Parses a SQL string containing only CREATE statements into a list of {@link SubstraitTable}s,
60+ * using the parser settings from the given {@link ConverterProvider}.
61+ *
62+ * <p>This method only supports simple table names without any additional qualifiers. Only used
63+ * with {@link io.substrait.isthmus.SqlExpressionToSubstrait}.
64+ *
65+ * @param converterProvider the converter provider whose parser config controls identifier casing
66+ * and other parser settings
67+ * @param createStatements a SQL string containing only CREATE statements; must not be null
68+ * @return list of {@link SubstraitTable}s generated from the CREATE statements
69+ * @throws SqlParseException if parsing fails or statements are invalid
70+ */
71+ public static List <SubstraitTable > processCreateStatements (
72+ @ NonNull final ConverterProvider converterProvider , @ NonNull final String createStatements )
73+ throws SqlParseException {
74+ return processCreateStatements (converterProvider .getSqlParserConfig (), createStatements );
7575 }
7676
7777 /**
@@ -89,6 +89,26 @@ public static CalciteCatalogReader processCreateStatementsToCatalog(
8989 return processCreateStatementsToCatalog (createStatements .toArray (new String [0 ]));
9090 }
9191
92+ /**
93+ * Parses one or more SQL strings containing only CREATE statements into a {@link
94+ * CalciteCatalogReader}, using the parser settings from the given {@link ConverterProvider}.
95+ *
96+ * <p>This method expects the use of fully qualified table names in the CREATE statements.
97+ *
98+ * @param converterProvider the converter provider whose parser config controls identifier casing
99+ * and other parser settings
100+ * @param createStatements List of SQL strings containing only CREATE statements, must not be null
101+ * @return a {@link CalciteCatalogReader} generated from the CREATE statements
102+ * @throws SqlParseException if there is an error parsing the SQL statements
103+ */
104+ public static CalciteCatalogReader processCreateStatementsToCatalog (
105+ @ NonNull final ConverterProvider converterProvider ,
106+ @ NonNull final List <String > createStatements )
107+ throws SqlParseException {
108+ return processCreateStatementsToCatalog (
109+ converterProvider , createStatements .toArray (new String [0 ]));
110+ }
111+
92112 /**
93113 * Parses one or more SQL strings containing only CREATE statements into a {@link
94114 * CalciteCatalogReader}
@@ -101,7 +121,32 @@ public static CalciteCatalogReader processCreateStatementsToCatalog(
101121 */
102122 public static CalciteCatalogReader processCreateStatementsToCatalog (
103123 @ NonNull final String ... createStatements ) throws SqlParseException {
104- final CalciteSchema rootSchema = processCreateStatementsToSchema (createStatements );
124+ final CalciteSchema rootSchema = processCreateStatementsToSchema (null , createStatements );
125+ final List <String > defaultSchema = Collections .emptyList ();
126+ return new CalciteCatalogReader (
127+ rootSchema ,
128+ defaultSchema ,
129+ SubstraitTypeSystem .TYPE_FACTORY ,
130+ SqlConverterBase .CONNECTION_CONFIG );
131+ }
132+
133+ /**
134+ * Parses one or more SQL strings containing only CREATE statements into a {@link
135+ * CalciteCatalogReader}, using the parser settings from the given {@link ConverterProvider}.
136+ *
137+ * <p>This method expects the use of fully qualified table names in the CREATE statements.
138+ *
139+ * @param converterProvider the converter provider whose parser config controls identifier casing
140+ * and other parser settings
141+ * @param createStatements a SQL string containing only CREATE statements, must not be null
142+ * @return a {@link CalciteCatalogReader} generated from the CREATE statements
143+ * @throws SqlParseException if parsing fails or statements are invalid
144+ */
145+ public static CalciteCatalogReader processCreateStatementsToCatalog (
146+ @ NonNull final ConverterProvider converterProvider , @ NonNull final String ... createStatements )
147+ throws SqlParseException {
148+ final CalciteSchema rootSchema =
149+ processCreateStatementsToSchema (converterProvider .getSqlParserConfig (), createStatements );
105150 final List <String > defaultSchema = Collections .emptyList ();
106151 return new CalciteCatalogReader (
107152 rootSchema ,
@@ -133,19 +178,53 @@ private static SqlParseException fail(@Nullable final String message) {
133178 }
134179
135180 /**
136- * Parses one or more SQL strings containing only CREATE statements into a {@link CalciteSchema}.
137- *
138- * @param createStatements one or more SQL strings containing only CREATE statements; must not be
139- * null
140- * @return a {@link CalciteSchema} generated from the CREATE statements
141- * @throws SqlParseException if parsing fails or statements are invalid
181+ * Parses a SQL string containing only CREATE statements into a list of {@link SubstraitTable}s
182+ * using the given parser config (may be {@code null} for the default).
183+ */
184+ private static List <SubstraitTable > processCreateStatements (
185+ SqlParser .Config parserConfig , @ NonNull final String createStatements )
186+ throws SqlParseException {
187+ final List <SubstraitTable > tableList = new ArrayList <>();
188+
189+ final List <SqlNode > sqlNode =
190+ parserConfig == null
191+ ? SubstraitSqlStatementParser .parseStatements (createStatements )
192+ : SubstraitSqlStatementParser .parseStatements (createStatements , parserConfig );
193+ for (final SqlNode parsed : sqlNode ) {
194+ if (!(parsed instanceof SqlCreateTable )) {
195+ throw fail ("Not a valid CREATE TABLE statement." );
196+ }
197+
198+ final SqlCreateTable create = (SqlCreateTable ) parsed ;
199+
200+ if (create .name .names .size () > 1 ) {
201+ throw fail ("Only simple table names are allowed." , create .name .getParserPosition ());
202+ }
203+
204+ if (create .query != null ) {
205+ throw fail ("CTAS not supported." , create .name .getParserPosition ());
206+ }
207+
208+ tableList .add (createSubstraitTable (create .name .names .get (0 ), create .columnList ));
209+ }
210+
211+ return tableList ;
212+ }
213+
214+ /**
215+ * Parses one or more SQL strings containing only CREATE statements into a {@link CalciteSchema}
216+ * using the given parser config (may be {@code null} for the default).
142217 */
143218 private static CalciteSchema processCreateStatementsToSchema (
144- @ NonNull final String ... createStatements ) throws SqlParseException {
219+ SqlParser .Config parserConfig , @ NonNull final String ... createStatements )
220+ throws SqlParseException {
145221 final CalciteSchema rootSchema = CalciteSchema .createRootSchema (false );
146222
147223 for (final String statement : createStatements ) {
148- final List <SqlNode > sqlNode = SubstraitSqlStatementParser .parseStatements (statement );
224+ final List <SqlNode > sqlNode =
225+ parserConfig == null
226+ ? SubstraitSqlStatementParser .parseStatements (statement )
227+ : SubstraitSqlStatementParser .parseStatements (statement , parserConfig );
149228 for (final SqlNode parsed : sqlNode ) {
150229 if (!(parsed instanceof SqlCreateTable )) {
151230 throw fail ("Not a valid CREATE TABLE statement." );
0 commit comments