Skip to content

Commit 330d171

Browse files
authored
feat: support const project online (#3376)
1 parent 07f6862 commit 330d171

File tree

17 files changed

+124
-103
lines changed

17 files changed

+124
-103
lines changed

cases/query/const_query.yaml

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
debugs: []
1616
version: 0.5.0
17+
# FIXME: support request procedure for const project
18+
# requires GetTablet impl for non-table procedure
1719
cases:
1820
- id: 0
1921
desc: select const number
20-
mode: request-unsupport
22+
mode: procedure-unsupport
2123
db: db1
2224
sql: |
2325
select 1 as id, 2 as col1, 3.3 as col2;
@@ -29,7 +31,7 @@ cases:
2931
- [1, 2, 3.3]
3032
- id: 1
3133
desc: select str
32-
mode: request-unsupport
34+
mode: procedure-unsupport
3335
db: db1
3436
sql: |
3537
select 1 as id, "hello_world" as col1;
@@ -41,7 +43,7 @@ cases:
4143
- [1, "hello_world"]
4244
- id: 2
4345
desc: const substr pos len
44-
mode: request-unsupport
46+
mode: procedure-unsupport
4547
db: db1
4648
sql: |
4749
select 1 as id, substring("hello_world", 3, 6) as col1;
@@ -53,7 +55,7 @@ cases:
5355
- [1, "llo_wo"]
5456
- id: 3
5557
desc: const substr pos
56-
mode: request-unsupport
58+
mode: procedure-unsupport
5759
db: db1
5860
sql: |
5961
select 1 as id, substring("hello_world", 3) as col1;
@@ -65,7 +67,7 @@ cases:
6567
- [1, "llo_world"]
6668
- id: 4
6769
desc: const concat 1
68-
mode: request-unsupport
70+
mode: procedure-unsupport
6971
db: db1
7072
sql: |
7173
select 1 as id, concat("hello", "world", "abc") as col1;
@@ -76,13 +78,8 @@ cases:
7678
- [1, "helloworldabc"]
7779
- id: 5
7880
desc: cast常量 using CAST operator
79-
mode: request-unsupport
81+
mode: procedure-unsupport
8082
db: db1
81-
inputs:
82-
- columns: ["c1 int", "c2 string", "c5 bigint"]
83-
indexs: ["index1:c1:c5"]
84-
rows:
85-
- [1, "2020-05-22 10:43:40", 1]
8683
sql: |
8784
select CAST (10 as int) as c1, CAST (10 as bigint) as c2, CAST (10 as float) as c3, CAST (10 as double) as c4, CAST (1590115460000 as timestamp) as c5, CAST ("2020-05-20" as date) as c6, CAST (10 as string) as c7;
8885
expect:
@@ -91,13 +88,8 @@ cases:
9188
- [10, 10, 10.0, 10.0, 1590115460000, '2020-05-20', "10"]
9289
- id: 6
9390
desc: cast NULL常量 using CAST operator
94-
mode: request-unsupport
91+
mode: procedure-unsupport
9592
db: db1
96-
inputs:
97-
- columns: ["c1 int", "c2 string", "c5 bigint"]
98-
indexs: ["index1:c1:c5"]
99-
rows:
100-
- [1, "2020-05-22 10:43:40", 1]
10193
sql: |
10294
select CAST (NULL as int) as c1, CAST (NULL as bigint) as c2, CAST (NULL as float) as c3, CAST (NULL as double) as c4, CAST (NULL as timestamp) as c5, CAST (NULL as date) as c6, CAST (NULL as string) as c7;
10395
expect:
@@ -106,13 +98,8 @@ cases:
10698
- [NULL, NULL, NULL, NULL, NULL, NULL, NULL]
10799
- id: 7
108100
desc: cast常量 using type() function
109-
mode: request-unsupport
101+
mode: procedure-unsupport
110102
db: db1
111-
inputs:
112-
- columns: ["c1 int", "c2 string", "c5 bigint"]
113-
indexs: ["index1:c1:c5"]
114-
rows:
115-
- [1, "2020-05-22 10:43:40", 1]
116103
sql: |
117104
select int(10) as c1, bigint(10) as c2, float(10) as c3, double(10) as c4, timestamp(1590115460000) as c5, date("2020-05-20") as c6, string(10) as c7;
118105
expect:
@@ -121,13 +108,8 @@ cases:
121108
- [10, 10, 10.0, 10.0, 1590115460000, '2020-05-20', "10"]
122109
- id: 8
123110
desc: cast NULL常量 using type(NULL) function
124-
mode: request-unsupport
111+
mode: procedure-unsupport
125112
db: db1
126-
inputs:
127-
- columns: ["c1 int", "c2 string", "c5 bigint"]
128-
indexs: ["index1:c1:c5"]
129-
rows:
130-
- [1, "2020-05-22 10:43:40", 1]
131113
sql: |
132114
select int(NULL) as c1, bigint(NULL) as c2, float(NULL) as c3, double(NULL) as c4, timestamp(NULL) as c5, date(NULL) as c6, string(NULL) as c7;
133115
expect:
@@ -136,7 +118,7 @@ cases:
136118
- [NULL, NULL, NULL, NULL, NULL, NULL, NULL]
137119
- id: 9
138120
desc: differnt const node type
139-
mode: request-unsupport
121+
mode: procedure-unsupport
140122
db: db1
141123
sql: |
142124
select true c1, int16(3) c2, 13 c3, 10.0 c4, 'a string' c5, date(timestamp(1590115420000)) c6, timestamp(1590115420000) c7;

hybridse/include/case/sql_case.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ class SqlCase {
205205
static std::string GenRand(const std::string& prefix) {
206206
return prefix + std::to_string(rand() % 10000000 + 1); // NOLINT
207207
}
208-
absl::StatusOr<std::string> BuildCreateSpSqlFromInput(int32_t input_idx, absl::string_view sql,
209-
const std::set<size_t>& common_idx);
210-
absl::StatusOr<std::string> BuildCreateSpSqlFromSchema(const type::TableDef& table, absl::string_view select_sql,
211-
const std::set<size_t>& common_idx);
208+
absl::StatusOr<std::string> BuildCreateSpSql(absl::string_view sql, const std::set<size_t>& common_idx,
209+
std::optional<int32_t> input_idx);
210+
absl::StatusOr<std::string> BuildCreateSpSql(absl::string_view select_sql, const std::set<size_t>& common_idx,
211+
std::optional<const type::TableDef*> table);
212212

213213
friend std::ostream& operator<<(std::ostream& output, const SqlCase& thiz);
214214
static bool IS_PERF() {

hybridse/include/sdk/base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct Status {
7171
cm.append("] ");
7272
cm.append(msg);
7373
return cm;
74-
};
74+
}
7575

7676
int code;
7777
// msg use prepend and append, it's better to use absl::Cord, but we may directly use msg

hybridse/include/vm/physical_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ class PhysicalConstProjectNode : public PhysicalOpNode {
738738
fn_infos_.push_back(&project_.fn_info());
739739
}
740740
virtual ~PhysicalConstProjectNode() {}
741-
virtual void Print(std::ostream &output, const std::string &tab) const;
741+
void Print(std::ostream &output, const std::string &tab) const override;
742742
static PhysicalConstProjectNode *CastFrom(PhysicalOpNode *node);
743743
const ColumnProjects &project() const { return project_; }
744744

hybridse/src/case/sql_case.cc

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,9 @@ const std::string SqlCase::case_name() const {
751751
}
752752
bool SqlCase::ExtractInputTableDef(type::TableDef& table,
753753
int32_t input_idx) const {
754+
if (inputs_.size() <= input_idx) {
755+
return false;
756+
}
754757
return ExtractInputTableDef(inputs_[input_idx], table);
755758
}
756759
bool SqlCase::ExtractInputTableDef(const TableInfo& input,
@@ -1639,35 +1642,41 @@ void InitCases(std::string yaml_path, std::vector<SqlCase>& cases, // NOLINT
16391642
const std::vector<std::string>& filters) {
16401643
SqlCase::CreateSqlCasesFromYaml(hybridse::sqlcase::FindSqlCaseBaseDirPath(), yaml_path, cases, filters);
16411644
}
1642-
absl::StatusOr<std::string> SqlCase::BuildCreateSpSqlFromInput(int32_t input_idx,
1643-
absl::string_view select_sql,
1644-
const std::set<size_t>& common_idx) {
1645-
type::TableDef table;
1646-
if (!ExtractInputTableDef(table, input_idx)) {
1647-
return absl::FailedPreconditionError("Fail to extract table schema");
1648-
}
1645+
absl::StatusOr<std::string> SqlCase::BuildCreateSpSql(absl::string_view select_sql, const std::set<size_t>& common_idx,
1646+
std::optional<int32_t> input_idx) {
1647+
if (input_idx.has_value()) {
1648+
type::TableDef table;
1649+
if (!ExtractInputTableDef(table, input_idx.value())) {
1650+
return absl::FailedPreconditionError("Fail to extract table schema");
1651+
}
16491652

1650-
return BuildCreateSpSqlFromSchema(table, select_sql, common_idx);
1653+
return BuildCreateSpSql(select_sql, common_idx, &table);
1654+
}
1655+
std::optional<const type::TableDef*> tab = {};
1656+
return BuildCreateSpSql(select_sql, common_idx, tab);
16511657
}
16521658

1653-
absl::StatusOr<std::string> SqlCase::BuildCreateSpSqlFromSchema(const type::TableDef& table,
1654-
absl::string_view select_sql,
1655-
const std::set<size_t>& common_idx) {
1656-
auto sql_view = absl::StripAsciiWhitespace(select_sql);
1657-
std::string query_stmt(sql_view);
1658-
if (query_stmt.back() != ';') {
1659+
absl::StatusOr<std::string> SqlCase::BuildCreateSpSql(absl::string_view select_sql, const std::set<size_t>& common_idx,
1660+
std::optional<const type::TableDef*> tab) {
1661+
auto sql_view = absl::StripAsciiWhitespace(select_sql);
1662+
std::string query_stmt(sql_view);
1663+
if (query_stmt.back() != ';') {
16591664
absl::StrAppend(&query_stmt, ";");
16601665
}
16611666

16621667
std::string sql = absl::Substitute("CREATE PROCEDURE $0 (\n", sp_name_);
1663-
for (int i = 0; i < table.columns_size(); i++) {
1664-
auto column = table.columns(i);
1665-
if (!common_idx.empty() && common_idx.count(i)) {
1666-
absl::StrAppend(&sql, "const ");
1667-
}
1668-
absl::SubstituteAndAppend(&sql, "$0 $1", column.name(), TypeString(column.type()));
1669-
if (i < table.columns_size() - 1) {
1670-
absl::StrAppend(&sql, ",\n");
1668+
if (tab.has_value()) {
1669+
auto table = tab.value();
1670+
1671+
for (int i = 0; i < table->columns_size(); i++) {
1672+
auto column = table->columns(i);
1673+
if (!common_idx.empty() && common_idx.count(i)) {
1674+
absl::StrAppend(&sql, "const ");
1675+
}
1676+
absl::SubstituteAndAppend(&sql, "$0 $1", column.name(), TypeString(column.type()));
1677+
if (i < table->columns_size() - 1) {
1678+
absl::StrAppend(&sql, ",\n");
1679+
}
16711680
}
16721681
}
16731682
absl::SubstituteAndAppend(&sql, ")\nBEGIN\n$0\nEND;", query_stmt);

hybridse/src/case/sql_case_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ TEST_F(SqlCaseTest, BuildCreateSpSqlFromInputTest) {
11681168
sql_case.sp_name_ = "sp";
11691169
std::string sql = " select c1, c2, c3, c4 from t1 ";
11701170
std::string sp_sql = "";
1171-
auto s = sql_case.BuildCreateSpSqlFromInput(0, sql, {});
1171+
auto s = sql_case.BuildCreateSpSql(sql, {}, 0);
11721172
ASSERT_TRUE(s.ok()) << s.status();
11731173
ASSERT_EQ(R"s(CREATE PROCEDURE sp (
11741174
c1 string,
@@ -1190,7 +1190,7 @@ END;)s",
11901190
sql_case.inputs_.push_back(input);
11911191
std::string sql = "select c1, c2, c3, c4 from t1;";
11921192
std::string sp_sql = "";
1193-
auto s = sql_case.BuildCreateSpSqlFromInput(0, sql, {0, 1, 3});
1193+
auto s = sql_case.BuildCreateSpSql(sql, {0, 1, 3}, 0);
11941194
ASSERT_TRUE(s.ok()) << s.status();
11951195
ASSERT_EQ(R"s(CREATE PROCEDURE sp1 (
11961196
const c1 string,

hybridse/src/plan/planner.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ absl::StatusOr<node::TablePlanNode*> Planner::IsTable(node::PlanNode *node) {
490490
// - SELECT
491491
// - JOIN
492492
// - WINDOW
493+
// - CONST PROJECT
493494
// - UnSupport Ops::
494495
// - CREATE TABLE
495496
// - INSERT TABLE
@@ -500,8 +501,10 @@ absl::StatusOr<node::TablePlanNode*> Planner::IsTable(node::PlanNode *node) {
500501
// - Not Impl
501502
// - Order By
502503
base::Status Planner::ValidateOnlineServingOp(node::PlanNode *node) {
503-
CHECK_TRUE(nullptr != node, common::kNullInputPointer,
504-
"Fail to validate request table: input node is null")
504+
if (node == nullptr) {
505+
// null is fine, e.g the const project
506+
return {};
507+
}
505508
switch (node->type_) {
506509
case node::kPlanTypeProject: {
507510
auto project_node = dynamic_cast<node::ProjectPlanNode *>(node);

hybridse/src/testing/engine_test_base.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,20 @@ class RequestEngineTestRunner : public EngineTestRunner {
271271
std::string request_db_name = request_session->GetRequestDbName();
272272
CHECK_TRUE(parameter_rows_.empty(), common::kUnSupport, "Request do not support parameterized query currently")
273273
Row parameter = parameter_rows_.empty() ? Row() : parameter_rows_[0];
274+
if (request_rows_.empty()) {
275+
// send empty request, trigger e.g const project in request mode
276+
CHECK_TRUE(request_name.empty() && request_db_name.empty(), common::kUnsupportSql,
277+
"no request data for request table: <", request_db_name, ".", request_name, ">")
278+
request_rows_.push_back(Row());
279+
}
274280
for (auto in_row : request_rows_) {
275281
Row out_row;
276282
int run_ret = request_session->Run(in_row, &out_row);
277283
if (run_ret != 0) {
278284
return_code_ = ENGINE_TEST_RET_EXECUTION_ERROR;
279285
return Status(common::kRunError, "Run request session failed");
280286
}
281-
if (!has_batch_request) {
287+
if (!has_batch_request && !request_name.empty()) {
282288
CHECK_TRUE(AddRowIntoTable(request_db_name, request_name, in_row), common::kTablePutFailed,
283289
"Fail add row into table ", request_db_name, ".", request_name);
284290
}
@@ -423,6 +429,12 @@ class BatchRequestEngineTestRunner : public EngineTestRunner {
423429
offset += row_num;
424430
}
425431
}
432+
433+
if (request_rows_.empty()) {
434+
// batch request rows will empty for const projects
435+
// workaround by add the one empty row
436+
request_rows_.push_back(Row());
437+
}
426438
return Status::OK();
427439
}
428440

hybridse/src/vm/runner.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,10 @@ std::shared_ptr<DataHandlerList> Runner::BatchRequestRun(RunnerContext& ctx) {
10741074
return cached;
10751075
}
10761076
}
1077-
std::shared_ptr<DataHandlerVector> outputs =
1078-
std::make_shared<DataHandlerVector>();
1077+
1078+
std::shared_ptr<DataHandlerVector> outputs = std::make_shared<DataHandlerVector>();
10791079
std::vector<std::shared_ptr<DataHandler>> inputs(producers_.size());
1080-
std::vector<std::shared_ptr<DataHandlerList>> batch_inputs(
1081-
producers_.size());
1080+
std::vector<std::shared_ptr<DataHandlerList>> batch_inputs(producers_.size());
10821081
for (size_t idx = producers_.size(); idx > 0; idx--) {
10831082
batch_inputs[idx - 1] = producers_[idx - 1]->BatchRequestRun(ctx);
10841083
}

hybridse/src/vm/transform.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,9 @@ Status RequestModeTransformer::TransformProjectPlanOp(
23002300
"Input node or output node is null");
23012301

23022302
PhysicalOpNode* depend = nullptr;
2303-
CHECK_STATUS(TransformPlanOp(node->GetChildren()[0], &depend));
2303+
if (!node->GetChildren().empty() && nullptr != node->GetChildren()[0]) {
2304+
CHECK_STATUS(TransformPlanOp(node->GetChildren()[0], &depend));
2305+
}
23042306

23052307
CHECK_STATUS(CompleteProjectList(node, depend));
23062308

@@ -2468,8 +2470,6 @@ Status RequestModeTransformer::ValidateRequestTable(
24682470
return Status::OK();
24692471
}
24702472
case vm::kPhysicalOpConstProject: {
2471-
FAIL_STATUS(kPlanError,
2472-
"Non-support Const Project in request mode", in->GetTreeString());
24732473
break;
24742474
}
24752475
default: {
@@ -2487,6 +2487,9 @@ Status RequestModeTransformer::TransformProjectOp(
24872487
node::ProjectListNode* project_list, PhysicalOpNode* depend,
24882488
bool append_input, PhysicalOpNode** output) {
24892489
PhysicalOpNode* new_depend = depend;
2490+
if (nullptr == depend) {
2491+
return CreatePhysicalConstProjectNode(project_list, output);
2492+
}
24902493
if (nullptr != project_list->GetW()) {
24912494
CHECK_STATUS(TransformWindowOp(depend, project_list->GetW(), &new_depend));
24922495
}

0 commit comments

Comments
 (0)