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
14 changes: 10 additions & 4 deletions internal/explain/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,18 @@ func explainWithElement(sb *strings.Builder, n *ast.WithElement, indent string,
Node(sb, e.Right, depth+2)
}
case *ast.Subquery:
if n.Name != "" {
fmt.Fprintf(sb, "%sSubquery (alias %s) (children %d)\n", indent, n.Name, 1)
// Check if this is "(subquery) AS alias" syntax vs "name AS (subquery)" syntax
if e.Alias != "" {
// "(subquery) AS alias" syntax: output Subquery with alias directly
fmt.Fprintf(sb, "%sSubquery (alias %s) (children 1)\n", indent, e.Alias)
Node(sb, e.Query, depth+1)
} else {
fmt.Fprintf(sb, "%sSubquery (children %d)\n", indent, 1)
// "name AS (subquery)" syntax: output WithElement wrapping the Subquery
// The alias/name is not shown in the EXPLAIN AST output
fmt.Fprintf(sb, "%sWithElement (children 1)\n", indent)
fmt.Fprintf(sb, "%s Subquery (children 1)\n", indent)
Node(sb, e.Query, depth+2)
}
Node(sb, e.Query, depth+1)
case *ast.CastExpr:
explainCastExprWithAlias(sb, e, n.Name, indent, depth)
default:
Expand Down
7 changes: 5 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,22 +654,25 @@ func (p *Parser) parseWithClause() []ast.Expression {
}
} else if p.currentIs(token.LPAREN) {
// Subquery: (SELECT ...) AS name
// In this syntax, the alias goes on the Subquery, not on WithElement
p.nextToken()
subquery := p.parseSelectWithUnion()
if !p.expect(token.RPAREN) {
return nil
}
elem.Query = &ast.Subquery{Query: subquery}
sq := &ast.Subquery{Query: subquery}

if !p.expect(token.AS) {
return nil
}

// Alias can be IDENT or certain keywords (VALUES, KEY, etc.)
// Set alias on the Subquery for "(subquery) AS name" syntax
if p.currentIs(token.IDENT) || p.current.Token.IsKeyword() {
elem.Name = p.current.Value
sq.Alias = p.current.Value
p.nextToken()
}
elem.Query = sq
} else {
// Scalar WITH: expr AS name (ClickHouse style)
// Examples: WITH 1 AS x, WITH 'hello' AS s, WITH func() AS f
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt1": true
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt7": true
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt5": true
}
}
5 changes: 5 additions & 0 deletions parser/testdata/00392_enum_nested_alter/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
"stmt13": true,
"stmt15": true,
"stmt17": true,
"stmt21": true,
"stmt23": true,
"stmt24": true,
"stmt29": true,
"stmt31": true,
"stmt4": true,
"stmt6": true,
"stmt7": true,
"stmt9": true
}
}
8 changes: 7 additions & 1 deletion parser/testdata/00531_aggregate_over_nullable/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{}
{
"explain_todo": {
"stmt4": true,
"stmt5": true,
"stmt6": true
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"explain_todo": {
"stmt12": true,
"stmt13": true,
"stmt14": true,
"stmt15": true,
"stmt16": true,
"stmt7": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt1": true
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt3": true
}
}
7 changes: 6 additions & 1 deletion parser/testdata/00615_nullable_alter_optimize/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{}
{
"explain_todo": {
"stmt4": true,
"stmt7": true
}
}
7 changes: 6 additions & 1 deletion parser/testdata/00671_max_intersections/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{}
{
"explain_todo": {
"stmt5": true,
"stmt6": true
}
}
6 changes: 5 additions & 1 deletion parser/testdata/00700_decimal_complex_types/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt3": true
}
}
9 changes: 8 additions & 1 deletion parser/testdata/00927_asof_joins/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
{}
{
"explain_todo": {
"stmt4": true,
"stmt5": true,
"stmt7": true,
"stmt8": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"explain_todo": {
"stmt2": true,
"stmt28": true,
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"stmt10": true,
"stmt14": true,
"stmt15": true,
"stmt17": true,
"stmt2": true,
"stmt3": true,
"stmt4": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt1": true
}
}
3 changes: 2 additions & 1 deletion parser/testdata/01018_ddl_dictionaries_create/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"stmt51": true,
"stmt52": true,
"stmt53": true,
"stmt6": true
"stmt6": true,
"stmt7": true
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
{
"explain_todo": {
"stmt10": true,
"stmt11": true,
"stmt12": true,
"stmt13": true,
"stmt14": true,
"stmt15": true,
"stmt16": true,
"stmt17": true,
"stmt18": true,
"stmt19": true,
"stmt20": true,
"stmt21": true,
"stmt22": true,
"stmt23": true,
"stmt25": true,
"stmt3": true
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true,
"stmt7": true,
"stmt8": true,
"stmt9": true
}
}
4 changes: 3 additions & 1 deletion parser/testdata/01069_database_memory/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"explain_todo": {
"stmt4": true,
"stmt5": true
"stmt5": true,
"stmt6": true,
"stmt7": true
}
}
6 changes: 5 additions & 1 deletion parser/testdata/01104_distributed_numbers_test/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt1": true
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"explain_todo": {
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt7": true,
"stmt8": true,
Expand Down
6 changes: 5 additions & 1 deletion parser/testdata/01115_prewhere_array_join/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt8": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"stmt17": true,
"stmt20": true,
"stmt34": true,
"stmt41": true,
"stmt44": true,
"stmt47": true,
"stmt52": true,
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/01191_rename_dictionary/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true,
"stmt9": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"stmt22": true,
"stmt37": true,
"stmt4": true,
"stmt5": true,
"stmt7": true
}
}
2 changes: 2 additions & 0 deletions parser/testdata/01257_dictionary_mismatch_types/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"explain_todo": {
"stmt11": true,
"stmt12": true,
"stmt4": true,
"stmt5": true,
"stmt6": true,
"stmt9": true
}
Expand Down
13 changes: 12 additions & 1 deletion parser/testdata/01268_dictionary_direct_layout/metadata.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
{
"explain_todo": {
"stmt10": true,
"stmt11": true,
"stmt12": true,
"stmt13": true,
"stmt14": true,
"stmt15": true,
"stmt16": true,
"stmt17": true,
"stmt18": true,
"stmt19": true,
"stmt25": true,
"stmt26": true,
"stmt27": true,
"stmt6": true,
"stmt8": true
"stmt7": true,
"stmt8": true,
"stmt9": true
}
}
1 change: 1 addition & 0 deletions parser/testdata/01292_create_user/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"stmt21": true,
"stmt219": true,
"stmt22": true,
"stmt221": true,
"stmt226": true,
"stmt23": true,
"stmt234": true,
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/01293_create_role/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt11": true,
"stmt12": true,
"stmt13": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt10": true,
"stmt100": true,
"stmt101": true,
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/01295_create_row_policy/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt11": true,
"stmt12": true,
"stmt13": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt10": true,
"stmt11": true,
"stmt12": true,
Expand Down
6 changes: 5 additions & 1 deletion parser/testdata/01323_add_scalars_in_time/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt9": true
}
}
6 changes: 5 additions & 1 deletion parser/testdata/01346_array_join_mrxotey/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"explain_todo": {
"stmt3": true
}
}
1 change: 1 addition & 0 deletions parser/testdata/01358_constexpr_constraint/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt4": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"explain_todo": {
"stmt11": true,
"stmt6": true,
"stmt7": true,
"stmt8": true
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
{"explain_todo":{"stmt3":true,"stmt4":true}}
{
"explain_todo": {
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true
}
}
1 change: 1 addition & 0 deletions parser/testdata/01418_custom_settings/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt41": true,
"stmt42": true,
"stmt43": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"stmt11": true,
"stmt14": true,
"stmt15": true,
"stmt18": true,
"stmt21": true,
"stmt5": true,
"stmt7": true,
"stmt9": true
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/01499_json_named_tuples/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"explain_todo": {
"stmt1": true,
"stmt2": true
}
}
Loading