Skip to content

Commit 99534b0

Browse files
kyleconroyclaude
andcommitted
Support ORDER BY in CREATE DATABASE and multiple SETTINGS clauses
Changes: - Add parsing for ORDER BY clause in CREATE DATABASE statements - Add QuerySettings field to CreateQuery AST for second SETTINGS clause - Update parser to store second SETTINGS in QuerySettings - Update explain to output QuerySettings as Set at CreateQuery level - Fixes 02184_default_table_engine stmt56 (CREATE DATABASE ORDER BY) - Fixes 02184_default_table_engine stmt107 (multiple SETTINGS) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6216399 commit 99534b0

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ type CreateQuery struct {
292292
SampleBy Expression `json:"sample_by,omitempty"`
293293
TTL *TTLClause `json:"ttl,omitempty"`
294294
Settings []*SettingExpr `json:"settings,omitempty"`
295+
QuerySettings []*SettingExpr `json:"query_settings,omitempty"` // Query-level SETTINGS (second SETTINGS clause)
295296
SettingsBeforeComment bool `json:"settings_before_comment,omitempty"` // True if SETTINGS comes before COMMENT
296297
AsSelect Statement `json:"as_select,omitempty"`
297298
AsTableFunction Expression `json:"as_table_function,omitempty"` // AS table_function(...) in CREATE TABLE

internal/explain/statements.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
226226
if n.Comment != "" && len(n.Settings) > 0 && !n.SettingsBeforeComment {
227227
children++
228228
}
229+
// QuerySettings (second SETTINGS clause) is a separate child of CreateQuery
230+
if len(n.QuerySettings) > 0 {
231+
children++
232+
}
229233
// For materialized views with TO clause but no storage, count ViewTargets as a child
230234
if n.Materialized && n.To != "" && !hasStorageChild {
231235
children++ // ViewTargets
@@ -558,6 +562,10 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
558562
if n.Comment != "" && len(n.Settings) > 0 && !n.SettingsBeforeComment {
559563
fmt.Fprintf(sb, "%s Set\n", indent)
560564
}
565+
// Output QuerySettings (second SETTINGS clause) at CreateQuery level
566+
if len(n.QuerySettings) > 0 {
567+
fmt.Fprintf(sb, "%s Set\n", indent)
568+
}
561569
}
562570

563571
func explainDropQuery(sb *strings.Builder, n *ast.DropQuery, indent string, depth int) {

parser/parser.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2692,7 +2692,13 @@ func (p *Parser) parseTableOptions(create *ast.CreateQuery) {
26922692
create.SettingsBeforeComment = true
26932693
}
26942694
p.nextToken()
2695-
create.Settings = p.parseSettingsList()
2695+
settings := p.parseSettingsList()
2696+
// If Settings is already set, this is a second SETTINGS clause (query-level)
2697+
if len(create.Settings) > 0 {
2698+
create.QuerySettings = settings
2699+
} else {
2700+
create.Settings = settings
2701+
}
26962702
case p.currentIs(token.COMMENT):
26972703
p.nextToken()
26982704
if p.currentIs(token.STRING) {
@@ -2742,6 +2748,22 @@ func (p *Parser) parseCreateDatabase(create *ast.CreateQuery) {
27422748
}
27432749
create.Engine = p.parseEngineClause()
27442750
}
2751+
2752+
// Handle ORDER BY clause (ClickHouse allows ORDER BY in CREATE DATABASE)
2753+
// This is stored as OrderBy for output in Storage definition
2754+
if p.currentIs(token.ORDER) {
2755+
p.nextToken() // skip ORDER
2756+
if p.currentIs(token.BY) {
2757+
p.nextToken() // skip BY
2758+
}
2759+
create.OrderBy = []ast.Expression{p.parseExpression(LOWEST)}
2760+
}
2761+
2762+
// Handle SETTINGS clause
2763+
if p.currentIs(token.SETTINGS) {
2764+
p.nextToken()
2765+
create.Settings = p.parseSettingsList()
2766+
}
27452767
}
27462768

27472769
func (p *Parser) parseCreateView(create *ast.CreateQuery) {
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt107": true,
4-
"stmt56": true
5-
}
6-
}
1+
{}

0 commit comments

Comments
 (0)