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
2 changes: 2 additions & 0 deletions ast/cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type CommonTableExpression struct {
ExpressionName *Identifier `json:"ExpressionName,omitempty"`
Columns []*Identifier `json:"Columns,omitempty"`
QueryExpression QueryExpression `json:"QueryExpression,omitempty"`
// WithCtesAndXmlNamespaces holds CTEs nested inside this CTE's body.
WithCtesAndXmlNamespaces *WithCtesAndXmlNamespaces `json:"WithCtesAndXmlNamespaces,omitempty"`
}

func (c *CommonTableExpression) node() {}
1 change: 1 addition & 0 deletions ast/qualified_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type QualifiedJoin struct {
SearchCondition BooleanExpression `json:"SearchCondition,omitempty"`
QualifiedJoinType string `json:"QualifiedJoinType,omitempty"`
JoinHint string `json:"JoinHint,omitempty"`
JoinHintColumnsCount *IntegerLiteral `json:"JoinHintColumnsCount,omitempty"`
FirstTableReference TableReference `json:"FirstTableReference,omitempty"`
SecondTableReference TableReference `json:"SecondTableReference,omitempty"`
}
Expand Down
6 changes: 6 additions & 0 deletions parser/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,9 @@ func tableReferenceToJSON(ref ast.TableReference) jsonNode {
if r.QualifiedJoinType != "" {
node["QualifiedJoinType"] = r.QualifiedJoinType
}
if r.JoinHintColumnsCount != nil {
node["JoinHintColumnsCount"] = scalarExpressionToJSON(r.JoinHintColumnsCount)
}
if r.JoinHint != "" {
node["JoinHint"] = r.JoinHint
} else {
Expand Down Expand Up @@ -4697,6 +4700,9 @@ func commonTableExpressionToJSON(cte *ast.CommonTableExpression) jsonNode {
if cte.QueryExpression != nil {
node["QueryExpression"] = queryExpressionToJSON(cte.QueryExpression)
}
if cte.WithCtesAndXmlNamespaces != nil {
node["WithCtesAndXmlNamespaces"] = withCtesAndXmlNamespacesToJSON(cte.WithCtesAndXmlNamespaces)
}
return addSpan(node, frag(cte))
}

Expand Down
245 changes: 134 additions & 111 deletions parser/parse_dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,119 +11,11 @@ import (
func (p *Parser) parseWithStatement() (ast.Statement, error) {
astStart := p.curTok

// Consume WITH
p.nextToken()

withClause := &ast.WithCtesAndXmlNamespaces{}

// Parse XMLNAMESPACES, CHANGE_TRACKING_CONTEXT or CTEs
for {
if strings.ToUpper(p.curTok.Literal) == "XMLNAMESPACES" {
p.nextToken() // consume XMLNAMESPACES
xmlNs := &ast.XmlNamespaces{}
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
// ScriptDom spans XMLNAMESPACES from the first element
// through the closing paren.
firstElemTok := p.curTok
for p.curTok.Type != TokenRParen && p.curTok.Type != TokenEOF {
elemTok := p.curTok
// Check for DEFAULT element
if strings.ToUpper(p.curTok.Literal) == "DEFAULT" {
p.nextToken() // consume DEFAULT
strLit, _ := p.parseStringLiteral()
elem := &ast.XmlNamespacesDefaultElement{String: strLit}
p.spanFrom(elemTok, elem)
xmlNs.XmlNamespacesElements = append(xmlNs.XmlNamespacesElements, elem)
} else {
// Alias element: string AS identifier
strLit, _ := p.parseStringLiteral()
elem := &ast.XmlNamespacesAliasElement{String: strLit}
if p.curTok.Type == TokenAs {
p.nextToken() // consume AS
elem.Identifier = p.parseIdentifier()
}
p.spanFrom(elemTok, elem)
xmlNs.XmlNamespacesElements = append(xmlNs.XmlNamespacesElements, elem)
}
if p.curTok.Type == TokenComma {
p.nextToken()
} else {
break
}
}
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
p.spanFrom(firstElemTok, xmlNs)
}
withClause.XmlNamespaces = xmlNs
} else if strings.ToUpper(p.curTok.Literal) == "CHANGE_TRACKING_CONTEXT" {
p.nextToken() // consume CHANGE_TRACKING_CONTEXT
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
expr, _ := p.parseScalarExpression()
withClause.ChangeTrackingContext = expr
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
}
} else if p.curTok.Type == TokenIdent || p.curTok.Type == TokenLBracket {
// Parse CTE: name (columns) AS (query)
cteTok := p.curTok
cte := &ast.CommonTableExpression{
ExpressionName: p.parseIdentifier(),
}

// Parse optional column list
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
for p.curTok.Type != TokenRParen && p.curTok.Type != TokenEOF {
cte.Columns = append(cte.Columns, p.parseIdentifier())
if p.curTok.Type == TokenComma {
p.nextToken()
}
}
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
}

// Expect AS
if p.curTok.Type == TokenAs {
p.nextToken() // consume AS
}

// Parse query in parentheses
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
queryExpr, err := p.parseQueryExpression()
if err != nil {
return nil, err
}
cte.QueryExpression = queryExpr
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
}

p.spanFrom(cteTok, cte)
withClause.CommonTableExpressions = append(withClause.CommonTableExpressions, cte)
} else {
break
}

// Check for comma (more CTEs or XMLNAMESPACES followed by CTEs)
if p.curTok.Type == TokenComma {
p.nextToken()
} else {
break
}
withClause, err := p.parseWithCtesAndXmlNamespaces()
if err != nil {
return nil, err
}

// The WITH clause spans from the WITH keyword through the last CTE.
p.spanFrom(astStart, withClause)

// Now dispatch to the appropriate statement parser
switch p.curTok.Type {
case TokenInsert:
Expand Down Expand Up @@ -3381,3 +3273,134 @@ func (p *Parser) parseCopyOption() (*ast.CopyOption, error) {
}
return spanned(p, opt, astStart), nil
}

// parseWithCtesAndXmlNamespaces parses a WITH clause (XMLNAMESPACES,
// CHANGE_TRACKING_CONTEXT and common table expressions). The current token
// must be WITH.
func (p *Parser) parseWithCtesAndXmlNamespaces() (*ast.WithCtesAndXmlNamespaces, error) {
astStart := p.curTok

// Consume WITH
p.nextToken()

withClause := &ast.WithCtesAndXmlNamespaces{}

// Parse XMLNAMESPACES, CHANGE_TRACKING_CONTEXT or CTEs
for {
if strings.ToUpper(p.curTok.Literal) == "XMLNAMESPACES" {
p.nextToken() // consume XMLNAMESPACES
xmlNs := &ast.XmlNamespaces{}
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
// ScriptDom spans XMLNAMESPACES from the first element
// through the closing paren.
firstElemTok := p.curTok
for p.curTok.Type != TokenRParen && p.curTok.Type != TokenEOF {
elemTok := p.curTok
// Check for DEFAULT element
if strings.ToUpper(p.curTok.Literal) == "DEFAULT" {
p.nextToken() // consume DEFAULT
strLit, _ := p.parseStringLiteral()
elem := &ast.XmlNamespacesDefaultElement{String: strLit}
p.spanFrom(elemTok, elem)
xmlNs.XmlNamespacesElements = append(xmlNs.XmlNamespacesElements, elem)
} else {
// Alias element: string AS identifier
strLit, _ := p.parseStringLiteral()
elem := &ast.XmlNamespacesAliasElement{String: strLit}
if p.curTok.Type == TokenAs {
p.nextToken() // consume AS
elem.Identifier = p.parseIdentifier()
}
p.spanFrom(elemTok, elem)
xmlNs.XmlNamespacesElements = append(xmlNs.XmlNamespacesElements, elem)
}
if p.curTok.Type == TokenComma {
p.nextToken()
} else {
break
}
}
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
p.spanFrom(firstElemTok, xmlNs)
}
withClause.XmlNamespaces = xmlNs
} else if strings.ToUpper(p.curTok.Literal) == "CHANGE_TRACKING_CONTEXT" {
p.nextToken() // consume CHANGE_TRACKING_CONTEXT
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
expr, _ := p.parseScalarExpression()
withClause.ChangeTrackingContext = expr
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
}
} else if p.curTok.Type == TokenIdent || p.curTok.Type == TokenLBracket {
// Parse CTE: name (columns) AS (query)
cteTok := p.curTok
cte := &ast.CommonTableExpression{
ExpressionName: p.parseIdentifier(),
}

// Parse optional column list
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
for p.curTok.Type != TokenRParen && p.curTok.Type != TokenEOF {
cte.Columns = append(cte.Columns, p.parseIdentifier())
if p.curTok.Type == TokenComma {
p.nextToken()
}
}
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
}

// Expect AS
if p.curTok.Type == TokenAs {
p.nextToken() // consume AS
}

// Parse query in parentheses
if p.curTok.Type == TokenLParen {
p.nextToken() // consume (
// A CTE body may itself start with a WITH clause (nested
// CTEs, Fabric DW).
if p.curTok.Type == TokenWith {
nested, err := p.parseWithCtesAndXmlNamespaces()
if err != nil {
return nil, err
}
cte.WithCtesAndXmlNamespaces = nested
}
queryExpr, err := p.parseQueryExpression()
if err != nil {
return nil, err
}
cte.QueryExpression = queryExpr
if p.curTok.Type == TokenRParen {
p.nextToken() // consume )
}
}

p.spanFrom(cteTok, cte)
withClause.CommonTableExpressions = append(withClause.CommonTableExpressions, cte)
} else {
break
}

// Check for comma (more CTEs or XMLNAMESPACES followed by CTEs)
if p.curTok.Type == TokenComma {
p.nextToken()
} else {
break
}
}

// The WITH clause spans from the WITH keyword through the last CTE.
p.spanFrom(astStart, withClause)

return withClause, nil
}
Loading
Loading