This is an automated email from the ASF dual-hosted git repository. bchapuis pushed a commit to branch calcite-framework in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit bed98b0ef03fd46f2ca3e74ed4dce4d36f5709c5 Author: Bertil Chapuis <[email protected]> AuthorDate: Wed Feb 26 10:32:46 2025 +0100 Some progress --- .../apache/baremaps/calcite/DataTableAdapter.java | 8 - ...erDdlExecutor.java => BaremapsDdlExecutor.java} | 54 ++---- .../calcite/baremaps/BaremapsMaterializedView.java | 6 +- ...ableTable.java => BaremapsModifiableTable.java} | 22 +-- .../apache/baremaps/calcite/BaremapsContext.java | 73 -------- .../apache/baremaps/calcite/CalciteDdlTest.java | 206 --------------------- .../org/apache/baremaps/calcite/CalciteTest.java | 4 +- 7 files changed, 20 insertions(+), 353 deletions(-) diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableAdapter.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableAdapter.java index b1fc3619b..2f244f1cc 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableAdapter.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableAdapter.java @@ -72,18 +72,10 @@ public class DataTableAdapter extends AbstractTable implements ScannableTable { private final DataTable table; - private final RelProtoDataType protoRowType; - private RelDataType rowType; public DataTableAdapter(DataTable table) { this.table = table; - this.protoRowType = null; - } - - public DataTableAdapter(DataTable table, RelProtoDataType protoRowType) { - this.table = table; - this.protoRowType = protoRowType; } @Override diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/ServerDdlExecutor.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java similarity index 93% rename from baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/ServerDdlExecutor.java rename to baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java index 0c2650f63..206067735 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/ServerDdlExecutor.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java @@ -97,11 +97,7 @@ import org.apache.calcite.sql.validate.SqlValidator; import org.apache.calcite.sql2rel.InitializerContext; import org.apache.calcite.sql2rel.InitializerExpressionFactory; import org.apache.calcite.sql2rel.NullInitializerExpressionFactory; -import org.apache.calcite.tools.FrameworkConfig; -import org.apache.calcite.tools.Frameworks; -import org.apache.calcite.tools.Planner; -import org.apache.calcite.tools.RelConversionException; -import org.apache.calcite.tools.ValidationException; +import org.apache.calcite.tools.*; import org.apache.calcite.util.NlsString; import org.apache.calcite.util.Pair; import org.apache.calcite.util.Util; @@ -115,9 +111,9 @@ import org.checkerframework.checker.nullness.qual.Nullable; * appropriate {@code execute} method. For example, "CREATE TABLE" ({@link SqlCreateTable}) is * dispatched to {@link #execute(SqlCreateTable, CalcitePrepare.Context)}. */ -public class ServerDdlExecutor extends DdlExecutorImpl { +public class BaremapsDdlExecutor extends DdlExecutorImpl { /** Singleton instance. */ - public static final ServerDdlExecutor INSTANCE = new ServerDdlExecutor(); + public static final BaremapsDdlExecutor INSTANCE = new BaremapsDdlExecutor(); /** Parser factory. */ @SuppressWarnings("unused") // used via reflection @@ -130,7 +126,7 @@ public class ServerDdlExecutor extends DdlExecutorImpl { @Override public DdlExecutor getDdlExecutor() { - return ServerDdlExecutor.INSTANCE; + return BaremapsDdlExecutor.INSTANCE; } }; @@ -138,7 +134,7 @@ public class ServerDdlExecutor extends DdlExecutorImpl { * Creates a ServerDdlExecutor. Protected only to allow sub-classing; use {@link #INSTANCE} where * possible. */ - protected ServerDdlExecutor() {} + protected BaremapsDdlExecutor() {} /** * Returns the schema in which to create an object; the left part is null if the schema does not @@ -208,8 +204,8 @@ public class ServerDdlExecutor extends DdlExecutorImpl { final CalciteSchema.TableEntry tableEntry = calciteSchema.getTable(tblName, context.config().caseSensitive()); final Table table = requireNonNull(tableEntry, "tableEntry").getTable(); - if (table instanceof BaremapsMutableTable) { - BaremapsMutableTable mutableArrayTable = (BaremapsMutableTable) table; + if (table instanceof BaremapsModifiableTable) { + BaremapsModifiableTable mutableArrayTable = (BaremapsModifiableTable) table; mutableArrayTable.rows.clear(); } else { // Not calcite-server created, so not support truncate. @@ -428,8 +424,7 @@ public class ServerDdlExecutor extends DdlExecutorImpl { // Table does not exist. Create it. final BaremapsMaterializedView table = - new BaremapsMaterializedView(pair.right, RelDataTypeImpl.proto(rowType), - NullInitializerExpressionFactory.INSTANCE, context.getTypeFactory()); + new BaremapsMaterializedView(pair.right, RelDataTypeImpl.proto(rowType), context.getTypeFactory()); pair.left.add(pair.right, table); populate(create.name, create.query, context); table.key = @@ -545,31 +540,6 @@ public class ServerDdlExecutor extends DdlExecutorImpl { } } final RelDataType rowType = builder.build(); - final RelDataType storedRowType = storedBuilder.build(); - final List<ColumnDef> columns = b.build(); - final InitializerExpressionFactory ief = - new NullInitializerExpressionFactory() { - @Override - public ColumnStrategy generationStrategy(RelOptTable table, - int iColumn) { - return columns.get(iColumn).strategy; - } - - @Override - public RexNode newColumnDefaultValue(RelOptTable table, - int iColumn, InitializerContext context) { - final ColumnDef c = columns.get(iColumn); - if (c.expr != null) { - // REVIEW Danny 2019-10-09: Should we support validation for DDL nodes? - final SqlNode validated = context.validateExpression(storedRowType, c.expr); - // The explicit specified type should have the same nullability - // with the column expression inferred type, - // actually they should be exactly the same. - return context.convertExpression(validated); - } - return super.newColumnDefaultValue(table, iColumn, context); - } - }; if (pair.left.plus().getTable(pair.right) != null) { // Table exists. if (create.ifNotExists) { @@ -583,8 +553,8 @@ public class ServerDdlExecutor extends DdlExecutorImpl { } // Table does not exist. Create it. pair.left.add(pair.right, - new BaremapsMutableTable(pair.right, - RelDataTypeImpl.proto(rowType), ief, context.getTypeFactory())); + new BaremapsModifiableTable(pair.right, + RelDataTypeImpl.proto(rowType), context.getTypeFactory())); if (create.query != null) { populate(create.name, create.query, context); } @@ -643,8 +613,8 @@ public class ServerDdlExecutor extends DdlExecutorImpl { final RelDataType rowType = table.getRowType(typeFactory); // Table does not exist. Create it. pair.left.add(pair.right, - new BaremapsMutableTable(pair.right, - RelDataTypeImpl.proto(rowType), ief, typeFactory)); + new BaremapsModifiableTable(pair.right, + RelDataTypeImpl.proto(rowType), typeFactory)); } /** Executes a {@code CREATE TYPE} command. */ diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMaterializedView.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMaterializedView.java index 2e24553be..da3f1b722 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMaterializedView.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMaterializedView.java @@ -22,11 +22,10 @@ import org.apache.calcite.materialize.MaterializationKey; import org.apache.calcite.rel.type.RelDataTypeFactory; import org.apache.calcite.rel.type.RelProtoDataType; import org.apache.calcite.schema.Schema; -import org.apache.calcite.sql2rel.InitializerExpressionFactory; import org.checkerframework.checker.nullness.qual.Nullable; /** A table that implements a materialized view. */ -class BaremapsMaterializedView extends BaremapsMutableTable { +class BaremapsMaterializedView extends BaremapsModifiableTable { /** * The key with which this was stored in the materialization service, or null if not (yet) * materialized. @@ -37,9 +36,8 @@ class BaremapsMaterializedView extends BaremapsMutableTable { BaremapsMaterializedView( String name, RelProtoDataType protoRowType, - InitializerExpressionFactory initializerExpressionFactory, RelDataTypeFactory typeFactory) { - super(name, protoRowType, initializerExpressionFactory, typeFactory); + super(name, protoRowType, typeFactory); } @Override diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMutableTable.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java similarity index 91% rename from baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMutableTable.java rename to baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java index d54a84bf3..58bfd1255 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsMutableTable.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java @@ -45,7 +45,6 @@ import org.apache.calcite.schema.Wrapper; import org.apache.calcite.schema.impl.AbstractTable; import org.apache.calcite.schema.impl.AbstractTableQueryable; import org.apache.calcite.sql.type.SqlTypeName; -import org.apache.calcite.sql2rel.InitializerExpressionFactory; import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Type; @@ -58,12 +57,10 @@ import java.util.List; import static java.util.Objects.requireNonNull; -class BaremapsMutableTable extends AbstractTable implements ModifiableTable, Wrapper { +class BaremapsModifiableTable extends AbstractTable implements ModifiableTable, Wrapper { private final String name; - private final InitializerExpressionFactory initializerExpressionFactory; - private final RelProtoDataType protoRowType; private final RelDataType rowType; @@ -72,14 +69,11 @@ class BaremapsMutableTable extends AbstractTable implements ModifiableTable, Wra final DataCollection<DataRow> rows; - BaremapsMutableTable(String name, - RelProtoDataType protoRowType, - InitializerExpressionFactory initializerExpressionFactory, - RelDataTypeFactory typeFactory) { + BaremapsModifiableTable(String name, + RelProtoDataType protoRowType, + RelDataTypeFactory typeFactory) { super(); this.name = requireNonNull(name, "name"); - this.initializerExpressionFactory = - requireNonNull(initializerExpressionFactory, "initializerExpressionFactory"); this.protoRowType = requireNonNull(protoRowType, "protoRowType"); this.rowType = this.protoRowType.apply(typeFactory); @@ -198,14 +192,6 @@ class BaremapsMutableTable extends AbstractTable implements ModifiableTable, Wra return rowType; } - @Override - public <C extends Object> @Nullable C unwrap(Class<C> aClass) { - if (aClass.isInstance(initializerExpressionFactory)) { - return aClass.cast(initializerExpressionFactory); - } - return super.unwrap(aClass); - } - private class CollectionAdapter implements Collection<Object[]> { private final int size; diff --git a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/BaremapsContext.java b/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/BaremapsContext.java deleted file mode 100644 index 8b94e123e..000000000 --- a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/BaremapsContext.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.apache.baremaps.calcite; - -import org.apache.calcite.DataContext; -import org.apache.calcite.adapter.java.JavaTypeFactory; -import org.apache.calcite.config.CalciteConnectionConfig; -import org.apache.calcite.jdbc.CalcitePrepare; -import org.apache.calcite.jdbc.CalcitePrepare.SparkHandler; -import org.apache.calcite.jdbc.CalciteSchema; -import org.apache.calcite.tools.RelRunner; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.List; - -public class BaremapsContext implements CalcitePrepare.Context { - - private final JavaTypeFactory typeFactory; - - private final CalciteSchema rootSchema; - - private final RelRunner planner; - - public BaremapsContext(JavaTypeFactory typeFactory, CalciteSchema rootSchema, RelRunner planner) { - this.typeFactory = typeFactory; - this.rootSchema = rootSchema; - this.planner = planner; - } - - @Override - public JavaTypeFactory getTypeFactory() { - return typeFactory; - } - - @Override - public CalciteSchema getRootSchema() { - return rootSchema; - } - - @Override - public CalciteSchema getMutableRootSchema() { - return rootSchema; - } - - @Override - public List<String> getDefaultSchemaPath() { - return List.of(); - } - - @Override - public CalciteConnectionConfig config() { - return null; - } - - @Override - public SparkHandler spark() { - return null; - } - - @Override - public DataContext getDataContext() { - return null; - } - - @Override - public @Nullable List<String> getObjectPath() { - return null; - } - - @Override - public RelRunner getRelRunner() { - return planner; - } - -} diff --git a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteDdlTest.java b/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteDdlTest.java deleted file mode 100644 index b4d035366..000000000 --- a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteDdlTest.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.baremaps.calcite; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.baremaps.calcite.DataColumn.Cardinality; -import org.apache.baremaps.calcite.DataColumn.Type; -import org.apache.baremaps.calcite.baremaps.BaremapsDataTable; -import org.apache.baremaps.calcite.baremaps.ServerDdlExecutor; -import org.apache.baremaps.data.collection.AppendOnlyLog; -import org.apache.baremaps.data.collection.IndexedDataList; -import org.apache.calcite.DataContext; -import org.apache.calcite.DataContexts; -import org.apache.calcite.config.Lex; -import org.apache.calcite.interpreter.Interpreter; -import org.apache.calcite.jdbc.CalciteSchema; -import org.apache.calcite.jdbc.JavaTypeFactoryImpl; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.Linq4j; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.schema.ScannableTable; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.schema.Table; -import org.apache.calcite.schema.impl.AbstractSchema; -import org.apache.calcite.schema.impl.AbstractTable; -import org.apache.calcite.sql.*; -import org.apache.calcite.sql.ddl.SqlCreateMaterializedView; -import org.apache.calcite.sql.ddl.SqlCreateTable; -import org.apache.calcite.sql.parser.SqlParser; -import org.apache.calcite.sql.type.SqlTypeName; -import org.apache.calcite.tools.FrameworkConfig; -import org.apache.calcite.tools.Frameworks; -import org.apache.calcite.tools.Planner; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.GeometryFactory; - -public class CalciteDdlTest { - - - public class ListTable extends AbstractTable implements ScannableTable { - private final List<Integer> data; - - public ListTable(List<Integer> data) { - this.data = data; - } - - @Override - public RelDataType getRowType(RelDataTypeFactory typeFactory) { - // Define a single column named "value" of type INTEGER - return typeFactory.builder() - .add("V", SqlTypeName.INTEGER) - .build(); - } - - @Override - public Enumerable<Object[]> scan(DataContext root) { - // Convert the List<Integer> to Enumerable<Object[]> - return Linq4j.asEnumerable(data) - .select(i -> new Object[] {i}); - } - } - - public class ListSchema extends AbstractSchema { - private final List<Integer> listA; - private final List<Integer> listB; - - public ListSchema(List<Integer> listA, List<Integer> listB) { - this.listA = listA; - this.listB = listB; - } - - @Override - protected Map<String, Table> getTableMap() { - Map<String, Table> tables = new HashMap<>(); - tables.put("LIST_A", new ListTable(listA)); - tables.put("LIST_B", new ListTable(listB)); // Initially empty - return tables; - } - } - - @Test - @Disabled - void test() throws Exception { - // Initialize your Java lists - List<Integer> listA = List.of(1, 2, 3, 4, 5); - List<Integer> listB = new ArrayList<>(); - - // Set up Calcite schema - CalciteSchema rootSchema = CalciteSchema.createRootSchema(true); - rootSchema.add("MY_SCHEMA", new ListSchema(listA, listB)); - - // Create and add 'city' table - DataSchema cityRowType = new DataSchema("city", List.of( - new DataColumnFixed("id", Cardinality.OPTIONAL, Type.INTEGER), - new DataColumnFixed("name", Cardinality.OPTIONAL, Type.STRING), - new DataColumnFixed("geometry", Cardinality.OPTIONAL, Type.GEOMETRY))); - - DataTable cityDataTable = new BaremapsDataTable( - cityRowType, - new IndexedDataList<>(new AppendOnlyLog<>(new DataRowType(cityRowType)))); - - GeometryFactory geometryFactory = new GeometryFactory(); - cityDataTable.add(new DataRow(cityDataTable.schema(), - List.of(1, "Paris", geometryFactory.createPoint(new Coordinate(2.3522, 48.8566))))); - cityDataTable.add(new DataRow(cityDataTable.schema(), - List.of(2, "New York", geometryFactory.createPoint(new Coordinate(-74.0060, 40.7128))))); - - DataTableAdapter cityDataTableAdapter = new DataTableAdapter(cityDataTable); - rootSchema.add("CITY", cityDataTableAdapter); - - // Create and add 'population' table - DataSchema populationRowType = new DataSchema("population", List.of( - new DataColumnFixed("city_id", Cardinality.OPTIONAL, Type.INTEGER), - new DataColumnFixed("population", Cardinality.OPTIONAL, Type.INTEGER))); - - DataTable populationDataTable = new BaremapsDataTable( - populationRowType, - new IndexedDataList<>(new AppendOnlyLog<>(new DataRowType(populationRowType)))); - - populationDataTable.add(new DataRow(populationDataTable.schema(), List.of(1, 2_161_000))); - populationDataTable.add(new DataRow(populationDataTable.schema(), List.of(2, 8_336_000))); - - DataTableAdapter populationDataTableAdapter = new DataTableAdapter(populationDataTable); - rootSchema.add("population", populationDataTableAdapter); - - // Configure the parser - SqlParser.Config parserConfig = SqlParser.configBuilder() - .setLex(Lex.MYSQL) - .setParserFactory(ServerDdlExecutor.PARSER_FACTORY) - .build(); - - // Configure the framework - FrameworkConfig config = Frameworks.newConfigBuilder() - .defaultSchema(rootSchema.plus()) - .parserConfig(parserConfig) - .build(); - - // Create a planner - Planner planner = Frameworks.getPlanner(config); - - // Define the SQL query to populate list_b from list_a - String sql = "CREATE MATERIALIZED VIEW city_population AS " - + "SELECT c.id, c.name, c.geometry, p.population " - + "FROM CITY c " - + "JOIN population p ON c.id = p.city_id"; - - // Parse the SQL query - SqlNode parsed = planner.parse(sql); - - // Create a context - //BaremapsContext context = new BaremapsContext(new JavaTypeFactoryImpl(), rootSchema, Frameworks.); - - // Create an executor - //ServerDdlExecutor.INSTANCE.execute((SqlCreateMaterializedView) parsed, context); - -// // Extract the select statement from the parsed SQL query -// SqlNode select; -// if (parsed instanceof SqlCreateMaterializedView createMaterializedView) { -// List<SqlNode> operands = createMaterializedView.getOperandList(); -// select = operands.get(operands.size() - 1); -// System.out.println(select); -// } else { -// throw new IllegalArgumentException( -// "Expected a CREATE MATERIALIZED VIEW statement, but got: " + parsed.getKind()); -// } -// -// // Validate the SQL query -// SqlNode validated = planner.validate(select); -// -// // Convert the SQL query to a relational expression -// RelNode rel = planner.rel(validated).rel; -// -// try (Interpreter interpreter = new Interpreter(DataContexts.EMPTY, rel)) { -// // Create an interpreter to execute the RelNode -// for (Object[] row : interpreter) { -// listB.add((Integer) row[0]); -// } -// } -// -// // Display the results -// System.out.println("List A: " + listA); -// System.out.println("List B (after SQL): " + listB); - } -} diff --git a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteTest.java b/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteTest.java index 82a02d1cc..e29f58f1d 100644 --- a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteTest.java +++ b/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/CalciteTest.java @@ -20,7 +20,7 @@ package org.apache.baremaps.calcite; import org.apache.baremaps.calcite.DataColumn.Cardinality; import org.apache.baremaps.calcite.DataColumn.Type; import org.apache.baremaps.calcite.baremaps.BaremapsDataTable; -import org.apache.baremaps.calcite.baremaps.ServerDdlExecutor; +import org.apache.baremaps.calcite.baremaps.BaremapsDdlExecutor; import org.apache.baremaps.data.collection.AppendOnlyLog; import org.apache.baremaps.data.collection.IndexedDataList; import org.apache.calcite.jdbc.CalciteConnection; @@ -47,7 +47,7 @@ public class CalciteTest { info.setProperty("caseSensitive", "false"); // Disable case sensitivity info.setProperty("unquotedCasing", "TO_LOWER"); // Convert unquoted identifiers to lowercase info.setProperty("quotedCasing", "TO_LOWER"); - info.setProperty("parserFactory", ServerDdlExecutor.class.getName() + "#PARSER_FACTORY"); + info.setProperty("parserFactory", BaremapsDdlExecutor.class.getName() + "#PARSER_FACTORY"); info.setProperty("materializationsEnabled", "true"); try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) {
