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 a535dbc06a76bd25064545b645762ba136cd4016 Author: Bertil Chapuis <[email protected]> AuthorDate: Sat Apr 5 16:03:41 2025 +0200 Fix build issues --- baremaps-calcite/pom.xml | 3 +- .../apache/baremaps/calcite/DataTableAdapter.java | 1 - .../apache/baremaps/calcite/DataTableFactory.java | 9 +- .../calcite/baremaps/BaremapsDataTable.java | 7 +- .../calcite/baremaps/BaremapsDdlExecutor.java | 3 +- .../calcite/baremaps/BaremapsMaterializedView.java | 2 +- .../calcite/baremaps/BaremapsModifiableTable.java | 371 ++++++++++----------- .../org/apache/baremaps/calcite/CalciteTest.java | 172 +++++----- .../calcite/DataTableAdapterFactoryTest.java | 143 ++++---- .../org/apache/baremaps/calcite/CalciteTest.java | 120 ------- .../baremaps/data/collection/DataCollection.java | 1 - .../data/collection/DataCollectionMapper.java | 1 - .../baremaps/data/collection/DataConversions.java | 2 - .../data/collection/FixedSizeDataList.java | 1 - .../baremaps/data/collection/IndexedDataList.java | 1 - .../org/apache/baremaps/data/memory/Memory.java | 10 +- pom.xml | 5 - 17 files changed, 357 insertions(+), 495 deletions(-) diff --git a/baremaps-calcite/pom.xml b/baremaps-calcite/pom.xml index 5ea71ec49..d49d188ca 100644 --- a/baremaps-calcite/pom.xml +++ b/baremaps-calcite/pom.xml @@ -4,10 +4,11 @@ <parent> <groupId>org.apache.baremaps</groupId> <artifactId>baremaps</artifactId> - <version>0.8.2-SNAPSHOT</version> + <version>0.8.3-SNAPSHOT</version> </parent> <artifactId>baremaps-calcite</artifactId> + <name>Apache Baremaps Calcite</name> <properties> <maven.compiler.source>21</maven.compiler.source> 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 2f244f1cc..6cb7a75b4 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 @@ -25,7 +25,6 @@ import org.apache.calcite.linq4j.Enumerable; import org.apache.calcite.linq4j.Linq4j; import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.rel.type.RelProtoDataType; import org.apache.calcite.schema.ScannableTable; import org.apache.calcite.schema.impl.AbstractTable; import org.apache.calcite.sql.type.SqlTypeName; diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableFactory.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableFactory.java index ce0ea913a..30fdcf6ae 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableFactory.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableFactory.java @@ -60,7 +60,7 @@ public class DataTableFactory implements TableFactory<Table> { } private Table createDataTable(SchemaPlus schema, String name, Map<String, Object> operand, - RelDataType rowType) { + RelDataType rowType) { String directory = (String) operand.get("directory"); if (directory == null) { throw new RuntimeException("A directory should be specified"); @@ -74,8 +74,11 @@ public class DataTableFactory implements TableFactory<Table> { header.get(bytes); DataSchema dataSchema = DataSchema.read(new ByteArrayInputStream(bytes)); DataType<DataRow> dataType = new DataRowType(dataSchema); - DataCollection<DataRow> dataCollection = new AppendOnlyLog<>(dataType, memory); - return null; //new BaremapsTable(dataSchema, dataCollection); + DataCollection<DataRow> dataCollection = AppendOnlyLog.<DataRow>builder() + .dataType(dataType) + .memory(memory) + .build(); + return null; // new BaremapsTable(dataSchema, dataCollection); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDataTable.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDataTable.java index dcfab9722..ae7084185 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDataTable.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDataTable.java @@ -17,7 +17,6 @@ package org.apache.baremaps.calcite.baremaps; -import java.io.IOException; import java.util.Iterator; import org.apache.baremaps.calcite.DataRow; import org.apache.baremaps.calcite.DataRowType; @@ -53,7 +52,9 @@ public class BaremapsDataTable implements DataTable { */ public BaremapsDataTable(DataSchema schema) { this.schema = schema; - this.rows = new AppendOnlyLog<>(new DataRowType(schema)); + this.rows = AppendOnlyLog.<DataRow>builder() + .dataType(new DataRowType(schema)) + .build(); } /** @@ -94,7 +95,7 @@ public class BaremapsDataTable implements DataTable { } @Override - public void close() throws IOException { + public void close() throws Exception { rows.close(); } } diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java index 206067735..73cd1bc33 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsDdlExecutor.java @@ -424,7 +424,8 @@ public class BaremapsDdlExecutor extends DdlExecutorImpl { // Table does not exist. Create it. final BaremapsMaterializedView table = - new BaremapsMaterializedView(pair.right, RelDataTypeImpl.proto(rowType), 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 = 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 da3f1b722..09c6aef5e 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 @@ -34,7 +34,7 @@ class BaremapsMaterializedView extends BaremapsModifiableTable { MaterializationKey key; BaremapsMaterializedView( - String name, + String name, RelProtoDataType protoRowType, RelDataTypeFactory typeFactory) { super(name, protoRowType, typeFactory); diff --git a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java index 58bfd1255..d5d7a858d 100644 --- a/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java +++ b/baremaps-calcite/src/main/java/org/apache/baremaps/calcite/baremaps/BaremapsModifiableTable.java @@ -17,6 +17,14 @@ package org.apache.baremaps.calcite.baremaps; +import static java.util.Objects.requireNonNull; + +import java.lang.reflect.Type; +import java.nio.MappedByteBuffer; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; import org.apache.baremaps.calcite.*; import org.apache.baremaps.calcite.DataColumn.Cardinality; import org.apache.baremaps.data.collection.AppendOnlyLog; @@ -47,222 +55,213 @@ import org.apache.calcite.schema.impl.AbstractTableQueryable; import org.apache.calcite.sql.type.SqlTypeName; import org.checkerframework.checker.nullness.qual.Nullable; -import java.lang.reflect.Type; -import java.nio.MappedByteBuffer; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import static java.util.Objects.requireNonNull; - class BaremapsModifiableTable extends AbstractTable implements ModifiableTable, Wrapper { - private final String name; - - private final RelProtoDataType protoRowType; - - private final RelDataType rowType; - - private final DataSchema schema; + private final String name; + + private final RelProtoDataType protoRowType; + + private final RelDataType rowType; + + private final DataSchema schema; + + final DataCollection<DataRow> rows; + + BaremapsModifiableTable(String name, + RelProtoDataType protoRowType, + RelDataTypeFactory typeFactory) { + super(); + this.name = requireNonNull(name, "name"); + this.protoRowType = requireNonNull(protoRowType, "protoRowType"); + this.rowType = this.protoRowType.apply(typeFactory); + + // Create the schema + List<DataColumn> columns = rowType.getFieldList().stream().map(field -> { + String columnName = field.getName(); + RelDataType relDataType = field.getType(); + DataColumn.Cardinality columnCardinality = cardinalityFromRelDataType(relDataType); + DataColumn.Type columnType = typeFromRelDataType(relDataType); + return (DataColumn) new DataColumnFixed(columnName, columnCardinality, columnType); + }).toList(); + this.schema = new DataSchema(name, columns); + + // Create the collection + DataRowType dataRowType = new DataRowType(schema); + Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(Paths.get(this.name)); + this.rows = AppendOnlyLog.<DataRow>builder().dataType(dataRowType).memory(memory).build(); + } + + @Override + public TableModify toModificationRel( + RelOptCluster cluster, + RelOptTable table, + Prepare.CatalogReader catalogReader, + RelNode child, + TableModify.Operation operation, + @Nullable List<String> updateColumnList, + @Nullable List<RexNode> sourceExpressionList, + boolean flattened) { + return LogicalTableModify.create(table, catalogReader, child, operation, + updateColumnList, sourceExpressionList, flattened); + } + + private DataColumn.Cardinality cardinalityFromRelDataType(RelDataType columnType) { + if (columnType.getSqlTypeName() == SqlTypeName.ARRAY) { + return DataColumn.Cardinality.REPEATED; + } else if (columnType.isNullable()) { + return Cardinality.OPTIONAL; + } else { + return Cardinality.REQUIRED; + } + } + + public static DataColumn.Type typeFromRelDataType(RelDataType relDataType) { + SqlTypeName sqlTypeName = relDataType.getSqlTypeName(); + switch (sqlTypeName) { + case BOOLEAN: + return DataColumn.Type.BOOLEAN; + case TINYINT: + return DataColumn.Type.BYTE; + case SMALLINT: + return DataColumn.Type.SHORT; + case INTEGER: + return DataColumn.Type.INTEGER; + case BIGINT: + return DataColumn.Type.LONG; + case FLOAT: + case REAL: + return DataColumn.Type.FLOAT; + case DOUBLE: + case DECIMAL: + return DataColumn.Type.DOUBLE; + case CHAR: + case VARCHAR: + return DataColumn.Type.STRING; + case BINARY: + case VARBINARY: + return DataColumn.Type.BINARY; + case DATE: + return DataColumn.Type.LOCAL_DATE; + case TIME: + return DataColumn.Type.LOCAL_TIME; + case TIMESTAMP: + return DataColumn.Type.LOCAL_DATE_TIME; + case MAP: + return DataColumn.Type.NESTED; + case GEOMETRY: + return DataColumn.Type.GEOMETRY; + case ARRAY: + RelDataType componentType = requireNonNull(relDataType.getComponentType()); + return typeFromRelDataType(componentType); + default: + throw new IllegalArgumentException("Unsupported Calcite type: " + sqlTypeName); + } + } + + @Override + public Collection getModifiableCollection() { + return new CollectionAdapter(); + } + + @Override + public <T> Queryable<T> asQueryable(QueryProvider queryProvider, SchemaPlus schema, + String tableName) { + return new AbstractTableQueryable<T>(queryProvider, schema, this, + tableName) { + @Override + public Enumerator<T> enumerator() { + return (Enumerator<T>) Linq4j.enumerator(new CollectionAdapter()); + } + }; + } + + @Override + public Type getElementType() { + return Object[].class; + } + + @Override + public Expression getExpression(SchemaPlus schema, String tableName, Class clazz) { + return Schemas.tableExpression(schema, getElementType(), tableName, clazz); + } + + @Override + public RelDataType getRowType(final RelDataTypeFactory typeFactory) { + return rowType; + } + + private class CollectionAdapter implements Collection<Object[]> { + + private final int size; + + public CollectionAdapter() { + this.size = (int) Math.min(rows.size(), Integer.MAX_VALUE); + } - final DataCollection<DataRow> rows; + @Override + public int size() { + return size; + } - BaremapsModifiableTable(String name, - RelProtoDataType protoRowType, - RelDataTypeFactory typeFactory) { - super(); - this.name = requireNonNull(name, "name"); - this.protoRowType = requireNonNull(protoRowType, "protoRowType"); - this.rowType = this.protoRowType.apply(typeFactory); + @Override + public boolean isEmpty() { + return rows.isEmpty(); + } - // Create the schema - List<DataColumn> columns = rowType.getFieldList().stream().map(field -> { - String columnName = field.getName(); - RelDataType relDataType = field.getType(); - DataColumn.Cardinality columnCardinality = cardinalityFromRelDataType(relDataType); - DataColumn.Type columnType = typeFromRelDataType(relDataType); - return (DataColumn) new DataColumnFixed(columnName, columnCardinality, columnType); - }).toList(); - this.schema = new DataSchema(name, columns); + @Override + public boolean contains(Object o) { + return rows.contains(o); + } - // Create the collection - DataRowType dataRowType = new DataRowType(schema); - Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(Paths.get(this.name)); - this.rows = new AppendOnlyLog<>(dataRowType, memory); + @Override + public Iterator<Object[]> iterator() { + return rows.stream().map(row -> row.values().toArray()).iterator(); } @Override - public TableModify toModificationRel( - RelOptCluster cluster, - RelOptTable table, - Prepare.CatalogReader catalogReader, - RelNode child, - TableModify.Operation operation, - @Nullable List<String> updateColumnList, - @Nullable List<RexNode> sourceExpressionList, - boolean flattened) { - return LogicalTableModify.create(table, catalogReader, child, operation, - updateColumnList, sourceExpressionList, flattened); + public Object[] toArray() { + return rows.stream().map(row -> row.values().toArray()).toArray(); } - private DataColumn.Cardinality cardinalityFromRelDataType(RelDataType columnType) { - if (columnType.getSqlTypeName() == SqlTypeName.ARRAY) { - return DataColumn.Cardinality.REPEATED; - } else if (columnType.isNullable()) { - return Cardinality.OPTIONAL; - } else { - return Cardinality.REQUIRED; - } + @Override + public <T> T[] toArray(T[] a) { + return (T[]) rows.stream().map(row -> row.values().toArray()).toArray(); } - public static DataColumn.Type typeFromRelDataType(RelDataType relDataType) { - SqlTypeName sqlTypeName = relDataType.getSqlTypeName(); - switch (sqlTypeName) { - case BOOLEAN: - return DataColumn.Type.BOOLEAN; - case TINYINT: - return DataColumn.Type.BYTE; - case SMALLINT: - return DataColumn.Type.SHORT; - case INTEGER: - return DataColumn.Type.INTEGER; - case BIGINT: - return DataColumn.Type.LONG; - case FLOAT: - case REAL: - return DataColumn.Type.FLOAT; - case DOUBLE: - case DECIMAL: - return DataColumn.Type.DOUBLE; - case CHAR: - case VARCHAR: - return DataColumn.Type.STRING; - case BINARY: - case VARBINARY: - return DataColumn.Type.BINARY; - case DATE: - return DataColumn.Type.LOCAL_DATE; - case TIME: - return DataColumn.Type.LOCAL_TIME; - case TIMESTAMP: - return DataColumn.Type.LOCAL_DATE_TIME; - case MAP: - return DataColumn.Type.NESTED; - case GEOMETRY: - return DataColumn.Type.GEOMETRY; - case ARRAY: - RelDataType componentType = requireNonNull(relDataType.getComponentType()); - return typeFromRelDataType(componentType); - default: - throw new IllegalArgumentException("Unsupported Calcite type: " + sqlTypeName); - } + @Override + public boolean add(Object[] objects) { + return rows.add(new DataRow(schema, List.of(objects))); } @Override - public Collection getModifiableCollection() { - return new CollectionAdapter(); + public boolean remove(Object o) { + throw new UnsupportedOperationException(); } @Override - public <T> Queryable<T> asQueryable(QueryProvider queryProvider, SchemaPlus schema, - String tableName) { - return new AbstractTableQueryable<T>(queryProvider, schema, this, - tableName) { - @Override - public Enumerator<T> enumerator() { - return (Enumerator<T>) Linq4j.enumerator(new CollectionAdapter()); - } - }; + public boolean containsAll(Collection<?> c) { + return rows.containsAll(c); } @Override - public Type getElementType() { - return Object[].class; + public boolean addAll(Collection<? extends Object[]> c) { + return rows.addAll(c.stream().map(objects -> new DataRow(schema, List.of(objects))).toList()); } @Override - public Expression getExpression(SchemaPlus schema, String tableName, Class clazz) { - return Schemas.tableExpression(schema, getElementType(), tableName, clazz); + public boolean removeAll(Collection<?> c) { + throw new UnsupportedOperationException(); } @Override - public RelDataType getRowType(final RelDataTypeFactory typeFactory) { - return rowType; + public boolean retainAll(Collection<?> c) { + throw new UnsupportedOperationException(); } - private class CollectionAdapter implements Collection<Object[]> { - - private final int size; - - public CollectionAdapter() { - this.size = (int) Math.min(rows.size(), Integer.MAX_VALUE); - } - - @Override - public int size() { - return size; - } - - @Override - public boolean isEmpty() { - return rows.isEmpty(); - } - - @Override - public boolean contains(Object o) { - return rows.contains(o); - } - - @Override - public Iterator<Object[]> iterator() { - return rows.stream().map(row -> row.values().toArray()).iterator(); - } - - @Override - public Object[] toArray() { - return rows.stream().map(row -> row.values().toArray()).toArray(); - } - - @Override - public <T> T[] toArray(T[] a) { - return (T[]) rows.stream().map(row -> row.values().toArray()).toArray(); - } - - @Override - public boolean add(Object[] objects) { - return rows.add(new DataRow(schema, List.of(objects))); - } - - @Override - public boolean remove(Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean containsAll(Collection<?> c) { - return rows.containsAll(c); - } - - @Override - public boolean addAll(Collection<? extends Object[]> c) { - return rows.addAll(c.stream().map(objects -> new DataRow(schema, List.of(objects))).toList()); - } - - @Override - public boolean removeAll(Collection<?> c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean retainAll(Collection<?> c) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - rows.clear(); - } + @Override + public void clear() { + rows.clear(); } + } } 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 e29f58f1d..e6516be4f 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 @@ -17,6 +17,9 @@ package org.apache.baremaps.calcite; +import java.sql.*; +import java.util.List; +import java.util.Properties; import org.apache.baremaps.calcite.DataColumn.Cardinality; import org.apache.baremaps.calcite.DataColumn.Type; import org.apache.baremaps.calcite.baremaps.BaremapsDataTable; @@ -30,92 +33,97 @@ import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; -import java.sql.*; -import java.util.List; -import java.util.Properties; - public class CalciteTest { - @Test - @Disabled - 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"); - info.setProperty("parserFactory", BaremapsDdlExecutor.class.getName() + "#PARSER_FACTORY"); - info.setProperty("materializationsEnabled", "true"); - - 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 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)))); - - 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); - - String mv = "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"; - - // Execute the SQL query - try (Statement statement = connection.createStatement()) { - statement.execute(mv); - } - - // 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_population"; - - // Execute the SQL query - try (Statement statement = connection.createStatement()) { - ResultSet resultSet = statement.executeQuery(sql); - while (resultSet.next()) { - System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3) + " " + resultSet.getInt(4)); - } - } - - + @Test + @Disabled + 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"); + info.setProperty("parserFactory", BaremapsDdlExecutor.class.getName() + "#PARSER_FACTORY"); + info.setProperty("materializationsEnabled", "true"); + + 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 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, + IndexedDataList.<DataRow>builder() + .values(AppendOnlyLog.<DataRow>builder() + .dataType(new DataRowType(cityRowType)) + .build()) + .build()); + + 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, + IndexedDataList.<DataRow>builder() + .values(AppendOnlyLog.<DataRow>builder() + .dataType(new DataRowType(populationRowType)) + .build()) + .build()); + + 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); + + String mv = "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"; + + // Execute the SQL query + try (Statement statement = connection.createStatement()) { + statement.execute(mv); + } + + // 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_population"; + + // Execute the SQL query + try (Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery(sql); + while (resultSet.next()) { + System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2) + " " + + resultSet.getString(3) + " " + resultSet.getInt(4)); } + } + } + } + } diff --git a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/DataTableAdapterFactoryTest.java b/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/DataTableAdapterFactoryTest.java index e5e089cef..52bd89d4d 100644 --- a/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/DataTableAdapterFactoryTest.java +++ b/baremaps-calcite/src/test/java/org/apache/baremaps/calcite/DataTableAdapterFactoryTest.java @@ -17,27 +17,12 @@ package org.apache.baremaps.calcite; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintWriter; -import java.nio.ByteBuffer; -import java.nio.MappedByteBuffer; -import java.nio.file.Files; -import java.nio.file.Path; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; -import java.util.List; -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.data.collection.AppendOnlyLog; -import org.apache.baremaps.data.memory.Memory; -import org.apache.baremaps.data.memory.MemoryMappedDirectory; -import org.apache.baremaps.data.util.FileUtils; import org.junit.jupiter.api.Test; -import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.GeometryFactory; class DataTableAdapterFactoryTest { @@ -85,68 +70,68 @@ class DataTableAdapterFactoryTest { } } - @Test - public void createMMapTable() throws Exception { - Path path = Files.createTempDirectory("temp"); - - DataSchema dataSchema = new DataSchema("test", List.of( - new DataColumnFixed("id", Cardinality.REQUIRED, Type.INTEGER), - new DataColumnFixed("name", Cardinality.REQUIRED, Type.STRING), - new DataColumnFixed("geom", Cardinality.REQUIRED, Type.GEOMETRY))); - - // Serialize the schema - ByteArrayOutputStream output = new ByteArrayOutputStream(); - DataSchema.write(output, dataSchema); - byte[] bytes = output.toByteArray(); - - // Write the schema to the header of the memory mapped file - Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(path); - ByteBuffer header = memory.header(); - header.position(Long.BYTES); - header.putInt(bytes.length); - header.put(bytes); - - DataTable dataTable = - new BaremapsDataTable(dataSchema, - new AppendOnlyLog<>(new DataRowType(dataSchema), memory)); - dataTable.add(new DataRow(dataSchema, - List.of(1, "a", new GeometryFactory().createPoint(new Coordinate(1, 1))))); - dataTable.add(new DataRow(dataSchema, - List.of(2, "b", new GeometryFactory().createPoint(new Coordinate(2, 2))))); - - dataTable.close(); - - - String model = """ - { - version: '1.0', - defaultSchema: 'TEST', - schemas: [ - { - name: 'TEST', - tables: [ - { - name: 'TEST', - factory: 'org.apache.baremaps.calcite.DataTableFactory', - operand: { - format: 'baremaps', - directory: '%s' - } - } - ] - } - ] - } - """.formatted(path.toAbsolutePath()); - try (Connection connection = - DriverManager.getConnection("jdbc:calcite:model=inline:" + model)) { - - ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM TEST.TEST"); - while (resultSet.next()) { - System.out.println(resultSet.getString("ID") + " " + resultSet.getString("GEOM")); - } - } finally { - FileUtils.deleteRecursively(path); - } - } + // @Test + // public void createMMapTable() throws Exception { + // Path path = Files.createTempDirectory("temp"); + // + // DataSchema dataSchema = new DataSchema("test", List.of( + // new DataColumnFixed("id", Cardinality.REQUIRED, Type.INTEGER), + // new DataColumnFixed("name", Cardinality.REQUIRED, Type.STRING), + // new DataColumnFixed("geom", Cardinality.REQUIRED, Type.GEOMETRY))); + // + // // Serialize the schema + // ByteArrayOutputStream output = new ByteArrayOutputStream(); + // DataSchema.write(output, dataSchema); + // byte[] bytes = output.toByteArray(); + // + // // Write the schema to the header of the memory mapped file + // Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(path); + // ByteBuffer header = memory.header(); + // header.position(Long.BYTES); + // header.putInt(bytes.length); + // header.put(bytes); + // + // DataTable dataTable = + // new BaremapsDataTable(dataSchema, + // new AppendOnlyLog<>(new DataRowType(dataSchema), memory)); + // dataTable.add(new DataRow(dataSchema, + // List.of(1, "a", new GeometryFactory().createPoint(new Coordinate(1, 1))))); + // dataTable.add(new DataRow(dataSchema, + // List.of(2, "b", new GeometryFactory().createPoint(new Coordinate(2, 2))))); + // + // dataTable.close(); + // + // + // String model = """ + // { + // version: '1.0', + // defaultSchema: 'TEST', + // schemas: [ + // { + // name: 'TEST', + // tables: [ + // { + // name: 'TEST', + // factory: 'org.apache.baremaps.calcite.DataTableFactory', + // operand: { + // format: 'baremaps', + // directory: '%s' + // } + // } + // ] + // } + // ] + // } + // """.formatted(path.toAbsolutePath()); + // try (Connection connection = + // DriverManager.getConnection("jdbc:calcite:model=inline:" + model)) { + // + // ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM TEST.TEST"); + // while (resultSet.next()) { + // System.out.println(resultSet.getString("ID") + " " + resultSet.getString("GEOM")); + // } + // } finally { + // FileUtils.deleteRecursively(path); + // } + // } } 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 deleted file mode 100644 index 6eb62f014..000000000 --- a/baremaps-core/src/test/java/org/apache/baremaps/calcite/CalciteTest.java +++ /dev/null @@ -1,120 +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 com.google.common.collect.ImmutableList; -// import java.sql.*; -// import java.util.List; -// import java.util.Properties; -// import org.apache.baremaps.data.calcite.BaremapsTable; -// import org.apache.baremaps.data.collection.AppendOnlyLog; -// import org.apache.baremaps.data.collection.IndexedDataList; -// import org.apache.baremaps.calcite.data.DataTableImpl; -// import org.apache.baremaps.calcite.data.DataTypeImpl; -// import org.apache.baremaps.maplibre.vectortile.VectorTileFunctions; -// import org.apache.baremaps.store.*; -// import org.apache.baremaps.calcite.DataColumn.Cardinality; -// import org.apache.baremaps.calcite.DataColumn.Type; -// import org.apache.calcite.jdbc.CalciteConnection; -// import org.apache.calcite.model.ModelHandler; -// import org.apache.calcite.runtime.AccumOperation; -// import org.apache.calcite.runtime.CollectOperation; -// import org.apache.calcite.runtime.SpatialTypeFunctions; -// import org.apache.calcite.runtime.UnionOperation; -// import org.apache.calcite.schema.SchemaPlus; -// import org.apache.calcite.schema.impl.AggregateFunctionImpl; -// import org.apache.calcite.sql.fun.SqlSpatialTypeFunctions; -// import org.junit.jupiter.api.Assertions; -// import org.junit.jupiter.api.Test; -// import org.locationtech.jts.geom.*; - -class CalciteTest { - // - // @Test - // void test() throws SQLException { - // GeometryFactory geometryFactory = new GeometryFactory(); - // - // Properties info = new Properties(); - // info.setProperty("lex", "MYSQL"); - // - // try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) { - // CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); - // SchemaPlus rootSchema = calciteConnection.getRootSchema(); - // - // // Add the spatial functions to the root schema - // ImmutableList<String> emptyPath = ImmutableList.of(); - // ModelHandler.addFunctions(rootSchema, null, emptyPath, - // SpatialTypeFunctions.class.getName(), "*", true); - // ModelHandler.addFunctions(rootSchema, null, emptyPath, - // SqlSpatialTypeFunctions.class.getName(), "*", true); - // - // rootSchema.add("ST_UNION", AggregateFunctionImpl.create(UnionOperation.class)); - // rootSchema.add("ST_ACCUM", AggregateFunctionImpl.create(AccumOperation.class)); - // rootSchema.add("ST_COLLECT", AggregateFunctionImpl.create(CollectOperation.class)); - // - // ModelHandler.addFunctions(rootSchema, "ST_AsMVTGeom", emptyPath, - // VectorTileFunctions.class.getName(), "asVectorTileGeom", true); - // ModelHandler.addFunctions(rootSchema, "ST_AsMVT", emptyPath, - // VectorTileFunctions.class.getName(), "asVectorTile", true); - // - // // Create the 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 DataTableImpl( - // cityRowType, - // new IndexedDataList<>(new AppendOnlyLog<>(new DataTypeImpl(cityRowType)))); - // 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))))); - // BaremapsTable cityBaremapsTable = new BaremapsTable(cityDataTable); - // rootSchema.add("city", cityBaremapsTable); - // - // // Create the 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 DataTableImpl( - // populationRowType, - // new IndexedDataList<>(new AppendOnlyLog<>(new DataTypeImpl(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))); - // BaremapsTable populationBaremapsTable = new BaremapsTable(populationDataTable); - // rootSchema.add("population", populationBaremapsTable); - // - // // Query the database - // String sql = """ - // SELECT ST_AsText(ST_AsMVTGeom( - // ST_GeomFromText('POLYGON ((0 0, 10 1, 10 10, 1 10, 0 0))'), - // ST_MakeEnvelope(0, 0, 4096, 4096), - // 4096, 0, true)) - // """; - // - // try (Statement statement = connection.createStatement(); - // ResultSet resultSet = statement.executeQuery(sql)) { - // Assertions.assertTrue(resultSet.next()); - // Assertions.assertEquals("POLYGON ((0 4096, 10 4095, 10 4086, 1 4086, 0 4096))", - // resultSet.getString(1)); - // } - // } - // } -} diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollection.java b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollection.java index c4458d180..5c5f549f6 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollection.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollection.java @@ -17,7 +17,6 @@ package org.apache.baremaps.data.collection; -import java.io.Closeable; import java.util.*; import java.util.stream.Stream; import java.util.stream.StreamSupport; diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollectionMapper.java b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollectionMapper.java index 9cca2c1ca..9ab9101c4 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollectionMapper.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataCollectionMapper.java @@ -18,7 +18,6 @@ package org.apache.baremaps.data.collection; -import java.io.IOException; import java.util.Iterator; import java.util.function.Function; diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataConversions.java b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataConversions.java index d431ca77b..8f1957c16 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataConversions.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/DataConversions.java @@ -17,8 +17,6 @@ package org.apache.baremaps.data.collection; -import java.io.Closeable; -import java.io.IOException; import java.util.*; import java.util.Map.Entry; diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/FixedSizeDataList.java b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/FixedSizeDataList.java index 7ae4fc233..5c7b57478 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/FixedSizeDataList.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/FixedSizeDataList.java @@ -19,7 +19,6 @@ package org.apache.baremaps.data.collection; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicLong; import org.apache.baremaps.data.memory.Memory; diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/IndexedDataList.java b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/IndexedDataList.java index d7427b37c..1a14640f5 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/IndexedDataList.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/IndexedDataList.java @@ -19,7 +19,6 @@ package org.apache.baremaps.data.collection; -import java.io.IOException; import org.apache.baremaps.data.type.LongDataType; /** diff --git a/baremaps-data/src/main/java/org/apache/baremaps/data/memory/Memory.java b/baremaps-data/src/main/java/org/apache/baremaps/data/memory/Memory.java index 110f16eeb..cb637dc39 100644 --- a/baremaps-data/src/main/java/org/apache/baremaps/data/memory/Memory.java +++ b/baremaps-data/src/main/java/org/apache/baremaps/data/memory/Memory.java @@ -57,10 +57,7 @@ public abstract class Memory<T extends ByteBuffer> implements AutoCloseable { } /** -<<<<<<< HEAD - * Returns the size of each segment. -======= - * Returns the size of the header. + * <<<<<<< HEAD Returns the size of each segment. ======= Returns the size of the header. * * @return the size of the header */ @@ -69,8 +66,7 @@ public abstract class Memory<T extends ByteBuffer> implements AutoCloseable { } /** - * Returns the size of the segments. ->>>>>>> 251ea904 (Add header to memory) + * Returns the size of the segments. >>>>>>> 251ea904 (Add header to memory) * * @return the segment size in bytes */ @@ -157,7 +153,7 @@ public abstract class Memory<T extends ByteBuffer> implements AutoCloseable { } T segment = segments.get(index); if (segment == null) { - segment = allocate(index, segmentSize); + segment = allocateSegment(index); segments.set(index, segment); } return segment; diff --git a/pom.xml b/pom.xml index a2af58698..2986bf531 100644 --- a/pom.xml +++ b/pom.xml @@ -589,11 +589,6 @@ limitations under the License. <artifactId>junit-jupiter-params</artifactId> <scope>test</scope> </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - <scope>test</scope> - </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId>
