Skip to content

Commit 46b7a23

Browse files
committed
Handle SETTINGS placement edge case for FROM clause presence
When SETTINGS comes after FORMAT and there's a FROM clause, Set should appear at both SelectQuery and SelectWithUnionQuery levels. When there's no FROM clause, Set only appears at SelectWithUnionQuery level. This fixes ~87 more explain tests.
1 parent 841bf14 commit 46b7a23

97 files changed

Lines changed: 92 additions & 376 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/explain/select.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string,
123123
Node(sb, expr, depth+2)
124124
}
125125
}
126-
// SETTINGS - output at SelectQuery level only if NOT after FORMAT
127-
// When SETTINGS comes after FORMAT, it's only at SelectWithUnionQuery level
128-
if len(n.Settings) > 0 && !n.SettingsAfterFormat {
126+
// SETTINGS - output at SelectQuery level in these cases:
127+
// 1. SETTINGS is before FORMAT (not after)
128+
// 2. SETTINGS is after FORMAT AND there's a FROM clause
129+
// When SETTINGS is after FORMAT without FROM, it's only at SelectWithUnionQuery level
130+
if len(n.Settings) > 0 && (!n.SettingsAfterFormat || n.From != nil) {
129131
fmt.Fprintf(sb, "%s Set\n", indent)
130132
}
131133
}
@@ -384,9 +386,11 @@ func countSelectQueryChildren(n *ast.SelectQuery) int {
384386
if n.Offset != nil {
385387
count++
386388
}
387-
// SETTINGS is counted at SelectQuery level only if NOT after FORMAT
388-
// When SETTINGS comes after FORMAT, it's only counted at SelectWithUnionQuery level
389-
if len(n.Settings) > 0 && !n.SettingsAfterFormat {
389+
// SETTINGS is counted at SelectQuery level in these cases:
390+
// 1. SETTINGS is before FORMAT (not after)
391+
// 2. SETTINGS is after FORMAT AND there's a FROM clause
392+
// When SETTINGS is after FORMAT without FROM, it's only at SelectWithUnionQuery level
393+
if len(n.Settings) > 0 && (!n.SettingsAfterFormat || n.From != nil) {
390394
count++
391395
}
392396
return count

parser/testdata/00152_insert_different_granularity/metadata.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt5": true,
4-
"stmt6": true,
53
"stmt7": true,
64
"stmt8": true
75
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt5": true,
4-
"stmt6": true,
5-
"stmt7": true
3+
"stmt5": true
64
}
75
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"explain_todo":{"stmt21":true,"stmt22":true}}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt3": true,
4-
"stmt5": true
5-
}
6-
}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt13": true,
4-
"stmt14": true
5-
}
6-
}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"explain_todo":{"stmt11":true,"stmt12":true}}
1+
{}
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
{
22
"explain_todo": {
3-
"stmt10": true,
4-
"stmt11": true,
53
"stmt13": true,
6-
"stmt14": true,
7-
"stmt15": true,
8-
"stmt16": true,
9-
"stmt17": true,
10-
"stmt18": true,
11-
"stmt19": true,
12-
"stmt20": true,
134
"stmt22": true,
14-
"stmt23": true,
15-
"stmt24": true,
16-
"stmt25": true,
17-
"stmt26": true,
18-
"stmt27": true,
19-
"stmt28": true,
20-
"stmt29": true,
21-
"stmt4": true,
22-
"stmt5": true,
23-
"stmt6": true,
24-
"stmt7": true,
25-
"stmt8": true,
26-
"stmt9": true
5+
"stmt4": true
276
}
287
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"explain_todo":{"stmt10":true,"stmt12":true,"stmt13":true,"stmt14":true,"stmt15":true,"stmt16":true,"stmt17":true,"stmt18":true,"stmt20":true,"stmt21":true,"stmt22":true,"stmt23":true,"stmt24":true,"stmt25":true,"stmt26":true,"stmt4":true,"stmt5":true,"stmt6":true,"stmt7":true,"stmt8":true,"stmt9":true}}
1+
{}
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt10": true,
4-
"stmt11": true,
5-
"stmt12": true,
6-
"stmt18": true,
7-
"stmt19": true,
8-
"stmt20": true,
9-
"stmt21": true,
10-
"stmt22": true,
11-
"stmt23": true,
12-
"stmt24": true,
13-
"stmt6": true,
14-
"stmt7": true,
15-
"stmt8": true,
16-
"stmt9": true
17-
}
18-
}
1+
{}

0 commit comments

Comments
 (0)