Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions internal/explain/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
explainDetachQuery(sb, n, indent)
case *ast.AttachQuery:
explainAttachQuery(sb, n, indent)
case *ast.AlterQuery:
explainAlterQuery(sb, n, indent, depth)
case *ast.OptimizeQuery:
explainOptimizeQuery(sb, n, indent)
case *ast.TruncateQuery:
explainTruncateQuery(sb, n, indent)

// Types
case *ast.DataType:
Expand Down
212 changes: 212 additions & 0 deletions internal/explain/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,215 @@ func explainDetachQuery(sb *strings.Builder, n *ast.DetachQuery, indent string)
func explainAttachQuery(sb *strings.Builder, n *ast.AttachQuery, indent string) {
fmt.Fprintf(sb, "%sAttachQuery\n", indent)
}

func explainAlterQuery(sb *strings.Builder, n *ast.AlterQuery, indent string, depth int) {
if n == nil {
fmt.Fprintf(sb, "%s*ast.AlterQuery\n", indent)
return
}

name := n.Table
if n.Database != "" {
name = n.Database + "." + n.Table
}

children := 2
fmt.Fprintf(sb, "%sAlterQuery %s (children %d)\n", indent, name, children)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Commands))
for _, cmd := range n.Commands {
explainAlterCommand(sb, cmd, indent+" ", depth+2)
}
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
}

func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent string, depth int) {
children := countAlterCommandChildren(cmd)
fmt.Fprintf(sb, "%sAlterCommand %s (children %d)\n", indent, cmd.Type, children)

switch cmd.Type {
case ast.AlterAddColumn:
if cmd.Column != nil {
Column(sb, cmd.Column, depth+1)
}
if cmd.AfterColumn != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.AfterColumn)
}
case ast.AlterModifyColumn:
if cmd.Column != nil {
Column(sb, cmd.Column, depth+1)
}
if cmd.AfterColumn != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.AfterColumn)
}
case ast.AlterDropColumn:
if cmd.ColumnName != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ColumnName)
}
case ast.AlterRenameColumn:
if cmd.ColumnName != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ColumnName)
}
if cmd.NewName != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.NewName)
}
case ast.AlterClearColumn:
if cmd.ColumnName != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ColumnName)
}
if cmd.Partition != nil {
Node(sb, cmd.Partition, depth+1)
}
case ast.AlterCommentColumn:
if cmd.ColumnName != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ColumnName)
}
case ast.AlterAddIndex, ast.AlterDropIndex, ast.AlterClearIndex, ast.AlterMaterializeIndex:
if cmd.Index != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.Index)
}
case ast.AlterAddConstraint:
if cmd.Constraint != nil {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.Constraint.Name)
}
case ast.AlterDropConstraint:
if cmd.ConstraintName != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ConstraintName)
}
case ast.AlterModifyTTL:
if cmd.TTL != nil && cmd.TTL.Expression != nil {
Node(sb, cmd.TTL.Expression, depth+1)
}
case ast.AlterModifySetting:
fmt.Fprintf(sb, "%s Set\n", indent)
case ast.AlterDropPartition, ast.AlterDetachPartition, ast.AlterAttachPartition,
ast.AlterReplacePartition, ast.AlterFreezePartition:
if cmd.Partition != nil {
Node(sb, cmd.Partition, depth+1)
}
case ast.AlterFreeze:
// No children
case ast.AlterDeleteWhere:
if cmd.Where != nil {
Node(sb, cmd.Where, depth+1)
}
case ast.AlterUpdate:
if len(cmd.Assignments) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(cmd.Assignments))
for _, assign := range cmd.Assignments {
fmt.Fprintf(sb, "%s Function equals (children 1)\n", indent)
fmt.Fprintf(sb, "%s ExpressionList (children 2)\n", indent)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, assign.Column)
Node(sb, assign.Value, depth+4)
}
}
if cmd.Where != nil {
Node(sb, cmd.Where, depth+1)
}
default:
if cmd.Partition != nil {
Node(sb, cmd.Partition, depth+1)
}
}
}

func countAlterCommandChildren(cmd *ast.AlterCommand) int {
children := 0
switch cmd.Type {
case ast.AlterAddColumn, ast.AlterModifyColumn:
if cmd.Column != nil {
children++
}
if cmd.AfterColumn != "" {
children++
}
case ast.AlterDropColumn, ast.AlterCommentColumn:
if cmd.ColumnName != "" {
children++
}
case ast.AlterRenameColumn:
if cmd.ColumnName != "" {
children++
}
if cmd.NewName != "" {
children++
}
case ast.AlterClearColumn:
if cmd.ColumnName != "" {
children++
}
if cmd.Partition != nil {
children++
}
case ast.AlterAddIndex, ast.AlterDropIndex, ast.AlterClearIndex, ast.AlterMaterializeIndex:
if cmd.Index != "" {
children++
}
case ast.AlterAddConstraint:
if cmd.Constraint != nil {
children++
}
case ast.AlterDropConstraint:
if cmd.ConstraintName != "" {
children++
}
case ast.AlterModifyTTL:
if cmd.TTL != nil && cmd.TTL.Expression != nil {
children++
}
case ast.AlterModifySetting:
children = 1
case ast.AlterDropPartition, ast.AlterDetachPartition, ast.AlterAttachPartition,
ast.AlterReplacePartition, ast.AlterFreezePartition:
if cmd.Partition != nil {
children++
}
case ast.AlterFreeze:
// No children
case ast.AlterDeleteWhere:
if cmd.Where != nil {
children++
}
case ast.AlterUpdate:
if len(cmd.Assignments) > 0 {
children++
}
if cmd.Where != nil {
children++
}
default:
if cmd.Partition != nil {
children++
}
}
return children
}

