Skip to content

Commit c34dd49

Browse files
committed
Add SETTINGS clause support to EXISTS, OPTIMIZE, TRUNCATE, RENAME
- Add Settings field to ExistsQuery, OptimizeQuery, TruncateQuery, RenameQuery - Add SETTINGS parsing to parseExistsStatement, parseOptimize, parseTruncate, parseRename - Add Set output in EXPLAIN for these statement types Fixes 4 of 5 statements in 02494_query_cache_eligible_queries plus 28 bonus tests across other files (03100_lwu_*, 02352_lightweight_delete, 02404_*, etc.).
1 parent 5bf1341 commit c34dd49

20 files changed

Lines changed: 91 additions & 94 deletions

File tree

ast/ast.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ type TruncateQuery struct {
687687
Database string `json:"database,omitempty"`
688688
Table string `json:"table"`
689689
OnCluster string `json:"on_cluster,omitempty"`
690+
Settings []*SettingExpr `json:"settings,omitempty"`
690691
}
691692

692693
func (t *TruncateQuery) Pos() token.Position { return t.Position }
@@ -847,6 +848,7 @@ type OptimizeQuery struct {
847848
Cleanup bool `json:"cleanup,omitempty"`
848849
Dedupe bool `json:"dedupe,omitempty"`
849850
OnCluster string `json:"on_cluster,omitempty"`
851+
Settings []*SettingExpr `json:"settings,omitempty"`
850852
}
851853

852854
func (o *OptimizeQuery) Pos() token.Position { return o.Position }
@@ -906,6 +908,7 @@ type RenameQuery struct {
906908
From string `json:"from,omitempty"` // Deprecated: for backward compat
907909
To string `json:"to,omitempty"` // Deprecated: for backward compat
908910
OnCluster string `json:"on_cluster,omitempty"`
911+
Settings []*SettingExpr `json:"settings,omitempty"`
909912
}
910913

911914
func (r *RenameQuery) Pos() token.Position { return r.Position }
@@ -942,6 +945,7 @@ type ExistsQuery struct {
942945
ExistsType ExistsType `json:"exists_type,omitempty"`
943946
Database string `json:"database,omitempty"`
944947
Table string `json:"table"`
948+
Settings []*SettingExpr `json:"settings,omitempty"`
945949
}
946950

947951
func (e *ExistsQuery) Pos() token.Position { return e.Position }

