github-code-scanning[bot] commented on code in PR #709: URL: https://github.com/apache/incubator-baremaps/pull/709#discussion_r1237170241
########## baremaps-core/src/main/java/org/apache/baremaps/calcite/Calcite.java: ########## @@ -0,0 +1,181 @@ +/* + * Licensed 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.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collection; +import java.util.List; +import java.util.Properties; +import org.apache.baremaps.collection.AppendOnlyBuffer; +import org.apache.baremaps.collection.DataCollectionAdapter; +import org.apache.baremaps.collection.IndexedDataList; +import org.apache.baremaps.collection.store.*; +import org.apache.baremaps.collection.type.RowDataType; +import org.apache.calcite.DataContext; +import org.apache.calcite.jdbc.CalciteConnection; +import org.apache.calcite.jdbc.JavaTypeFactoryImpl; +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.Linq4j; +import org.apache.calcite.model.ModelHandler; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.runtime.SpatialTypeFunctions; +import org.apache.calcite.runtime.SpatialTypeFunctions.Accum; +import org.apache.calcite.runtime.SpatialTypeFunctions.Collect; +import org.apache.calcite.runtime.SpatialTypeFunctions.Union; +import org.apache.calcite.schema.ScannableTable; +import org.apache.calcite.schema.SchemaPlus; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.calcite.schema.impl.AggregateFunctionImpl; +import org.apache.calcite.sql.fun.SqlSpatialTypeFunctions; +import org.apache.calcite.sql.type.SqlTypeName; +import org.locationtech.jts.geom.*; + +public class Calcite { + + private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(); + + private static final DataSchema CITY_SCHEMA = new DataSchemaImpl("country", List.of( + new DataColumnImpl("id", Integer.class), + new DataColumnImpl("name", String.class), + new DataColumnImpl("geometry", Geometry.class))); + + private static final DataTable CITY_TABLE = new DataTableImpl( + CITY_SCHEMA, + new IndexedDataList<>(new AppendOnlyBuffer<>(new RowDataType(CITY_SCHEMA)))); + + static { + CITY_TABLE.add(new DataRowImpl(CITY_TABLE.schema(), + List.of(1, "Paris", GEOMETRY_FACTORY.createPoint(new Coordinate(2.3522, 48.8566))))); + CITY_TABLE.add(new DataRowImpl(CITY_TABLE.schema(), + List.of(2, "New York", GEOMETRY_FACTORY.createPoint(new Coordinate(-74.0060, 40.7128))))); + } + + private static final DataSchema POPULATION_SCHEMA = new DataSchemaImpl("population", List.of( + new DataColumnImpl("country_id", Integer.class), + new DataColumnImpl("population", Integer.class))); + + private static final DataTable POPULATION_TABLE = new DataTableImpl( + POPULATION_SCHEMA, + new IndexedDataList<>(new AppendOnlyBuffer<>(new RowDataType(POPULATION_SCHEMA)))); + + static { + POPULATION_TABLE + .add(new DataRowImpl(POPULATION_TABLE.schema(), List.of(1, 2_161_000))); + POPULATION_TABLE + .add(new DataRowImpl(POPULATION_TABLE.schema(), List.of(2, 8_336_000))); + } + + public static void main(String[] args) throws SQLException { + Properties info = new Properties(); + info.setProperty("lex", "MYSQL"); + + Connection connection = DriverManager.getConnection("jdbc:calcite:", info); + CalciteConnection calciteConnection = + connection.unwrap(CalciteConnection.class); + + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + + final 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(Union.class)); + rootSchema.add("ST_ACCUM", AggregateFunctionImpl.create(Accum.class)); + rootSchema.add("ST_COLLECT", AggregateFunctionImpl.create(Collect.class)); + + ListTable cityTable = new ListTable(CITY_TABLE); + rootSchema.add("country", cityTable); + + ListTable populationTable = new ListTable(POPULATION_TABLE); + rootSchema.add("population", populationTable); + + String sql = + "SELECT name, ST_Buffer(geometry, 10), population FROM country INNER JOIN population ON country.id = population.country_id"; + ResultSet resultSet = connection.createStatement().executeQuery(sql); Review Comment: ## Potential database resource leak This Statement is not always closed on method exit. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/692) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
