cockroachdb / cockroachdb/cockroach

sql: invalid cast of prepared statement argument does not match Postgres

Open
#104,113 0 comments 0 reactions 0 assignees View on GitHub
A-prepared-stmts A-sql-typing C-bug T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Consider the empty table:

```sql
CREATE TABLE t (
b BOOL,
i INT
);
```

If we query the table with an invalid cast from a string to integer expression, we get an error:

```
SELECT * FROM t WHERE b AND i = 'a'::INT;
ERROR: could not parse "a" as type int: strconv.ParseInt: parsing "a": invalid syntax
```

The same error occurs in `EXPLAIN (OPT)`, suggesting that the error originates from the optimizer:

```
EXPLAIN (OPT) SELECT * FROM t WHERE b AND i = 'a'::INT;
ERROR: could not parse "a" as type int: strconv.ParseInt: parsing "a": invalid syntax
```

This is all expected behavior and it matches Postgres. However, if we perform the same invalid cast in a prepared statement, the query does not error:

```
PREPARE p(TEXT) AS SELECT * FROM t WHERE b AND i = $1::INT;

EXECUTE p('a');
b | i
----+----
(0 rows)
```

In Postgres, the `EXECUTE` statement fails with the error:

```
psql:cast.sql:13: ERROR: 22P02: invalid input syntax for type integer: "a"
```

We should match Postgres's behavior.

Jira issue: CRDB-28357

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.