Skip to content

Commit cd189e8

Browse files
craig[bot]michae2
andcommitted
Merge #153689
153689: sql, tree: add InjectHints function r=DrewKimball a=michae2 This PR adds `tree.(*HintInjectionDonor).InjectHints`, which rewrites a target statement with hints copied from a donor statement. This is based on the last commit of [Drew's plan_hints prototype](#119120) but uses `tree.WalkStmt` instead of creating a new AST-walker. See individual commits for details. Informs: #153633 Release note: None Co-authored-by: Michael Erickson <[email protected]>
2 parents 7a39b97 + 4d09c5d commit cd189e8

20 files changed

+733
-64
lines changed

pkg/internal/sqlsmith/relational.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ func makeJoinExpr(s *Smither, refs colRefs, forJoin bool) (tree.TableExpr, colRe
200200
Left: left,
201201
Right: right,
202202
}
203+
switch s.rnd.Intn(10) {
204+
// INVERTED is unlikely to work unless we get very lucky, so skip it for now.
205+
case 0:
206+
joinExpr.Hint = "HASH"
207+
case 1:
208+
joinExpr.Hint = "LOOKUP"
209+
case 2:
210+
joinExpr.Hint = "MERGE"
211+
case 3:
212+
joinExpr.Hint = "STRAIGHT"
213+
}
203214

204215
if s.disableCrossJoins {
205216
if available, ok := getAvailablePairedColsForJoinPreds(s, leftRefs, rightRefs); ok {

pkg/sql/conn_executor.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,23 +3643,6 @@ var allowBufferedWritesForWeakIsolation = settings.RegisterBoolSetting(
36433643

36443644
var logIsolationLevelLimiter = log.Every(10 * time.Second)
36453645

3646-
// Bitmask for enabling various query fingerprint formatting styles.
3647-
// We don't publish this setting because most users should not need
3648-
// to tweak the fingerprint generation.
3649-
var queryFormattingForFingerprintsMask = settings.RegisterIntSetting(
3650-
settings.ApplicationLevel,
3651-
"sql.stats.statement_fingerprint.format_mask",
3652-
"enables setting additional fmt flags for statement fingerprint formatting. "+
3653-
"Flags set here will be applied in addition to FmtHideConstants",
3654-
int64(tree.FmtCollapseLists|tree.FmtConstantsAsUnderscores),
3655-
settings.WithValidateInt(func(i int64) error {
3656-
if i == 0 || int64(tree.FmtCollapseLists|tree.FmtConstantsAsUnderscores)&i == i {
3657-
return nil
3658-
}
3659-
return errors.Newf("invalid value %d", i)
3660-
}),
3661-
)
3662-
36633646
func (ex *connExecutor) txnIsolationLevelToKV(
36643647
ctx context.Context, level tree.IsolationLevel,
36653648
) isolation.Level {

pkg/sql/conn_executor_exec.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (ex *connExecutor) execStmtInOpenState(
380380
os := ex.machine.CurState().(stateOpen)
381381

382382
isExtendedProtocol := prepared != nil
383-
stmtFingerprintFmtMask := tree.FmtHideConstants | tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV))
383+
stmtFingerprintFmtMask := tree.FmtHideConstants | tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV))
384384

385385
var stmt Statement
386386
if isExtendedProtocol {
@@ -905,7 +905,7 @@ func (ex *connExecutor) execStmtInOpenState(
905905
NumAnnotations: stmt.NumAnnotations,
906906
},
907907
ex.server.cfg.GenerateID(),
908-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV)),
908+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV)),
909909
)
910910
var rawTypeHints []oid.Oid
911911

@@ -1256,7 +1256,7 @@ func (ex *connExecutor) execStmtInOpenStateWithPausablePortal(
12561256
os := ex.machine.CurState().(stateOpen)
12571257

12581258
isExtendedProtocol := portal != nil && portal.Stmt != nil
1259-
stmtFingerprintFmtMask := tree.FmtHideConstants | tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV))
1259+
stmtFingerprintFmtMask := tree.FmtHideConstants | tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV))
12601260

12611261
if isExtendedProtocol {
12621262
vars.stmt = makeStatementFromPrepared(portal.Stmt, queryID)
@@ -1867,7 +1867,7 @@ func (ex *connExecutor) execStmtInOpenStateWithPausablePortal(
18671867
NumAnnotations: vars.stmt.NumAnnotations,
18681868
},
18691869
ex.server.cfg.GenerateID(),
1870-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV)),
1870+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV)),
18711871
)
18721872
var rawTypeHints []oid.Oid
18731873