func explainOptimizeQuery(sb *strings.Builder, n *ast.OptimizeQuery, indent string) {
if n == nil {
fmt.Fprintf(sb, "%s*ast.OptimizeQuery\n", indent)
return
}

name := n.Table
if n.Final {
name += "_final"
}

fmt.Fprintf(sb, "%sOptimizeQuery %s (children %d)\n", indent, name, 1)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
}

func explainTruncateQuery(sb *strings.Builder, n *ast.TruncateQuery, indent string) {
if n == nil {
fmt.Fprintf(sb, "%s*ast.TruncateQuery\n", indent)
return
}

name := n.Table
if n.Database != "" {
name = n.Database + "." + n.Table
}

fmt.Fprintf(sb, "%sTruncateQuery %s (children %d)\n", indent, name, 1)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
}
10 changes: 0 additions & 10 deletions parser/testdata/00030_alter_table/metadata.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
{
"explain_todo": {
"stmt10": true,
"stmt12": true,
"stmt13": true,
"stmt14": true,
"stmt15": true,
"stmt16": true,
"stmt17": true,
"stmt18": true,
"stmt19": true,
"stmt20": true,
"stmt21": true,
"stmt22": true,
"stmt5": true,
"stmt6": true,
"stmt7": true,
"stmt8": true,
"stmt9": true
}
}
10 changes: 0 additions & 10 deletions parser/testdata/00041_aggregating_materialized_view/metadata.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"explain_todo": {
"stmt10": true,
"stmt11": true,
"stmt12": true,
"stmt13": true,
"stmt14": true,
"stmt15": true,
"stmt16": true,
"stmt17": true,
"stmt18": true,
"stmt19": true,
"stmt5": true
}
}
2 changes: 1 addition & 1 deletion parser/testdata/00043_summing_empty_part/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"explain_todo":{"stmt11":true,"stmt6":true}}
{}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"explain_todo": {
"stmt10": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"explain_todo": {
"stmt10": true
}
}
{}
9 changes: 1 addition & 8 deletions parser/testdata/00061_merge_tree_alter/metadata.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
{
"explain_todo": {
"stmt11": true,
"stmt13": true,
"stmt16": true,
"stmt18": true,
"stmt21": true,
"stmt23": true,
"stmt25": true,
"stmt27": true,
"stmt29": true,
"stmt31": true,
"stmt33": true,
"stmt35": true,
"stmt37": true,
"stmt39": true,
"stmt41": true,
"stmt43": true,
"stmt45": true,
"stmt47": true,
"stmt6": true,
"stmt8": true
"stmt6": true
}
}
3 changes: 1 addition & 2 deletions parser/testdata/00061_storage_buffer/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"explain_todo": {
"stmt3": true,
"stmt8": true
"stmt3": true
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
{
"explain_todo": {
"stmt11": true,
"stmt13": true,
"stmt16": true,
"stmt18": true,
"stmt20": true,
"stmt23": true,
"stmt25": true,
"stmt27": true,
"stmt30": true,
"stmt32": true,
"stmt34": true,
"stmt37": true,
"stmt39": true,
"stmt41": true,
"stmt43": true,
"stmt45": true,
"stmt47": true,
"stmt49": true,
"stmt51": true,
"stmt53": true,
Expand All @@ -28,10 +22,8 @@
"stmt65": true,
"stmt67": true,
"stmt69": true,
"stmt71": true,
"stmt73": true,
"stmt75": true,
"stmt77": true,
"stmt79": true,
"stmt81": true,
"stmt9": true
Expand Down
3 changes: 1 addition & 2 deletions parser/testdata/00071_merge_tree_optimize_aio/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"explain_todo": {
"stmt6": true,
"stmt7": true,
"stmt9": true
"stmt7": true
}
}
10 changes: 1 addition & 9 deletions parser/testdata/00079_defaulted_columns/metadata.json
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
{
"explain_todo": {
"stmt16": true,
"stmt22": true,
"stmt25": true,
"stmt28": true,
"stmt30": true
}
}
{}
6 changes: 5 additions & 1 deletion parser/testdata/00084_summing_merge_tree/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"explain_todo":{"stmt16":true,"stmt17":true,"stmt18":true,"stmt25":true,"stmt7":true,"stmt8":true,"stmt9":true}}
{
"explain_todo": {
"stmt25": true
}
}
8 changes: 1 addition & 7 deletions parser/testdata/00121_drop_column_zookeeper/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{
"explain_todo": {
"stmt10": true,
"stmt13": true,
"stmt5": true
}
}
{}
2 changes: 1 addition & 1 deletion parser/testdata/00140_prewhere_column_order/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"explain_todo":{"stmt5":true}}
{}
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{
"explain_todo": {
"stmt12": true,
"stmt19": true,
"stmt6": true
}
}
{}
Loading