cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: mismatched column-definition list causes an internal error

Open DrewKimball opened this issue 1 year ago • 0 comments

When a record-returning UDF is used as a data source, it must be given a column-definition list, which names the columns returned by the UDF and determines their types. When the type of the column-definition list doesn't match the actual type returned by the function, postgres attempts an assignment cast:

postgres=# create function f() returns record language sql as $$ select 1.99; $$;
CREATE FUNCTION
postgres=# select * from f() AS foo(x int);
 x
---
 2
(1 row)

postgres=# select * from f() AS foo(x timestamp);
ERROR:  return type mismatch in function declared to return record

Currently, CRDB doesn't handle this case at all; instead, it returns an internal error during execution due to an unexpected type:

root@localhost:26257/system/defaultdb> select * from f() AS foo(x int);
ERROR: internal error: invalid datum type given: DECIMAL, expected INT8
SQLSTATE: XX000
DETAIL: stack trace:
github.com/cockroachdb/cockroach/pkg/sql/rowenc/encoded_datum.go:195: DatumToEncDatum()
github.com/cockroachdb/cockroach/pkg/sql/rowexec/project_set.go:334: toEncDatum()
github.com/cockroachdb/cockroach/pkg/sql/rowexec/project_set.go:241: nextGeneratorValues()
github.com/cockroachdb/cockroach/pkg/sql/rowexec/project_set.go:313: Next()
github.com/cockroachdb/cockroach/pkg/sql/colexec/columnarizer.go:239: Next()
github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:118: next()

Jira issue: CRDB-32791

DrewKimball avatar Oct 27 '23 08:10 DrewKimball