Skip to content

Commit 7baa9ea

Browse files
committed
Add YYYY support for EXTRACT and fix alias handling
- Added YYYY as an alias for YEAR in EXTRACT field parsing - ExtractExpr now properly uses its Alias field for EXPLAIN output - Added ExtractExpr case in explainAliasedExpr for alias wrapping Fixes 00619_extract test.
1 parent ec74bfb commit 7baa9ea

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

internal/explain/expressions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ func explainAliasedExpr(sb *strings.Builder, n *ast.AliasedExpr, depth int) {
529529
case *ast.Lambda:
530530
// Lambda expressions with alias
531531
explainLambdaWithAlias(sb, e, n.Alias, indent, depth)
532+
case *ast.ExtractExpr:
533+
// EXTRACT expressions with alias
534+
explainExtractExprWithAlias(sb, e, n.Alias, indent, depth)
532535
case *ast.Identifier:
533536
// Identifiers with alias
534537
fmt.Fprintf(sb, "%sIdentifier %s (alias %s)\n", indent, e.Name(), escapeAlias(n.Alias))

internal/explain/functions.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,23 @@ func explainExistsExpr(sb *strings.Builder, n *ast.ExistsExpr, indent string, de
882882
}
883883

884884
func explainExtractExpr(sb *strings.Builder, n *ast.ExtractExpr, indent string, depth int) {
885+
explainExtractExprWithAlias(sb, n, n.Alias, indent, depth)
886+
}
887+
888+
func explainExtractExprWithAlias(sb *strings.Builder, n *ast.ExtractExpr, alias string, indent string, depth int) {
885889
// EXTRACT is represented as Function toYear, toMonth, etc.
886890
// ClickHouse uses specific function names for date/time extraction
887891
fnName := extractFieldToFunction(n.Field)
888-
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
892+
// Use alias from parameter, or fall back to expression's alias
893+
effectiveAlias := alias
894+
if effectiveAlias == "" {
895+
effectiveAlias = n.Alias
896+
}
897+
if effectiveAlias != "" {
898+
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, effectiveAlias, 1)
899+
} else {
900+
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
901+
}
889902
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
890903
Node(sb, n.From, depth+2)
891904
}
@@ -897,7 +910,7 @@ func extractFieldToFunction(field string) string {
897910
return "toDayOfMonth"
898911
case "MONTH":
899912
return "toMonth"
900-
case "YEAR":
913+
case "YEAR", "YYYY":
901914
return "toYear"
902915
case "SECOND":
903916
return "toSecond"

parser/expression.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ func (p *Parser) parseExtract() ast.Expression {
12821282
field := strings.ToUpper(p.current.Value)
12831283
// Check if it's a known date/time field
12841284
dateTimeFields := map[string]bool{
1285-
"YEAR": true, "QUARTER": true, "MONTH": true, "WEEK": true,
1285+
"YEAR": true, "YYYY": true, "QUARTER": true, "MONTH": true, "WEEK": true,
12861286
"DAY": true, "DAYOFWEEK": true, "DAYOFYEAR": true,
12871287
"HOUR": true, "MINUTE": true, "SECOND": true,
12881288
"TIMEZONE_HOUR": true, "TIMEZONE_MINUTE": true,
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+
{}

parser/testdata/01095_tpch_like_smoke/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"stmt30": true,
88
"stmt32": true,
99
"stmt34": true,
10-
"stmt36": true,
1110
"stmt38": true,
1211
"stmt42": true,
1312
"stmt46": true,

0 commit comments

Comments
 (0)