You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Attaching a Postgres database breaks spatial functions taking a BOX_2D computed from a column #673
Attaching a Postgres database makes spatial functions reject a BOX_2D computed from a column: the value reaches the binder as a plain STRUCT(min_x DOUBLE, min_y DOUBLE, max_x DOUBLE, max_y DOUBLE).
Reproduction
LOAD spatial;
LOAD postgres;
SELECT ST_Hilbert(x, x, {'min_x': -180.0, 'min_y': -90.0, 'max_x': 180.0, 'max_y': 90.0}::BOX_2D)
FROM (SELECT1.0::DOUBLE AS x); -- works
ATTACH ''AS pg (TYPE postgres, SECRET pg, READ_ONLY); -- any Postgres databaseSELECT ST_Hilbert(x, x, {'min_x': -180.0, 'min_y': -90.0, 'max_x': 180.0, 'max_y': 90.0}::BOX_2D)
FROM (SELECT1.0::DOUBLE AS x);
-- Binder Error: No function matches the given name and argument types-- 'ST_Hilbert(DOUBLE, DOUBLE, STRUCT(min_x DOUBLE, min_y DOUBLE, max_x DOUBLE, max_y DOUBLE))'.-- Candidate functions:-- ST_Hilbert(x DOUBLE, y DOUBLE, bounds BOX_2D) -> UINTEGER-- ...
The same happens with && between two BOX_2D values, and with a BOX_2D returned by ST_Extent(column) instead of a cast.
What works and what does not
Before the ATTACH: everything works.
After the ATTACH: constant arguments still work (ST_Hilbert(1.0::DOUBLE, 1.0::DOUBLE, …::BOX_2D)); arguments that depend on a column fail as above.
ST_Intersects, ST_Extent_Agg, ST_Area_Spheroid on columns are unaffected.
A plain TYPE postgres attach triggers it, and so does a DuckLake catalog stored in Postgres (ATTACH 'ducklake:postgres:' …). A DuckLake catalog stored in a local file does not.
SET search_path or USE memory after the attach does not help. duckdb_types() lists a single BOX_2D (system.main).
I traced this to DuckDB core rather than this extension.
Cause. As soon as a catalog reporting RemoteCapability::IS_REMOTE is attached, Optimizer::OptimizeStatement runs the RemotePushdownOptimizer on every statement (GetRemoteCatalogCount() > 0), whatever pg_enable_remote_pushdown says. That optimizer constant-folds sub-expressions and turns the folded Value back into a parsed expression with ConstantExpression::FromValue (src/parser/expression/value_expression.cpp). For a STRUCT, that path drops the type alias:
case LogicalTypeId::STRUCT:
return StructExpression(value); // plain struct_pack(...), the "BOX_2D" alias is lost
BOX_2D is STRUCT(min_x, min_y, max_x, max_y).WithAlias("BOX_2D"), so the folded {...}::BOX_2D comes back as an anonymous STRUCT and overloads that expect BOX_2D no longer match. This explains the pattern in the report:
all arguments constant: the whole ST_Hilbert(...) call is folded to a UINTEGER, nothing is re-bound;
one argument depends on a column: the call cannot be folded, only its children are, and the box child loses its alias;
DuckLake with a local file catalog: no remote catalog is attached, the optimizer does not run.
The GEOMETRY, MAP and INTERVAL branches of FromValue already re-apply the alias with type.HasAlias() ? CastTo(type, ...) : ...; the STRUCT branch does not. The non-empty LIST branch looks like it has the same gap (it would affect LINESTRING_2D / POLYGON_2D), but I have not tested that one.
Workaround (verified on DuckDB 2.0.1, spatial 37bff11aea, postgres_scanner 8b9d0bbe8d, from Python):
SET disabled_optimizers ='remote_pushdown';
With it, after the attach, the ST_Hilbert query from the report returns 2147602908 and ST_Extent(ST_Point(x, x)) && {...}::BOX_2D returns true. SET pg_enable_remote_pushdown = false before the attach does not help, since the optimizer still runs.
Proposed fix (in duckdb/duckdb):
case LogicalTypeId::STRUCT: {
auto result = StructExpression(value);
return type.HasAlias() ? CastTo(type, std::move(result)) : std::move(result);
}
I'm preparing a PR against duckdb/duckdb with a regression test that uses a test remote catalog (as in test/api/test_remote_pushdown_macro_arguments.cpp), so it doesn't depend on spatial or Postgres. I'll link it here.
Activity
LeDuc34 commentedon Nov 29, 2026
I traced this to DuckDB core rather than this extension.
Cause. As soon as a catalog reporting
RemoteCapability::IS_REMOTEis attached,Optimizer::OptimizeStatementruns theRemotePushdownOptimizeron every statement (GetRemoteCatalogCount() > 0), whateverpg_enable_remote_pushdownsays. That optimizer constant-folds sub-expressions and turns the foldedValueback into a parsed expression withConstantExpression::FromValue(src/parser/expression/value_expression.cpp). For aSTRUCT, that path drops the type alias:BOX_2DisSTRUCT(min_x, min_y, max_x, max_y).WithAlias("BOX_2D"), so the folded{...}::BOX_2Dcomes back as an anonymousSTRUCTand overloads that expectBOX_2Dno longer match. This explains the pattern in the report:ST_Hilbert(...)call is folded to aUINTEGER, nothing is re-bound;The
GEOMETRY,MAPandINTERVALbranches ofFromValuealready re-apply the alias withtype.HasAlias() ? CastTo(type, ...) : ...; theSTRUCTbranch does not. The non-emptyLISTbranch looks like it has the same gap (it would affectLINESTRING_2D/POLYGON_2D), but I have not tested that one.Workaround (verified on DuckDB 2.0.1, spatial
37bff11aea, postgres_scanner8b9d0bbe8d, from Python):With it, after the attach, the
ST_Hilbertquery from the report returns2147602908andST_Extent(ST_Point(x, x)) && {...}::BOX_2Dreturnstrue.SET pg_enable_remote_pushdown = falsebefore the attach does not help, since the optimizer still runs.Proposed fix (in duckdb/duckdb):
I'm preparing a PR against duckdb/duckdb with a regression test that uses a test remote catalog (as in
test/api/test_remote_pushdown_macro_arguments.cpp), so it doesn't depend on spatial or Postgres. I'll link it here.LeDuc34 commentedon Nov 29, 2026
PR with the fix and regression tests: duckdb/duckdb#29565
staticlibs commentedon Nov 29, 2026
Hi, thanks for reporting the issue and for filing the PR - I confirm these findings.