cockroachdb / cockroachdb/cockroach

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

Open
#114,676 1 comment 0 reactions 0 assignees View on GitHub
A-sql-plpgsql C-bug docs-done docs-known-limitation O-qa P-3 T-sql-foundations X-anchored-telemetry
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Postgres allows PLpgSQL variable declarations to inherit the type of a table row or column using special syntax ([docs](https://www.postgresql.org/docs/current/plpgsql-declarations.html#PLPGSQL-DECLARATION-TYPE)):
```
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

Epic CRDB-49018

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.