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
26 changes: 26 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ type CreateQuery struct {
OrderByHasModifiers bool `json:"order_by_has_modifiers,omitempty"` // True if ORDER BY has ASC/DESC modifiers
PartitionBy Expression `json:"partition_by,omitempty"`
PrimaryKey []Expression `json:"primary_key,omitempty"`
UniqueKey []Expression `json:"unique_key,omitempty"` // UNIQUE KEY
SampleBy Expression `json:"sample_by,omitempty"`
TTL *TTLClause `json:"ttl,omitempty"`
Settings []*SettingExpr `json:"settings,omitempty"`
Expand Down Expand Up @@ -1197,6 +1198,7 @@ func (s *ShowCreateSettingsProfileQuery) statementNode() {}
type CreateRowPolicyQuery struct {
Position token.Position `json:"-"`
IsAlter bool `json:"is_alter,omitempty"`
Masking bool `json:"masking,omitempty"` // MASKING POLICY
}

func (c *CreateRowPolicyQuery) Pos() token.Position { return c.Position }
Expand All @@ -1207,6 +1209,7 @@ func (c *CreateRowPolicyQuery) statementNode() {}
type DropRowPolicyQuery struct {
Position token.Position `json:"-"`
IfExists bool `json:"if_exists,omitempty"`
Masking bool `json:"masking,omitempty"` // MASKING POLICY
}

func (d *DropRowPolicyQuery) Pos() token.Position { return d.Position }
Expand All @@ -1217,6 +1220,7 @@ func (d *DropRowPolicyQuery) statementNode() {}
type ShowCreateRowPolicyQuery struct {
Position token.Position `json:"-"`
Format string `json:"format,omitempty"`
Masking bool `json:"masking,omitempty"` // MASKING POLICY
}

func (s *ShowCreateRowPolicyQuery) Pos() token.Position { return s.Position }
Expand Down Expand Up @@ -1810,3 +1814,25 @@ type ParallelWithQuery struct {
func (p *ParallelWithQuery) Pos() token.Position { return p.Position }
func (p *ParallelWithQuery) End() token.Position { return p.Position }
func (p *ParallelWithQuery) statementNode() {}

// ExecuteAsQuery represents EXECUTE AS user [statement].
type ExecuteAsQuery struct {
Position token.Position `json:"-"`
User Expression `json:"user"`
Statement Statement `json:"statement,omitempty"`
}

func (e *ExecuteAsQuery) Pos() token.Position { return e.Position }
func (e *ExecuteAsQuery) End() token.Position { return e.Position }
func (e *ExecuteAsQuery) statementNode() {}

// ShowAccessEntitiesQuery represents SHOW USERS, SHOW ROLES, SHOW [ROW] POLICIES, etc.
type ShowAccessEntitiesQuery struct {
Position token.Position `json:"-"`
Keyword string `json:"keyword"` // e.g. "USERS", "ROW POLICIES"
Format string `json:"format,omitempty"`
}

func (s *ShowAccessEntitiesQuery) Pos() token.Position { return s.Position }
func (s *ShowAccessEntitiesQuery) End() token.Position { return s.Position }
func (s *ShowAccessEntitiesQuery) statementNode() {}
39 changes: 37 additions & 2 deletions internal/explain/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
fmt.Fprintf(sb, "%sASTTransactionControl\n", indent)
case *ast.ExplainQuery:
explainExplainQuery(sb, n, indent, depth)
case *ast.ExecuteAsQuery:
children := 1
if n.Statement != nil {
children++
}
fmt.Fprintf(sb, "%sExecuteAsQuery (children %d)\n", indent, children)
fmt.Fprintf(sb, "%s UserNameWithHost (children 1)\n", indent)
Node(sb, n.User, depth+2)
if n.Statement != nil {
Node(sb, n.Statement, depth+1)
}
case *ast.ShowQuery:
explainShowQuery(sb, n, indent)
case *ast.ShowPrivilegesQuery:
Expand Down Expand Up @@ -253,10 +264,34 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
fmt.Fprintf(sb, "%s%s\n", indent, queryName)
}
case *ast.CreateRowPolicyQuery:
fmt.Fprintf(sb, "%sCREATE ROW POLICY or ALTER ROW POLICY query\n", indent)
if n.Masking {
fmt.Fprintf(sb, "%sCREATE MASKING POLICY or ALTER MASKING POLICY query\n", indent)
} else {
fmt.Fprintf(sb, "%sCREATE ROW POLICY or ALTER ROW POLICY query\n", indent)
}
case *ast.DropRowPolicyQuery:
fmt.Fprintf(sb, "%sDROP ROW POLICY query\n", indent)
if n.Masking {
fmt.Fprintf(sb, "%sDROP MASKING POLICY query\n", indent)
} else {
fmt.Fprintf(sb, "%sDROP ROW POLICY query\n", indent)
}
case *ast.ShowAccessEntitiesQuery:
if n.Format != "" {
fmt.Fprintf(sb, "%sSHOW %s query (children 1)\n", indent, n.Keyword)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
} else {
fmt.Fprintf(sb, "%sSHOW %s query\n", indent, n.Keyword)
}
case *ast.ShowCreateRowPolicyQuery:
if n.Masking {
if n.Format != "" {
fmt.Fprintf(sb, "%sSHOW CREATE MASKING POLICIES query (children 1)\n", indent)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
} else {
fmt.Fprintf(sb, "%sSHOW CREATE MASKING POLICIES query\n", indent)
}
break
}
// ClickHouse uses "ROW POLICIES" (plural) when FORMAT is present
if n.Format != "" {
fmt.Fprintf(sb, "%sSHOW CREATE ROW POLICIES query (children 1)\n", indent)
Expand Down
112 changes: 83 additions & 29 deletions internal/explain/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
return
}

n = moveInlinePrimaryKeyToStorage(n)
name := n.Table
if n.View != "" {
name = n.View
Expand Down Expand Up @@ -227,7 +228,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
settingsInStorage := len(n.Settings) > 0 && (n.Comment == "" || n.SettingsBeforeComment)
// For WINDOW VIEW with INNER ENGINE, ORDER BY goes inside ViewTargets, not in regular Storage definition
orderByInRegularStorage := len(n.OrderBy) > 0 && !(n.WindowView && n.InnerEngine != nil)
hasStorageChild := n.Engine != nil || orderByInRegularStorage || len(n.PrimaryKey) > 0 || n.PartitionBy != nil || n.SampleBy != nil || n.TTL != nil || settingsInStorage || len(n.ColumnsPrimaryKey) > 0 || hasColumnPrimaryKey
hasStorageChild := n.Engine != nil || orderByInRegularStorage || len(n.PrimaryKey) > 0 || len(n.UniqueKey) > 0 || n.PartitionBy != nil || n.SampleBy != nil || n.TTL != nil || settingsInStorage || len(n.ColumnsPrimaryKey) > 0 || hasColumnPrimaryKey
if hasStorageChild {
children++
}
Expand Down Expand Up @@ -388,7 +389,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
}
// For WINDOW VIEW with INNER ENGINE, ORDER BY goes inside ViewTargets
hasOrderByInStorage := len(n.OrderBy) > 0 && !(n.WindowView && n.InnerEngine != nil)
hasStorage := n.Engine != nil || hasOrderByInStorage || len(n.PrimaryKey) > 0 || n.PartitionBy != nil || n.SampleBy != nil || n.TTL != nil || settingsInStorage || len(n.ColumnsPrimaryKey) > 0 || hasColumnPrimaryKey
hasStorage := n.Engine != nil || hasOrderByInStorage || len(n.PrimaryKey) > 0 || len(n.UniqueKey) > 0 || n.PartitionBy != nil || n.SampleBy != nil || n.TTL != nil || settingsInStorage || len(n.ColumnsPrimaryKey) > 0 || hasColumnPrimaryKey
if hasStorage {
storageChildren := 0
if n.Engine != nil {
Expand All @@ -403,6 +404,9 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
if len(n.PrimaryKey) > 0 {
storageChildren++
}
if len(n.UniqueKey) > 0 {
storageChildren++
}
// SAMPLE BY is always shown in EXPLAIN AST when present
if n.SampleBy != nil {
storageChildren++
Expand Down Expand Up @@ -453,33 +457,7 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
}
}
// PRIMARY KEY comes before ORDER BY in EXPLAIN output
if len(n.PrimaryKey) > 0 {
if len(n.PrimaryKey) == 1 {
if ident, ok := n.PrimaryKey[0].(*ast.Identifier); ok {
fmt.Fprintf(sb, "%s Identifier %s\n", storageIndent, ident.Name())
} else if lit, ok := n.PrimaryKey[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
// Handle tuple literal (including empty tuple from PRIMARY KEY ())
exprs, _ := lit.Value.([]ast.Expression)
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", storageIndent, 1)
if len(exprs) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", storageIndent, len(exprs))
for _, e := range exprs {
Node(sb, e, storageChildDepth+2)
}
} else {
fmt.Fprintf(sb, "%s ExpressionList\n", storageIndent)
}
} else {
Node(sb, n.PrimaryKey[0], storageChildDepth)
}
} else {
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", storageIndent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", storageIndent, len(n.PrimaryKey))
for _, p := range n.PrimaryKey {
Node(sb, p, storageChildDepth+2)
}
}
}
explainStorageKey(sb, n.PrimaryKey, storageIndent, storageChildDepth)
// ORDER BY comes after PRIMARY KEY in EXPLAIN output
if len(n.OrderBy) > 0 {
if len(n.OrderBy) == 1 {
Expand Down Expand Up @@ -521,6 +499,8 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
}
}
}
// UNIQUE KEY comes between ORDER BY and SAMPLE BY
explainStorageKey(sb, n.UniqueKey, storageIndent, storageChildDepth)
// SAMPLE BY is always shown in EXPLAIN AST when present
if n.SampleBy != nil {
Node(sb, n.SampleBy, storageChildDepth)
Expand Down Expand Up @@ -2718,3 +2698,77 @@ func explainAlterPartition(sb *strings.Builder, cmd *ast.AlterCommand, indent st
fmt.Fprintf(sb, "%s Partition (children 1)\n", indent)
Node(sb, cmd.Partition, depth+2)
}

// explainStorageKey prints a PRIMARY KEY or UNIQUE KEY storage expression.
func explainStorageKey(sb *strings.Builder, keys []ast.Expression, storageIndent string, storageChildDepth int) {
if len(keys) > 0 {
if len(keys) == 1 {
if ident, ok := keys[0].(*ast.Identifier); ok {
fmt.Fprintf(sb, "%s Identifier %s\n", storageIndent, ident.Name())
} else if lit, ok := keys[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
// Handle tuple literal (including empty tuple from PRIMARY KEY ())
exprs, _ := lit.Value.([]ast.Expression)
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", storageIndent, 1)
if len(exprs) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", storageIndent, len(exprs))
for _, e := range exprs {
Node(sb, e, storageChildDepth+2)
}
} else {
fmt.Fprintf(sb, "%s ExpressionList\n", storageIndent)
}
} else {
Node(sb, keys[0], storageChildDepth)
}
} else {
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", storageIndent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", storageIndent, len(keys))
for _, k := range keys {
Node(sb, k, storageChildDepth+2)
}
}
}
}

