Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Parse column alias lists on CTEs and table expressions
Supports WITH t (a, b) AS (SELECT ...) and FROM (...) t(a, b).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LuRZ4KB7DMX365Nmn5ksnY
  • Loading branch information
claude committed Dec 1, 2026
commit c74d55a72c434b7ddfa2634007c0edb0179cbed0
4 changes: 4 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ type TableExpression struct {
Alias string `json:"alias,omitempty"`
Final bool `json:"final,omitempty"`
Sample *SampleClause `json:"sample,omitempty"`
// ColumnAliases holds the column alias list of "t(a, b, ...)".
ColumnAliases []string `json:"column_aliases,omitempty"`
}

func (t *TableExpression) Pos() token.Position { return t.Position }
Expand Down Expand Up @@ -1609,6 +1611,8 @@ type WithElement struct {
ScalarWith bool `json:"scalar_with"` // True for "(expr) AS name" syntax, false for "name AS (SELECT ...)"
// Materialized is true for "name AS MATERIALIZED (SELECT ...)".
Materialized bool `json:"materialized,omitempty"`
// ColumnAliases holds the column alias list of "name (a, b, ...) AS (SELECT ...)".
ColumnAliases []string `json:"column_aliases,omitempty"`
}

func (w *WithElement) Pos() token.Position { return w.Position }
Expand Down
17 changes: 17 additions & 0 deletions internal/explain/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string,
if n.From != nil || n.ArrayJoin != nil {
TablesWithArrayJoin(sb, n.From, n.ArrayJoin, depth+1)
}
// CTE column aliases (WITH t (a, b) AS ...)
explainIdentifierList(sb, cteAliases(n), indent+" ")
// PREWHERE
if n.PreWhere != nil {
Node(sb, n.PreWhere, depth+1)
Expand Down Expand Up @@ -799,6 +801,9 @@ func countSelectQueryChildren(n *ast.SelectQuery) int {
if n.From != nil || n.ArrayJoin != nil {
count++
}
if len(cteAliases(n)) > 0 {
count++
}
if n.PreWhere != nil {
count++
}
Expand Down Expand Up @@ -862,3 +867,15 @@ func explainWindowListElement(sb *strings.Builder, w *ast.WindowDefinition, inde
fmt.Fprintf(sb, "%sWindowListElement (children 1)\n", indent)
explainWindowSpec(sb, w.Spec, indent+" ", depth+1)
}

// cteAliases returns the column alias list of the last WITH element that has
// one. ClickHouse keeps only that list on the SELECT query.
func cteAliases(n *ast.SelectQuery) []string {
var aliases []string
for _, w := range n.With {
if we, ok := w.(*ast.WithElement); ok && len(we.ColumnAliases) > 0 {
aliases = we.ColumnAliases
}
}
return aliases
}
15 changes: 15 additions & 0 deletions internal/explain/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func explainTableExpression(sb *strings.Builder, n *ast.TableExpression, indent
children++ // for sample offset
}
}
if len(n.ColumnAliases) > 0 {
children++
}
fmt.Fprintf(sb, "%sTableExpression (children %d)\n", indent, children)
// If there's a subquery with an alias, pass the alias to the subquery output
if subq, ok := n.Table.(*ast.Subquery); ok {
Expand Down Expand Up @@ -79,6 +82,18 @@ func explainTableExpression(sb *strings.Builder, n *ast.TableExpression, indent
if n.Sample != nil {
explainSampleClause(sb, n.Sample, indent+" ", depth+1)
}
explainIdentifierList(sb, n.ColumnAliases, indent+" ")
}

// explainIdentifierList prints names as an ExpressionList of Identifiers.
func explainIdentifierList(sb *strings.Builder, names []string, indent string) {
if len(names) == 0 {
return
}
fmt.Fprintf(sb, "%sExpressionList (children %d)\n", indent, len(names))
for _, name := range names {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
}
}

func explainSampleClause(sb *strings.Builder, n *ast.SampleClause, indent string, depth int) {
Expand Down
60 changes: 60 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,36 @@ func (p *Parser) parseWithClause() []ast.Expression {
elem.ScalarWith = true
elem.Query = p.parseExpression(ALIAS_PREC) // Use ALIAS_PREC to stop before AS

// name (a, b, ...) AS [MATERIALIZED] (subquery): a CTE with column aliases.
// The name and alias list were parsed as a function call.
if fn, ok := elem.Query.(*ast.FunctionCall); ok && p.currentIs(token.AS) &&
(p.peekIs(token.LPAREN) || p.peekIs(token.MATERIALIZED)) && len(fn.Parameters) == 0 && fn.Alias == "" {
if aliases, ok := identifierNames(fn.Arguments); ok {
p.nextToken() // skip AS
if p.currentIs(token.MATERIALIZED) {
elem.Materialized = true
p.nextToken()
}
if !p.expect(token.LPAREN) {
return nil
}
subquery := p.parseSelectWithUnion()
if !p.expect(token.RPAREN) {
return nil
}
elem.ScalarWith = false
elem.Name = fn.Name
elem.ColumnAliases = aliases
elem.Query = &ast.Subquery{Query: subquery}
elements = append(elements, elem)
if !p.currentIs(token.COMMA) {
break
}
p.nextToken()
continue
}
}

// AS name is optional
if p.currentIs(token.AS) {
p.nextToken()
Expand Down Expand Up @@ -1752,6 +1782,19 @@ func (p *Parser) parseTableExpression() *ast.TableExpression {
p.nextToken()
}

// Column aliases: t(a, b, ...)
if expr.Alias != "" && p.currentIs(token.LPAREN) {
p.nextToken()
for !p.currentIs(token.RPAREN) && !p.currentIs(token.EOF) {
expr.ColumnAliases = append(expr.ColumnAliases, p.parseIdentifierName())
if !p.currentIs(token.COMMA) {
break
}
p.nextToken()
}
p.expect(token.RPAREN)
}

// Handle FINAL (after alias)
if p.currentIs(token.FINAL) {
expr.Final = true
Expand Down Expand Up @@ -8623,3 +8666,20 @@ func (p *Parser) parseAlterInPartition(cmd *ast.AlterCommand) {
}
cmd.Partition = p.parseExpression(LOWEST)
}

// identifierNames returns the names of exprs if every one is a plain,
// unaliased single-part identifier.
func identifierNames(exprs []ast.Expression) ([]string, bool) {
if len(exprs) == 0 {
return nil, false
}
names := make([]string, 0, len(exprs))
for _, e := range exprs {
ident, ok := e.(*ast.Identifier)
if !ok || len(ident.Parts) != 1 || ident.Alias != "" {
return nil, false
}
names = append(names, ident.Parts[0])
}
return names, true
}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"explain_todo": {
"stmt3": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
{
"explain_todo": {
"stmt10": true,
"stmt11": true,
"stmt2": true,
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true,
"stmt7": true,
"stmt8": true,
"stmt9": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"explain_todo": {
"stmt2": true,
"stmt4": true,
"stmt6": true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
{
"explain_todo": {
"stmt2": true,
"stmt3": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
{
"explain_todo": {
"stmt2": true,
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true,
"stmt7": true,
"stmt8": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,57 +1 @@
{
"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,
"stmt24": true,
"stmt25": true,
"stmt26": true,
"stmt27": true,
"stmt28": true,
"stmt29": true,
"stmt3": true,
"stmt30": true,
"stmt31": true,
"stmt32": true,
"stmt33": true,
"stmt37": true,
"stmt38": true,
"stmt4": true,
"stmt40": true,
"stmt41": true,
"stmt42": true,
"stmt43": true,
"stmt44": true,
"stmt46": true,
"stmt47": true,
"stmt48": true,
"stmt49": true,
"stmt5": true,
"stmt51": true,
"stmt52": true,
"stmt53": true,
"stmt54": true,
"stmt55": true,
"stmt56": true,
"stmt57": true,
"stmt58": true,
"stmt59": true,
"stmt6": true,
"stmt60": true,
"stmt62": true,
"stmt7": true,
"stmt8": true,
"stmt9": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
{
"explain_todo": {
"stmt10": true,
"stmt11": true,
"stmt13": true,
"stmt3": true,
"stmt4": true,
"stmt5": true,
"stmt6": true,
"stmt8": true,
"stmt9": true
}
}
{}
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
{
"explain_todo": {
"stmt10": true,
"stmt11": true,
"stmt12": true,
"stmt13": true,
"stmt14": true,
"stmt16": true,
"stmt17": true,
"stmt18": true,
"stmt19": true,
"stmt20": true,
"stmt21": true,
"stmt22": true,
"stmt23": true,
"stmt24": true,
"stmt25": true,
"stmt26": true,
"stmt38": true,
"stmt39": true,
"stmt40": true,
"stmt43": true,
"stmt44": true,
"stmt45": true,
"stmt46": true,
"stmt47": true,
"stmt49": true,
"stmt5": true,
"stmt6": true,
"stmt7": true,
"stmt8": true,
"stmt9": true
}
}
{}