Skip to content

Commit

Permalink
follow: validate function call in column default.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashigeru committed Jul 18, 2024
1 parent d707dfb commit 06118ba
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/yugawara/analyzer/expression_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,30 @@ class engine {
});
success = false;
}
} else if (value.kind() == storage::column_value_kind::function) {
auto&& func = value.element<storage::column_value_kind::function>();
if (!func->parameter_types().empty()) {
report({
code::inconsistent_elements,
"function call for default value must not have any arguments",
first_region(stmt.definition(), stmt),
});
success = false;
}
if (auto r = type::is_assignment_convertible(func->return_type(), column.type());
r != ternary::yes) {
report({
code::inconsistent_type,
string_builder {}
<< "function \"" << func->name() << "\" "
<< "has inconsistent type (" << func->return_type() << ") "
<< "for the column \"" << column.simple_name() << "\" "
<< "(" << column.type() << ")"
<< string_builder::to_string,
first_region(stmt.definition(), stmt),
});
success = false;
}
}
}
return success;
Expand Down
115 changes: 113 additions & 2 deletions test/yugawara/analyzer/expression_analyzer_statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#include <takatori/type/primitive.h>
#include <takatori/type/date.h>

#include <takatori/value/primitive.h>
#include <takatori/value/character.h>
#include <takatori/util/optional_ptr.h>

#include <takatori/scalar/immediate.h>
#include <takatori/scalar/variable_reference.h>

#include <takatori/relation/scan.h>
Expand All @@ -24,13 +25,16 @@
#include <takatori/statement/write.h>
#include <takatori/statement/create_table.h>

#include <takatori/util/optional_ptr.h>

#include <yugawara/binding/factory.h>
#include <yugawara/extension/type/error.h>

#include <yugawara/function/declaration.h>

#include <yugawara/storage/table.h>
#include <yugawara/storage/index.h>
#include <yugawara/storage/configurable_provider.h>
#include <takatori/scalar/immediate.h>

namespace yugawara::analyzer {

Expand Down Expand Up @@ -247,6 +251,41 @@ TEST_F(expression_analyzer_statement_test, create_table_default_value_sequence)
EXPECT_TRUE(ok());
}

TEST_F(expression_analyzer_statement_test, create_table_default_value_function) {
auto func = std::make_shared<function::declaration>(function::declaration {
function::declaration::minimum_builtin_function_id + 1,
"func",
t::int4(),
{},
});
auto schema = std::make_shared<schema::declaration>("s");
auto table = std::make_shared<storage::table>(::takatori::util::clone_tag, storage::table {
"T0",
{
{ "C0", t::int4() },
{
"C1",
t::int4(),
variable::nullable,
{ func },
},
{ "C2", t::int4() },
},
});
auto index = std::make_shared<storage::index>(storage::index {
table,
"I0",
});
statement::create_table stmt {
bindings(schema),
bindings(table),
bindings(index),
};
auto b = analyzer.resolve(stmt, true);
ASSERT_TRUE(b);
EXPECT_TRUE(ok());
}

TEST_F(expression_analyzer_statement_test, create_table_default_value_immediate_inconsistent) {
auto schema = std::make_shared<schema::declaration>("s");
auto table = std::make_shared<storage::table>(::takatori::util::clone_tag, storage::table {
Expand Down Expand Up @@ -308,4 +347,76 @@ TEST_F(expression_analyzer_statement_test, create_table_default_value_sequence_i
EXPECT_FALSE(ok());
}

TEST_F(expression_analyzer_statement_test, create_table_default_value_function_inconsistent_return_type) {
auto func = std::make_shared<function::declaration>(function::declaration {
function::declaration::minimum_builtin_function_id + 1,
"func",
t::int4(),
{},
});
auto schema = std::make_shared<schema::declaration>("s");
auto table = std::make_shared<storage::table>(::takatori::util::clone_tag, storage::table {
"T0",
{
{ "C0", t::int4() },
{
"C1",
t::date {},
variable::nullable,
{ func },
},
{ "C2", t::int4() },
},
});
auto index = std::make_shared<storage::index>(storage::index {
table,
"I0",
});
statement::create_table stmt {
bindings(schema),
bindings(table),
bindings(index),
};
auto b = analyzer.resolve(stmt, true);
EXPECT_FALSE(b);
EXPECT_FALSE(ok());
}

TEST_F(expression_analyzer_statement_test, create_table_default_value_function_inconsistent_parameters) {
auto func = std::make_shared<function::declaration>(function::declaration {
function::declaration::minimum_builtin_function_id + 1,
"func",
t::int4(),
{
t::int4(),
},
});
auto schema = std::make_shared<schema::declaration>("s");
auto table = std::make_shared<storage::table>(::takatori::util::clone_tag, storage::table {
"T0",
{
{ "C0", t::int4() },
{
"C1",
t::int4(),
variable::nullable,
{ func },
},
{ "C2", t::int4() },
},
});
auto index = std::make_shared<storage::index>(storage::index {
table,
"I0",
});
statement::create_table stmt {
bindings(schema),
bindings(table),
bindings(index),
};
auto b = analyzer.resolve(stmt, true);
EXPECT_FALSE(b);
EXPECT_FALSE(ok());
}

} // namespace yugawara::analyzer

0 comments on commit 06118ba

Please sign in to comment.