Skip to content
Open
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: 14 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (UnaryExpr) isExpr() {}
func (InExpr) isExpr() {}
func (IsNullExpr) isExpr() {}
func (IsBoolExpr) isExpr() {}
func (IsUnknownExpr) isExpr() {}
func (IsSourceExpr) isExpr() {}
func (IsDestinationExpr) isExpr() {}
func (IsLabeledExpr) isExpr() {}
Expand Down Expand Up @@ -1438,6 +1439,19 @@ type IsBoolExpr struct {
Right bool
}

// IsUnknownExpr is IS UNKNOWN expression node.
//
// {{.Left | sql}} IS {{if .Not}}NOT{{end}} UNKNOWN

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better?

Suggested change
// {{.Left | sql}} IS {{if .Not}}NOT{{end}} UNKNOWN
// {{.Left | sql}} IS{{if .Not}} NOT{{end}} UNKNOWN

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current form is preferable as documentation. These templates are not intended to specify whitespace exactly, and keeping spaces outside the template actions makes the SQL token boundaries easier to read. Tight adjacency is useful when tokens are intentionally concatenated, such as SAFE_ and CAST, but that is not the case here. This also matches the nearby IsNullExpr and IsBoolExpr templates.

type IsUnknownExpr struct {
// pos = Left.pos
// end = Unknown + 7

Unknown token.Pos // position of "UNKNOWN"

Not bool
Left Expr
}

// IsSourceExpr is IS SOURCE [OF] expression node.
//
// {{.Left | sql}} IS {{if .Not}}NOT {{end}}SOURCE{{if .Of.Invalid | not}} OF{{end}} {{.Right | sql}}
Expand Down
1 change: 1 addition & 0 deletions ast/expr_impls_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ast/pos.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func exprPrec(e Expr) prec {
return precLit
case *IndexExpr, *SelectorExpr:
return precSelector
case *InExpr, *IsNullExpr, *IsBoolExpr, *IsSourceExpr, *IsDestinationExpr, *IsLabeledExpr, *BetweenExpr:
case *InExpr, *IsNullExpr, *IsBoolExpr, *IsUnknownExpr, *IsSourceExpr, *IsDestinationExpr, *IsLabeledExpr, *BetweenExpr:
return precComparison
case *BinaryExpr:
switch e.Op {
Expand Down Expand Up @@ -449,6 +449,11 @@ func (i *IsBoolExpr) SQL() string {
return paren(p, i.Left) + " IS " + strOpt(i.Not, "NOT ") + formatBoolUpper(i.Right)
}

func (i *IsUnknownExpr) SQL() string {
p := exprPrec(i)
return paren(p, i.Left) + " IS " + strOpt(i.Not, "NOT ") + "UNKNOWN"
}

func (i *IsSourceExpr) SQL() string {
p := exprPrec(i)
return paren(p, i.Left) + " IS " + strOpt(i.Not, "NOT ") + "SOURCE" + strOpt(!i.Of.Invalid(), " OF") + " " + paren(p, i.Right)
Expand Down
3 changes: 3 additions & 0 deletions ast/walk_internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,13 @@ func (p *Parser) parseComparison() ast.Expr {
}
case token.TokenIdent:
switch {
case p.Token.IsKeywordLike("UNKNOWN"):
p.nextToken()
return &ast.IsUnknownExpr{
Unknown: pos,
Left: expr,
Not: not,
}
case p.Token.IsKeywordLike("SOURCE") || p.Token.IsKeywordLike("DESTINATION"):
isSource := p.Token.IsKeywordLike("SOURCE")
p.nextToken()
Expand Down Expand Up @@ -1711,7 +1718,7 @@ func (p *Parser) parseComparison() ast.Expr {
}
}
}
p.panicfAtToken(&p.Token, "expected token: NULL, TRUE, FALSE, SOURCE, DESTINATION, LABELED, but: %s", p.Token.Kind)
p.panicfAtToken(&p.Token, "expected token: NULL, TRUE, FALSE, UNKNOWN, SOURCE, DESTINATION, LABELED, but: %s", p.Token.Kind)
default:
return expr
}
Expand Down
1 change: 1 addition & 0 deletions testdata/input/query/select_is_unknown.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT true IS UNKNOWN, true IS NOT UNKNOWN
32 changes: 32 additions & 0 deletions testdata/result/query/select_is_unknown.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- select_is_unknown.sql
SELECT true IS UNKNOWN, true IS NOT UNKNOWN

--- AST
&ast.QueryStatement{
Query: &ast.Select{
Results: []ast.SelectItem{
&ast.ExprSelectItem{
Expr: &ast.IsUnknownExpr{
Unknown: 15,
Left: &ast.BoolLiteral{
ValuePos: 7,
Value: true,
},
},
},
&ast.ExprSelectItem{
Expr: &ast.IsUnknownExpr{
Unknown: 36,
Not: true,
Left: &ast.BoolLiteral{
ValuePos: 24,
Value: true,
},
},
},
},
},
}

--- SQL
SELECT TRUE IS UNKNOWN, TRUE IS NOT UNKNOWN
32 changes: 32 additions & 0 deletions testdata/result/statement/select_is_unknown.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- select_is_unknown.sql
SELECT true IS UNKNOWN, true IS NOT UNKNOWN

--- AST
&ast.QueryStatement{
Query: &ast.Select{
Results: []ast.SelectItem{
&ast.ExprSelectItem{
Expr: &ast.IsUnknownExpr{
Unknown: 15,
Left: &ast.BoolLiteral{
ValuePos: 7,
Value: true,
},
},
},
&ast.ExprSelectItem{
Expr: &ast.IsUnknownExpr{
Unknown: 36,
Not: true,
Left: &ast.BoolLiteral{
ValuePos: 24,
Value: true,
},
},
},
},
},
}

--- SQL
SELECT TRUE IS UNKNOWN, TRUE IS NOT UNKNOWN
Loading