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 97799717bab8f23348c6886003c943bb712aa925 Author: Bertil Chapuis <[email protected]> AuthorDate: Fri Jan 17 18:39:12 2025 +0100 Format code --- .../org/apache/baremaps/calcite/CalciteTest.java | 2 +- .../data/calcite/CalciteMultipleDDLParser.java | 14 - .../data/{calcite => store}/BaremapsDataTable.java | 2 +- .../apache/baremaps/data/calcite/CalciteTest.java | 365 ++++++++++----------- 4 files changed, 170 insertions(+), 213 deletions(-) diff --git a/baremaps-core/src/test/java/org/apache/baremaps/calcite/CalciteTest.java b/baremaps-core/src/test/java/org/apache/baremaps/calcite/CalciteTest.java index 1b5e8b3b2..efc13eb4d 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/calcite/CalciteTest.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/calcite/CalciteTest.java @@ -22,10 +22,10 @@ import com.google.common.collect.ImmutableList; import java.sql.*; import java.util.List; import java.util.Properties; -import org.apache.baremaps.data.calcite.BaremapsDataTable; import org.apache.baremaps.data.calcite.SqlDataTable; import org.apache.baremaps.data.collection.AppendOnlyLog; import org.apache.baremaps.data.collection.IndexedDataList; +import org.apache.baremaps.data.store.BaremapsDataTable; import org.apache.baremaps.data.type.RowDataType; import org.apache.baremaps.maplibre.vectortile.VectorTileFunctions; import org.apache.baremaps.store.*; diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/calcite/CalciteMultipleDDLParser.java b/baremaps-data/src/main/java/org/apache/baremaps/data/calcite/CalciteMultipleDDLParser.java deleted file mode 100644 index 04984fd3e..000000000 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/calcite/CalciteMultipleDDLParser.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.apache.baremaps.data.calcite; - -import org.apache.calcite.config.Lex; -import org.apache.calcite.sql.SqlNode; -import org.apache.calcite.sql.SqlNodeList; -import org.apache.calcite.sql.parser.SqlParser; -import org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl; -import org.apache.calcite.sql.validate.SqlConformanceEnum; - -public class CalciteMultipleDDLParser { - public static void main(String[] args) throws Exception { - - } -} diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/calcite/BaremapsDataTable.java b/baremaps-data/src/main/java/org/apache/baremaps/data/store/BaremapsDataTable.java similarity index 97% rename from baremaps-data/src/main/java/org/apache/baremaps/data/calcite/BaremapsDataTable.java rename to baremaps-data/src/main/java/org/apache/baremaps/data/store/BaremapsDataTable.java index 55ddee115..89123dab6 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/calcite/BaremapsDataTable.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/store/BaremapsDataTable.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.baremaps.data.calcite; +package org.apache.baremaps.data.store; import java.util.Iterator; import org.apache.baremaps.data.collection.DataCollection; diff --git a/baremaps-data/src/test/java/org/apache/baremaps/data/calcite/CalciteTest.java b/baremaps-data/src/test/java/org/apache/baremaps/data/calcite/CalciteTest.java index 9e7fc96d6..fd645d163 100644 --- a/baremaps-data/src/test/java/org/apache/baremaps/data/calcite/CalciteTest.java +++ b/baremaps-data/src/test/java/org/apache/baremaps/data/calcite/CalciteTest.java @@ -17,257 +17,228 @@ package org.apache.baremaps.data.calcite; -import com.google.common.collect.ImmutableList; +import java.sql.*; +import java.util.*; import org.apache.baremaps.data.collection.AppendOnlyLog; import org.apache.baremaps.data.collection.IndexedDataList; +import org.apache.baremaps.data.store.BaremapsDataTable; import org.apache.baremaps.data.type.RowDataType; +import org.apache.baremaps.store.*; import org.apache.baremaps.store.DataColumn.Cardinality; import org.apache.baremaps.store.DataColumn.Type; -import org.apache.baremaps.store.*; import org.apache.calcite.DataContext; import org.apache.calcite.DataContexts; -import org.apache.calcite.adapter.enumerable.EnumerableConvention; -import org.apache.calcite.adapter.enumerable.EnumerableInterpreter; -import org.apache.calcite.config.Lex; import org.apache.calcite.interpreter.Interpreter; import org.apache.calcite.jdbc.CalciteConnection; import org.apache.calcite.linq4j.Enumerable; import org.apache.calcite.linq4j.Linq4j; -import org.apache.calcite.model.ModelHandler; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelOptPlanner; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.plan.volcano.VolcanoPlanner; import org.apache.calcite.rel.RelNode; import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.rex.RexBuilder; -import org.apache.calcite.runtime.SpatialTypeFunctions; import org.apache.calcite.schema.ScannableTable; import org.apache.calcite.schema.SchemaPlus; import org.apache.calcite.schema.Table; import org.apache.calcite.schema.impl.*; -import org.apache.calcite.sql.SqlNode; -import org.apache.calcite.sql.SqlNodeList; -import org.apache.calcite.sql.fun.SqlSpatialTypeFunctions; -import org.apache.calcite.sql.parser.SqlParseException; -import org.apache.calcite.sql.parser.SqlParser; -import org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl; import org.apache.calcite.sql.type.SqlTypeName; -import org.apache.calcite.sql.validate.SqlConformanceEnum; import org.apache.calcite.tools.FrameworkConfig; import org.apache.calcite.tools.Frameworks; import org.apache.calcite.tools.Planner; import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; -import org.apache.calcite.adapter.enumerable.EnumerableRel; -import org.apache.calcite.adapter.enumerable.EnumerableInterpreter; -import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor; -import org.apache.calcite.linq4j.Enumerator; -import org.apache.calcite.plan.*; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.tools.*; - -import java.sql.*; -import java.util.*; public class CalciteTest { - @Test - void sql() throws SQLException { - GeometryFactory geometryFactory = new GeometryFactory(); - - // Configure Calcite connection properties - Properties info = new Properties(); - info.setProperty("lex", "MYSQL"); // Use MySQL dialect - info.setProperty("caseSensitive", "false"); // Disable case sensitivity - info.setProperty("unquotedCasing", "TO_LOWER"); // Convert unquoted identifiers to lowercase - info.setProperty("quotedCasing", "TO_LOWER"); - - try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) { - CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - - // Create and add 'city' table - DataSchema cityRowType = new DataSchemaImpl("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 RowDataType(cityRowType)))); - - cityDataTable.add(new DataRowImpl(cityDataTable.schema(), - List.of(1, "Paris", geometryFactory.createPoint(new Coordinate(2.3522, 48.8566))))); - cityDataTable.add(new DataRowImpl(cityDataTable.schema(), - List.of(2, "New York", geometryFactory.createPoint(new Coordinate(-74.0060, 40.7128))))); - - SqlDataTable citySqlDataTable = new SqlDataTable(cityDataTable); - rootSchema.add("city", citySqlDataTable); - - // Create and add 'population' table - DataSchema populationRowType = new DataSchemaImpl("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 RowDataType(populationRowType)))); - - populationDataTable.add(new DataRowImpl(populationDataTable.schema(), List.of(1, 2_161_000))); - populationDataTable.add(new DataRowImpl(populationDataTable.schema(), List.of(2, 8_336_000))); - - SqlDataTable populationSqlDataTable = new SqlDataTable(populationDataTable); - rootSchema.add("population", populationSqlDataTable); - - // Create view 'city_population' - String mvSql = "SELECT c.id, c.name, c.geometry, p.population " + - "FROM city c " + // lowercase and unquoted - "JOIN population p ON c.id = p.city_id"; - - ViewTableMacro materializedView = MaterializedViewTable.viewMacro( - rootSchema, - mvSql, - Collections.emptyList(), // Schema path - List.of("city_population"), // Name parts - false); // Not a materialized view - - - rootSchema.add("city_population", materializedView); - - // Debug: List all tables in the root schema - System.out.println("Available tables in the root schema:"); - for (String tableName : rootSchema.getTableNames()) { - System.out.println(" - " + tableName); - } - - String sql = "SELECT * FROM city"; - try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(sql)) { - while (resultSet.next()) { - System.out.println(resultSet.getString("id") + " " + resultSet.getString("geometry")); - } - } - - // Query the view - sql = "SELECT * FROM city_population"; - try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(sql)) { - while (resultSet.next()) { - System.out.println( - resultSet.getString("id") + " " + resultSet.getString("name") - ); - } - } + @Test + void sql() throws SQLException { + GeometryFactory geometryFactory = new GeometryFactory(); + + // Configure Calcite connection properties + Properties info = new Properties(); + info.setProperty("lex", "MYSQL"); // Use MySQL dialect + info.setProperty("caseSensitive", "false"); // Disable case sensitivity + info.setProperty("unquotedCasing", "TO_LOWER"); // Convert unquoted identifiers to lowercase + info.setProperty("quotedCasing", "TO_LOWER"); + + try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) { + CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + + // Create and add 'city' table + DataSchema cityRowType = new DataSchemaImpl("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 RowDataType(cityRowType)))); + + cityDataTable.add(new DataRowImpl(cityDataTable.schema(), + List.of(1, "Paris", geometryFactory.createPoint(new Coordinate(2.3522, 48.8566))))); + cityDataTable.add(new DataRowImpl(cityDataTable.schema(), + List.of(2, "New York", geometryFactory.createPoint(new Coordinate(-74.0060, 40.7128))))); + + SqlDataTable citySqlDataTable = new SqlDataTable(cityDataTable); + rootSchema.add("city", citySqlDataTable); + + // Create and add 'population' table + DataSchema populationRowType = new DataSchemaImpl("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 RowDataType(populationRowType)))); + + populationDataTable.add(new DataRowImpl(populationDataTable.schema(), List.of(1, 2_161_000))); + populationDataTable.add(new DataRowImpl(populationDataTable.schema(), List.of(2, 8_336_000))); + + SqlDataTable populationSqlDataTable = new SqlDataTable(populationDataTable); + rootSchema.add("population", populationSqlDataTable); + + // Create view 'city_population' + String mvSql = "SELECT c.id, c.name, c.geometry, p.population " + + "FROM city c " + // lowercase and unquoted + "JOIN population p ON c.id = p.city_id"; + + ViewTableMacro materializedView = MaterializedViewTable.viewMacro( + rootSchema, + mvSql, + Collections.emptyList(), // Schema path + List.of("city_population"), // Name parts + false); // Not a materialized view + + + rootSchema.add("city_population", materializedView); + + // Debug: List all tables in the root schema + System.out.println("Available tables in the root schema:"); + for (String tableName : rootSchema.getTableNames()) { + System.out.println(" - " + tableName); + } + + String sql = "SELECT * FROM city"; + try (Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + System.out.println(resultSet.getString("id") + " " + resultSet.getString("geometry")); } - + } + + // Query the view + sql = "SELECT * FROM city_population"; + try (Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + System.out.println( + resultSet.getString("id") + " " + resultSet.getString("name")); + } + } } - public class ListTable extends AbstractTable implements ScannableTable { - private final List<Integer> data; + } - public ListTable(List<Integer> data) { - this.data = data; - } + public class ListTable extends AbstractTable implements ScannableTable { + private final List<Integer> data; - @Override - public RelDataType getRowType(RelDataTypeFactory typeFactory) { - // Define a single column named "value" of type INTEGER - return typeFactory.builder() - .add("V", SqlTypeName.INTEGER) - .build(); - } + public ListTable(List<Integer> data) { + this.data = data; + } - @Override - public Enumerable<Object[]> scan(DataContext root) { - // Convert the List<Integer> to Enumerable<Object[]> - return Linq4j.asEnumerable(data) - .select(i -> new Object[]{i}); - } + @Override + public RelDataType getRowType(RelDataTypeFactory typeFactory) { + // Define a single column named "value" of type INTEGER + return typeFactory.builder() + .add("V", SqlTypeName.INTEGER) + .build(); } - public class ListSchema extends AbstractSchema { - private final List<Integer> listA; - private final List<Integer> listB; + @Override + public Enumerable<Object[]> scan(DataContext root) { + // Convert the List<Integer> to Enumerable<Object[]> + return Linq4j.asEnumerable(data) + .select(i -> new Object[] {i}); + } + } - public ListSchema(List<Integer> listA, List<Integer> listB) { - this.listA = listA; - this.listB = listB; - } + public class ListSchema extends AbstractSchema { + private final List<Integer> listA; + private final List<Integer> 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; - } + public ListSchema(List<Integer> listA, List<Integer> listB) { + this.listA = listA; + this.listB = listB; } - @Test - void list() throws Exception { - // Initialize your Java lists - List<Integer> listA = List.of(1, 2, 3, 4, 5); - List<Integer> listB = new ArrayList<>(); + @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; + } + } - // Set up Calcite schema - SchemaPlus rootSchema = Frameworks.createRootSchema(true); - rootSchema.add("MY_SCHEMA", new ListSchema(listA, listB)); + @Test + void list() throws Exception { + // Initialize your Java lists + List<Integer> listA = List.of(1, 2, 3, 4, 5); + List<Integer> listB = new ArrayList<>(); - // Create and add 'city' table - DataSchema cityRowType = new DataSchemaImpl("city", List.of( - new DataColumnFixed("id", Cardinality.OPTIONAL, Type.INTEGER), - new DataColumnFixed("name", Cardinality.OPTIONAL, Type.STRING), - new DataColumnFixed("geometry", Cardinality.OPTIONAL, Type.GEOMETRY))); + // Set up Calcite schema + SchemaPlus rootSchema = Frameworks.createRootSchema(true); + rootSchema.add("MY_SCHEMA", new ListSchema(listA, listB)); - DataTable cityDataTable = new BaremapsDataTable( - cityRowType, - new IndexedDataList<>(new AppendOnlyLog<>(new RowDataType(cityRowType)))); + // Create and add 'city' table + DataSchema cityRowType = new DataSchemaImpl("city", List.of( + new DataColumnFixed("id", Cardinality.OPTIONAL, Type.INTEGER), + new DataColumnFixed("name", Cardinality.OPTIONAL, Type.STRING), + new DataColumnFixed("geometry", Cardinality.OPTIONAL, Type.GEOMETRY))); - GeometryFactory geometryFactory = new GeometryFactory(); - cityDataTable.add(new DataRowImpl(cityDataTable.schema(), - List.of(1, "Paris", geometryFactory.createPoint(new Coordinate(2.3522, 48.8566))))); - cityDataTable.add(new DataRowImpl(cityDataTable.schema(), - List.of(2, "New York", geometryFactory.createPoint(new Coordinate(-74.0060, 40.7128))))); + DataTable cityDataTable = new BaremapsDataTable( + cityRowType, + new IndexedDataList<>(new AppendOnlyLog<>(new RowDataType(cityRowType)))); - SqlDataTable citySqlDataTable = new SqlDataTable(cityDataTable); - rootSchema.add("CITY", citySqlDataTable); + GeometryFactory geometryFactory = new GeometryFactory(); + cityDataTable.add(new DataRowImpl(cityDataTable.schema(), + List.of(1, "Paris", geometryFactory.createPoint(new Coordinate(2.3522, 48.8566))))); + cityDataTable.add(new DataRowImpl(cityDataTable.schema(), + List.of(2, "New York", geometryFactory.createPoint(new Coordinate(-74.0060, 40.7128))))); - // Configure the framework - FrameworkConfig config = Frameworks.newConfigBuilder() - .defaultSchema(rootSchema.getSubSchema("MY_SCHEMA")) - .build(); + SqlDataTable citySqlDataTable = new SqlDataTable(cityDataTable); + rootSchema.add("CITY", citySqlDataTable); - // Create a planner - Planner planner = Frameworks.getPlanner(config); + // Configure the framework + FrameworkConfig config = Frameworks.newConfigBuilder() + .defaultSchema(rootSchema.getSubSchema("MY_SCHEMA")) + .build(); - // Define the SQL query to populate list_b from list_a - String sql = "SELECT V * 2 AS V FROM LIST_A"; + // Create a planner + Planner planner = Frameworks.getPlanner(config); - // Parse the SQL query - org.apache.calcite.sql.SqlNode parsed = planner.parse(sql); + // Define the SQL query to populate list_b from list_a + String sql = "SELECT V * 2 AS V FROM LIST_A"; - // Validate the SQL query - org.apache.calcite.sql.SqlNode validated = planner.validate(parsed); + // Parse the SQL query + org.apache.calcite.sql.SqlNode parsed = planner.parse(sql); - // Convert the SQL query to a relational expression - RelNode rel = planner.rel(validated).rel; + // Validate the SQL query + org.apache.calcite.sql.SqlNode validated = planner.validate(parsed); - Interpreter interpreter = new Interpreter(DataContexts.EMPTY, rel); + // Convert the SQL query to a relational expression + RelNode rel = planner.rel(validated).rel; - // Create an interpreter to execute the RelNode - for (Object[] row : interpreter.asEnumerable()) { - listB.add((Integer) row[0]); - } + Interpreter interpreter = new Interpreter(DataContexts.EMPTY, rel); - // Display the results - System.out.println("List A: " + listA); - System.out.println("List B (after SQL): " + listB); + // Create an interpreter to execute the RelNode + for (Object[] row : interpreter.asEnumerable()) { + listB.add((Integer) row[0]); } + // Display the results + System.out.println("List A: " + listA); + System.out.println("List B (after SQL): " + listB); + } + }
