Skip to content

Commit 0ccea21

Browse files
committed
Add alias support for ArrayAccess and BetweenExpr in WITH clauses
- Add explainBetweenExprWithAlias to support aliases on BETWEEN expressions - Add ArrayAccess and BetweenExpr cases to explainWithElement - Enables WITH expr AS name syntax for array subscripts and BETWEEN clauses
1 parent 20a7593 commit 0ccea21

5 files changed

Lines changed: 49 additions & 16 deletions

File tree

internal/explain/expressions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ func explainWithElement(sb *strings.Builder, n *ast.WithElement, indent string,
10601060
}
10611061
case *ast.CastExpr:
10621062
explainCastExprWithAlias(sb, e, n.Name, indent, depth)
1063+
case *ast.ArrayAccess:
1064+
explainArrayAccessWithAlias(sb, e, n.Name, indent, depth)
1065+
case *ast.BetweenExpr:
1066+
explainBetweenExprWithAlias(sb, e, n.Name, indent, depth)
10631067
default:
10641068
// For other types, just output the expression (alias may be lost)
10651069
Node(sb, n.Query, depth)

internal/explain/functions.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,48 @@ func explainBetweenExpr(sb *strings.Builder, n *ast.BetweenExpr, indent string,
14421442
}
14431443
}
14441444

1445+
func explainBetweenExprWithAlias(sb *strings.Builder, n *ast.BetweenExpr, alias string, indent string, depth int) {
1446+
if n.Not {
1447+
// NOT BETWEEN is transformed to: expr < low OR expr > high
1448+
// Represented as: Function or with two comparisons: less and greater
1449+
if alias != "" {
1450+
fmt.Fprintf(sb, "%sFunction or (alias %s) (children %d)\n", indent, alias, 1)
1451+
} else {
1452+
fmt.Fprintf(sb, "%sFunction or (children %d)\n", indent, 1)
1453+
}
1454+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
1455+
// less(expr, low)
1456+
fmt.Fprintf(sb, "%s Function less (children %d)\n", indent, 1)
1457+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
1458+
Node(sb, n.Expr, depth+4)
1459+
Node(sb, n.Low, depth+4)
1460+
// greater(expr, high)
1461+
fmt.Fprintf(sb, "%s Function greater (children %d)\n", indent, 1)
1462+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
1463+
Node(sb, n.Expr, depth+4)
1464+
Node(sb, n.High, depth+4)
1465+
} else {
1466+
// BETWEEN is represented as Function and with two comparisons
1467+
// expr >= low AND expr <= high
1468+
if alias != "" {
1469+
fmt.Fprintf(sb, "%sFunction and (alias %s) (children %d)\n", indent, alias, 1)
1470+
} else {
1471+
fmt.Fprintf(sb, "%sFunction and (children %d)\n", indent, 1)
1472+
}
1473+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
1474+
// greaterOrEquals(expr, low)
1475+
fmt.Fprintf(sb, "%s Function greaterOrEquals (children %d)\n", indent, 1)
1476+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
1477+
Node(sb, n.Expr, depth+4)
1478+
Node(sb, n.Low, depth+4)
1479+
// lessOrEquals(expr, high)
1480+
fmt.Fprintf(sb, "%s Function lessOrEquals (children %d)\n", indent, 1)
1481+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
1482+
Node(sb, n.Expr, depth+4)
1483+
Node(sb, n.High, depth+4)
1484+
}
1485+
}
1486+
14451487
func explainIsNullExpr(sb *strings.Builder, n *ast.IsNullExpr, indent string, depth int) {
14461488
// IS NULL is represented as Function isNull
14471489
fnName := "isNull"
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt13": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt8": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt3": true,
4-
"stmt9": true
5-
}
6-
}
1+
{}

0 commit comments

Comments
 (0)