Skip to content

Commit

Permalink
Implement null, true, and false
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
zombiezen committed Feb 5, 2024
1 parent ed052f8 commit a8aa99f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Node interface {
type Ident struct {
Name string
NameSpan Span

// Quoted is true if the identifier is quoted.
Quoted bool
}

func (id *Ident) Span() Span {
Expand Down
1 change: 1 addition & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ func (p *parser) ident() (*Ident, error) {
return &Ident{
Name: tok.Value,
NameSpan: tok.Span,
Quoted: tok.Kind == TokenQuotedIdentifier,
}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ func TestParse(t *testing.T) {
},
},
},
{
name: "OnlyQuotedTableName",
query: `["StormEvents"]`,
want: &TabularExpr{
Source: &TableRef{
Table: &Ident{
Name: "StormEvents",
NameSpan: newSpan(0, 15),
Quoted: true,
},
},
},
},
{
name: "PipeCount",
query: "StormEvents | count",
Expand Down
12 changes: 11 additions & 1 deletion pql.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ func quoteIdentifier(sb *strings.Builder, name string) {
sb.WriteString(`"`)
}

var builtinIdentifiers = map[string]string{
"true": "TRUE",
"false": "FALSE",
"null": "NULL",
}

var binaryOps = map[parser.TokenKind]string{
parser.TokenAnd: "AND",
parser.TokenOr: "OR",
Expand Down Expand Up @@ -248,7 +254,11 @@ func writeExpression(sb *strings.Builder, source string, x parser.Expr) error {

switch x := x.(type) {
case *parser.Ident:
quoteIdentifier(sb, x.Name)
if sql, ok := builtinIdentifiers[x.Name]; !x.Quoted && ok {
sb.WriteString(sql)
} else {
quoteIdentifier(sb, x.Name)
}
case *parser.BasicLit:
switch x.Kind {
case parser.TokenNumber:
Expand Down
2 changes: 2 additions & 0 deletions testdata/Goldens/WhereTrue/input.pql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
StormEvents
| where true
5 changes: 5 additions & 0 deletions testdata/Goldens/WhereTrue/output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
11032,ATLANTIC SOUTH,Waterspout,0
11098,FLORIDA,Heavy Rain,0
60913,FLORIDA,Tornado,6200000
11503,GEORGIA,Thunderstorm Wind,2000
13913,MISSISSIPPI,Thunderstorm Wind,20000
1 change: 1 addition & 0 deletions testdata/Goldens/WhereTrue/output.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM "StormEvents" WHERE TRUE;
Empty file.

0 comments on commit a8aa99f

Please sign in to comment.