Skip to content

Commit 572025d

Browse files
committed
opt: fix hints for placeholder fast path
Fixes #147363 Release note (bug fix): A bug has been fixed that cause the optimizer to ignore index hints when optimizing some forms of prepared statements. This could result in one of two unexepcted behaviors: a query errors with the message "index cannot be used for this query" when the index can actually be used, or query using an index that does not adhere to the hint. The hints relevant to this bug are regular index hints, e.g., `SELECT * FROM tab@index`, `FORCE_INVERTED_INDEX` and `FORCE_ZIGZAG`.
1 parent 4aa37fa commit 572025d

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

pkg/sql/opt/exec/execbuilder/testdata/select_index_flags

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,41 @@ vectorized: true
357357
missing stats
358358
table: abcd@abcd_pkey
359359
spans: FULL SCAN
360+
361+
# Regression tests for #147363. Statements should not error because the fast path
362+
# picks a plan that does not adhere to hints.
363+
statement ok
364+
CREATE TABLE t147363 (
365+
a INT,
366+
b INT,
367+
c INT,
368+
PRIMARY KEY (a, b),
369+
INDEX i (a, b)
370+
)
371+
372+
statement ok
373+
PREPARE p147363 AS
374+
SELECT * FROM t147363@i WHERE a = $1 AND b = $2
375+
376+
statement ok
377+
EXECUTE p147363(1, 2)
378+
379+
statement ok
380+
DEALLOCATE p147363
381+
382+
statement ok
383+
PREPARE p147363 AS
384+
SELECT * FROM t147363@{FORCE_INVERTED_INDEX} WHERE a = $1 AND b = $2
385+
386+
statement error pgcode XXUUU could not produce a query plan conforming to the FORCE_INVERTED_INDEX hint
387+
EXECUTE p147363(1, 2)
388+
389+
statement ok
390+
DEALLOCATE p147363
391+
392+
statement ok
393+
PREPARE p147363 AS
394+
SELECT * FROM t147363@{FORCE_ZIGZAG} WHERE a = $1 AND b = $2
395+
396+
statement error pgcode XXUUU could not produce a query plan conforming to the FORCE_ZIGZAG hint
397+
EXECUTE p147363(1, 2)

pkg/sql/opt/xform/placeholder_fast_path.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func (o *Optimizer) TryPlaceholderFastPath() (ok bool, err error) {
8888
return false, nil
8989
}
9090

91+
if scan.Flags.ForceInvertedIndex || scan.Flags.ForceZigzag {
92+
// We don't support inverted or zigzag indexes in the fast path.
93+
return false, nil
94+
}
95+
9196
var constrainedCols opt.ColSet
9297
for i := range sel.Filters {
9398
// Each condition must be an equality between a variable and a constant
@@ -126,6 +131,10 @@ func (o *Optimizer) TryPlaceholderFastPath() (ok bool, err error) {
126131
// Skip inverted and vector indexes.
127132
continue
128133
}
134+
if scan.Flags.ForceIndex && scan.ScanPrivate.Flags.Index != ord {
135+
// If an index is forced, skip all other indexes.
136+
continue
137+
}
129138

130139
if pred, isPartialIndex := tabMeta.PartialIndexPredicate(ord); isPartialIndex {
131140
// We are not equipped to handle partial indexes.

pkg/sql/opt/xform/testdata/placeholder-fast-path/scan

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,33 @@ placeholder-scan t_dec
345345
├── fd: ()-->(1,2)
346346
└── span
347347
└── $1
348+
349+
# Regression tests for #147363. The placeholder fast path should respect index
350+
# flags.
351+
exec-ddl
352+
CREATE TABLE t147363 (
353+
a INT,
354+
b INT,
355+
c INT,
356+
PRIMARY KEY (a, b),
357+
INDEX i (a, b)
358+
)
359+
----
360+
361+
# No fast path is selected because i is not covering.
362+
placeholder-fast-path
363+
SELECT * FROM t147363@i WHERE a = $1 AND b = $2
364+
----
365+
no fast path
366+
367+
# The fast path does not support inverted indexes.
368+
placeholder-fast-path
369+
SELECT * FROM t147363@{FORCE_INVERTED_INDEX} WHERE a = $1 AND b = $2
370+
----
371+
no fast path
372+
373+
# The fast path does not support zig-zag joins.
374+
placeholder-fast-path
375+
SELECT * FROM t147363@{FORCE_ZIGZAG} WHERE a = $1 AND b = $2
376+
----
377+
no fast path

0 commit comments

Comments
 (0)