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
20 changes: 19 additions & 1 deletion embedded/sql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3182,6 +3182,24 @@ func TestQuery(t *testing.T) {
"SELECT * FROM (VALUES (1, true, 'test'))",
)
})

t.Run("should resolve rows equivalently for BETWEEN and >= AND <=", func(t *testing.T) {
betweenRows, err := engine.queryAll(
context.Background(),
nil,
"SELECT id, title FROM table1 WHERE id BETWEEN 1 AND 3 ORDER BY id",
nil,
)
require.NoError(t, err)

rangeRows, err := engine.queryAll(
context.Background(), nil,
"SELECT id, title FROM table1 WHERE id >= 1 AND id <= 3 ORDER BY id",
nil,
)
require.NoError(t, err)
require.Equal(t, betweenRows, rangeRows)
})
}

func TestJSON(t *testing.T) {
Expand Down Expand Up @@ -6499,7 +6517,7 @@ func TestInferParametersUnbounded(t *testing.T) {
require.Len(t, params, 1)
require.Equal(t, AnyType, params["param1"])

params, err = engine.InferParameters(context.Background(), nil, "SELECT * FROM mytable WHERE @param1 != NOT NULL")
params, err = engine.InferParameters(context.Background(), nil, "SELECT * FROM mytable WHERE @param1 OR TRUE")
require.NoError(t, err)
require.Len(t, params, 1)
require.Equal(t, BooleanType, params["param1"])
Expand Down
1 change: 1 addition & 0 deletions embedded/sql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var reservedWords = map[string]int{
"NOT": NOT,
"LIKE": LIKE,
"EXISTS": EXISTS,
"BETWEEN": BETWEEN,
"IN": IN,
"AUTO_INCREMENT": AUTO_INCREMENT,
"NULL": NULL,
Expand Down
14 changes: 14 additions & 0 deletions embedded/sql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,20 @@ func TestParseExp(t *testing.T) {
},
},
},
{
input: "SELECT price FROM items WHERE price BETWEEN 1.5 and 3.9",
expectedOutput: []SQLStmt{
&SelectStmt{
targets: []TargetEntry{{Exp: &ColSelector{col: "price"}}},
ds: &tableRef{table: "items"},
where: &BinBoolExp{
op: And,
left: &CmpBoolExp{op: GE, left: &ColSelector{col: "price"}, right: &Float64{1.5}},
right: &CmpBoolExp{op: LE, left: &ColSelector{col: "price"}, right: &Float64{3.9}},
},
},
},
},
}

for i, tc := range testCases {
Expand Down
213 changes: 94 additions & 119 deletions embedded/sql/sql_grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func setResult(l yyLexer, stmts []SQLStmt) {
%token NOT LIKE IF EXISTS IN IS
%token AUTO_INCREMENT NULL CAST SCAST
%token SHOW DATABASES TABLES USERS
%token BETWEEN
%token <id> NPARAM
%token <pparam> PPARAM
%token <joinType> JOINTYPE
Expand All @@ -105,19 +106,20 @@ func setResult(l yyLexer, stmts []SQLStmt) {
%left ','
%right AS

%nonassoc BETWEEN

%left OR
%left AND

%right NOT_MATCHES_OP
%right LIKE
%right NOT

%left CMPOP
%nonassoc CMPOP LIKE NOT_MATCHES_OP IS

%left '+' '-'
%left '*' '/' '%'
%left '.'
%left '.'

%right STMT_SEPARATOR
%left IS

%type <stmts> sql sqlstmts
%type <stmt> sqlstmt ddlstmt dmlstmt dqlstmt select_stmt
Expand All @@ -144,8 +146,8 @@ func setResult(l yyLexer, stmts []SQLStmt) {
%type <check> check
%type <tableElem> tableElem
%type <tableElems> tableElems
%type <exp> exp opt_exp opt_where opt_having boundexp opt_else
%type <binExp> binExp
%type <exp> exp opt_exp opt_where opt_having boundexp opt_else orExp andExp cmpExp primaryBool addExp notExp
mulExp unaryExp primary
%type <cols> opt_groupby
%type <exp> opt_limit opt_offset case_when_exp
%type <targets> opt_targets targets
Expand Down Expand Up @@ -1140,63 +1142,6 @@ opt_exp:
}
;

exp:
boundexp
{
$$ = $1
}
|
binExp
{
$$ = $1
}
|
NOT exp
{
$$ = &NotBoolExp{exp: $2}
}
|
'-' exp
{
i, isInt := $2.(*Integer)
if isInt {
i.val = -i.val
$$ = i
} else {
$$ = &NumExp{left: &Integer{val: 0}, op: SUBSOP, right: $2}
}
}
|
boundexp opt_not LIKE exp
{
$$ = &LikeBoolExp{val: $1, notLike: $2, pattern: $4}
}
|
boundexp NOT_MATCHES_OP exp
{
$$ = &LikeBoolExp{val: $1, notLike: true, pattern: $3}
}
|
EXISTS '(' dqlstmt ')'
{
$$ = &ExistsBoolExp{q: ($3).(DataSource)}
}
|
boundexp opt_not IN '(' dqlstmt ')'
{
$$ = &InSubQueryExp{val: $1, notIn: $2, q: $5.(*SelectStmt)}
}
|
boundexp opt_not IN '(' values ')'
{
$$ = &InListExp{val: $1, notIn: $2, values: $5}
}
|
case_when_exp
{
$$ = $1
}

case_when_exp:
CASE opt_exp when_then_clauses opt_else END
{
Expand Down Expand Up @@ -1231,6 +1176,91 @@ opt_else:
}
;

exp
: orExp { $$ = $1 }
;

orExp
: orExp OR andExp { $$ = &BinBoolExp{left: $1, op: Or, right: $3} }
| andExp
;

andExp
: andExp AND notExp { $$ = &BinBoolExp{left: $1, op: And, right: $3} }
| notExp
;

notExp
: NOT notExp { $$ = &NotBoolExp{exp: $2} }
| cmpExp
;

cmpExp
: addExp CMPOP addExp { $$ = &CmpBoolExp{left: $1, op: $2, right: $3} }
| addExp IS NULL { $$ = &CmpBoolExp{left: $1, op: EQ, right: &NullValue{t: AnyType}} }
| addExp IS NOT NULL { $$ = &CmpBoolExp{left: $1, op: NE, right: &NullValue{t: AnyType}} }
| addExp BETWEEN addExp AND addExp
{
$$ = &BinBoolExp{
left: &CmpBoolExp{
left: $1,
op: GE,
right: $3,
},
op: And,
right: &CmpBoolExp{
left: $1,
op: LE,
right: $5,
},
}
}
| addExp opt_not LIKE addExp { $$ = &LikeBoolExp{val: $1, notLike: $2, pattern: $4} }
| addExp NOT_MATCHES_OP addExp { $$ = &LikeBoolExp{val: $1, notLike: true, pattern: $3} }
| primaryBool
;

primaryBool
: EXISTS '(' dqlstmt ')' { $$ = &ExistsBoolExp{q: ($3).(DataSource)} }
| addExp opt_not IN '(' dqlstmt ')' { $$ = &InSubQueryExp{val: $1, notIn: $2, q: $5.(*SelectStmt)} }
| addExp opt_not IN '(' values ')' { $$ = &InListExp{val: $1, notIn: $2, values: $5} }
| case_when_exp { $$ = $1 }
| addExp
;

addExp
: addExp '+' mulExp { $$ = &NumExp{left: $1, op: ADDOP, right: $3} }
| addExp '-' mulExp { $$ = &NumExp{left: $1, op: SUBSOP, right: $3} }
| mulExp
;

mulExp
: mulExp '*' unaryExp { $$ = &NumExp{left: $1, op: MULTOP, right: $3} }
| mulExp '/' unaryExp { $$ = &NumExp{left: $1, op: DIVOP, right: $3} }
| mulExp '%' unaryExp { $$ = &NumExp{left: $1, op: MODOP, right: $3} }
| unaryExp
;

unaryExp
: '-' unaryExp
{
i, isInt := $2.(*Integer)
if isInt {
i.val = -i.val
$$ = i
} else {
$$ = &NumExp{left: &Integer{val: 0}, op: SUBSOP, right: $2}
}
}
|
primary
;

primary
: '(' exp ')' { $$ = $2 }
| boundexp
;

boundexp:
selector
{
Expand All @@ -1241,11 +1271,6 @@ boundexp:
{
$$ = $1
}
|
'(' exp ')'
{
$$ = $2
}
|
boundexp SCAST TYPE
{
Expand All @@ -1262,53 +1287,3 @@ opt_not:
$$ = true
}

binExp:
exp '+' exp
{
$$ = &NumExp{left: $1, op: ADDOP, right: $3}
}
|
exp '-' exp
{
$$ = &NumExp{left: $1, op: SUBSOP, right: $3}
}
|
exp '/' exp
{
$$ = &NumExp{left: $1, op: DIVOP, right: $3}
}
|
exp '*' exp
{
$$ = &NumExp{left: $1, op: MULTOP, right: $3}
}
|
exp '%' exp
{
$$ = &NumExp{left: $1, op: MODOP, right: $3}
}
|
exp AND exp
{
$$ = &BinBoolExp{left: $1, op: And, right: $3}
}
|
exp OR exp
{
$$ = &BinBoolExp{left: $1, op: Or, right: $3}
}
|
exp CMPOP exp
{
$$ = &CmpBoolExp{left: $1, op: $2, right: $3}
}
|
exp IS NULL
{
$$ = &CmpBoolExp{left: $1, op: EQ, right: &NullValue{t: AnyType}}
}
|
exp IS NOT NULL
{
$$ = &CmpBoolExp{left: $1, op: NE, right: &NullValue{t: AnyType}}
}
Loading
Loading