Skip to content

Commit 21519c5

Browse files
committed
grammar: Add NULLIF(...).
1 parent fc906ed commit 21519c5

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

expr.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ shared_ptr<value_expr> value_expr::factory(prod *p, sqltype *type_constraint)
1919
try {
2020
if (1 == d20() && p->level < d6() && window_function::allowed(p))
2121
return make_shared<window_function>(p, type_constraint);
22-
else if (1 == d20() && p->level < d6())
22+
else if (1 == d42() && p->level < d6())
2323
return make_shared<coalesce>(p, type_constraint);
24+
else if (1 == d42() && p->level < d6())
25+
return make_shared<nullif>(p, type_constraint);
2426
else if (d9()<3 && p->level < d6())
2527
return make_shared<funcall>(p, type_constraint);
2628
else if (d6()<3)
@@ -156,7 +158,8 @@ comparison_op::comparison_op(prod *p) : bool_binop(p)
156158
}
157159
}
158160

159-
coalesce::coalesce(prod *p, sqltype *type_constraint) : value_expr(p)
161+
coalesce::coalesce(prod *p, sqltype *type_constraint, const char *abbrev)
162+
: value_expr(p), abbrev_(abbrev)
160163
{
161164
auto first_expr = value_expr::factory(this, type_constraint);
162165
auto second_expr = value_expr::factory(this, first_expr->type);
@@ -177,7 +180,7 @@ coalesce::coalesce(prod *p, sqltype *type_constraint) : value_expr(p)
177180

178181
void coalesce::out(std::ostream &out)
179182
{
180-
out << "cast(coalesce(";
183+
out << "cast(" << abbrev_ << "(";
181184
for (auto expr = value_exprs.begin(); expr != value_exprs.end(); expr++) {
182185
out << **expr;
183186
if (expr+1 != value_exprs.end())

expr.hh

+9-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ struct column_reference: value_expr {
6666
};
6767

6868
struct coalesce : value_expr {
69+
const char *abbrev_;
6970
vector<shared_ptr<value_expr> > value_exprs;
7071
virtual ~coalesce() { };
71-
coalesce(prod *p, sqltype *type_constraint = 0);
72+
coalesce(prod *p, sqltype *type_constraint = 0, const char *abbrev = "coalesce");
7273
virtual void out(std::ostream &out);
7374
virtual void accept(prod_visitor *v) {
7475
v->visit(this);
@@ -77,6 +78,13 @@ struct coalesce : value_expr {
7778
}
7879
};
7980

81+
struct nullif : coalesce {
82+
virtual ~nullif() { };
83+
nullif(prod *p, sqltype *type_constraint = 0)
84+
: coalesce(p, type_constraint, "nullif")
85+
{ };
86+
};
87+
8088
struct bool_expr : value_expr {
8189
virtual ~bool_expr() { }
8290
bool_expr(prod *p) : value_expr(p) { type = scope->schema->booltype; }

0 commit comments

Comments
 (0)