Skip to content

Commit 9ee3a6e

Browse files
michae2claude
andcommitted
cli: skip non-.sql files when loading statement bundle stats
When loading statistics from a statement bundle directory, the code would previously attempt to process any file starting with "stats-" regardless of its extension. This caused issues when backup files (e.g., files ending with .sql~) were present in the bundle directory, as these would be processed as valid statistics files. This change adds a check to ensure only files with a .sql extension are processed when loading statement bundle statistics. This allows users to keep backup copies of statistics files (e.g., when manually editing stats for testing different scenarios) without breaking the bundle recreation process. A new interactive test test_sb_recreate_edited_stats.tcl is added to verify that manually edited statistics are properly loaded during statement bundle recreation. The test creates a bundle, backs up the original stats file, edits the histogram values, and verifies that the edited statistics are used correctly. Epic: None Release note: none Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8b1fce6 commit 9ee3a6e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

pkg/acceptance/generated_cli_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#! /usr/bin/env expect -f
2+
3+
source [file join [file dirname $argv0] common.tcl]
4+
5+
proc file_exists {filepath} {
6+
if {! [ file exist $filepath]} {
7+
report "MISSING EXPECTED FILE: $filepath"
8+
exit 1
9+
}
10+
}
11+
12+
start_test "Ensure that 'debug sb recreate' uses edited statistics"
13+
14+
spawn $argv demo --no-line-editor --empty --log-dir=logs
15+
eexpect "defaultdb>"
16+
17+
# Create table and insert data
18+
send "CREATE TABLE t (a INT, INDEX (a));\r"
19+
eexpect "defaultdb>"
20+
21+
send "INSERT INTO t SELECT 100 FROM generate_series(0, 99);\r"
22+
eexpect "defaultdb>"
23+
24+
# Collect statistics on the table
25+
send "ANALYZE t;\r"
26+
eexpect "defaultdb>"
27+
28+
# Create a statement bundle with a query using the statistics
29+
send "EXPLAIN ANALYZE (DEBUG) SELECT * FROM t WHERE a = 100;\r"
30+
eexpect "Statement diagnostics bundle generated."
31+
expect -re "SQL shell: \\\\statement-diag download (\\d+)" {
32+
set id1 $expect_out(1,string)
33+
}
34+
35+
send "\\statement-diag download $id1\r"
36+
eexpect "Bundle saved to"
37+
eexpect root@
38+
39+
file_exists "stmt-bundle-$id1.zip"
40+
41+
send_eof
42+
eexpect eof
43+
44+
# Extract the bundle
45+
set python "python2.7"
46+
set pyfile [file join [file dirname $argv0] unzip.py]
47+
system "mkdir bundle"
48+
system "$python $pyfile stmt-bundle-$id1.zip bundle"
49+
50+
# Back up the original stats file
51+
system "cp bundle/stats-defaultdb.public.t.sql bundle/stats-defaultdb.public.t.sql~"
52+
53+
# Edit the histogram to change a=100 to a=200 in the JSON
54+
# This simulates a user manually editing stats to test different scenarios
55+
system "sed -i.bak 's/\"upper_bound\": \"100\"/\"upper_bound\": \"200\"/g' bundle/stats-defaultdb.public.t.sql"
56+
57+
# Recreate the bundle with edited stats
58+
spawn $argv debug sb recreate bundle
59+
eexpect "Statement was:"
60+
eexpect "SELECT"
61+
eexpect "defaultdb>"
62+
63+
# Verify the statistics were loaded by checking SHOW STATISTICS
64+
send "SELECT statistics_name, column_names, row_count, distinct_count FROM [SHOW STATISTICS FOR TABLE t];\r"
65+
eexpect "100"
66+
eexpect "defaultdb>"
67+
68+
# Verify that the edited stats are used by running EXPLAIN queries
69+
# The optimizer should now think the data has a=200 instead of a=100
70+
send "EXPLAIN SELECT * FROM t WHERE a = 100;\r"
71+
eexpect "estimated row count: 1"
72+
eexpect "defaultdb>"
73+
74+
send "EXPLAIN SELECT * FROM t WHERE a = 200;\r"
75+
eexpect "estimated row count: 100"
76+
eexpect "defaultdb>"
77+
78+
send_eof
79+
eexpect eof
80+
81+
end_test

pkg/cli/statement_bundle.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func loadStatementBundle(zipdir string) (*statementBundle, error) {
108108
if !strings.HasPrefix(d.Name(), "stats-") {
109109
return nil
110110
}
111+
if !strings.HasSuffix(d.Name(), ".sql") {
112+
return nil
113+
}
111114
f, err := os.ReadFile(path)
112115
if err != nil {
113116
return err

0 commit comments

Comments
 (0)