Skip to content

Commit b976bb2

Browse files
committed
Clean and fix code
1 parent 83cdca3 commit b976bb2

File tree

8 files changed

+27
-133
lines changed

8 files changed

+27
-133
lines changed

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/BaremapsDdlExecutor.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -557,27 +557,6 @@ public void execute(SqlCreateTableLike create,
557557
schema.getTable(tableName, context.config().caseSensitive());
558558
final Table table = requireNonNull(tableEntry, "tableEntry").getTable();
559559

560-
InitializerExpressionFactory ief = NullInitializerExpressionFactory.INSTANCE;
561-
if (table instanceof Wrapper) {
562-
final InitializerExpressionFactory sourceIef =
563-
((Wrapper) table).unwrap(InitializerExpressionFactory.class);
564-
if (sourceIef != null) {
565-
final Set<SqlCreateTableLike.LikeOption> optionSet = create.options();
566-
final boolean includingGenerated =
567-
optionSet.contains(SqlCreateTableLike.LikeOption.GENERATED)
568-
|| optionSet.contains(SqlCreateTableLike.LikeOption.ALL);
569-
final boolean includingDefaults =
570-
optionSet.contains(SqlCreateTableLike.LikeOption.DEFAULTS)
571-
|| optionSet.contains(SqlCreateTableLike.LikeOption.ALL);
572-
573-
// initializes columns based on the source table InitializerExpressionFactory
574-
// and like options.
575-
ief =
576-
new CopiedTableInitializerExpressionFactory(
577-
includingGenerated, includingDefaults, sourceIef);
578-
}
579-
}
580-
581560
final JavaTypeFactory typeFactory = context.getTypeFactory();
582561
final RelDataType rowType = table.getRowType(typeFactory);
583562
// Table does not exist. Create it.

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/BaremapsTableFactory.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Table create(
7777
RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl();
7878

7979
return switch (format) {
80-
case "data" -> createDataTable(name, operand, protoRowType, typeFactory);
80+
case "data" -> createDataTable(name, operand, typeFactory);
8181
case "osm" -> createOpenStreetMapTable(operand);
8282
case "csv" -> createCsvTable(operand);
8383
case "shp" -> createShapefileTable(operand);
@@ -94,14 +94,12 @@ public Table create(
9494
*
9595
* @param name the table name
9696
* @param operand the operand properties
97-
* @param protoRowType the prototype row type
9897
* @param typeFactory the type factory to use
9998
* @return the created table
10099
*/
101100
private Table createDataTable(
102101
String name,
103102
Map<String, Object> operand,
104-
RelProtoDataType protoRowType,
105103
RelDataTypeFactory typeFactory) {
106104
String directory = (String) operand.get("directory");
107105
if (directory == null) {
@@ -110,7 +108,7 @@ private Table createDataTable(
110108
try {
111109
Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(Paths.get(directory));
112110
ByteBuffer header = memory.header();
113-
long size = header.getLong();
111+
header.getLong(); // Skip the size
114112
int length = header.getInt();
115113
byte[] bytes = new byte[length];
116114
header.get(bytes);

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/postgres/PostgresDdlExecutor.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ private DataSource getDataSource(CalcitePrepare.Context context) {
218218
/** Truncate the PostgreSQL table. */
219219
static void truncate(SqlIdentifier name, CalcitePrepare.Context context, DataSource dataSource) {
220220
final Pair<@Nullable CalciteSchema, String> pair = schema(context, true, name);
221-
final @Nullable CalciteSchema schema = pair.left;
222221
final String tableName = pair.right;
223-
224222
try (Connection connection = dataSource.getConnection();
225223
PreparedStatement stmt =
226224
connection.prepareStatement("TRUNCATE TABLE \"" + tableName + "\"")) {
@@ -619,7 +617,6 @@ public void execute(SqlCreateTable create,
619617
boolean first = true;
620618
// Process column declarations for the CREATE TABLE statement
621619
final ImmutableList.Builder<ColumnDef> b = ImmutableList.builder();
622-
final RelDataTypeFactory.Builder builder = typeFactory.builder();
623620
final RelDataTypeFactory.Builder storedBuilder = typeFactory.builder();
624621
final SqlValidator validator = validator(context, true);
625622

@@ -632,7 +629,6 @@ public void execute(SqlCreateTable create,
632629
if (c.e instanceof SqlColumnDeclaration) {
633630
final SqlColumnDeclaration d = (SqlColumnDeclaration) c.e;
634631
final RelDataType type = d.dataType.deriveType(validator, true);
635-
builder.add(d.name.getSimple(), type);
636632
if (d.strategy != ColumnStrategy.VIRTUAL) {
637633
storedBuilder.add(d.name.getSimple(), type);
638634
}
@@ -655,7 +651,6 @@ public void execute(SqlCreateTable create,
655651
? ColumnStrategy.NULLABLE
656652
: ColumnStrategy.NOT_NULLABLE;
657653
b.add(ColumnDef.of(c.e, f.getType(), strategy));
658-
builder.add(id.getSimple(), f.getType());
659654
storedBuilder.add(id.getSimple(), f.getType());
660655

661656
// Add column to SQL statement
@@ -671,7 +666,6 @@ public void execute(SqlCreateTable create,
671666

672667
createTableSql.append(")");
673668

674-
final RelDataType rowType = builder.build();
675669
if (pair.left.plus().getTable(pair.right) != null) {
676670
// Table exists.
677671
if (create.ifNotExists) {

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/postgres/PostgresModifiableTable.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,15 @@ public boolean contains(Object o) {
498498
}
499499

500500
String sql = "SELECT COUNT(*) FROM \"" + tableName + "\" WHERE " + whereClause;
501-
PreparedStatement pstmt = connection.prepareStatement(sql);
502-
503-
for (int i = 0; i < values.length; i++) {
504-
pstmt.setObject(i + 1, values[i]);
505-
}
506-
507-
ResultSet rs = pstmt.executeQuery();
508-
if (rs.next()) {
509-
return rs.getInt(1) > 0;
501+
try (PreparedStatement statement = connection.prepareStatement(sql)) {
502+
for (int i = 0; i < values.length; i++) {
503+
statement.setObject(i + 1, values[i]);
504+
}
505+
try (ResultSet rs = statement.executeQuery()) {
506+
if (rs.next()) {
507+
return rs.getInt(1) > 0;
508+
}
509+
}
510510
}
511511
return false;
512512
} catch (SQLException e) {

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/postgres/PostgresTypeConversion.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
package org.apache.baremaps.calcite.postgres;
1919

2020
import java.util.ArrayList;
21-
import java.util.HashMap;
2221
import java.util.List;
23-
import java.util.Map;
2422
import org.apache.baremaps.calcite.data.DataColumn;
2523
import org.apache.baremaps.calcite.data.DataSchema;
2624
import org.apache.calcite.rel.type.RelDataType;
@@ -36,44 +34,6 @@ private PostgresTypeConversion() {
3634
// Prevent instantiation
3735
}
3836

39-
private static final Map<String, SqlTypeName> POSTGRES_TO_SQL_TYPE = new HashMap<>();
40-
41-
static {
42-
// Standard PostgreSQL types
43-
POSTGRES_TO_SQL_TYPE.put("varchar", SqlTypeName.VARCHAR);
44-
POSTGRES_TO_SQL_TYPE.put("char", SqlTypeName.CHAR);
45-
POSTGRES_TO_SQL_TYPE.put("text", SqlTypeName.VARCHAR);
46-
POSTGRES_TO_SQL_TYPE.put("smallint", SqlTypeName.SMALLINT);
47-
POSTGRES_TO_SQL_TYPE.put("int2", SqlTypeName.SMALLINT);
48-
POSTGRES_TO_SQL_TYPE.put("integer", SqlTypeName.INTEGER);
49-
POSTGRES_TO_SQL_TYPE.put("int4", SqlTypeName.INTEGER);
50-
POSTGRES_TO_SQL_TYPE.put("bigint", SqlTypeName.BIGINT);
51-
POSTGRES_TO_SQL_TYPE.put("int8", SqlTypeName.BIGINT);
52-
POSTGRES_TO_SQL_TYPE.put("decimal", SqlTypeName.DECIMAL);
53-
POSTGRES_TO_SQL_TYPE.put("numeric", SqlTypeName.DECIMAL);
54-
POSTGRES_TO_SQL_TYPE.put("real", SqlTypeName.REAL);
55-
POSTGRES_TO_SQL_TYPE.put("float4", SqlTypeName.FLOAT);
56-
POSTGRES_TO_SQL_TYPE.put("double precision", SqlTypeName.DOUBLE);
57-
POSTGRES_TO_SQL_TYPE.put("float8", SqlTypeName.DOUBLE);
58-
POSTGRES_TO_SQL_TYPE.put("boolean", SqlTypeName.BOOLEAN);
59-
POSTGRES_TO_SQL_TYPE.put("bool", SqlTypeName.BOOLEAN);
60-
POSTGRES_TO_SQL_TYPE.put("date", SqlTypeName.DATE);
61-
POSTGRES_TO_SQL_TYPE.put("time", SqlTypeName.TIME);
62-
POSTGRES_TO_SQL_TYPE.put("timestamp", SqlTypeName.TIMESTAMP);
63-
POSTGRES_TO_SQL_TYPE.put("timestamptz", SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
64-
POSTGRES_TO_SQL_TYPE.put("uuid", SqlTypeName.VARCHAR);
65-
POSTGRES_TO_SQL_TYPE.put("json", SqlTypeName.OTHER);
66-
POSTGRES_TO_SQL_TYPE.put("jsonb", SqlTypeName.OTHER);
67-
POSTGRES_TO_SQL_TYPE.put("bytea", SqlTypeName.BINARY);
68-
POSTGRES_TO_SQL_TYPE.put("interval", SqlTypeName.INTERVAL_DAY_SECOND);
69-
70-
// PostGIS specific types
71-
POSTGRES_TO_SQL_TYPE.put("geometry", SqlTypeName.GEOMETRY);
72-
POSTGRES_TO_SQL_TYPE.put("geography", SqlTypeName.GEOMETRY);
73-
POSTGRES_TO_SQL_TYPE.put("box2d", SqlTypeName.OTHER);
74-
POSTGRES_TO_SQL_TYPE.put("box3d", SqlTypeName.OTHER);
75-
}
76-
7737
/**
7838
* Converts a PostgreSQL type to a Calcite RelDataType.
7939
*

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/postgres/PostgresDdlExecutorTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Tag;
3030
import org.junit.jupiter.api.Test;
31-
import org.locationtech.jts.geom.Coordinate;
32-
import org.locationtech.jts.geom.GeometryFactory;
33-
import org.locationtech.jts.geom.Point;
3431

3532
/**
3633
* Tests for the PostgresDdlExecutor class, which provides DDL execution abilities for PostgreSQL
3734
* tables through Calcite.
3835
*/
3936
class PostgresDdlExecutorTest extends PostgresContainerTest {
4037

41-
private GeometryFactory geometryFactory = new GeometryFactory();
42-
4338
@BeforeEach
4439
void setUp() throws SQLException {
4540
// Set ThreadLocal DataSource for PostgresDdlExecutor.INSTANCE to use
@@ -80,9 +75,6 @@ void testMaterializedView() throws SQLException {
8075
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
8176
SchemaPlus rootSchema = calciteConnection.getRootSchema();
8277

83-
// Create a custom PostgresDdlExecutor with our DataSource
84-
PostgresDdlExecutor ddlExecutor = new PostgresDdlExecutor(dataSource());
85-
8678
// Add schema and tables to Calcite
8779
try {
8880
// Create PostgreSQL tables in Calcite schema
@@ -164,7 +156,6 @@ private void createTestData() throws SQLException {
164156
"population INTEGER)");
165157

166158
// Insert Paris
167-
Point parisPoint = geometryFactory.createPoint(new Coordinate(2.3522, 48.8566));
168159
String parisWKT = "POINT(2.3522 48.8566)";
169160
try (PreparedStatement ps = connection.prepareStatement(
170161
"INSERT INTO city VALUES (1, 'Paris', ST_GeomFromText(?, 4326))")) {
@@ -173,7 +164,6 @@ private void createTestData() throws SQLException {
173164
}
174165

175166
// Insert New York
176-
Point nyPoint = geometryFactory.createPoint(new Coordinate(-74.0060, 40.7128));
177167
String nyWKT = "POINT(-74.0060 40.7128)";
178168
try (PreparedStatement ps = connection.prepareStatement(
179169
"INSERT INTO city VALUES (2, 'New York', ST_GeomFromText(?, 4326))")) {
@@ -202,9 +192,6 @@ void testCreateAndDropTable() throws SQLException {
202192
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
203193
SchemaPlus rootSchema = calciteConnection.getRootSchema();
204194

205-
// Create a custom PostgresDdlExecutor with our DataSource
206-
PostgresDdlExecutor ddlExecutor = new PostgresDdlExecutor(dataSource());
207-
208195
try {
209196
// Test CREATE TABLE
210197
try (Statement statement = connection.createStatement()) {
@@ -280,9 +267,6 @@ void testCreateAndDropView() throws SQLException {
280267
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
281268
SchemaPlus rootSchema = calciteConnection.getRootSchema();
282269

283-
// Create a custom PostgresDdlExecutor with our DataSource
284-
PostgresDdlExecutor ddlExecutor = new PostgresDdlExecutor(dataSource());
285-
286270
try {
287271
// Register tables with Calcite
288272
PostgresModifiableTable cityTable = new PostgresModifiableTable(dataSource(), "city");

baremaps-core/src/main/java/org/apache/baremaps/tasks/ImportGeoPackage.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ public void execute(WorkflowContext context) throws Exception {
127127
logger.info("Is GeoPackageSchema: {}", registeredSchema instanceof GeoPackageSchema);
128128

129129
// Get the list of tables in the GeoPackage
130-
List<String> tables = getGeoPackageTables(geoPackageSchema);
130+
List<String> tables = new ArrayList<>();
131+
132+
// Get the tables directly from the GeoPackage file
133+
GeoPackage geoPackage = GeoPackageManager.open(file.toFile());
134+
tables.addAll(geoPackage.getFeatureTables());
135+
geoPackage.close();
131136

132137
if (tables.isEmpty()) {
133138
logger.warn("No tables found in GeoPackage: {}", path);
@@ -152,13 +157,11 @@ public void execute(WorkflowContext context) throws Exception {
152157
}
153158

154159
// Set SRID on geometry column if specified
155-
if (databaseSrid != null) {
156-
try (Connection pgConnection = dataSource.getConnection();
157-
Statement stmt = pgConnection.createStatement()) {
158-
stmt.execute(String.format(
159-
"SELECT UpdateGeometrySRID('%s', 'geom', %d)",
160-
sanitizedTableName, databaseSrid));
161-
}
160+
try (Connection pgConnection = dataSource.getConnection();
161+
Statement stmt = pgConnection.createStatement()) {
162+
stmt.execute(String.format(
163+
"SELECT UpdateGeometrySRID('%s', 'geom', %d)",
164+
sanitizedTableName, databaseSrid));
162165
}
163166

164167
// Verify that the table was created in PostgreSQL
@@ -196,24 +199,6 @@ public void execute(WorkflowContext context) throws Exception {
196199
logger.info("Successfully imported GeoPackage to database");
197200
}
198201

199-
/**
200-
* Gets the list of tables in the GeoPackage.
201-
*
202-
* @param geoPackageSchema the GeoPackage schema
203-
* @return the list of table names
204-
* @throws Exception if an error occurs
205-
*/
206-
private List<String> getGeoPackageTables(GeoPackageSchema geoPackageSchema) throws Exception {
207-
List<String> tables = new ArrayList<>();
208-
209-
// Get the tables directly from the GeoPackage file
210-
GeoPackage geoPackage = GeoPackageManager.open(file.toFile());
211-
tables.addAll(geoPackage.getFeatureTables());
212-
geoPackage.close();
213-
214-
return tables;
215-
}
216-
217202
/**
218203
* Sanitizes a table name to prevent SQL injection.
219204
*

baremaps-core/src/main/java/org/apache/baremaps/tasks/ImportGeoParquet.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import org.apache.baremaps.workflow.Task;
2929
import org.apache.baremaps.workflow.WorkflowContext;
3030
import org.apache.baremaps.workflow.WorkflowException;
31-
import org.apache.calcite.jdbc.CalciteConnection;
32-
import org.apache.calcite.schema.SchemaPlus;
3331
import org.slf4j.Logger;
3432
import org.slf4j.LoggerFactory;
3533

@@ -108,8 +106,6 @@ public void execute(WorkflowContext context) throws Exception {
108106

109107
// Create a connection to Calcite
110108
try (Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) {
111-
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
112-
SchemaPlus rootSchema = calciteConnection.getRootSchema();
113109

114110
// Get the list of tables in the GeoParquet
115111
String[] tables = getGeoParquetTables(connection);
@@ -137,13 +133,11 @@ public void execute(WorkflowContext context) throws Exception {
137133
}
138134

139135
// Set SRID on geometry column if specified
140-
if (databaseSrid != null) {
141-
try (Connection pgConnection = dataSource.getConnection();
142-
Statement stmt = pgConnection.createStatement()) {
143-
stmt.execute(String.format(
144-
"SELECT UpdateGeometrySRID('%s', 'geometry', %d)",
145-
sanitizedTableName, databaseSrid));
146-
}
136+
try (Connection pgConnection = dataSource.getConnection();
137+
Statement stmt = pgConnection.createStatement()) {
138+
stmt.execute(String.format(
139+
"SELECT UpdateGeometrySRID('%s', 'geometry', %d)",
140+
sanitizedTableName, databaseSrid));
147141
}
148142

149143
// Verify that the table was created in PostgreSQL

0 commit comments

Comments
 (0)