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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ type AlterCommand struct {
Position token.Position `json:"-"`
Type AlterCommandType `json:"type"`
ExecuteArgs []Expression `json:"execute_args,omitempty"`
Refresh *CreateQuery `json:"refresh,omitempty"` // MODIFY REFRESH strategy (only the Refresh* fields are set)
Column *ColumnDeclaration `json:"column,omitempty"`
ColumnName string `json:"column_name,omitempty"`
AfterColumn string `json:"after_column,omitempty"`
Expand Down Expand Up @@ -771,6 +772,7 @@ const (
AlterModifySQLSecurity AlterCommandType = "MODIFY_SQL_SECURITY"
AlterModifySampleBy AlterCommandType = "MODIFY_SAMPLE_BY"
AlterModifyQuery AlterCommandType = "MODIFY_QUERY"
AlterModifyRefresh AlterCommandType = "MODIFY_REFRESH"
AlterRemoveSampleBy AlterCommandType = "REMOVE_SAMPLE_BY"
AlterApplyDeletedMask AlterCommandType = "APPLY_DELETED_MASK"
AlterRewriteParts AlterCommandType = "REWRITE_PARTS"
Expand Down Expand Up @@ -1818,6 +1820,10 @@ type Parameter struct {
Position token.Position `json:"-"`
Name string `json:"name,omitempty"`
Type *DataType `json:"type,omitempty"`
// Value, when set, is the literal the parameter was substituted with.
// ClickHouse substitutes parameters after parsing, so EXPLAIN prints the
// literal while the surrounding AST keeps its parameter shape.
Value *Literal `json:"value,omitempty"`
}

func (p *Parameter) Pos() token.Position { return p.Position }
Expand Down
4 changes: 4 additions & 0 deletions internal/explain/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ func explainAliasedExpr(sb *strings.Builder, n *ast.AliasedExpr, depth int) {
case *ast.BetweenExpr:
explainBetweenExprWithAlias(sb, e, n.Alias, indent, depth)
case *ast.Parameter:
if e.Value != nil {
explainLiteralWithAlias(sb, e.Value, n.Alias, indent, depth)
return
}
// QueryParameter with alias
if e.Name != "" {
if e.Type != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/explain/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func FormatFloat(val float64) string {
// EscapeIdentifier escapes single quotes in identifiers for EXPLAIN AST output
// ClickHouse escapes ' as \' in identifier names
func EscapeIdentifier(s string) string {
return strings.ReplaceAll(s, "'", "\\'")
return escapeDumpName(s)
}

// escapeStringLiteral escapes special characters in a string for EXPLAIN AST output
Expand Down
70 changes: 50 additions & 20 deletions internal/explain/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,24 +398,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
}
// Output REFRESH strategy for materialized views with REFRESH clause
if n.HasRefresh {
rc := 1
if len(n.RefreshDependsOn) > 0 {
rc++
}
if len(n.RefreshSettings) > 0 {
rc++
}
fmt.Fprintf(sb, "%s Refresh strategy definition (children %d)\n", indent, rc)
fmt.Fprintf(sb, "%s TimeInterval\n", indent)
if len(n.RefreshDependsOn) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.RefreshDependsOn))
for _, t := range n.RefreshDependsOn {
explainTableIdentifier(sb, t, indent+" ")
}
}
if len(n.RefreshSettings) > 0 {
fmt.Fprintf(sb, "%s Set\n", indent)
}
explainRefreshStrategy(sb, n, indent)
}
// COMMENT comes before the SELECT (and the view targets) for views.
commentDone := false
Expand Down Expand Up @@ -1489,6 +1472,10 @@ func explainNameTypePair(sb *strings.Builder, n *ast.NameTypePair, indent string
}

func explainParameter(sb *strings.Builder, n *ast.Parameter, indent string) {
if n.Value != nil {
Node(sb, n.Value, len(indent))
return
}
if substituteNullParams && n.Name != "" && n.Type != nil {
typ := FormatDataType(n.Type)
if strings.HasPrefix(typ, "Nullable(") || strings.HasPrefix(typ, "LowCardinality(Nullable(") {
Expand Down Expand Up @@ -1682,7 +1669,7 @@ func explainAttachQuery(sb *strings.Builder, n *ast.AttachQuery, indent string,
if len(n.Engine.Parameters) > 0 {
fmt.Fprintf(sb, "%s Function %s (children 1)\n", indent, n.Engine.Name)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Engine.Parameters))
for _, param := range n.Engine.Parameters {
for _, param := range attachEngineParams(n) {
Node(sb, param, depth+5)
}
} else {
Expand Down Expand Up @@ -1712,7 +1699,7 @@ func explainAttachQuery(sb *strings.Builder, n *ast.AttachQuery, indent string,
if len(n.Engine.Parameters) > 0 {
fmt.Fprintf(sb, "%s Function %s (children 1)\n", indent, n.Engine.Name)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Engine.Parameters))
for _, param := range n.Engine.Parameters {
for _, param := range attachEngineParams(n) {
Node(sb, param, depth+4)
}
} else {
Expand Down Expand Up @@ -2088,6 +2075,10 @@ func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent stri
if cmd.SampleByExpr != nil {
Node(sb, cmd.SampleByExpr, depth+1)
}
case ast.AlterModifyRefresh:
if cmd.Refresh != nil {
explainRefreshStrategy(sb, cmd.Refresh, strings.Repeat(" ", depth))
}
case ast.AlterModifyQuery:
// MODIFY QUERY: output the SELECT statement
if cmd.Query != nil {
Expand Down Expand Up @@ -2404,6 +2395,10 @@ func countAlterCommandChildren(cmd *ast.AlterCommand) int {
if cmd.SampleByExpr != nil {
children = 1
}
case ast.AlterModifyRefresh:
if cmd.Refresh != nil {
children = 1
}
case ast.AlterModifyQuery:
// MODIFY QUERY: SELECT statement (1 child)
if cmd.Query != nil {
Expand Down Expand Up @@ -2811,10 +2806,14 @@ func explainUpdateQuery(sb *strings.Builder, n *ast.UpdateQuery, indent string,
if len(n.Settings) > 0 {
children++
}
if n.Database != "" {
children++
}

// UpdateQuery with two spaces before table name
if n.Database != "" {
fmt.Fprintf(sb, "%sUpdateQuery %s %s (children %d)\n", indent, n.Database, n.Table, children)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
} else {
fmt.Fprintf(sb, "%sUpdateQuery %s (children %d)\n", indent, n.Table, children)
}
Expand Down Expand Up @@ -3159,3 +3158,34 @@ func viewColumnAliases(n *ast.CreateQuery) bool {
}
return true
}

// attachEngineParams returns the ATTACH query's engine parameters with
// secrets hidden, as for CREATE.
func attachEngineParams(n *ast.AttachQuery) []ast.Expression {
if n.Table == "" && n.Dictionary == "" {
return maskDatabaseEngineSecrets(n.Engine.Name, n.Engine.Parameters)
}
return maskEngineSecrets(n.Engine.Name, n.Engine.Parameters)
}

// explainRefreshStrategy prints a REFRESH clause one level below indent.
func explainRefreshStrategy(sb *strings.Builder, n *ast.CreateQuery, indent string) {
rc := 1
if len(n.RefreshDependsOn) > 0 {
rc++
}
if len(n.RefreshSettings) > 0 {
rc++
}
fmt.Fprintf(sb, "%s Refresh strategy definition (children %d)\n", indent, rc)
fmt.Fprintf(sb, "%s TimeInterval\n", indent)
if len(n.RefreshDependsOn) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.RefreshDependsOn))
for _, t := range n.RefreshDependsOn {
explainTableIdentifier(sb, t, indent+" ")
}
}
if len(n.RefreshSettings) > 0 {
fmt.Fprintf(sb, "%s Set\n", indent)
}
}
Loading
Loading