Skip to content

Commit b7828e6

Browse files
committed
implement CASE WHEN expressions
1 parent 5605dba commit b7828e6

7 files changed

Lines changed: 64 additions & 17 deletions

File tree

‎src/parser/bison_parser.y‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,11 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
169169
%token INSERT ISNULL OFFSET RENAME SCHEMA SELECT SORTED
170170
%token TABLES UNIQUE UNLOAD UPDATE VALUES AFTER ALTER CROSS
171171
%token DELTA GROUP INDEX INNER LIMIT LOCAL MERGE MINUS ORDER
172-
%token OUTER RIGHT TABLE UNION USING WHERE CALL DATE DESC
173-
%token DROP FILE FROM FULL HASH HINT INTO JOIN LEFT LIKE
174-
%token LOAD NULL PART PLAN SHOW TEXT TIME VIEW WITH ADD ALL
175-
%token AND ASC CSV FOR INT KEY NOT OFF SET TBL TOP AS BY IF
176-
%token IN IS OF ON OR TO
177-
172+
%token OUTER RIGHT TABLE UNION USING WHERE CALL CASE DATE
173+
%token DESC DROP ELSE FILE FROM FULL HASH HINT INTO JOIN
174+
%token LEFT LIKE LOAD NULL PART PLAN SHOW TEXT THEN TIME
175+
%token VIEW WHEN WITH ADD ALL AND ASC CSV END FOR INT KEY
176+
%token NOT OFF SET TBL TOP AS BY IF IN IS OF ON OR TO
178177

179178
/*********************************
180179
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
@@ -198,7 +197,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
198197
%type <expr> expr operand scalar_expr unary_expr binary_expr logic_expr exists_expr
199198
%type <expr> function_expr between_expr star_expr expr_alias placeholder_expr
200199
%type <expr> column_name literal int_literal num_literal string_literal
201-
%type <expr> comp_expr opt_where join_condition opt_having
200+
%type <expr> comp_expr opt_where join_condition opt_having case_expr
202201
%type <limit> opt_limit opt_top
203202
%type <order> order_desc
204203
%type <order_type> opt_order_type
@@ -606,6 +605,7 @@ expr:
606605
| between_expr
607606
| logic_expr
608607
| exists_expr
608+
| case_expr
609609
;
610610

611611
operand:
@@ -645,6 +645,11 @@ logic_expr:
645645
| expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); }
646646
;
647647

648+
// TODO: allow no else specified
649+
case_expr:
650+
CASE WHEN operand THEN operand ELSE operand END { $$ = Expr::makeCase($3, $5, $7); }
651+
;
652+
648653
exists_expr:
649654
EXISTS '(' select_no_paren ')' { $$ = Expr::makeExists($3); }
650655
;

‎src/parser/flex_lexer.l‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@
5454
<COMMENT>[^\n]* /* skipping comment content until a end of line is read */;
5555
<COMMENT>\n BEGIN(INITIAL);
5656

57-
5857
[ \t\n]+ /* skip whitespace */;
5958

60-
61-
6259
DEALLOCATE TOKEN(DEALLOCATE)
6360
PARAMETERS TOKEN(PARAMETERS)
6461
INTERSECT TOKEN(INTERSECT)
@@ -127,9 +124,11 @@ UNION TOKEN(UNION)
127124
USING TOKEN(USING)
128125
WHERE TOKEN(WHERE)
129126
CALL TOKEN(CALL)
127+
CASE TOKEN(CASE)
130128
DATE TOKEN(DATE)
131129
DESC TOKEN(DESC)
132130
DROP TOKEN(DROP)
131+
ELSE TOKEN(ELSE)
133132
FILE TOKEN(FILE)
134133
FROM TOKEN(FROM)
135134
FULL TOKEN(FULL)
@@ -145,14 +144,17 @@ PART TOKEN(PART)
145144
PLAN TOKEN(PLAN)
146145
SHOW TOKEN(SHOW)
147146
TEXT TOKEN(TEXT)
147+
THEN TOKEN(THEN)
148148
TIME TOKEN(TIME)
149149
VIEW TOKEN(VIEW)
150+
WHEN TOKEN(WHEN)
150151
WITH TOKEN(WITH)
151152
ADD TOKEN(ADD)
152153
ALL TOKEN(ALL)
153154
AND TOKEN(AND)
154155
ASC TOKEN(ASC)
155156
CSV TOKEN(CSV)
157+
END TOKEN(END)
156158
FOR TOKEN(FOR)
157159
INT TOKEN(INT)
158160
KEY TOKEN(KEY)
@@ -171,15 +173,12 @@ ON TOKEN(ON)
171173
OR TOKEN(OR)
172174
TO TOKEN(TO)
173175

174-
175176
"<>" TOKEN(NOTEQUALS)
176177
"<=" TOKEN(LESSEQ)
177178
">=" TOKEN(GREATEREQ)
178179

179-
180180
[-+*/(){},.;<>=^%:?] { return yytext[0]; }
181181

182-
183182
[0-9]+"."[0-9]* |
184183
"."[0-9]* {
185184
yylval->fval = atof(yytext);
@@ -202,7 +201,6 @@ TO TOKEN(TO)
202201
return SQL_IDENTIFIER;
203202
}
204203

205-
206204
'[^'\n]*' {
207205
// Crop the leading and trailing quote char
208206
yylval->sval = hsql::substr(yytext, 1, strlen(yytext)-1);

‎src/parser/sql_keywords.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ IS
140140
ISNULL
141141
BETWEEN
142142
ESCAPE
143+
CASE
144+
WHEN
145+
THEN
146+
ELSE
147+
END
143148

144149
// With
145150
WITH

‎src/sql/Expr.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ namespace hsql {
5757
return e;
5858
}
5959

60+
Expr* Expr::makeCase(Expr* expr, Expr* then, Expr* other) {
61+
Expr* e = new Expr(kExprOperator);
62+
e->expr = expr;
63+
e->opType = CASE;
64+
e->exprList = new std::vector<Expr*>();
65+
e->exprList->push_back(then);
66+
e->exprList->push_back(other);
67+
return e;
68+
}
69+
6070
Expr* Expr::makeLiteral(int64_t val) {
6171
Expr* e = new Expr(kExprLiteralInt);
6272
e->ival = val;

‎src/sql/Expr.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace hsql {
3838

3939
// Ternary operators
4040
BETWEEN,
41+
CASE,
4142

4243
// Binary operators.
4344
SIMPLE_OP,
@@ -111,6 +112,8 @@ namespace hsql {
111112

112113
static Expr* makeBetween(Expr* expr, Expr* left, Expr* right);
113114

115+
static Expr* makeCase(Expr* expr, Expr* then, Expr* other);
116+
114117
static Expr* makeLiteral(int64_t val);
115118

116119
static Expr* makeLiteral(double val);

‎test/queries/tpc-h-08.sql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- http://www.sqlserver-dba.com/2011/09/this-is-a-followup-on-my-earlier-post-of-sql-server-test-data-generation-testing-tools-i-had-some-requests-for-my-set-up-pr.html
22
SELECT O_YEAR, SUM(CASE WHEN NATION = 'BRAZIL' THEN VOLUME ELSE 0 END)/SUM(VOLUME) AS MKT_SHARE
33
FROM (SELECT datepart(yy,O_ORDERDATE) AS O_YEAR, L_EXTENDEDPRICE*(1-L_DISCOUNT) AS VOLUME, N2.N_NAME AS NATION
4-
FROM PART, SUPPLIER, LINEITEM, ORDERS, CUSTOMER, NATION N1, NATION N2, REGION
4+
FROM "PART", SUPPLIER, LINEITEM, ORDERS, CUSTOMER, NATION N1, NATION N2, REGION
55
WHERE P_PARTKEY = L_PARTKEY AND S_SUPPKEY = L_SUPPKEY AND L_ORDERKEY = O_ORDERKEY
66
AND O_CUSTKEY = C_CUSTKEY AND C_NATIONKEY = N1.N_NATIONKEY AND
77
N1.N_REGIONKEY = R_REGIONKEY AND R_NAME = 'AMERICA' AND S_NATIONKEY = N2.N_NATIONKEY

‎test/select_tests.cpp‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ TEST(SelectConditionalSelectTest) {
184184

185185
SelectStatement* select2 = cond1->expr2->select;
186186
ASSERT_NOTNULL(select2);
187-
ASSERT_STREQ(select2->fromTable->getName(), "tt")
187+
ASSERT_STREQ(select2->fromTable->getName(), "tt");
188188

189189

190190
// EXISTS (SELECT ...)
@@ -193,7 +193,33 @@ TEST(SelectConditionalSelectTest) {
193193
ASSERT_NOTNULL(cond2->select);
194194

195195
SelectStatement* ex_select = cond2->select;
196-
ASSERT_STREQ(ex_select->fromTable->getName(), "test")
196+
ASSERT_STREQ(ex_select->fromTable->getName(), "test");
197+
198+
delete result;
199+
}
200+
201+
TEST(SelectCaseWhen) {
202+
TEST_PARSE_SINGLE_SQL(
203+
"SELECT MAX(CASE WHEN a = 'foo' THEN x ELSE 0 END) FROM test;",
204+
kStmtSelect,
205+
SelectStatement,
206+
result,
207+
stmt);
208+
209+
ASSERT_EQ(stmt->selectList->size(), 1);
210+
Expr* func = stmt->selectList->at(0);
211+
212+
ASSERT_NOTNULL(func);
213+
ASSERT(func->isType(kExprFunctionRef));
214+
ASSERT_EQ(func->exprList->size(), 1);
215+
216+
Expr* caseExpr = func->exprList->at(0);
217+
ASSERT_NOTNULL(caseExpr);
218+
ASSERT(caseExpr->isType(kExprOperator));
219+
ASSERT_EQ(caseExpr->opType, Expr::CASE);
220+
ASSERT(caseExpr->expr->isType(kExprOperator));
221+
ASSERT(caseExpr->expr->isSimpleOp('='));
222+
ASSERT_EQ(caseExpr->exprList->size(), 2);
197223

198224
delete result;
199225
}

0 commit comments

Comments
 (0)