2727import org .apache .calcite .sql2rel .SqlToRelConverter ;
2828import org .apache .calcite .sql2rel .StandardConvertletTable ;
2929
30+ /**
31+ * Converts SQL expressions to Substrait {@link io.substrait.proto.ExtendedExpression} payloads.
32+ *
33+ * <p>Supports optional CREATE TABLE statements to provide schema and column bindings for expression
34+ * validation and Rex conversion.
35+ */
3036public class SqlExpressionToSubstrait extends SqlConverterBase {
3137
38+ /** Converter for RexNodes to Substrait expressions. */
3239 protected final RexExpressionConverter rexConverter ;
3340
41+ /** Creates a converter with default features and the default extension catalog. */
3442 public SqlExpressionToSubstrait () {
3543 this (new ConverterProvider ());
3644 }
3745
46+ /**
47+ * Creates a converter with the given feature board and extension collection.
48+ *
49+ * @param converterProvider Converter Provider to use for configuration
50+ */
3851 public SqlExpressionToSubstrait (ConverterProvider converterProvider ) {
3952 super (converterProvider );
4053 this .rexConverter = new RexExpressionConverter (converterProvider .getScalarFunctionConverter ());
4154 }
4255
56+ /** Bundled result carrying validator, catalog reader, and name/type and name/node maps. */
4357 private static final class Result {
4458 final SqlValidator validator ;
4559 final CalciteCatalogReader catalogReader ;
4660 final Map <String , RelDataType > nameToTypeMap ;
4761 final Map <String , RexNode > nameToNodeMap ;
4862
63+ /**
64+ * Creates a result bundle.
65+ *
66+ * @param validator SQL validator
67+ * @param catalogReader Calcite catalog reader
68+ * @param nameToTypeMap mapping from column name to Calcite type
69+ * @param nameToNodeMap mapping from column name to Rex input ref
70+ */
4971 Result (
5072 SqlValidator validator ,
5173 CalciteCatalogReader catalogReader ,
@@ -59,25 +81,25 @@ private static final class Result {
5981 }
6082
6183 /**
62- * Converts the given SQL expression to an {@link io.substrait.proto.ExtendedExpression }
84+ * Converts a single SQL expression to a Substrait {@link io.substrait.proto.ExtendedExpression}.
6385 *
6486 * @param sqlExpression a SQL expression
6587 * @param createStatements table creation statements defining fields referenced by the expression
66- * @return a {@link io.substrait.proto.ExtendedExpression }
67- * @throws SqlParseException
88+ * @return the Substrait extended expression proto
89+ * @throws SqlParseException if parsing or validation fails
6890 */
6991 public io .substrait .proto .ExtendedExpression convert (
7092 String sqlExpression , List <String > createStatements ) throws SqlParseException {
7193 return convert (new String [] {sqlExpression }, createStatements );
7294 }
7395
7496 /**
75- * Converts the given SQL expressions to an {@link io.substrait.proto.ExtendedExpression }
97+ * Converts multiple SQL expressions to a Substrait {@link io.substrait.proto.ExtendedExpression}.
7698 *
77- * @param sqlExpressions an array of SQL expressions
78- * @param createStatements table creation statements defining fields referenced by the expression
79- * @return a {@link io.substrait.proto.ExtendedExpression }
80- * @throws SqlParseException
99+ * @param sqlExpressions array of SQL expressions
100+ * @param createStatements table creation statements defining fields referenced by the expressions
101+ * @return the Substrait extended expression proto
102+ * @throws SqlParseException if parsing or validation fails
81103 */
82104 public io .substrait .proto .ExtendedExpression convert (
83105 String [] sqlExpressions , List <String > createStatements ) throws SqlParseException {
@@ -90,6 +112,17 @@ public io.substrait.proto.ExtendedExpression convert(
90112 result .nameToNodeMap );
91113 }
92114
115+ /**
116+ * Converts the given SQL expressions using the provided validator/catalog and column bindings.
117+ *
118+ * @param sqlExpressions array of SQL expressions
119+ * @param validator SQL validator
120+ * @param catalogReader Calcite catalog reader
121+ * @param nameToTypeMap mapping from column name to Calcite type
122+ * @param nameToNodeMap mapping from column name to Rex input ref
123+ * @return the Substrait extended expression proto
124+ * @throws SqlParseException if parsing or validation fails
125+ */
93126 private io .substrait .proto .ExtendedExpression executeInnerSQLExpressions (
94127 String [] sqlExpressions ,
95128 SqlValidator validator ,
@@ -120,6 +153,17 @@ private io.substrait.proto.ExtendedExpression executeInnerSQLExpressions(
120153 return new ExtendedExpressionProtoConverter ().toProto (extendedExpression .build ());
121154 }
122155
156+ /**
157+ * Parses and validates a SQL expression, then converts it to a {@link RexNode}.
158+ *
159+ * @param sql SQL expression string
160+ * @param validator SQL validator
161+ * @param catalogReader Calcite catalog reader
162+ * @param nameToTypeMap mapping from column name to Calcite type
163+ * @param nameToNodeMap mapping from column name to Rex input ref
164+ * @return the converted RexNode
165+ * @throws SqlParseException if parsing or validation fails
166+ */
123167 private RexNode sqlToRexNode (
124168 String sql ,
125169 SqlValidator validator ,
@@ -141,6 +185,13 @@ private RexNode sqlToRexNode(
141185 return converter .convertExpression (validSqlNode , nameToNodeMap );
142186 }
143187
188+ /**
189+ * Registers tables from CREATE statements and prepares validator, catalog, and column bindings.
190+ *
191+ * @param tables list of CREATE TABLE statements; may be null
192+ * @return result bundle containing validator, catalog reader, and name/type and name/node maps
193+ * @throws SqlParseException if any CREATE statement is invalid
194+ */
144195 private Result registerCreateTablesForExtendedExpression (List <String > tables )
145196 throws SqlParseException {
146197 Map <String , RelDataType > nameToTypeMap = new LinkedHashMap <>();
@@ -177,6 +228,12 @@ private Result registerCreateTablesForExtendedExpression(List<String> tables)
177228 return new Result (validator , catalogReader , nameToTypeMap , nameToNodeMap );
178229 }
179230
231+ /**
232+ * Converts a name-to-type map into a {@link NamedStruct} in Substrait types.
233+ *
234+ * @param nameToTypeMap mapping from column name to Calcite type
235+ * @return a {@link NamedStruct} with non-nullable struct type
236+ */
180237 private NamedStruct toNamedStruct (Map <String , RelDataType > nameToTypeMap ) {
181238 ArrayList <String > names = new ArrayList <String >();
182239 ArrayList <Type > types = new ArrayList <Type >();
0 commit comments