Skip to content

Commit af7c77c

Browse files
committed
sql, tree: move two formatting helper functions into tree
Move `formatStatementHideConstants` and `formatStatementSummary` from sql to tree so that we can call `tree.FormatStatementHideConstants` from the optbuilder package. Epic: None Release note: None
1 parent 39159bd commit af7c77c

File tree

6 files changed

+36
-36
lines changed

6 files changed

+36
-36
lines changed

pkg/sql/conn_executor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4458,7 +4458,7 @@ func (ex *connExecutor) serialize() serverpb.Session {
44584458
"serialization")
44594459
continue
44604460
}
4461-
sqlNoConstants := truncateSQL(formatStatementHideConstants(parsed.AST))
4461+
sqlNoConstants := truncateSQL(tree.FormatStatementHideConstants(parsed.AST))
44624462
nPlaceholders := 0
44634463
if query.placeholders != nil {
44644464
nPlaceholders = len(query.placeholders.Values)
@@ -4483,7 +4483,7 @@ func (ex *connExecutor) serialize() serverpb.Session {
44834483
ElapsedTime: elapsedTime,
44844484
Sql: sql,
44854485
SqlNoConstants: sqlNoConstants,
4486-
SqlSummary: formatStatementSummary(parsed.AST),
4486+
SqlSummary: tree.FormatStatementSummary(parsed.AST),
44874487
Placeholders: placeholders,
44884488
IsDistributed: query.isDistributed,
44894489
Phase: (serverpb.ActiveQuery_Phase)(query.phase),
@@ -4498,7 +4498,7 @@ func (ex *connExecutor) serialize() serverpb.Session {
44984498
lastActiveQueryNoConstants := ""
44994499
if ex.mu.LastActiveQuery != nil {
45004500
lastActiveQuery = truncateSQL(ex.mu.LastActiveQuery.String())
4501-
lastActiveQueryNoConstants = truncateSQL(formatStatementHideConstants(ex.mu.LastActiveQuery))
4501+
lastActiveQueryNoConstants = truncateSQL(tree.FormatStatementHideConstants(ex.mu.LastActiveQuery))
45024502
}
45034503
status := serverpb.Session_IDLE
45044504
if len(activeQueries) > 0 {

pkg/sql/conn_executor_exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4395,7 +4395,7 @@ func (ex *connExecutor) execWithProfiling(
43954395
if prepared != nil {
43964396
stmtNoConstants = prepared.StatementNoConstants
43974397
} else {
4398-
stmtNoConstants = formatStatementHideConstants(ast)
4398+
stmtNoConstants = tree.FormatStatementHideConstants(ast)
43994399
}
44004400
labels := pprof.Labels(
44014401
"appname", ex.sessionData().ApplicationName,

pkg/sql/exec_util.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3481,35 +3481,6 @@ func HashForReporting(secret, appName string) string {
34813481
return hex.EncodeToString(hash.Sum(nil)[:4])
34823482
}
34833483

3484-
// formatStatementHideConstants formats the statement using
3485-
// tree.FmtHideConstants. It does *not* anonymize the statement, since
3486-
// the result will still contain names and identifiers.
3487-
func formatStatementHideConstants(ast tree.Statement, optFlags ...tree.FmtFlags) string {
3488-
if ast == nil {
3489-
return ""
3490-
}
3491-
fmtFlags := tree.FmtHideConstants
3492-
for _, f := range optFlags {
3493-
fmtFlags |= f
3494-
}
3495-
return tree.AsStringWithFlags(ast, fmtFlags)
3496-
}
3497-
3498-
// formatStatementSummary formats the statement using tree.FmtSummary
3499-
// and tree.FmtHideConstants. This returns a summarized version of the
3500-
// query. It does *not* anonymize the statement, since the result will
3501-
// still contain names and identifiers.
3502-
func formatStatementSummary(ast tree.Statement, optFlags ...tree.FmtFlags) string {
3503-
if ast == nil {
3504-
return ""
3505-
}
3506-
fmtFlags := tree.FmtSummary | tree.FmtHideConstants
3507-
for _, f := range optFlags {
3508-
fmtFlags |= f
3509-
}
3510-
return tree.AsStringWithFlags(ast, fmtFlags)
3511-
}
3512-
35133484
// DescsTxn is a convenient method for running a transaction on descriptors
35143485
// when you have an ExecutorConfig.
35153486
//

pkg/sql/executor_statement_metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func makeTestQuery(i int) (*Statement, error) {
138138
}
139139

140140
return &Statement{
141-
StmtNoConstants: formatStatementHideConstants(stmt.AST, tree.FmtCollapseLists|tree.FmtConstantsAsUnderscores),
141+
StmtNoConstants: tree.FormatStatementHideConstants(stmt.AST, tree.FmtCollapseLists|tree.FmtConstantsAsUnderscores),
142142
Statement: stmt,
143143
}, nil
144144
}

pkg/sql/sem/tree/format.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,35 @@ func SerializeForDisplay(n NodeFormatter) string {
874874
return AsStringWithFlags(n, FmtParsable)
875875
}
876876

877+
// FormatStatementHideConstants formats the statement using FmtHideConstants. It
878+
// does *not* anonymize the statement, since the result will still contain names
879+
// and identifiers.
880+
func FormatStatementHideConstants(ast Statement, optFlags ...FmtFlags) string {
881+
if ast == nil {
882+
return ""
883+
}
884+
fmtFlags := FmtHideConstants
885+
for _, f := range optFlags {
886+
fmtFlags |= f
887+
}
888+
return AsStringWithFlags(ast, fmtFlags)
889+
}
890+
891+
// FormatStatementSummary formats the statement using FmtSummary and
892+
// FmtHideConstants. This returns a summarized version of the query. It does
893+
// *not* anonymize the statement, since the result will still contain names and
894+
// identifiers.
895+
func FormatStatementSummary(ast Statement, optFlags ...FmtFlags) string {
896+
if ast == nil {
897+
return ""
898+
}
899+
fmtFlags := FmtSummary | FmtHideConstants
900+
for _, f := range optFlags {
901+
fmtFlags |= f
902+
}
903+
return AsStringWithFlags(ast, fmtFlags)
904+
}
905+
877906
var fmtCtxPool = sync.Pool{
878907
New: func() interface{} {
879908
return &FmtCtx{}

pkg/sql/statement.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func makeStatement(
5454

5555
return Statement{
5656
Statement: parserStmt,
57-
StmtNoConstants: formatStatementHideConstants(parserStmt.AST, fmtFlags),
58-
StmtSummary: formatStatementSummary(parserStmt.AST, fmtFlags),
57+
StmtNoConstants: tree.FormatStatementHideConstants(parserStmt.AST, fmtFlags),
58+
StmtSummary: tree.FormatStatementSummary(parserStmt.AST, fmtFlags),
5959
QueryID: queryID,
6060
QueryTags: tags,
6161
}

0 commit comments

Comments
 (0)