Skip to content

Commit

Permalink
sql: deserialize sequence name for SHOW CREATE FUNCTION
Browse files Browse the repository at this point in the history
Previously, `SHOW CREATE FUNCTION` returns a function body
with sequence OIDs. This commit deserializes sequence OID
into qualified names.

Release note: None.
Release justification: low risk enhancement.
  • Loading branch information
chengxiong-ruan committed Sep 7, 2022
1 parent 4d224ab commit 0d5f6e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
6 changes: 5 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,11 @@ CREATE TABLE crdb_internal.create_function_statements (
}
for i := range treeNode.Options {
if body, ok := treeNode.Options[i].(tree.FunctionBodyStr); ok {
stmtStrs := strings.Split(string(body), "\n")
seqReplacedBody, err := formatQuerySequencesForDisplay(ctx, &p.semaCtx, string(body), true /* multiStmt */)
if err != nil {
return err
}
stmtStrs := strings.Split(seqReplacedBody, "\n")
for i := range stmtStrs {
stmtStrs[i] = "\t" + stmtStrs[i]
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/udf
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ CREATE FUNCTION public.f(IN a test.public.notmyworkday)
SELECT a FROM test.public.t;
SELECT b FROM test.public.t@t_idx_b;
SELECT c FROM test.public.t@t_idx_c;
SELECT nextval(114:::REGCLASS);
SELECT nextval('public.sq1'::REGCLASS);
$$

statement error pq: unimplemented: alter function depends on extension not supported.*
Expand Down
45 changes: 34 additions & 11 deletions pkg/sql/show_create_clauses.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func formatViewQueryForDisplay(
}

// Convert sequences referenced by ID in the view back to their names.
sequenceReplacedViewQuery, err := formatViewQuerySequencesForDisplay(ctx, semaCtx, typeReplacedViewQuery)
sequenceReplacedViewQuery, err := formatQuerySequencesForDisplay(ctx, semaCtx, typeReplacedViewQuery, false /* multiStmt */)
if err != nil {
log.Warningf(ctx, "error converting sequence IDs to names for view %s (%v): %+v",
desc.GetName(), desc.GetID(), err)
Expand All @@ -177,11 +177,11 @@ func formatViewQueryForDisplay(
return sequenceReplacedViewQuery
}

// formatViewQuerySequencesForDisplay walks the view query and
// formatQuerySequencesForDisplay walks the view query and
// looks for sequence IDs in the statement. If it finds any,
// it will replace the IDs with the descriptor's fully qualified name.
func formatViewQuerySequencesForDisplay(
ctx context.Context, semaCtx *tree.SemaContext, viewQuery string,
func formatQuerySequencesForDisplay(
ctx context.Context, semaCtx *tree.SemaContext, queries string, multiStmt bool,
) (string, error) {
replaceFunc := func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
newExpr, err = schemaexpr.ReplaceIDsWithFQNames(ctx, expr, semaCtx)
Expand All @@ -191,16 +191,39 @@ func formatViewQuerySequencesForDisplay(
return false, newExpr, nil
}

stmt, err := parser.ParseOne(viewQuery)
if err != nil {
return "", err
var stmts tree.Statements
if multiStmt {
parsedStmts, err := parser.Parse(queries)
if err != nil {
return "", err
}
stmts = make(tree.Statements, len(parsedStmts))
for i, stmt := range parsedStmts {
stmts[i] = stmt.AST
}
} else {
stmt, err := parser.ParseOne(queries)
if err != nil {
return "", err
}
stmts = tree.Statements{stmt.AST}
}

newStmt, err := tree.SimpleStmtVisit(stmt.AST, replaceFunc)
if err != nil {
return "", err
fmtCtx := tree.NewFmtCtx(tree.FmtSimple)
for i, stmt := range stmts {
newStmt, err := tree.SimpleStmtVisit(stmt, replaceFunc)
if err != nil {
return "", err
}
if i > 0 {
fmtCtx.WriteString("\n")
}
fmtCtx.FormatNode(newStmt)
if multiStmt {
fmtCtx.WriteString(";")
}
}
return newStmt.String(), nil
return fmtCtx.CloseAndGetString(), nil
}

// formatViewQueryTypesForDisplay walks the view query and
Expand Down

0 comments on commit 0d5f6e4

Please sign in to comment.