DuckDB: spatial join on a GEOMETRY column of an Arrow scan fails (EXPRESSION_FILTER pushdown) — draft for upstream

未关闭
#278 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
28/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
backend, databases

调研方向

The report identifies DuckDB’s Python Arrow scan and SPATIAL_JOIN filter pushdown as the failure path, but this is a draft for upstream DuckDB rather than a change scoped to this repository. Start by checking the reported reproduction against DuckDB 1.5.5 with PyArrow 23.0.0; done means unsupported filters are skipped and applied after the scan, allowing the join to return 4. The issue also raises a separate CRS documentation mismatch without specifying a file to change.

由索引模型根据 Issue 内容生成。

描述

A draft for DuckDB upstream, filed here for review first.

Summary

A spatial join in DuckDB fails when one side is an Arrow scan whose geometry column is a native GEOMETRY, meaning a GeoArrow WKB column (ARROW:extension:name = geoarrow.wkb). The failure:

NotImplementedException: Not implemented Error: Pushdown Filter Type EXPRESSION_FILTER is not currently supported in PyArrow Scans

The SPATIAL_JOIN operator pushes a filter on the geometry column into the Arrow scan, which the Python Arrow scan can't translate. DuckDB raises an error instead of skipping that pushdown and filtering after the scan.

This breaks the most common geospatial query on xarray-sql tables: xql.register(con, name, ds, geometry=(x, y)) produces exactly such a column, and joining it to a table of polygons with ST_Within fails. A constant polygon works; a polygon from a table, a join, or a scalar subquery fails.

Repro, without xarray-sql (DuckDB 1.5.5, pyarrow 23.0.0)

import duckdb, struct, json, pyarrow as pa, pyarrow.dataset as pads

con = duckdb.connect()
con.execute("INSTALL spatial; LOAD spatial")
xy = [(x, y) for y in (0.0, 1.0, 2.0) for x in (0.0, 1.0, 2.0)]
wkb = pa.array([struct.pack("<BIdd", 1, 1, x, y) for x, y in xy], pa.binary())
geometry = pa.field("geometry", pa.binary(), metadata={
    "ARROW:extension:name": "geoarrow.wkb",
    "ARROW:extension:metadata": json.dumps({"crs": "OGC:CRS84"}),
})
points = pa.Table.from_arrays([wkb], schema=pa.schema([geometry]))
con.execute("""CREATE TABLE p AS SELECT ST_SetCRS(ST_GeomFromText(
    'POLYGON((-0.5 -0.5, 1.5 -0.5, 1.5 1.5, -0.5 1.5, -0.5 -0.5))'), 'OGC:CRS84') AS geom""")

for obj in (points, pads.dataset(points)):
    con.register("g", obj)
    con.sql("SELECT count(*) FROM g JOIN p ON ST_Within(g.geometry, p.geom)").fetchone()
    # NotImplementedException: ... EXPRESSION_FILTER ... PyArrow Scans

Results by query shape, on an xarray-sql table registered with geometry=("longitude", "latitude"):

Query Result
WHERE ST_Within(geometry, ST_GeomFromText('POLYGON ...')) (constant) works
WHERE ST_Within(geometry, (SELECT geom FROM p)) fails
JOIN p ON ST_Within(g.geometry, p.geom) fails
FROM g, p WHERE ST_Within(g.geometry, p.geom) fails
FROM (FROM g LIMIT ...) g JOIN p ON ... fails
JOIN p ON ST_Within(ST_Point(g.longitude, g.latitude), p.geom) works
CREATE TABLE gm AS FROM g, then join gm works

The same join on a plain binary column, ST_Within(ST_GeomFromWKB(g.wkb), p.geom), also works. So the trigger is the native GEOMETRY type on an Arrow scan.

Setting disabled_optimizers to join_filter_pushdown, filter_pushdown or both doesn't change it.

Expected

The Arrow scan should decline filters it can't translate, as it does for other expressions, and DuckDB should apply them after the scan, so the join returns 4.

Workarounds today

  • Build the point in the join: ST_Point(longitude, latitude). This loses the CRS tag.
  • Copy the table into DuckDB first with CREATE TABLE ... AS.

For xarray-sql

  • Until DuckDB is fixed, the geospatial docs could show the ST_Point form for joins against polygon tables.
  • A related mismatch: xarray-sql tags its points OGC:CRS84, while DuckDB's ST_Read tags GeoJSON (always longitude/latitude on WGS84) EPSG:4326. ST_Within then refuses with "Cannot call function 'ST_Within' with geometries of different coordinate reference systems". Users have to pass geometry_crs="EPSG:4326" or use ST_SetCRS. That deserves a line in the docs, or a separate issue on which default fits DuckDB better.
主要语言
Python
星标
142
派生
19
平均合并
3 天 47 分钟
30 天内合并 PR
8

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

xqlsystems/xarray-sql 的其他 Issue

查看 xqlsystems/xarray-sql 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。