Skip to content

Attaching a Postgres database breaks spatial functions taking a BOX_2D computed from a column #673

Backlog
duckdb/duckdb
#29565

Description

@LeDuc34

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 (SELECT 1.0::DOUBLE AS x);            -- works

ATTACH '' AS pg (TYPE postgres, SECRET pg, READ_ONLY);   -- any Postgres database

SELECT ST_Hilbert(x, x, {'min_x': -180.0, 'min_y': -90.0, 'max_x': 180.0, 'max_y': 90.0}::BOX_2D)
  FROM (SELECT 1.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).

Versions (linux_amd64)

Reproduced with:

  • DuckDB 2.0.1, spatial 37bff11aea, postgres_scanner 8b9d0bbe8d
  • DuckDB 2.0.2.dev2611230823, same extensions
  • DuckDB 2.0.1 with nightly extensions: spatial 6a4dac1c15, postgres_scanner be6a912f2e

Server: PostgreSQL 17.5 (Debian). PostGIS 3.5.2 is available on the server but not installed in the attached database (only plpgsql).

Also reproduced from Python (duckdb package) and Node (@duckdb/node-api), and through dbt-duckdb.

Activity

  1. LeDuc34 commented on Nov 29, 2026

    @LeDuc34
    Author

    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.

  2. LeDuc34 commented on Nov 29, 2026

    @LeDuc34
    Author

    PR with the fix and regression tests: duckdb/duckdb#29565

  3. staticlibs commented on Nov 29, 2026

    @staticlibs
    Member

    Hi, thanks for reporting the issue and for filing the PR - I confirm these findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions