Skip to content

Commit

Permalink
chore(embedded/sql): Add support for simple CASE statements
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Scafiti <stefano.scafiti96@gmail.com>
  • Loading branch information
ostafen committed Nov 27, 2024
1 parent 51c0742 commit fef9d8d
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 362 deletions.
6 changes: 3 additions & 3 deletions embedded/sql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2973,13 +2973,13 @@ func TestQuery(t *testing.T) {
`SELECT
department,
job_title,
CASE
WHEN department = 'sales' THEN
CASE department
WHEN 'sales' THEN
CASE
WHEN job_title = 'manager' THEN '20% Bonus'
ELSE '10% Bonus'
END
WHEN department = 'engineering' THEN
WHEN 'engineering' THEN
CASE
WHEN job_title = 'senior engineer' THEN '15% Bonus'
ELSE '5% Bonus'
Expand Down
51 changes: 51 additions & 0 deletions embedded/sql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,57 @@ func TestParseExp(t *testing.T) {
}},
expectedError: nil,
},
{
input: "SELECT CASE 1 + 1 WHEN 2 THEN 1 ELSE 0 END FROM my_table",
expectedOutput: []SQLStmt{
&SelectStmt{
ds: &tableRef{table: "my_table"},
targets: []TargetEntry{
{
Exp: &CaseWhenExp{
exp: &NumExp{
op: ADDOP,
left: &Integer{1},
right: &Integer{1},
},
whenThen: []whenThenClause{
{
when: &Integer{2},
then: &Integer{1},
},
},
elseExp: &Integer{0},
},
},
},
},
},
},
{
input: "SELECT CASE WHEN is_deleted OR is_expired THEN 1 END AS is_deleted_or_expired FROM my_table",
expectedOutput: []SQLStmt{
&SelectStmt{
ds: &tableRef{table: "my_table"},
targets: []TargetEntry{
{
Exp: &CaseWhenExp{
whenThen: []whenThenClause{
{
when: &BinBoolExp{
op: OR,
left: &ColSelector{col: "is_deleted"},
right: &ColSelector{col: "is_expired"},
},
then: &Integer{1},
},
},
},
As: "is_deleted_or_expired",
},
},
},
},
},
{
input: "SELECT CASE WHEN is_deleted OR is_expired THEN 1 END AS is_deleted_or_expired FROM my_table",
expectedOutput: []SQLStmt{
Expand Down
27 changes: 16 additions & 11 deletions embedded/sql/sql_grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func setResult(l yyLexer, stmts []SQLStmt) {
%type <join> join
%type <joinType> opt_join_type
%type <checks> opt_checks
%type <exp> exp opt_where opt_having boundexp opt_else when_then_else
%type <exp> exp opt_exp opt_where opt_having boundexp opt_else
%type <binExp> binExp
%type <cols> opt_groupby
%type <exp> opt_limit opt_offset case_when_exp
Expand Down Expand Up @@ -1057,6 +1057,17 @@ opt_checks:
$$ = append([]CheckConstraint{{name: $2, exp: $4}}, $6...)
}

opt_exp:
{
$$ = nil
}
|
exp
{
$$ = $1
}
;

exp:
boundexp
{
Expand Down Expand Up @@ -1104,18 +1115,12 @@ exp:
}

case_when_exp:
CASE when_then_else END
{
$$ = $2
}
;

when_then_else:
when_then_clauses opt_else
CASE opt_exp when_then_clauses opt_else END
{
$$ = &CaseWhenExp{
whenThen: $1,
elseExp: $2,
exp: $2,
whenThen: $3,
elseExp: $4,
}
}
;
Expand Down
Loading

0 comments on commit fef9d8d

Please sign in to comment.