cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
29.95k stars 3.79k forks source link

plpgsql: support syntax to use column/table type for variable #114676

Open DrewKimball opened 10 months ago

DrewKimball commented 10 months ago

Postgres allows PLpgSQL variable declarations to inherit the type of a table row or column using special syntax (docs):

name table_name%ROWTYPE; -- Row type
name var/col%TYPE -- Type of column or variable

This issue tracks adding support for this syntax.

CRDB does not yet support this syntax, but instead of returning an "unsupported" error, we currently return an internal error during parsing:

root@localhost:26257/system/defaultdb> CREATE TABLE xy (x INT, y INT);
CREATE TABLE

Time: 41ms total (execution 40ms / network 0ms)

root@localhost:26257/system/defaultdb> CREATE PROCEDURE foo() LANGUAGE PLpgSQL AS $$ DECLARE a xy%ROWTYPE; BEGIN END $$;
ERROR: internal error: at or near "rowtype": syntax error: expected a tree.CastExpr, but found *tree.BinaryExpr
SQLSTATE: XX000
DETAIL: source SQL:
DECLARE a xy%ROWTYPE; BEGIN END
             ^stack trace:
github.com/cockroachdb/cockroach/pkg/sql/parser/parse.go:414: GetTypeFromCastOrCollate()
github.com/cockroachdb/cockroach/pkg/sql/parser/parse.go:398: GetTypeFromValidSQLSyntax()
github.com/cockroachdb/cockroach/pkg/sql/plpgsql/parser/lexer.go:477: GetTypeFromValidSQLSyntax()
plpgsql-gen.y:554: Parse()
github.com/cockroachdb/cockroach/pkg/sql/plpgsql/parser/parse.go:101: parse()
github.com/cockroachdb/cockroach/pkg/sql/plpgsql/parser/parse.go:85: parseWithDepth()
github.com/cockroachdb/cockroach/pkg/sql/plpgsql/parser/parse.go:135: Parse()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/create_function.go:245: buildCreateFunction()

Jira issue: CRDB-33620

yuzefovich commented 5 months ago

We saw this get triggered at least once in the wild #121876.