Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree: handle format of START/BEGIN inside of functions #109618

Merged
merged 1 commit into from
Aug 29, 2023
Merged
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
7 changes: 6 additions & 1 deletion pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ func (u *sqlSymUnion) showTenantOpts() tree.ShowTenantOptions {
func (u *sqlSymUnion) showCreateFormatOption() tree.ShowCreateFormatOption {
return u.val.(tree.ShowCreateFormatOption)
}
func (u *sqlSymUnion) beginTransaction() *tree.BeginTransaction {
return u.val.(*tree.BeginTransaction)
}
%}

// NB: the %token definitions must come before the %type definitions in this
Expand Down Expand Up @@ -11679,7 +11682,9 @@ transaction_stmt:
begin_stmt:
START TRANSACTION begin_transaction
{
$$.val = $3.stmt()
s := $3.beginTransaction()
s.FormatWithStart = true
$$.val = s
}
| START error // SHOW HELP: BEGIN

Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/parser/testdata/begin_commit
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ BEGIN TRANSACTION -- identifiers removed
parse
START TRANSACTION
----
BEGIN TRANSACTION -- normalized!
BEGIN TRANSACTION -- fully parenthesized
BEGIN TRANSACTION -- literals removed
BEGIN TRANSACTION -- identifiers removed
START TRANSACTION
START TRANSACTION -- fully parenthesized
START TRANSACTION -- literals removed
START TRANSACTION -- identifiers removed

parse
BEGIN TRANSACTION READ ONLY
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/parser/testdata/create_procedure
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,15 @@ CREATE PROCEDURE _()
CALLED ON NULL INPUT
LANGUAGE SQL
AS $$_$$ -- identifiers removed

parse
CREATE PROCEDURE FAMILY () BEGIN ATOMIC START TRANSACTION; COMMIT; END;
----
CREATE PROCEDURE "family"()
BEGIN ATOMIC START TRANSACTION; COMMIT TRANSACTION; END -- normalized!
CREATE PROCEDURE "family"()
BEGIN ATOMIC START TRANSACTION; COMMIT TRANSACTION; END -- fully parenthesized
CREATE PROCEDURE "family"()
BEGIN ATOMIC START TRANSACTION; COMMIT TRANSACTION; END -- literals removed
CREATE PROCEDURE _()
BEGIN ATOMIC START TRANSACTION; COMMIT TRANSACTION; END -- identifiers removed
12 changes: 10 additions & 2 deletions pkg/sql/sem/tree/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,20 @@ func (node *TransactionModes) Merge(other TransactionModes) error {

// BeginTransaction represents a BEGIN statement
type BeginTransaction struct {
Modes TransactionModes
// FormatWithStart says whether this statement must be formatted with
// "START" rather than "BEGIN". This is needed if this statement is in a
// BEGIN ATOMIC block of a procedure or function.
FormatWithStart bool
Modes TransactionModes
}

// Format implements the NodeFormatter interface.
func (node *BeginTransaction) Format(ctx *FmtCtx) {
ctx.WriteString("BEGIN TRANSACTION")
if node.FormatWithStart {
ctx.WriteString("START TRANSACTION")
} else {
ctx.WriteString("BEGIN TRANSACTION")
}
ctx.FormatNode(&node.Modes)
}

Expand Down