Skip to content

Commit

Permalink
feat: support COALESCE.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashigeru committed Aug 16, 2024
1 parent 786f420 commit f5c9637
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/mizugaki/analyzer/details/analyze_scalar_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ class engine {
case kind::nullif:
return process_nullif(expr, context);
case kind::coalesce:
// FIXME: impl coalesce (built-in function)
return process_coalesce(expr, context);
case kind::next_value_for:
// FIXME: impl next_value_for (built-in function)

Expand Down Expand Up @@ -812,6 +812,33 @@ class engine {
return result;
}

[[nodiscard]] std::unique_ptr<tscalar::expression> process_coalesce(
ast::scalar::builtin_function_invocation const& expr,
value_context const&) {
if (expr.arguments().empty()) {
context_.report(
sql_analyzer_code::malformed_syntax,
"COALESCE must have one or more operands",
expr.region());
return {};
}

::takatori::util::reference_vector<tscalar::expression> operands {};
operands.reserve(expr.arguments().size());
for (auto&& argument : expr.arguments()) {
auto r = process(*argument, {});
if (!r) {
return {};
}
operands.push_back(r.release());
}
auto result = context_.create<tscalar::coalesce>(
expr.region(),
std::move(operands));

return result;
}

[[nodiscard]] std::unique_ptr<tscalar::expression> operator()(
ast::scalar::builtin_set_function_invocation const& expr,
value_context const&) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <takatori/type/primitive.h>

#include <takatori/scalar/binary.h>
#include <takatori/scalar/coalesce.h>
#include <takatori/scalar/compare.h>
#include <takatori/scalar/conditional.h>
#include <takatori/scalar/let.h>
Expand Down Expand Up @@ -392,4 +393,57 @@ TEST_F(analyze_scalar_expression_case_test, nullif_invalid) {
});
}

TEST_F(analyze_scalar_expression_case_test, coalesce) {
auto r = analyze_scalar_expression(
context(),
ast::scalar::builtin_function_invocation {
ast::scalar::builtin_function_kind::coalesce,
{
literal(number("1")),
},
},
{},
{});
ASSERT_TRUE(r) << diagnostics();
expect_no_error();

EXPECT_EQ(*r, (tscalar::coalesce {
{
immediate(1),
},
}));
}

TEST_F(analyze_scalar_expression_case_test, coalesce_multiple) {
auto r = analyze_scalar_expression(
context(),
ast::scalar::builtin_function_invocation {
ast::scalar::builtin_function_kind::coalesce,
{
literal(number("1")),
literal(number("2")),
literal(number("3")),
},
},
{},
{});
ASSERT_TRUE(r) << diagnostics();
expect_no_error();

EXPECT_EQ(*r, (tscalar::coalesce {
{
immediate(1),
immediate(2),
immediate(3),
},
}));
}

TEST_F(analyze_scalar_expression_case_test, coalesce_invalid) {
invalid(sql_analyzer_code::malformed_syntax, ast::scalar::builtin_function_invocation {
ast::scalar::builtin_function_kind::coalesce,
{},
});
}

} // namespace mizugaki::analyzer::details

0 comments on commit f5c9637

Please sign in to comment.