// moveInlinePrimaryKeyToStorage returns n with a PRIMARY KEY declared in the
// column list ("a T PRIMARY KEY" or "PRIMARY KEY (a, b)") moved to the storage
// PRIMARY KEY, as ClickHouse 26 does.
func moveInlinePrimaryKeyToStorage(n *ast.CreateQuery) *ast.CreateQuery {
if len(n.PrimaryKey) > 0 {
return n
}
var pk []ast.Expression
var cols []string
for _, col := range n.Columns {
if col.PrimaryKey {
cols = append(cols, col.Name)
}
}
switch {
case len(cols) > 0:
exprs := make([]ast.Expression, len(cols))
for i, c := range cols {
exprs[i] = &ast.Identifier{Parts: []string{c}}
}
pk = []ast.Expression{&ast.Literal{Type: ast.LiteralTuple, Value: exprs}}
case n.HasEmptyColumnsPrimaryKey:
pk = []ast.Expression{&ast.Literal{Type: ast.LiteralTuple, Value: []ast.Expression{}}}
case len(n.ColumnsPrimaryKey) == 1:
pk = n.ColumnsPrimaryKey
case len(n.ColumnsPrimaryKey) > 1:
pk = []ast.Expression{&ast.Literal{Type: ast.LiteralTuple, Value: n.ColumnsPrimaryKey}}
default:
return n
}
c := *n
c.PrimaryKey = pk
c.ColumnsPrimaryKey = nil
c.HasEmptyColumnsPrimaryKey = false
c.Columns = make([]*ast.ColumnDeclaration, len(n.Columns))
for i, col := range n.Columns {
cc := *col
cc.PrimaryKey = false
c.Columns[i] = &cc
}
return &c
}
Loading
Loading