Skip to content

Fix UNKNOWN parameter type error for params used in expressions - #1

Closed
GavinPizza wants to merge 1 commit into
mainfrom
fix/480-prepared-param-cast
Closed

Fix UNKNOWN parameter type error for params used in expressions#1
GavinPizza wants to merge 1 commit into
mainfrom
fix/480-prepared-param-cast

Conversation

@GavinPizza

Copy link
Copy Markdown
Owner

CI-only PR on the fork to validate the branch before opening the upstream PR against duckdb/pg_duckdb.


A prepared or parameterized statement fails when a bind parameter is used inside an expression, e.g. SELECT count(*) FROM t WHERE id = $1 + 1:

ERROR:   (CreatePlan) Cache lookup failed for type 0
WARNING: (GetPostgresDuckDBType) Could not convert DuckDB type: UNKNOWN to Postgres type

pg_duckdb deparses the query and calls DuckDB Prepare() at plan time to learn the result types. DuckDB can't infer a parameter's type inside an expression, so it stays UNKNOWN and result-type resolution fails (same root cause as duckdb#396). Since Postgres already resolved the parameter's type, we now deparse each external parameter as CAST($n AS <pgtype>) instead of a bare $n, handing DuckDB the type it needs. A regression test is included.

This also changes the error for CTAS-with-a-parameter (still unsupported): the parameter now gets a type, so DuckDB fails later with "Not all parameters were bound" instead of "Could not find parameter with identifier 1" -- same clean failure, different message.

Known limitation: jsonb parameters still fail (CAST($n AS jsonb) -- DuckDB has no jsonb type); pre-existing, tracked in duckdb#516.

Fixes duckdb#480.

Parameters used inside an expression (e.g. `WHERE id = $1 + 1`) could not
have their type inferred by DuckDB at plan time, failing with "Could not
convert DuckDB type: UNKNOWN". Deparse external parameters as
CAST($n AS <type>) so DuckDB gets the type Postgres already resolved.

Fixes duckdb#480.
@GavinPizza GavinPizza closed this Jul 13, 2026
@GavinPizza GavinPizza reopened this Jul 13, 2026
@GavinPizza

Copy link
Copy Markdown
Owner Author

CI validation done (all 8 checks green). Superseded by the upstream PR: duckdb#1062

@GavinPizza GavinPizza closed this Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prepared statement error: Could not convert DuckDB type: UNKNOWN to Postgres type

1 participant