duckdb / duckdb/duckdb-spatial

Cannot cast LINESTRING_3D from Parquet to GEOMETRY

Open
#735 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
708
Forks
96
Avg merge
1d 21h
Merged PRs (30d)
5

Description

Hello !
I encountered an error when casting LINESTRING_3D to GEOMETRY after reading from a Parquet file.
When I declare a geometry using the GeoArrow style, everything works fine:

```
SELECT [{'x': -122, 'y': 47, 'z': 18},
{'x': -123, 'y': 46, 'z': 17},
{'x': -124, 'y': 45, 'z': 17}]::LINESTRING_3D::GEOMETRY AS geometry
```

However, when I store this geometry as LINESTRING_3D in a Parquet file :
```
COPY(
SELECT [{'x': -122, 'y': 47, 'z': 18},
{'x': -123, 'y': 46, 'z': 17},
{'x': -124, 'y': 45, 'z': 17}]::LINESTRING_3D as geometry
) TO 'test_linestring_3d_float.parquet' (FORMAT 'parquet')
```
And then try to read and cast it to GEOMETRY, I get the following error:

```
SELECT geometry::LINESTRING_3D::GEOMETRY AS geom
FROM read_parquet('test_linestring_3d.parquet')
```

Error message:
_Conversion Error: Unimplemented type for cast (STRUCT(x DOUBLE, y DOUBLE, z DOUBLE)[] -> GEOMETRY)
when casting from source column geometry (Line Number: 1)_

Casting works if I first cast to LINESTRING_2D instead of LINESTRING_3D.

**Additional Observation**
I was able to cast the LINESTRING_3D from Parquet to GEOMETRY by first casting it to STRUCT(x FLOAT, y FLOAT, z FLOAT)[], then to LINESTRING_3D, and finally to GEOMETRY.
Here’s the query that works:
```
SELECT geometry::STRUCT(x FLOAT, y FLOAT, z FLOAT)[]::LINESTRING_3D::GEOMETRY AS geom
FROM read_parquet('test_linestring_3d_float.parquet')
```
This raises two questions:

- Why is it necessary to go through a STRUCT of FLOAT instead of DOUBLE?
- This workaround introduces an extra cast and, if the file is stored as FLOAT, it becomes unreadable by GDAL and QGIS.

It reminds me of a related issue: (https://github.com/duckdb/duckdb-spatial/issues/197)
DuckDB version: 1.4.1
Thanks

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the reported queries with DuckDB 1.4.1, especially the direct cast from read_parquet's STRUCT(x DOUBLE, y DOUBLE, z DOUBLE)[] to LINESTRING_3D and GEOMETRY. Trace the cast path for the Parquet result and compare it with the FLOAT workaround. Done means the direct DOUBLE-based cast works without an intermediate cast and remains compatible with GDAL and QGIS.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.