Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/pgduckdb/pgduckdb_ruleutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bool pgduckdb_is_duckdb_row(Oid type_oid);
bool pgduckdb_is_unresolved_type(Oid type_oid);
bool pgduckdb_is_fake_type(Oid type_oid);
bool pgduckdb_var_is_duckdb_row(Var *var);
void pgduckdb_get_param(StringInfo buf, Param *param);
bool pgduckdb_func_returns_duckdb_row(RangeTblFunction *rtfunc);
Var *pgduckdb_duckdb_subscript_var(Expr *expr);
bool pgduckdb_reconstruct_star_step(StarReconstructionContext *ctx, ListCell *tle_cell);
Expand Down
19 changes: 19 additions & 0 deletions src/pgduckdb_ruleutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ pgduckdb_is_fake_type(Oid type_oid) {
return false;
}

/*
* Deparse an external parameter as CAST($n AS <type>) instead of a bare $n.
* DuckDB can't always infer a parameter's type (e.g. inside "$1 + 1"), leaving
* it UNKNOWN and unconvertible to a Postgres type (issue #480); the cast hands
* it the type Postgres already resolved. Fake types are skipped (see above).
*/
void
pgduckdb_get_param(StringInfo buf, Param *param) {
Oid paramtype = param->paramtype;

if (param->paramkind == PARAM_EXTERN && OidIsValid(paramtype) && paramtype != UNKNOWNOID &&
!pgduckdb_is_fake_type(paramtype)) {
appendStringInfo(buf, "CAST($%d AS %s)", param->paramid,
format_type_with_typemod(paramtype, param->paramtypmod));
} else {
appendStringInfo(buf, "$%d", param->paramid);
}
}

static bool
pgduckdb_is_duckdb_subscript_type(Oid type_oid) {
if (pgduckdb_is_unresolved_type(type_oid)) {
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/pg_ruleutils_14.c
Original file line number Diff line number Diff line change
Expand Up @@ -8175,7 +8175,7 @@ get_parameter(Param *param, deparse_context *context)
/*
* Not PARAM_EXEC, or couldn't find referent: just print $N.
*/
appendStringInfo(context->buf, "$%d", param->paramid);
pgduckdb_get_param(context->buf, param);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/pg_ruleutils_15.c
Original file line number Diff line number Diff line change
Expand Up @@ -8378,7 +8378,7 @@ get_parameter(Param *param, deparse_context *context)
/*
* Not PARAM_EXEC, or couldn't find referent: just print $N.
*/
appendStringInfo(context->buf, "$%d", param->paramid);
pgduckdb_get_param(context->buf, param);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/pg_ruleutils_16.c
Original file line number Diff line number Diff line change
Expand Up @@ -8313,7 +8313,7 @@ get_parameter(Param *param, deparse_context *context)
/*
* Not PARAM_EXEC, or couldn't find referent: just print $N.
*/
appendStringInfo(context->buf, "$%d", param->paramid);
pgduckdb_get_param(context->buf, param);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/pg_ruleutils_17.c
Original file line number Diff line number Diff line change
Expand Up @@ -8515,7 +8515,7 @@ get_parameter(Param *param, deparse_context *context)
*/
Assert(param->paramkind == PARAM_EXTERN);

appendStringInfo(context->buf, "$%d", param->paramid);
pgduckdb_get_param(context->buf, param);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/pg_ruleutils_18.c
Original file line number Diff line number Diff line change
Expand Up @@ -8863,7 +8863,7 @@ get_parameter(Param *param, deparse_context *context)
*/
Assert(param->paramkind == PARAM_EXTERN);

appendStringInfo(context->buf, "$%d", param->paramid);
pgduckdb_get_param(context->buf, param);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/pg_ruleutils_19.c
Original file line number Diff line number Diff line change
Expand Up @@ -9438,7 +9438,7 @@ get_parameter(Param *param, deparse_context *context)
*/
Assert(param->paramkind == PARAM_EXTERN);

appendStringInfo(context->buf, "$%d", param->paramid);
pgduckdb_get_param(context->buf, param);
}

/*
Expand Down
60 changes: 59 additions & 1 deletion test/pycheck/prepared_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,64 @@ def test_prepared(cur: Cursor):
assert cur.sql(q2, (4,)) == 0


def test_prepared_param_in_expression(cur: Cursor):
# Regression test for issue #480: a parameter used inside an expression
# (rather than a direct comparison) cannot have its type inferred by DuckDB.
# We deparse it with an explicit cast so DuckDB knows the type instead of
# reporting it as UNKNOWN.
cur.sql("CREATE TEMP TABLE test_table (id int) USING duckdb")
cur.sql("INSERT INTO test_table VALUES (1), (2), (3)")

q = "SELECT count(*) FROM test_table WHERE id = %s + 1"

cur.sql("SET plan_cache_mode = 'force_custom_plan'")
assert cur.sql(q, (1,), prepare=True) == 1 # matches id = 2
assert cur.sql(q, (2,)) == 1 # matches id = 3
assert cur.sql(q, (3,)) == 0 # id = 4 does not exist

cur.sql("SET plan_cache_mode = 'force_generic_plan'")
assert cur.sql(q, (1,)) == 1 # creates generic plan
assert cur.sql(q, (2,)) == 1
assert cur.sql(q, (3,)) == 0


def test_prepared_param_cast_types(cur: Cursor):
# Regression test for issue #480: every external parameter now deparses as
# CAST($n AS <pgtype>), so the emitted type name must be one DuckDB can parse.
# This covers more types than int, including the multi-word "timestamp with
# time zone".
cur.sql(
"CREATE TEMP TABLE test_table (t text, ts timestamptz, u uuid) USING duckdb"
)
u = uuid.UUID("12345678-1234-5678-1234-567812345678")
cur.sql(
"INSERT INTO test_table VALUES (%s, %s, %s)",
("foo", datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc), u),
)

for mode in ("force_custom_plan", "force_generic_plan"):
cur.sql(f"SET plan_cache_mode = '{mode}'")
# text: parameter concatenated
assert (
cur.sql(
"SELECT count(*) FROM test_table WHERE t = %s || 'o'",
("fo",),
prepare=True,
)
== 1
)
# timestamptz: parameter used in interval arithmetic
assert (
cur.sql(
"SELECT count(*) FROM test_table WHERE ts = %s + interval '1 day'",
(datetime.datetime(2019, 12, 31, tzinfo=datetime.timezone.utc),),
)
== 1
)
# uuid: cast is emitted even for a plain equality
assert cur.sql("SELECT count(*) FROM test_table WHERE u = %s", (u,)) == 1


def test_extended(cur: Cursor):
cur.sql("""
CREATE TABLE t(
Expand Down Expand Up @@ -161,7 +219,7 @@ def test_prepared_ctas(cur: Cursor):
# crash.
with pytest.raises(
psycopg.errors.InternalError,
match="Could not find parameter with identifier 1",
match="Not all parameters were bound",
):
cur.sql(
"CREATE TEMP TABLE t2 USING duckdb AS SELECT * FROM heapt where id = %s",
Expand Down
Loading