Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand All @@ -22,7 +23,7 @@
* <p>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.
*
* <p>2. Parse the SQL query to convert (in the source SQL dialect).
* <p>2. Parse the SQL query to convert.
*
* <p>3. Convert the SQL query to Calcite Relations.
*
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ public class AutomaticDynamicFunctionMappingConverterProvider extends ConverterP
*
* <p>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());
}

/**
Expand All @@ -58,10 +63,15 @@ public AutomaticDynamicFunctionMappingConverterProvider() {
* <p>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));
}

/**
Expand All @@ -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<SqlOperator> dynamicScalarOperators = getDynamicScalarOperators();
this.scalarFunctionConverter = createScalarFunctionConverter(dynamicScalarOperators);
Expand Down
Loading
Loading