diff --git a/examples/isthmus-api/src/main/java/io/substrait/examples/CustomDialectDynamicFnToSql.java b/examples/isthmus-api/src/main/java/io/substrait/examples/CustomDialectDynamicFnToSql.java index 61375262c..34e1a4e69 100644 --- a/examples/isthmus-api/src/main/java/io/substrait/examples/CustomDialectDynamicFnToSql.java +++ b/examples/isthmus-api/src/main/java/io/substrait/examples/CustomDialectDynamicFnToSql.java @@ -5,6 +5,7 @@ import io.substrait.expression.Expression; import io.substrait.extension.DefaultExtensionCatalog; import io.substrait.extension.SimpleExtension; +import io.substrait.isthmus.ConverterProvider; import io.substrait.isthmus.DynamicConverterProvider; import io.substrait.isthmus.SubstraitToSql; import io.substrait.plan.Plan; @@ -79,7 +80,8 @@ public void run(final String[] args) { // Convert the plan to SQL final SubstraitToSql substraitToSql = - new SubstraitToSql(new DynamicConverterProvider(extensions)); + new SubstraitToSql( + new DynamicConverterProvider(ConverterProvider.builder().extensions(extensions))); System.out.println("\nWith custom SparkSQL SqlDialect::"); substraitToSql.convert(plan, customSqlDialect()).stream().forEachOrdered(System.out::println); diff --git a/examples/isthmus-api/src/main/java/io/substrait/examples/DynamicFnToSql.java b/examples/isthmus-api/src/main/java/io/substrait/examples/DynamicFnToSql.java index 0c3193781..b637c4b61 100644 --- a/examples/isthmus-api/src/main/java/io/substrait/examples/DynamicFnToSql.java +++ b/examples/isthmus-api/src/main/java/io/substrait/examples/DynamicFnToSql.java @@ -5,6 +5,7 @@ import io.substrait.expression.Expression; import io.substrait.extension.DefaultExtensionCatalog; import io.substrait.extension.SimpleExtension; +import io.substrait.isthmus.ConverterProvider; import io.substrait.isthmus.DynamicConverterProvider; import io.substrait.isthmus.SubstraitToSql; import io.substrait.plan.Plan; @@ -77,7 +78,8 @@ public void run(final String[] args) { // Convert the plan to SQL final SubstraitToSql substraitToSql = - new SubstraitToSql(new DynamicConverterProvider(extensions)); + new SubstraitToSql( + new DynamicConverterProvider(ConverterProvider.builder().extensions(extensions))); System.out.println("\nWith default DuckDB SqlDialect::"); substraitToSql.convert(plan, SqlDialect.DatabaseProduct.DUCKDB.getDialect()).stream() .forEachOrdered(System.out::println); diff --git a/examples/isthmus-api/src/main/java/io/substrait/examples/FromSql.java b/examples/isthmus-api/src/main/java/io/substrait/examples/FromSql.java index 17973b683..15f179f60 100644 --- a/examples/isthmus-api/src/main/java/io/substrait/examples/FromSql.java +++ b/examples/isthmus-api/src/main/java/io/substrait/examples/FromSql.java @@ -1,6 +1,7 @@ package io.substrait.examples; import io.substrait.examples.IsthmusAppExamples.Action; +import io.substrait.isthmus.ConverterProvider; import io.substrait.isthmus.SqlToSubstrait; import io.substrait.isthmus.sql.SubstraitCreateStatementParser; import io.substrait.plan.Plan; @@ -10,8 +11,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import org.apache.calcite.avatica.util.Casing; import org.apache.calcite.prepare.CalciteCatalogReader; -import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.parser.SqlParseException; /** @@ -22,7 +23,7 @@ *
1. Create a fully typed schema for the inputs. Within a SQL context this represents the CREATE * TABLE commands, which need to be converted to a Calcite Schema. * - *
2. Parse the SQL query to convert (in the source SQL dialect). + *
2. Parse the SQL query to convert. * *
3. Convert the SQL query to Calcite Relations. * @@ -49,22 +50,27 @@ public void run(final String[] args) { "test_result" varchar(15),"test_mileage" int, "postcode_area" varchar(15)); """); + // The unquoted identifier casing applied while parsing is configurable via a + // ConverterProvider. The same provider is used for both the schema and the query so that + // identifier casing stays consistent end-to-end. Casing.UNCHANGED preserves identifiers as + // written, matching the lower-case names used in the CREATE TABLE statements above. + final ConverterProvider converterProvider = + ConverterProvider.builder().unquotedCasing(Casing.UNCHANGED).build(); + final CalciteCatalogReader catalogReader = - SubstraitCreateStatementParser.processCreateStatementsToCatalog(createSqlStatements); + SubstraitCreateStatementParser.processCreateStatementsToCatalog( + converterProvider, createSqlStatements); - // Query that needs to be converted; again this could be in a variety of SQL - // dialects + // Query that needs to be converted final String sqlQuery = """ SELECT vehicles.colour, count(*) as colourcount FROM vehicles INNER JOIN tests ON vehicles.vehicle_id=tests.vehicle_id WHERE tests.test_result = 'P' GROUP BY vehicles.colour ORDER BY count(*) """; - final SqlToSubstrait sqlToSubstrait = new SqlToSubstrait(); - // choose DuckDB as an example dialect - final SqlDialect dialect = SqlDialect.DatabaseProduct.DUCKDB.getDialect(); - final Plan substraitPlan = sqlToSubstrait.convert(sqlQuery, catalogReader, dialect); + final SqlToSubstrait sqlToSubstrait = new SqlToSubstrait(converterProvider); + final Plan substraitPlan = sqlToSubstrait.convert(sqlQuery, catalogReader); // Create the proto plan to display to stdout - as it has a better format final PlanProtoConverter planToProto = new PlanProtoConverter(); diff --git a/examples/isthmus-api/src/main/java/io/substrait/examples/ToOptimizedSql.java b/examples/isthmus-api/src/main/java/io/substrait/examples/ToOptimizedSql.java index 9500262c5..a09ac7705 100644 --- a/examples/isthmus-api/src/main/java/io/substrait/examples/ToOptimizedSql.java +++ b/examples/isthmus-api/src/main/java/io/substrait/examples/ToOptimizedSql.java @@ -40,7 +40,8 @@ public void run(String[] args) { final Plan substraitPlan = new ProtoPlanConverter().from(proto); // Configure Isthmus Utilities - final SubstraitToCalcite substraitToCalcite = new SubstraitToCalcite(new ConverterProvider()); + final SubstraitToCalcite substraitToCalcite = + new SubstraitToCalcite(ConverterProvider.DEFAULT); // Configure Calcite Utilities final SqlDialect sqlDialect = SqlDialect.DatabaseProduct.MYSQL.getDialect(); diff --git a/isthmus-cli/src/main/java/io/substrait/isthmus/cli/IsthmusEntryPoint.java b/isthmus-cli/src/main/java/io/substrait/isthmus/cli/IsthmusEntryPoint.java index 03d4fef2b..8c4a40098 100644 --- a/isthmus-cli/src/main/java/io/substrait/isthmus/cli/IsthmusEntryPoint.java +++ b/isthmus-cli/src/main/java/io/substrait/isthmus/cli/IsthmusEntryPoint.java @@ -15,7 +15,6 @@ import java.util.concurrent.Callable; import org.apache.calcite.avatica.util.Casing; import org.apache.calcite.prepare.Prepare; -import org.apache.calcite.sql.parser.SqlParser; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; @@ -60,15 +59,6 @@ enum OutputFormat { description = "Calcite's casing policy for unquoted identifiers: ${COMPLETION-CANDIDATES}") private Casing unquotedCasing = Casing.TO_UPPER; - private ConverterProvider converterProvider() { - return new ConverterProvider() { - @Override - public SqlParser.Config getSqlParserConfig() { - return super.getSqlParserConfig().withUnquotedCasing(unquotedCasing); - } - }; - } - /** * Standard Java Main method invoked by the isthmus CLI command. * @@ -96,16 +86,17 @@ public static void main(String... args) { @Override public Integer call() throws Exception { + ConverterProvider provider = ConverterProvider.builder().unquotedCasing(unquotedCasing).build(); // Isthmus image is parsing SQL Expression if that argument is defined if (sqlExpressions != null) { - SqlExpressionToSubstrait converter = new SqlExpressionToSubstrait(converterProvider()); + SqlExpressionToSubstrait converter = new SqlExpressionToSubstrait(provider); ExtendedExpression extendedExpression = converter.convert(sqlExpressions, createStatements); printMessage(extendedExpression); } else { // by default Isthmus image are parsing SQL Query - SqlToSubstrait converter = new SqlToSubstrait(converterProvider()); + SqlToSubstrait converter = new SqlToSubstrait(provider); Prepare.CatalogReader catalog = SubstraitCreateStatementParser.processCreateStatementsToCatalog( - createStatements.toArray(String[]::new)); + provider, createStatements); Plan plan = new PlanProtoConverter().toProto(converter.convert(sql, catalog)); printMessage(plan); } diff --git a/isthmus/src/main/java/io/substrait/isthmus/AutomaticDynamicFunctionMappingConverterProvider.java b/isthmus/src/main/java/io/substrait/isthmus/AutomaticDynamicFunctionMappingConverterProvider.java index 7d8a3726e..9869625c6 100644 --- a/isthmus/src/main/java/io/substrait/isthmus/AutomaticDynamicFunctionMappingConverterProvider.java +++ b/isthmus/src/main/java/io/substrait/isthmus/AutomaticDynamicFunctionMappingConverterProvider.java @@ -47,9 +47,14 @@ public class AutomaticDynamicFunctionMappingConverterProvider extends ConverterP * *
Uses {@link DefaultExtensionCatalog#DEFAULT_COLLECTION} for extensions and {@link * SubstraitTypeSystem#TYPE_FACTORY} for type operations. + * + * @deprecated Use {@link + * #AutomaticDynamicFunctionMappingConverterProvider(ConverterProvider.Builder)} with {@link + * ConverterProvider#builder()} instead. */ + @Deprecated public AutomaticDynamicFunctionMappingConverterProvider() { - this(DefaultExtensionCatalog.DEFAULT_COLLECTION, SubstraitTypeSystem.TYPE_FACTORY); + this(ConverterProvider.builder()); } /** @@ -58,10 +63,15 @@ public AutomaticDynamicFunctionMappingConverterProvider() { *
Uses {@link SubstraitTypeSystem#TYPE_FACTORY} for type operations.
*
* @param extensions the extension collection containing function definitions
+ * @deprecated Use {@link
+ * #AutomaticDynamicFunctionMappingConverterProvider(ConverterProvider.Builder)} instead, e.g.
+ * {@code new AutomaticDynamicFunctionMappingConverterProvider(
+ * ConverterProvider.builder().extensions(extensions))}.
*/
+ @Deprecated
public AutomaticDynamicFunctionMappingConverterProvider(
SimpleExtension.ExtensionCollection extensions) {
- this(extensions, SubstraitTypeSystem.TYPE_FACTORY);
+ this(ConverterProvider.builder().extensions(extensions));
}
/**
@@ -72,10 +82,27 @@ public AutomaticDynamicFunctionMappingConverterProvider(
*
* @param extensions the extension collection containing function definitions
* @param typeFactory the type factory for creating and managing Calcite data types
+ * @deprecated Use {@link
+ * #AutomaticDynamicFunctionMappingConverterProvider(ConverterProvider.Builder)} instead, e.g.
+ * {@code new AutomaticDynamicFunctionMappingConverterProvider(
+ * ConverterProvider.builder().extensions(extensions).typeFactory(typeFactory))}.
*/
+ @Deprecated
public AutomaticDynamicFunctionMappingConverterProvider(
SimpleExtension.ExtensionCollection extensions, RelDataTypeFactory typeFactory) {
- super(extensions, typeFactory);
+ this(ConverterProvider.builder().extensions(extensions).typeFactory(typeFactory));
+ }
+
+ /**
+ * Creates a new provider from a {@link ConverterProvider.Builder}, seeding base state from the
+ * builder and then installing the automatically generated dynamic function mappings and operator
+ * table. This is the seam through which the other constructors — and subclasses, via {@code
+ * super(builder)} — route.
+ *
+ * @param builder the builder carrying the configured components
+ */
+ public AutomaticDynamicFunctionMappingConverterProvider(ConverterProvider.Builder builder) {
+ super(builder);
List It is consumed by all conversion classes as their primary source of configuration.
*
- * The no argument constructor {@link #ConverterProvider()} provides reasonable system defaults.
+ * {@link #DEFAULT} is a shared instance configured with reasonable system defaults, equivalent
+ * to {@code builder().build()}.
*
- * Other constructors allow for further customization of conversion behaviours.
+ * For customized conversion behaviour — including supplying a full Calcite {@link
+ * SqlParser.Config} for SQL parsing — use the {@link #builder()}.
*
* More in-depth customization can be achieved by extending this class, as is done in {@link
* DynamicConverterProvider}.
*/
public class ConverterProvider {
+ /**
+ * The default Calcite {@link SqlParser.Config} used by isthmus: {@link SqlParser.Config#DEFAULT}
+ * with {@link Casing#TO_UPPER} unquoted-identifier casing, the {@link SqlDdlParserImpl} parser
+ * factory (so {@code CREATE TABLE} statements parse), and {@link SqlConformanceEnum#LENIENT}
+ * conformance.
+ *
+ * This is the recommended starting point for a customized parser configuration: derive from it
+ * with Calcite's {@code withXxx} methods and pass the result to {@link
+ * Builder#sqlParserConfig(SqlParser.Config)}, e.g. {@code
+ * DEFAULT_SQL_PARSER_CONFIG.withUnquotedCasing(Casing.UNCHANGED)}.
+ */
+ public static final SqlParser.Config DEFAULT_SQL_PARSER_CONFIG =
+ SqlParser.Config.DEFAULT
+ .withUnquotedCasing(Casing.TO_UPPER)
+ .withParserFactory(SqlDdlParserImpl.FACTORY)
+ .withConformance(SqlConformanceEnum.LENIENT);
+
+ /**
+ * A shared default {@link ConverterProvider} instance using all system defaults. Equivalent to
+ * {@code builder().build()} but avoids redundant construction at every call site.
+ *
+ * This instance is safe to share because {@link ConverterProvider} is effectively immutable
+ * after construction — all fields are set only in constructors.
+ */
+ public static final ConverterProvider DEFAULT = builder().build();
+
/** The Calcite type factory used for creating and managing data types. */
protected RelDataTypeFactory typeFactory;
/** The collection of Substrait extensions (functions and types) available for conversion. */
protected final SimpleExtension.ExtensionCollection extensions;
+ /** The Calcite SQL parser configuration, controlling parsing behaviour like identifier casing. */
+ protected final SqlParser.Config sqlParserConfig;
+
/** Converter for Substrait scalar functions. */
protected ScalarFunctionConverter scalarFunctionConverter;
@@ -73,18 +104,24 @@ public class ConverterProvider {
/**
* Creates a ConverterProvider with default extension collection and type factory. Uses {@link
* DefaultExtensionCatalog#DEFAULT_COLLECTION} and {@link SubstraitTypeSystem#TYPE_FACTORY}.
+ *
+ * @deprecated Use {@link #builder()} (or the shared {@link #DEFAULT} instance) instead.
*/
+ @Deprecated
public ConverterProvider() {
- this(DefaultExtensionCatalog.DEFAULT_COLLECTION, SubstraitTypeSystem.TYPE_FACTORY);
+ this(builder());
}
/**
* Creates a ConverterProvider with the specified extension collection and default type factory.
*
* @param extensions the Substrait extension collection to use
+ * @deprecated Use {@link #builder()} instead, e.g. {@code
+ * builder().extensions(extensions).build()}.
*/
+ @Deprecated
public ConverterProvider(SimpleExtension.ExtensionCollection extensions) {
- this(extensions, SubstraitTypeSystem.TYPE_FACTORY);
+ this(builder().extensions(extensions));
}
/**
@@ -92,16 +129,13 @@ public ConverterProvider(SimpleExtension.ExtensionCollection extensions) {
*
* @param extensions the Substrait extension collection to use
* @param typeFactory the Calcite type factory to use
+ * @deprecated Use {@link #builder()} instead, e.g. {@code
+ * builder().extensions(extensions).typeFactory(typeFactory).build()}.
*/
+ @Deprecated
public ConverterProvider(
SimpleExtension.ExtensionCollection extensions, RelDataTypeFactory typeFactory) {
- this(
- typeFactory,
- extensions,
- new ScalarFunctionConverter(extensions.scalarFunctions(), typeFactory),
- new AggregateFunctionConverter(extensions.aggregateFunctions(), typeFactory),
- new WindowFunctionConverter(extensions.windowFunctions(), typeFactory),
- TypeConverter.DEFAULT);
+ this(builder().extensions(extensions).typeFactory(typeFactory));
}
/**
@@ -113,7 +147,10 @@ public ConverterProvider(
* @param afc the aggregate function converter to use
* @param wfc the window function converter to use
* @param tc the type converter to use
+ * @deprecated Use {@link #builder()} instead; the growing set of components is more readably
+ * configured through the builder than through this positional constructor.
*/
+ @Deprecated
public ConverterProvider(
RelDataTypeFactory typeFactory,
SimpleExtension.ExtensionCollection extensions,
@@ -121,7 +158,14 @@ public ConverterProvider(
AggregateFunctionConverter afc,
WindowFunctionConverter wfc,
TypeConverter tc) {
- this(typeFactory, extensions, sfc, afc, wfc, tc, createDefaultExecutionBehavior());
+ this(
+ builder()
+ .typeFactory(typeFactory)
+ .extensions(extensions)
+ .scalarFunctionConverter(sfc)
+ .aggregateFunctionConverter(afc)
+ .windowFunctionConverter(wfc)
+ .typeConverter(tc));
}
/**
@@ -134,7 +178,10 @@ public ConverterProvider(
* @param wfc the window function converter to use
* @param tc the type converter to use
* @param executionBehavior the execution behavior to use for plans
+ * @deprecated Use {@link #builder()} instead; the growing set of components is more readably
+ * configured through the builder than through this positional constructor.
*/
+ @Deprecated
public ConverterProvider(
RelDataTypeFactory typeFactory,
SimpleExtension.ExtensionCollection extensions,
@@ -143,13 +190,50 @@ public ConverterProvider(
WindowFunctionConverter wfc,
TypeConverter tc,
Plan.ExecutionBehavior executionBehavior) {
- this.typeFactory = typeFactory;
- this.extensions = extensions;
- this.scalarFunctionConverter = sfc;
- this.aggregateFunctionConverter = afc;
- this.windowFunctionConverter = wfc;
- this.typeConverter = tc;
- this.executionBehavior = executionBehavior;
+ this(
+ builder()
+ .typeFactory(typeFactory)
+ .extensions(extensions)
+ .scalarFunctionConverter(sfc)
+ .aggregateFunctionConverter(afc)
+ .windowFunctionConverter(wfc)
+ .typeConverter(tc)
+ .executionBehavior(executionBehavior));
+ }
+
+ /**
+ * Master constructor and the sole subclassing seam: derives any unset function converters from
+ * the configured extensions and type factory, then assigns all components. {@link
+ * Builder#build()} and every delegating public constructor route here, and subclasses (e.g.
+ * {@link DynamicConverterProvider}) invoke it via {@code super(builder)}.
+ *
+ * Taking the {@link Builder} rather than a positional argument list keeps this seam stable as
+ * new components are added: a new field is a change to the builder and this constructor only, not
+ * to every subclass's {@code super(...)} call.
+ *
+ * @param builder the builder carrying the configured components
+ */
+ protected ConverterProvider(Builder builder) {
+ this.typeFactory = builder.typeFactory;
+ this.extensions = builder.extensions;
+ this.scalarFunctionConverter =
+ builder.scalarFunctionConverter != null
+ ? builder.scalarFunctionConverter
+ : new ScalarFunctionConverter(
+ builder.extensions.scalarFunctions(), builder.typeFactory);
+ this.aggregateFunctionConverter =
+ builder.aggregateFunctionConverter != null
+ ? builder.aggregateFunctionConverter
+ : new AggregateFunctionConverter(
+ builder.extensions.aggregateFunctions(), builder.typeFactory);
+ this.windowFunctionConverter =
+ builder.windowFunctionConverter != null
+ ? builder.windowFunctionConverter
+ : new WindowFunctionConverter(
+ builder.extensions.windowFunctions(), builder.typeFactory);
+ this.typeConverter = builder.typeConverter;
+ this.executionBehavior = builder.executionBehavior;
+ this.sqlParserConfig = builder.sqlParserConfig;
}
/**
@@ -169,13 +253,14 @@ private static Plan.ExecutionBehavior createDefaultExecutionBehavior() {
* {@link SqlParser.Config} is a Calcite class which controls SQL parsing behaviour like
* identifier casing.
*
+ * Defaults to {@link #DEFAULT_SQL_PARSER_CONFIG}. Provide a custom configuration via {@link
+ * Builder#sqlParserConfig(SqlParser.Config)} (or the {@link Builder#unquotedCasing(Casing)}
+ * convenience), or override this method in a subclass for fully dynamic behaviour.
+ *
* @return the SQL parser configuration
*/
public SqlParser.Config getSqlParserConfig() {
- return SqlParser.Config.DEFAULT
- .withUnquotedCasing(Casing.TO_UPPER)
- .withParserFactory(SqlDdlParserImpl.FACTORY)
- .withConformance(SqlConformanceEnum.LENIENT);
+ return sqlParserConfig;
}
/**
@@ -415,12 +500,168 @@ public TypeConverter getTypeConverter() {
*
* The default execution behavior uses {@link
* Plan.ExecutionBehavior.VariableEvaluationMode#PER_PLAN}, which evaluates variables once per
- * plan execution. This can be customized by providing a different execution behavior through the
- * constructor.
+ * plan execution. This can be customized via {@link
+ * Builder#executionBehavior(Plan.ExecutionBehavior)}.
*
* @return the execution behavior to use when creating plans
*/
public Plan.ExecutionBehavior getExecutionBehavior() {
return executionBehavior;
}
+
+ /**
+ * Creates a new {@link Builder} for configuring a {@link ConverterProvider}.
+ *
+ * The builder starts from reasonable system defaults (the same ones behind {@link #DEFAULT})
+ * and lets callers override individual components — most notably the Calcite {@link
+ * SqlParser.Config} used for SQL parsing, via {@link Builder#sqlParserConfig(SqlParser.Config)}
+ * for full control or {@link Builder#unquotedCasing(Casing)} for the common casing-only case.
+ *
+ * @return a new builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Fluent builder for {@link ConverterProvider}.
+ *
+ * Unset components fall back to reasonable system defaults. The scalar, aggregate and window
+ * function converters, if not set explicitly, are derived from the configured {@link
+ * #extensions(SimpleExtension.ExtensionCollection) extensions} and {@link
+ * #typeFactory(RelDataTypeFactory) type factory} when {@link #build()} is called.
+ */
+ public static class Builder {
+ private SimpleExtension.ExtensionCollection extensions =
+ DefaultExtensionCatalog.DEFAULT_COLLECTION;
+ private RelDataTypeFactory typeFactory = SubstraitTypeSystem.TYPE_FACTORY;
+ private ScalarFunctionConverter scalarFunctionConverter;
+ private AggregateFunctionConverter aggregateFunctionConverter;
+ private WindowFunctionConverter windowFunctionConverter;
+ private TypeConverter typeConverter = TypeConverter.DEFAULT;
+ private Plan.ExecutionBehavior executionBehavior = createDefaultExecutionBehavior();
+ private SqlParser.Config sqlParserConfig = DEFAULT_SQL_PARSER_CONFIG;
+
+ /**
+ * Sets the Substrait extension collection to use.
+ *
+ * @param extensions the extension collection
+ * @return this builder
+ */
+ public Builder extensions(SimpleExtension.ExtensionCollection extensions) {
+ this.extensions = extensions;
+ return this;
+ }
+
+ /**
+ * Sets the Calcite type factory to use.
+ *
+ * @param typeFactory the type factory
+ * @return this builder
+ */
+ public Builder typeFactory(RelDataTypeFactory typeFactory) {
+ this.typeFactory = typeFactory;
+ return this;
+ }
+
+ /**
+ * Sets the scalar function converter. When left unset, it is derived from the configured
+ * extensions and type factory.
+ *
+ * @param scalarFunctionConverter the scalar function converter
+ * @return this builder
+ */
+ public Builder scalarFunctionConverter(ScalarFunctionConverter scalarFunctionConverter) {
+ this.scalarFunctionConverter = scalarFunctionConverter;
+ return this;
+ }
+
+ /**
+ * Sets the aggregate function converter. When left unset, it is derived from the configured
+ * extensions and type factory.
+ *
+ * @param aggregateFunctionConverter the aggregate function converter
+ * @return this builder
+ */
+ public Builder aggregateFunctionConverter(
+ AggregateFunctionConverter aggregateFunctionConverter) {
+ this.aggregateFunctionConverter = aggregateFunctionConverter;
+ return this;
+ }
+
+ /**
+ * Sets the window function converter. When left unset, it is derived from the configured
+ * extensions and type factory.
+ *
+ * @param windowFunctionConverter the window function converter
+ * @return this builder
+ */
+ public Builder windowFunctionConverter(WindowFunctionConverter windowFunctionConverter) {
+ this.windowFunctionConverter = windowFunctionConverter;
+ return this;
+ }
+
+ /**
+ * Sets the type converter.
+ *
+ * @param typeConverter the type converter
+ * @return this builder
+ */
+ public Builder typeConverter(TypeConverter typeConverter) {
+ this.typeConverter = typeConverter;
+ return this;
+ }
+
+ /**
+ * Sets the execution behavior for plans created by the resulting converter.
+ *
+ * @param executionBehavior the execution behavior
+ * @return this builder
+ */
+ public Builder executionBehavior(Plan.ExecutionBehavior executionBehavior) {
+ this.executionBehavior = executionBehavior;
+ return this;
+ }
+
+ /**
+ * Sets the full Calcite {@link SqlParser.Config} used for SQL parsing, replacing the default.
+ *
+ * Use {@link ConverterProvider#DEFAULT_SQL_PARSER_CONFIG} as a starting point to retain
+ * isthmus' DDL parser factory and conformance while overriding individual settings.
+ *
+ * @param sqlParserConfig the parser configuration
+ * @return this builder
+ */
+ public Builder sqlParserConfig(SqlParser.Config sqlParserConfig) {
+ this.sqlParserConfig = sqlParserConfig;
+ return this;
+ }
+
+ /**
+ * Convenience for the common case of overriding only the unquoted-identifier casing, applied on
+ * top of the current {@link #sqlParserConfig(SqlParser.Config) parser configuration}.
+ *
+ * Equivalent to {@code sqlParserConfig(currentConfig.withUnquotedCasing(unquotedCasing))}.
+ * Because it layers onto the current configuration, a subsequent {@link
+ * #sqlParserConfig(SqlParser.Config)} call replaces the whole configuration and discards the
+ * casing set here; set the full config first, then apply this convenience.
+ *
+ * @param unquotedCasing the casing to apply to unquoted SQL identifiers during parsing
+ * @return this builder
+ */
+ public Builder unquotedCasing(Casing unquotedCasing) {
+ this.sqlParserConfig = this.sqlParserConfig.withUnquotedCasing(unquotedCasing);
+ return this;
+ }
+
+ /**
+ * Builds a {@link ConverterProvider} from the configured components, deriving any unset
+ * function converters from the configured extensions and type factory.
+ *
+ * @return a new {@link ConverterProvider}
+ */
+ public ConverterProvider build() {
+ return new ConverterProvider(this);
+ }
+ }
}
diff --git a/isthmus/src/main/java/io/substrait/isthmus/DynamicConverterProvider.java b/isthmus/src/main/java/io/substrait/isthmus/DynamicConverterProvider.java
index 8719ea9d4..ceece951d 100644
--- a/isthmus/src/main/java/io/substrait/isthmus/DynamicConverterProvider.java
+++ b/isthmus/src/main/java/io/substrait/isthmus/DynamicConverterProvider.java
@@ -36,9 +36,13 @@ public class DynamicConverterProvider extends ConverterProvider {
*
* Uses {@link DefaultExtensionCatalog#DEFAULT_COLLECTION} for extensions and {@link
* SubstraitTypeSystem#TYPE_FACTORY} for type operations.
+ *
+ * @deprecated Use {@link #DynamicConverterProvider(ConverterProvider.Builder)} with {@link
+ * ConverterProvider#builder()} instead.
*/
+ @Deprecated
public DynamicConverterProvider() {
- this(DefaultExtensionCatalog.DEFAULT_COLLECTION, SubstraitTypeSystem.TYPE_FACTORY);
+ this(ConverterProvider.builder());
}
/**
@@ -47,9 +51,12 @@ public DynamicConverterProvider() {
* Uses {@link SubstraitTypeSystem#TYPE_FACTORY} for type operations.
*
* @param extensions the collection of Substrait extensions to use for function mappings
+ * @deprecated Use {@link #DynamicConverterProvider(ConverterProvider.Builder)} instead, e.g.
+ * {@code new DynamicConverterProvider(ConverterProvider.builder().extensions(extensions))}.
*/
+ @Deprecated
public DynamicConverterProvider(SimpleExtension.ExtensionCollection extensions) {
- this(extensions, SubstraitTypeSystem.TYPE_FACTORY);
+ this(ConverterProvider.builder().extensions(extensions));
}
/**
@@ -61,10 +68,25 @@ public DynamicConverterProvider(SimpleExtension.ExtensionCollection extensions)
*
* @param extensions the collection of Substrait extensions to use for function mappings
* @param typeFactory the factory to use for creating and managing relational data types
+ * @deprecated Use {@link #DynamicConverterProvider(ConverterProvider.Builder)} instead, e.g.
+ * {@code new DynamicConverterProvider(ConverterProvider.builder().extensions(extensions)
+ * .typeFactory(typeFactory))}.
*/
+ @Deprecated
public DynamicConverterProvider(
SimpleExtension.ExtensionCollection extensions, RelDataTypeFactory typeFactory) {
- super(extensions, typeFactory);
+ this(ConverterProvider.builder().extensions(extensions).typeFactory(typeFactory));
+ }
+
+ /**
+ * Creates a new DynamicConverterProvider from a {@link ConverterProvider.Builder}, seeding base
+ * state from the builder and then installing the converter that handles dynamic extension
+ * functions. This is the seam through which subclasses route via {@code super(builder)}.
+ *
+ * @param builder the builder carrying the configured components
+ */
+ public DynamicConverterProvider(ConverterProvider.Builder builder) {
+ super(builder);
this.scalarFunctionConverter = createScalarFunctionConverter();
}
diff --git a/isthmus/src/main/java/io/substrait/isthmus/SqlExpressionToSubstrait.java b/isthmus/src/main/java/io/substrait/isthmus/SqlExpressionToSubstrait.java
index 5c020d5d1..9637cd6fd 100644
--- a/isthmus/src/main/java/io/substrait/isthmus/SqlExpressionToSubstrait.java
+++ b/isthmus/src/main/java/io/substrait/isthmus/SqlExpressionToSubstrait.java
@@ -40,7 +40,7 @@ public class SqlExpressionToSubstrait extends SqlConverterBase {
/** Creates a converter with default configuration. */
public SqlExpressionToSubstrait() {
- this(new ConverterProvider());
+ this(ConverterProvider.DEFAULT);
}
/**
@@ -202,7 +202,7 @@ private Result registerCreateTablesForExtendedExpression(List The {@code sqlDialect} parameter was previously used to influence identifier casing during
+ * parsing. This is now configurable via {@link ConverterProvider#builder()} (for example {@code
+ * ConverterProvider.builder().unquotedCasing(...)}), or for fully custom parser behaviour, by
+ * subclassing {@link ConverterProvider} and overriding {@link
+ * ConverterProvider#getSqlParserConfig()}.
+ *
* @param sqlStatements a string containing one more SQL statements
* @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in
* the SQL statements
* @param sqlDialect The sql dialect to use for parsing.
* @return the Substrait {@link Plan}
* @throws SqlParseException if there is an error while parsing the SQL statements
+ * @deprecated Prefer constructing {@link SqlToSubstrait} with a {@link ConverterProvider}
+ * configured for the desired casing (via {@link ConverterProvider#builder()}) and calling
+ * {@link #convert(String, Prepare.CatalogReader)}. For fully custom parser behaviour,
+ * subclass {@link ConverterProvider} and override {@link
+ * ConverterProvider#getSqlParserConfig()}.
*/
+ @Deprecated
public Plan convert(
final String sqlStatements,
final Prepare.CatalogReader catalogReader,
final SqlDialect sqlDialect)
throws SqlParseException {
- Builder builder = io.substrait.plan.Plan.builder();
- builder.version(Version.builder().from(Version.DEFAULT_VERSION).producer("isthmus").build());
- builder.executionBehavior(converterProvider.getExecutionBehavior());
-
- final SqlParser.Config sqlParserConfig =
- sqlDialect.configureParser(
- SqlParser.config().withParserFactory(ServerDdlExecutor.PARSER_FACTORY));
-
- // TODO: consider case in which one sql passes conversion while others don't
- SubstraitSqlToCalcite.convertQueries(sqlStatements, catalogReader, sqlParserConfig).stream()
- .map(root -> SubstraitRelVisitor.convert(root, converterProvider))
- .forEach(root -> builder.addRoots(root));
-
- return builder.build();
+ return convert(sqlStatements, catalogReader);
}
}
diff --git a/isthmus/src/main/java/io/substrait/isthmus/SubstraitToSql.java b/isthmus/src/main/java/io/substrait/isthmus/SubstraitToSql.java
index 690bd5774..ac07d8d65 100644
--- a/isthmus/src/main/java/io/substrait/isthmus/SubstraitToSql.java
+++ b/isthmus/src/main/java/io/substrait/isthmus/SubstraitToSql.java
@@ -23,7 +23,7 @@ public class SubstraitToSql extends SqlConverterBase {
/** Creates a SubstraitToSql converter with default configuration and extensions. */
public SubstraitToSql() {
- this(new ConverterProvider());
+ this(ConverterProvider.DEFAULT);
}
/**
diff --git a/isthmus/src/main/java/io/substrait/isthmus/sql/SubstraitCreateStatementParser.java b/isthmus/src/main/java/io/substrait/isthmus/sql/SubstraitCreateStatementParser.java
index b008d577d..f8179e397 100644
--- a/isthmus/src/main/java/io/substrait/isthmus/sql/SubstraitCreateStatementParser.java
+++ b/isthmus/src/main/java/io/substrait/isthmus/sql/SubstraitCreateStatementParser.java
@@ -1,5 +1,6 @@
package io.substrait.isthmus.sql;
+import io.substrait.isthmus.ConverterProvider;
import io.substrait.isthmus.SqlConverterBase;
import io.substrait.isthmus.SubstraitTypeSystem;
import io.substrait.isthmus.Utils;
@@ -50,28 +51,7 @@ public class SubstraitCreateStatementParser {
*/
public static List This method expects the use of fully qualified table names in the CREATE statements.
+ *
+ * @param converterProvider the converter provider whose parser config controls identifier casing
+ * and other parser settings
+ * @param createStatements List of SQL strings containing only CREATE statements, must not be null
+ * @return a {@link CalciteCatalogReader} generated from the CREATE statements
+ * @throws SqlParseException if there is an error parsing the SQL statements
+ */
+ public static CalciteCatalogReader processCreateStatementsToCatalog(
+ @NonNull final ConverterProvider converterProvider,
+ @NonNull final List This method expects the use of fully qualified table names in the CREATE statements.
+ *
+ * @param converterProvider the converter provider whose parser config controls identifier casing
+ * and other parser settings
+ * @param createStatements a SQL string containing only CREATE statements, must not be null
+ * @return a {@link CalciteCatalogReader} generated from the CREATE statements
+ * @throws SqlParseException if parsing fails or statements are invalid
+ */
+ public static CalciteCatalogReader processCreateStatementsToCatalog(
+ @NonNull final ConverterProvider converterProvider, @NonNull final String... createStatements)
+ throws SqlParseException {
+ final CalciteSchema rootSchema =
+ processCreateStatementsToSchema(converterProvider, createStatements);
final List This method only supports simple table names without any additional qualifiers. Only used
+ * with {@link io.substrait.isthmus.SqlExpressionToSubstrait}.
*
- * @param createStatements one or more SQL strings containing only CREATE statements; must not be
- * null
- * @return a {@link CalciteSchema} generated from the CREATE statements
+ * @param converterProvider the converter provider whose parser config controls identifier casing
+ * and other parser settings
+ * @param createStatements a SQL string containing only CREATE statements; must not be null
+ * @return list of {@link SubstraitTable}s generated from the CREATE statements
* @throws SqlParseException if parsing fails or statements are invalid
*/
+ public static List To use a custom parser configuration, build the {@link ConverterProvider} via {@link
+ * ConverterProvider#builder()} and its {@code sqlParserConfig(...)}; for fully dynamic behaviour,
+ * subclass {@link ConverterProvider} and override {@link ConverterProvider#getSqlParserConfig()}.
*
* @param sqlStatements a string containing one or more SQL statements
- * @param parserConfig Calcite SqlParser.Config to control the parser
+ * @param converterProvider the converter provider whose parser config controls identifier casing
+ * and other parser settings
* @return a list of {@link SqlNode}s corresponding to the given statements
* @throws SqlParseException if there is an error while parsing the SQL statements
*/
- public static List