Skip to content

Commit cabf482

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 ee5f7d2 commit cabf482

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
@@ -414,8 +414,9 @@ func (b *Builder) maybeAnnotateWithEstimates(node exec.Node, e memo.RelExpr) {
414414
// The first stat is the most recent full one.
415415
var first int
416416
for first < tab.StatisticCount() &&
417-
tab.Statistic(first).IsPartial() ||
418-
(tab.Statistic(first).IsForecast() && !b.evalCtx.SessionData().OptimizerUseForecasts) {
417+
(tab.Statistic(first).IsPartial() ||
418+
(tab.Statistic(first).IsMerged() && !b.evalCtx.SessionData().OptimizerUseMergedPartialStatistics) ||
419+
(tab.Statistic(first).IsForecast() && !b.evalCtx.SessionData().OptimizerUseForecasts)) {
419420
first++
420421
}
421422

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ distribution: local
13971397
vectorized: true
13981398
·
13991399
• scan
1400-
estimated row count: 4 (36% of the table; stats collected <hidden> ago)
1400+
estimated row count: 4 (40% of the table; stats collected <hidden> ago)
14011401
table: ka@ka_a_idx
14021402
spans: [/6 - ]
14031403

@@ -1436,3 +1436,38 @@ vectorized: true
14361436
estimated row count: 1 (9.1% of the table; stats collected <hidden> ago)
14371437
table: ka@ka_a_idx
14381438
spans: [/10 - /10]
1439+
1440+
subtest regression_148316
1441+
1442+
# Ensure we can run DELETE statement on system.table_statistics.
1443+
statement ok
1444+
INSERT INTO system.users VALUES ('node', NULL, true, 3);
1445+
1446+
statement ok
1447+
GRANT node TO root;
1448+
1449+
# Keep only partial stats on the target table.
1450+
statement ok
1451+
DELETE FROM system.table_statistics WHERE name NOT LIKE '%partial%' AND "tableID" = 'ka'::REGCLASS::OID;
1452+
1453+
query TT
1454+
SELECT statistics_name, column_names FROM [SHOW STATISTICS FOR TABLE ka] ORDER BY created
1455+
----
1456+
ka_partialstat {a}
1457+
1458+
# Ensure that the system table is read on the next query.
1459+
statement ok
1460+
SELECT crdb_internal.clear_table_stats_cache();
1461+
1462+
query T
1463+
EXPLAIN SELECT * FROM ka WHERE a = 10
1464+
----
1465+
distribution: local
1466+
vectorized: true
1467+
·
1468+
• scan
1469+
missing stats
1470+
table: ka@ka_a_idx
1471+
spans: [/10 - /10]
1472+
1473+
subtest end

0 commit comments

Comments
 (0)