@@ -3453,7 +3453,7 @@ func (ex *connExecutor) execStmtInNoTxnState(
34533453

34543454
p := &ex.planner
34553455
stmt := makeStatement(parserStmt, ex.server.cfg.GenerateID(),
3456-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV)))
3456+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&ex.server.cfg.Settings.SV)))
34573457
p.stmt = stmt
34583458
p.semaCtx.Annotations = tree.MakeAnnotations(stmt.NumAnnotations)
34593459
p.extendedEvalCtx.Annotations = &p.semaCtx.Annotations

pkg/sql/conn_executor_prepare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (ex *connExecutor) execPrepare(
7373
}
7474

7575
stmt := makeStatement(parseCmd.Statement, ex.server.cfg.GenerateID(),
76-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(ex.server.cfg.SV())))
76+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(ex.server.cfg.SV())))
7777
_, err := ex.addPreparedStmt(
7878
ctx,
7979
parseCmd.Name,

pkg/sql/distsql_plan_changefeed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func PlanCDCExpression(
8282
AST: cdcExpr,
8383
SQL: tree.AsString(cdcExpr),
8484
}, clusterunique.ID{}, /* queryID */
85-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&p.execCfg.Settings.SV)),
85+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&p.execCfg.Settings.SV)),
8686
)
8787

8888
p.curPlan.init(&p.stmt, &p.instrumentation)

pkg/sql/distsql_running_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func TestDistSQLRunningInAbortedTxn(t *testing.T) {
168168
// We need to re-plan every time, since the plan is closed automatically
169169
// by PlanAndRun() below making it unusable across retries.
170170
p.stmt = makeStatement(stmt, clusterunique.ID{},
171-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&execCfg.Settings.SV)))
171+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&execCfg.Settings.SV)))
172172
if err := p.makeOptimizerPlan(ctx); err != nil {
173173
t.Fatal(err)
174174
}
@@ -286,7 +286,7 @@ func TestDistSQLRunningParallelFKChecksAfterAbort(t *testing.T) {
286286
)
287287

288288
p.stmt = makeStatement(stmt, clusterunique.ID{},
289-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&s.ClusterSettings().SV)))
289+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&s.ClusterSettings().SV)))
290290
if err := p.makeOptimizerPlan(ctx); err != nil {
291291
t.Fatal(err)
292292
}

pkg/sql/exec_log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (p *planner) maybeLogStatementInternal(
353353
if stmtFingerprintID == 0 {
354354
repQuery := p.stmt.StmtNoConstants
355355
if repQuery == "" {
356-
flags := tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&p.execCfg.Settings.SV))
356+
flags := tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&p.execCfg.Settings.SV))
357357
f := tree.NewFmtCtx(flags)
358358
f.FormatNode(p.stmt.AST)
359359
repQuery = f.CloseAndGetString()

pkg/sql/explain_tree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestPlanToTreeAndPlanToString(t *testing.T) {
7272
ih.collectBundle = true
7373

7474
p.stmt = makeStatement(stmt, clusterunique.ID{},
75-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&execCfg.Settings.SV)))
75+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&execCfg.Settings.SV)))
7676
if err := p.makeOptimizerPlan(ctx); err != nil {
7777
t.Fatal(err)
7878
}

pkg/sql/plan_opt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ func TestPlanGistControl(t *testing.T) {
646646
)
647647
defer cleanup()
648648

649-
fmtFingerprintMask := tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&s.ClusterSettings().SV))
649+
fmtFingerprintMask := tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&s.ClusterSettings().SV))
650650
p := internalPlanner.(*planner)
651651
stmt, err := parser.ParseOne("SELECT 1")
652652
if err != nil {

pkg/sql/schema_changer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ func (sc *SchemaChanger) backfillQueryIntoTable(
504504
localPlanner.MaybeReallocateAnnotations(stmt.NumAnnotations)
505505
// Construct an optimized logical plan of the AS source stmt.
506506
localPlanner.stmt = makeStatement(stmt, clusterunique.ID{}, /* queryID */
507-
tree.FmtFlags(queryFormattingForFingerprintsMask.Get(&localPlanner.execCfg.Settings.SV)))
507+
tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&localPlanner.execCfg.Settings.SV)))
508508
localPlanner.optPlanningCtx.init(localPlanner)
509509

510510
localPlanner.runWithOptions(resolveFlags{skipCache: true}, func() {

0 commit comments

Comments
 (0)