Skip to content

Commit 16a67f2

Browse files
committed
execbuilder: fix a silly bug around finding the most recent full stat
This commit fixes a bug that was added in 3bc0992. In short, in a complicated logical expression we had `v1 && v2 || v3` when we wanted `v1 && (v2 || v3)`. This allowed us to hit an index of bounds when evaluating `v3`, which in this concrete case could mean that we iterated over all stats and didn't find any full ones. There are two other places with similar conditionals, and they have the correct usage of parenthesis, so I didn't change the structure overall. I did find that we want to also skip the merged stat, depending on the session variable. Also the bug seems quite difficult to hit, so I omitted the release note. Release note: None
1 parent 905320b commit 16a67f2

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

pkg/sql/opt/exec/execbuilder/relational.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,9 @@ func (b *Builder) maybeAnnotateWithEstimates(node exec.Node, e memo.RelExpr) {
410410
// The first stat is the most recent full one.
411411
var first int
412412
for first < tab.StatisticCount() &&
413-
tab.Statistic(first).IsPartial() ||
414-
(tab.Statistic(first).IsForecast() && !b.evalCtx.SessionData().OptimizerUseForecasts) {
413+
(tab.Statistic(first).IsPartial() ||
414+
(tab.Statistic(first).IsMerged() && !b.evalCtx.SessionData().OptimizerUseMergedPartialStatistics) ||
415+
(tab.Statistic(first).IsForecast() && !b.evalCtx.SessionData().OptimizerUseForecasts)) {
415416
first++
416417
}
417418

pkg/sql/opt/exec/execbuilder/testdata/partial_stats

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ distribution: local
13811381
vectorized: true
13821382
·
13831383
• scan
1384-
estimated row count: 4 (36% of the table; stats collected <hidden> ago)
1384+
estimated row count: 4 (40% of the table; stats collected <hidden> ago)
13851385
table: ka@ka_a_idx
13861386
spans: [/6 - ]
13871387

@@ -1420,3 +1420,38 @@ vectorized: true
14201420
estimated row count: 1 (9.1% of the table; stats collected <hidden> ago)
14211421
table: ka@ka_a_idx
14221422
spans: [/10 - /10]
1423+
1424+
subtest regression_148316
1425+
1426+
# Ensure we can run DELETE statement on system.table_statistics.
1427+
statement ok
1428+
INSERT INTO system.users VALUES ('node', NULL, true, 3);
1429+
1430+
statement ok
1431+
GRANT node TO root;
1432+
1433+
# Keep only partial stats on the target table.
1434+
statement ok
1435+
DELETE FROM system.table_statistics WHERE name NOT LIKE '%partial%' AND "tableID" = 'ka'::REGCLASS::OID;
1436+
1437+
query TT
1438+
SELECT statistics_name, column_names FROM [SHOW STATISTICS FOR TABLE ka] ORDER BY created
1439+
----
1440+
ka_partialstat {a}
1441+
1442+
# Ensure that the system table is read on the next query.
1443+
statement ok
1444+
SELECT crdb_internal.clear_table_stats_cache();
1445+
1446+
query T
1447+
EXPLAIN SELECT * FROM ka WHERE a = 10
1448+
----
1449+
distribution: local
1450+
vectorized: true
1451+
·
1452+
• scan
1453+
missing stats
1454+
table: ka@ka_a_idx
1455+
spans: [/10 - /10]
1456+
1457+
subtest end

0 commit comments

Comments
 (0)