Skip to content

Commit b0fce07

Browse files
committed
Fix conversions, enumerator and table
1 parent 348dbbf commit b0fce07

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/geoparquet/GeoParquetTable.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,18 @@ private static class GeoParquetEnumerator implements Enumerator<Object[]> {
101101
private GeoParquetGroup currentRow;
102102
private boolean hasNext;
103103
private final Path path;
104+
private java.util.Iterator<GeoParquetGroup> iterator;
104105

105106
public GeoParquetEnumerator(GeoParquetReader reader, GeoParquetSchema schema) {
106107
this.reader = reader;
107108
this.schema = schema;
108109
this.path = new Path(reader.getGeoParquetSchema().name());
110+
try {
111+
// Initialize the iterator once from the stream
112+
this.iterator = reader.read().iterator();
113+
} catch (Exception e) {
114+
throw new RuntimeException("Failed to initialize GeoParquet reader", e);
115+
}
109116
this.hasNext = true;
110117
moveNext();
111118
}
@@ -124,12 +131,14 @@ public boolean moveNext() {
124131
if (!hasNext) {
125132
return false;
126133
}
127-
try {
128-
currentRow = reader.read().findFirst().orElse(null);
129-
hasNext = currentRow != null;
130-
return hasNext;
131-
} catch (Exception e) {
132-
throw new RuntimeException("Failed to read GeoParquet row", e);
134+
135+
if (iterator.hasNext()) {
136+
currentRow = iterator.next();
137+
return true;
138+
} else {
139+
currentRow = null;
140+
hasNext = false;
141+
return false;
133142
}
134143
}
135144

@@ -138,6 +147,7 @@ public void reset() {
138147
try {
139148
reader.close();
140149
GeoParquetReader newReader = new GeoParquetReader(this.path);
150+
this.iterator = newReader.read().iterator();
141151
currentRow = null;
142152
hasNext = true;
143153
moveNext();

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/geoparquet/GeoParquetTypeConversion.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -244,35 +244,13 @@ public static List<Object> asPostgresRowValues(GeoParquetGroup group) {
244244

245245
for (int i = 0; i < schema.fields().size(); i++) {
246246
Field field = schema.fields().get(i);
247+
// Use convertValue to get values converted to Calcite types
247248
Object value = convertValue(field, group, i);
248249

249-
// Convert record types to JSON strings
250-
if (field.type() == Type.GROUP || field.type() == Type.ENVELOPE) {
250+
// Only handle GROUP types for PostgreSQL - other types pass through Calcite
251+
if (field.type() == Type.GROUP) {
251252
try {
252-
if (field.type() == Type.GROUP) {
253-
value = MAPPER.writeValueAsString(value);
254-
} else if (field.type() == Type.ENVELOPE) {
255-
// Convert envelope to a map with minx, miny, maxx, maxy
256-
Map<String, Object> envelopeMap = new HashMap<>();
257-
if (value instanceof org.locationtech.jts.geom.Envelope) {
258-
org.locationtech.jts.geom.Envelope envelope =
259-
(org.locationtech.jts.geom.Envelope) value;
260-
envelopeMap.put("minx", envelope.getMinX());
261-
envelopeMap.put("miny", envelope.getMinY());
262-
envelopeMap.put("maxx", envelope.getMaxX());
263-
envelopeMap.put("maxy", envelope.getMaxY());
264-
} else if (value instanceof Object[]) {
265-
Object[] envelope = (Object[]) value;
266-
envelopeMap.put("minx", envelope[0]);
267-
envelopeMap.put("miny", envelope[1]);
268-
envelopeMap.put("maxx", envelope[2]);
269-
envelopeMap.put("maxy", envelope[3]);
270-
} else {
271-
throw new IllegalArgumentException(
272-
"Unexpected envelope type: " + value.getClass().getName());
273-
}
274-
value = MAPPER.writeValueAsString(envelopeMap);
275-
}
253+
value = MAPPER.writeValueAsString(value);
276254
} catch (Exception e) {
277255
throw new RuntimeException("Error converting record type to JSON", e);
278256
}

0 commit comments

Comments
 (0)