Skip to content

Commit cfc44a7

Browse files
authored
Add query element wrappers: Partition, CheckQuery, Assignment, Constraint, TTLElement (#88)
1 parent 059657b commit cfc44a7

File tree

146 files changed

+232
-510
lines changed

Some content is hidden

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

146 files changed

+232
-510
lines changed

ast/ast.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,18 @@ func (o *OptimizeQuery) Pos() token.Position { return o.Position }
713713
func (o *OptimizeQuery) End() token.Position { return o.Position }
714714
func (o *OptimizeQuery) statementNode() {}
715715

716+
// CheckQuery represents a CHECK TABLE statement.
717+
type CheckQuery struct {
718+
Position token.Position `json:"-"`
719+
Database string `json:"database,omitempty"`
720+
Table string `json:"table"`
721+
Settings []*SettingExpr `json:"settings,omitempty"`
722+
}
723+
724+
func (c *CheckQuery) Pos() token.Position { return c.Position }
725+
func (c *CheckQuery) End() token.Position { return c.Position }
726+
func (c *CheckQuery) statementNode() {}
727+
716728
// SystemQuery represents a SYSTEM statement.
717729
type SystemQuery struct {
718730
Position token.Position `json:"-"`

internal/explain/explain.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
143143
explainOptimizeQuery(sb, n, indent)
144144
case *ast.TruncateQuery:
145145
explainTruncateQuery(sb, n, indent)
146+
case *ast.CheckQuery:
147+
explainCheckQuery(sb, n, indent)
146148

147149
// Types
148150
case *ast.DataType:

internal/explain/statements.go

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
122122
}
123123
// Count children: name + columns + engine/storage
124124
children := 1 // name identifier
125-
if len(n.Columns) > 0 {
125+
if len(n.Columns) > 0 || len(n.Indexes) > 0 || len(n.Constraints) > 0 {
126126
children++
127127
}
128128
if n.Engine != nil || len(n.OrderBy) > 0 || len(n.PrimaryKey) > 0 || n.PartitionBy != nil {
@@ -141,14 +141,17 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
141141
fmt.Fprintf(sb, "%sCreateQuery %s (children %d)\n", indent, name, children)
142142
}
143143
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
144-
if len(n.Columns) > 0 || len(n.Indexes) > 0 {
144+
if len(n.Columns) > 0 || len(n.Indexes) > 0 || len(n.Constraints) > 0 {
145145
childrenCount := 0
146146
if len(n.Columns) > 0 {
147147
childrenCount++
148148
}
149149
if len(n.Indexes) > 0 {
150150
childrenCount++
151151
}
152+
if len(n.Constraints) > 0 {
153+
childrenCount++
154+
}
152155
// Check for PRIMARY KEY constraints in column declarations
153156
var primaryKeyColumns []string
154157
for _, col := range n.Columns {
@@ -172,6 +175,14 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
172175
Index(sb, idx, depth+3)
173176
}
174177
}
178+
// Output constraints wrapped in Constraint nodes
179+
if len(n.Constraints) > 0 {
180+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Constraints))
181+
for _, constraint := range n.Constraints {
182+
fmt.Fprintf(sb, "%s Constraint (children 1)\n", indent)
183+
Node(sb, constraint.Expression, depth+4)
184+
}
185+
}
175186
// Output PRIMARY KEY columns as Function tuple
176187
if len(primaryKeyColumns) > 0 {
177188
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
@@ -181,7 +192,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
181192
}
182193
}
183194
}
184-
if n.Engine != nil || len(n.OrderBy) > 0 || len(n.PrimaryKey) > 0 || n.PartitionBy != nil || n.SampleBy != nil || len(n.Settings) > 0 {
195+
if n.Engine != nil || len(n.OrderBy) > 0 || len(n.PrimaryKey) > 0 || n.PartitionBy != nil || n.SampleBy != nil || n.TTL != nil || len(n.Settings) > 0 {
185196
storageChildren := 0
186197
if n.Engine != nil {
187198
storageChildren++
@@ -214,6 +225,9 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
214225
}
215226
}
216227
}
228+
if n.TTL != nil {
229+
storageChildren++
230+
}
217231
if len(n.Settings) > 0 {
218232
storageChildren++
219233
}
@@ -313,6 +327,11 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
313327
}
314328
}
315329
}
330+
if n.TTL != nil {
331+
fmt.Fprintf(sb, "%s ExpressionList (children 1)\n", indent)
332+
fmt.Fprintf(sb, "%s TTLElement (children 1)\n", indent)
333+
Node(sb, n.TTL.Expression, depth+4)
334+
}
316335
if len(n.Settings) > 0 {
317336
fmt.Fprintf(sb, "%s Set\n", indent)
318337
}
@@ -681,7 +700,8 @@ func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent stri
681700
case ast.AlterDropPartition, ast.AlterDetachPartition, ast.AlterAttachPartition,
682701
ast.AlterReplacePartition, ast.AlterFreezePartition:
683702
if cmd.Partition != nil {
684-
Node(sb, cmd.Partition, depth+1)
703+
fmt.Fprintf(sb, "%s Partition (children 1)\n", indent)
704+
Node(sb, cmd.Partition, depth+2)
685705
}
686706
case ast.AlterFreeze:
687707
// No children
@@ -690,18 +710,16 @@ func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent stri
690710
Node(sb, cmd.Where, depth+1)
691711
}
692712
case ast.AlterUpdate:
713+
if cmd.Where != nil {
714+
Node(sb, cmd.Where, depth+1)
715+
}
693716
if len(cmd.Assignments) > 0 {
694717
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(cmd.Assignments))
695718
for _, assign := range cmd.Assignments {
696-
fmt.Fprintf(sb, "%s Function equals (children 1)\n", indent)
697-
fmt.Fprintf(sb, "%s ExpressionList (children 2)\n", indent)
698-
fmt.Fprintf(sb, "%s Identifier %s\n", indent, assign.Column)
699-
Node(sb, assign.Value, depth+4)
719+
fmt.Fprintf(sb, "%s Assignment %s (children 1)\n", indent, assign.Column)
720+
Node(sb, assign.Value, depth+3)
700721
}
701722
}
702-
if cmd.Where != nil {
703-
Node(sb, cmd.Where, depth+1)
704-
}
705723
case ast.AlterAddProjection:
706724
if cmd.Projection != nil {
707725
explainProjection(sb, cmd.Projection, indent, depth+1)
@@ -866,3 +884,26 @@ func explainTruncateQuery(sb *strings.Builder, n *ast.TruncateQuery, indent stri
866884
fmt.Fprintf(sb, "%sTruncateQuery %s (children %d)\n", indent, name, 1)
867885
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
868886
}
887+
888+
func explainCheckQuery(sb *strings.Builder, n *ast.CheckQuery, indent string) {
889+
if n == nil {
890+
fmt.Fprintf(sb, "%s*ast.CheckQuery\n", indent)
891+
return
892+
}
893+
894+
name := n.Table
895+
if n.Database != "" {
896+
name = n.Database + "." + n.Table
897+
}
898+
899+
children := 1 // identifier
900+
if len(n.Settings) > 0 {
901+
children++
902+
}
903+
904+
fmt.Fprintf(sb, "%sCheckQuery %s (children %d)\n", indent, name, children)
905+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
906+
if len(n.Settings) > 0 {
907+
fmt.Fprintf(sb, "%s Set\n", indent)
908+
}
909+
}

parser/parser.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ func (p *Parser) parseStatement() ast.Statement {
153153
return p.parseDetach()
154154
case token.ATTACH:
155155
return p.parseAttach()
156+
case token.CHECK:
157+
return p.parseCheck()
156158
default:
157159
p.errors = append(p.errors, fmt.Errorf("unexpected token %s at line %d, column %d",
158160
p.current.Token, p.current.Pos.Line, p.current.Pos.Column))
@@ -3636,6 +3638,37 @@ func (p *Parser) parseAttach() *ast.AttachQuery {
36363638
return attach
36373639
}
36383640

3641+
func (p *Parser) parseCheck() *ast.CheckQuery {
3642+
check := &ast.CheckQuery{
3643+
Position: p.current.Pos,
3644+
}
3645+
3646+
p.nextToken() // skip CHECK
3647+
3648+
// Skip optional TABLE keyword
3649+
if p.currentIs(token.TABLE) {
3650+
p.nextToken()
3651+
}
3652+
3653+
// Parse table name (can be qualified: database.table)
3654+
tableName := p.parseIdentifierName()
3655+
if p.currentIs(token.DOT) {
3656+
p.nextToken()
3657+
check.Database = tableName
3658+
check.Table = p.parseIdentifierName()
3659+
} else {
3660+
check.Table = tableName
3661+
}
3662+
3663+
// Parse optional SETTINGS
3664+
if p.currentIs(token.SETTINGS) {
3665+
p.nextToken() // skip SETTINGS
3666+
check.Settings = p.parseSettingsList()
3667+
}
3668+
3669+
return check
3670+
}
3671+
36393672
func (p *Parser) parseArrayJoin() *ast.ArrayJoinClause {
36403673
aj := &ast.ArrayJoinClause{
36413674
Position: p.current.Pos,
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
{"explain_todo":{"stmt12":true,"stmt16":true,"stmt20":true,"stmt8":true}}
1+
{
2+
"explain_todo": {
3+
"stmt16": true,
4+
"stmt8": true
5+
}
6+
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt13": true,
4-
"stmt5": true,
5-
"stmt6": true,
6-
"stmt7": true,
7-
"stmt8": true,
83
"stmt9": true
94
}
105
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt7": true,
4-
"stmt8": true
3+
"stmt7": true
54
}
65
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"explain_todo":{"stmt9":true}}
1+
{}

parser/testdata/00446_clear_column_in_partition_zookeeper_long/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"stmt46": true,
1717
"stmt47": true,
1818
"stmt48": true,
19-
"stmt49": true,
2019
"stmt6": true,
2120
"stmt8": true
2221
}

parser/testdata/00502_custom_partitioning_local/metadata.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"stmt25": true,
77
"stmt26": true,
88
"stmt28": true,
9-
"stmt31": true,
109
"stmt41": true,
1110
"stmt42": true,
1211
"stmt43": true,
@@ -15,10 +14,8 @@
1514
"stmt58": true,
1615
"stmt59": true,
1716
"stmt61": true,
18-
"stmt64": true,
1917
"stmt7": true,
2018
"stmt73": true,
21-
"stmt76": true,
2219
"stmt8": true
2320
}
2421
}

0 commit comments

Comments
 (0)