internal/explain/statements.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ func explainRenameQuery(sb *strings.Builder, n *ast.RenameQuery, indent string,
619619
return
620620
}
621621
// Count identifiers: 2 per pair if no database, 4 per pair if databases specified
622+
hasSettings := len(n.Settings) > 0
622623
children := 0
623624
for _, pair := range n.Pairs {
624625
if pair.FromDatabase != "" {
@@ -630,6 +631,9 @@ func explainRenameQuery(sb *strings.Builder, n *ast.RenameQuery, indent string,
630631
}
631632
children++ // to table
632633
}
634+
if hasSettings {
635+
children++
636+
}
633637
fmt.Fprintf(sb, "%sRename (children %d)\n", indent, children)
634638
for _, pair := range n.Pairs {
635639
// From database (only if specified)
@@ -645,6 +649,9 @@ func explainRenameQuery(sb *strings.Builder, n *ast.RenameQuery, indent string,
645649
// To table
646650
fmt.Fprintf(sb, "%s Identifier %s\n", indent, pair.ToTable)
647651
}
652+
if hasSettings {
653+
fmt.Fprintf(sb, "%s Set\n", indent)
654+
}
648655
}
649656

650657
func explainExchangeQuery(sb *strings.Builder, n *ast.ExchangeQuery, indent string) {
@@ -1067,11 +1074,20 @@ func explainExistsTableQuery(sb *strings.Builder, n *ast.ExistsQuery, indent str
10671074
queryType = "ExistsViewQuery"
10681075
}
10691076

1077+
hasSettings := len(n.Settings) > 0
1078+
10701079
// EXISTS DATABASE has only one child (the database name stored in Table)
10711080
if n.ExistsType == ast.ExistsDatabase {
10721081
name := n.Table
1073-
fmt.Fprintf(sb, "%s%s %s (children %d)\n", indent, queryType, name, 1)
1082+
children := 1
1083+
if hasSettings {
1084+
children++
1085+
}
1086+
fmt.Fprintf(sb, "%s%s %s (children %d)\n", indent, queryType, name, children)
10741087
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
1088+
if hasSettings {
1089+
fmt.Fprintf(sb, "%s Set\n", indent)
1090+
}
10751091
return
10761092
}
10771093

@@ -1082,11 +1098,17 @@ func explainExistsTableQuery(sb *strings.Builder, n *ast.ExistsQuery, indent str
10821098
name = n.Database + " " + n.Table
10831099
children = 2
10841100
}
1101+
if hasSettings {
1102+
children++
1103+
}
10851104
fmt.Fprintf(sb, "%s%s %s (children %d)\n", indent, queryType, name, children)
10861105
if n.Database != "" {
10871106
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
10881107
}
10891108
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
1109+
if hasSettings {
1110+
fmt.Fprintf(sb, "%s Set\n", indent)
1111+
}
10901112
}
10911113

10921114
func explainDataType(sb *strings.Builder, n *ast.DataType, indent string, depth int) {
@@ -1779,13 +1801,17 @@ func explainOptimizeQuery(sb *strings.Builder, n *ast.OptimizeQuery, indent stri
17791801
name += "_cleanup"
17801802
}
17811803

1804+
hasSettings := len(n.Settings) > 0
17821805
children := 1 // identifier
17831806
if n.Database != "" {
17841807
children++ // extra identifier for database
17851808
}
17861809
if n.Partition != nil {
17871810
children++
17881811
}
1812+
if hasSettings {
1813+
children++
1814+
}
17891815

17901816
if n.Database != "" {
17911817
// Database-qualified: OptimizeQuery db table (children N)
@@ -1801,6 +1827,9 @@ func explainOptimizeQuery(sb *strings.Builder, n *ast.OptimizeQuery, indent stri
18011827
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
18021828
}
18031829
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
1830+
if hasSettings {
1831+
fmt.Fprintf(sb, "%s Set\n", indent)
1832+
}
18041833
}
18051834

18061835
func explainTruncateQuery(sb *strings.Builder, n *ast.TruncateQuery, indent string) {
@@ -1809,15 +1838,29 @@ func explainTruncateQuery(sb *strings.Builder, n *ast.TruncateQuery, indent stri
18091838
return
18101839
}
18111840

1841+
// Count children (table identifiers + settings)
1842+
hasSettings := len(n.Settings) > 0
1843+
18121844
if n.Database != "" {
1813-
// Database-qualified: TruncateQuery db table (children 2)
1814-
fmt.Fprintf(sb, "%sTruncateQuery %s %s (children 2)\n", indent, n.Database, n.Table)
1845+
// Database-qualified: TruncateQuery db table (children 2 or 3)
1846+
children := 2
1847+
if hasSettings {
1848+
children++
1849+
}
1850+
fmt.Fprintf(sb, "%sTruncateQuery %s %s (children %d)\n", indent, n.Database, n.Table, children)
18151851
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
18161852
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
18171853
} else {
1818-
fmt.Fprintf(sb, "%sTruncateQuery %s (children 1)\n", indent, n.Table)
1854+
children := 1
1855+
if hasSettings {
1856+
children++
1857+
}
1858+
fmt.Fprintf(sb, "%sTruncateQuery %s (children %d)\n", indent, n.Table, children)
18191859
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
18201860
}
1861+
if hasSettings {
1862+
fmt.Fprintf(sb, "%s Set\n", indent)
1863+
}
18211864
}
18221865

18231866
func explainDeleteQuery(sb *strings.Builder, n *ast.DeleteQuery, indent string, depth int) {

parser/parser.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5258,6 +5258,12 @@ func (p *Parser) parseTruncate() *ast.TruncateQuery {
52585258
}
52595259
}
52605260

5261+
// Handle SETTINGS
5262+
if p.currentIs(token.SETTINGS) {
5263+
p.nextToken()
5264+
trunc.Settings = p.parseSettingsList()
5265+
}
5266+
52615267
return trunc
52625268
}
52635269

@@ -5851,6 +5857,12 @@ func (p *Parser) parseOptimize() *ast.OptimizeQuery {
58515857
p.nextToken()
58525858
}
58535859

5860+
// Handle SETTINGS
5861+
if p.currentIs(token.SETTINGS) {
5862+
p.nextToken()
5863+
opt.Settings = p.parseSettingsList()
5864+
}
5865+
58545866
return opt
58555867
}
58565868

@@ -6088,6 +6100,12 @@ func (p *Parser) parseRename() *ast.RenameQuery {
60886100
}
60896101
}
60906102

6103+
// Handle SETTINGS
6104+
if p.currentIs(token.SETTINGS) {
6105+
p.nextToken()
6106+
rename.Settings = p.parseSettingsList()
6107+
}
6108+
60916109
return rename
60926110
}
60936111

@@ -6778,6 +6796,12 @@ func (p *Parser) parseExistsStatement() *ast.ExistsQuery {
67786796
}
67796797
}
67806798

6799+
// Handle SETTINGS
6800+
if p.currentIs(token.SETTINGS) {
6801+
p.nextToken()
6802+
exists.Settings = p.parseSettingsList()
6803+
}
6804+
67816805
return exists
67826806
}
67836807

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt12": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt21": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt45": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt50": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt14": true,
4-
"stmt29": true,
5-
"stmt46": true
6-
}
7-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt28": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt12": true,
4-
"stmt24": true,
5-
"stmt36": true
6-
}
7-
}
1+
{}

0 commit comments

Comments
 (0)