Commit c956f73
Merge #158096
158096: sql/*: add hint injection r=DrewKimball a=michae2
**sql: add parseHint step when loading hint into hints cache**
When loading an external statement hint into the statement hints cache,
we might need to call some function to get the hint ready for use. (For
hint injections, this function is `tree.NewHintInjectionDonor` which
parses and walks the donor statement fingerprint.) This function could
fail, in which case we want to skip over the hint but not return an
error from `GetStatementHintsFromDB`. This function could succeed but
create some extra state which we need to save.
This commit adds a new `parseHint` step which calls any functions needed
to get the hint ready, and creates a new `hints.Hint` struct which holds
the object(s) created when parsing hints. (These are analogous to
`parseStats` and `TableStatistic` from the stats cache.)
Informs: #153633
Release note: None
---
**sql/\*: add hint injection**
1. During `ReloadHintsIfStale` we now call `Validate` and `InjectHints`
using the donor to perform the AST rewrite. We save the rewritten AST
in the statement separately from the original AST.
2. We wrap `prepareUsingOptimizer` and `makeOptimizerPlan` with
functions that first try preparing / planning with injected hints,
and then try again without injected hints in case the injected hints
are invalid.
With these two pieces we can now actually perform hint injection.
Fixes: #153633
Release note (sql change): A new "hint injection" ability has been
added, which allows operators to dynamically inject inline hints into
statements, without modifying the text of those statements. Hints can be
injected using the builtin function `crdb_internal.inject_hint` with the
target statement fingerprint to rewrite. For example, to add an index
hint to the statement `SELECT * FROM my_table WHERE col = 3`, use:
```
SELECT crdb_internal.inject_hint(
'SELECT * FROM my_table WHERE col = _',
'SELECT * FROM my_table@my_table_col_idx WHERE col = _'
);
```
Whenever a statement is executed matching statement fingerprint
`SELECT * FROM my_table WHERE col = _`, it will first be rewritten
to include the injected index hint.
---
**sql/\*: invalidate cached memos after hint injection changes**
If we build a memo with hint injection, and then later we realize that
memo won't work (maybe because we discover the hint is unsatisfiable
during execution of a prepared statement) we need to invalidate the
cached memo.
To do this, add a usingHintInjection field which tells the memo
staleness check whether we're trying with or without hint injection.
Also, in a related but separate change, this commit adds all matching
HintIDs to the optimizer metadata so that we don't invalidate cached
memos if the hintsGeneration changed due to some unrelated statement
hints changing.
Informs: #153633
Release note: None
Co-authored-by: Michael Erickson <michae2@cockroachlabs.com>File tree
20 files changed
+1024
-117
lines changed- pkg/sql
- faketreeeval
- hints
- logictest/testdata/logic_test
- opt
- exec/execbuilder/testdata
- memo
- prep
- sem
- eval
- tree
20 files changed
+1024
-117
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
565 | 565 | | |
566 | 566 | | |
567 | 567 | | |
| 568 | + | |
568 | 569 | | |
569 | 570 | | |
570 | 571 | | |
| |||
1465 | 1466 | | |
1466 | 1467 | | |
1467 | 1468 | | |
| 1469 | + | |
1468 | 1470 | | |
1469 | 1471 | | |
1470 | 1472 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| 250 | + | |
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
603 | 603 | | |
604 | 604 | | |
605 | 605 | | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
606 | 616 | | |
607 | 617 | | |
608 | 618 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| 69 | + | |
66 | 70 | | |
| 71 | + | |
67 | 72 | | |
68 | 73 | | |
69 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
| |||
406 | 405 | | |
407 | 406 | | |
408 | 407 | | |
409 | | - | |
410 | | - | |
| 408 | + | |
| 409 | + | |
411 | 410 | | |
412 | 411 | | |
413 | 412 | | |
| |||
430 | 429 | | |
431 | 430 | | |
432 | 431 | | |
433 | | - | |
| 432 | + | |
434 | 433 | | |
435 | 434 | | |
436 | 435 | | |
| |||
464 | 463 | | |
465 | 464 | | |
466 | 465 | | |
467 | | - | |
468 | | - | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
469 | 471 | | |
470 | 472 | | |
471 | 473 | | |
| |||
483 | 485 | | |
484 | 486 | | |
485 | 487 | | |
486 | | - | |
| 488 | + | |
487 | 489 | | |
488 | 490 | | |
489 | 491 | | |
| |||
517 | 519 | | |
518 | 520 | | |
519 | 521 | | |
520 | | - | |
| 522 | + | |
521 | 523 | | |
522 | 524 | | |
523 | 525 | | |
524 | 526 | | |
525 | 527 | | |
526 | 528 | | |
527 | | - | |
528 | | - | |
529 | | - | |
| 529 | + | |
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
| |||
0 commit comments