Skip to content

Commit

Permalink
Make query expression always present in SearchStmt (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice committed Apr 10, 2024
1 parent fd5c376 commit 8c1b0ad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/search/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ struct IndexRef : Ref {
struct SearchStmt : Node {
std::unique_ptr<SelectExpr> select_expr;
std::unique_ptr<IndexRef> index;
std::unique_ptr<QueryExpr> query_expr; // optional
std::unique_ptr<QueryExpr> query_expr;
std::unique_ptr<LimitClause> limit; // optional
std::unique_ptr<SortByClause> sort_by; // optional

Expand All @@ -377,10 +377,9 @@ struct SearchStmt : Node {
std::string_view Name() const override { return "SearchStmt"; }
std::string Dump() const override {
std::string opt;
if (query_expr) opt += " where " + query_expr->Dump();
if (sort_by) opt += " " + sort_by->Dump();
if (limit) opt += " " + limit->Dump();
return fmt::format("{} from {}{}", select_expr->Dump(), index->Dump(), opt);
return fmt::format("{} from {} where {}{}", select_expr->Dump(), index->Dump(), query_expr->Dump(), opt);
}

static inline const std::vector<std::function<Node *(Node *)>> ChildMap = {
Expand Down
2 changes: 1 addition & 1 deletion src/search/ir_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct Visitor : Pass {
virtual std::unique_ptr<Node> Visit(std::unique_ptr<SearchStmt> node) {
node->index = VisitAs<IndexRef>(std::move(node->index));
node->select_expr = VisitAs<SelectExpr>(std::move(node->select_expr));
if (node->query_expr) node->query_expr = TransformAs<QueryExpr>(std::move(node->query_expr));
node->query_expr = TransformAs<QueryExpr>(std::move(node->query_expr));
if (node->sort_by) node->sort_by = VisitAs<SortByClause>(std::move(node->sort_by));
if (node->limit) node->limit = VisitAs<LimitClause>(std::move(node->limit));
return node;
Expand Down
2 changes: 1 addition & 1 deletion src/search/ir_sema_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct SemaChecker {
result.emplace(v->index.get(), current_index);

GET_OR_RET(Check(v->select_expr.get()));
if (v->query_expr) GET_OR_RET(Check(v->query_expr.get()));
GET_OR_RET(Check(v->query_expr.get()));
if (v->limit) GET_OR_RET(Check(v->limit.get()));
if (v->sort_by) GET_OR_RET(Check(v->sort_by.get()));
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/search/sql_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ struct Transformer : ir::TreeTransformer {
}
}

if (!query_expr) {
query_expr = std::make_unique<BoolLiteral>(true);
}

return Node::Create<ir::SearchStmt>(std::move(index), std::move(query_expr), std::move(limit), std::move(sort_by),
std::move(select));
} else if (IsRoot(node)) {
Expand Down
24 changes: 12 additions & 12 deletions tests/cppunit/sql_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ TEST(SQLParserTest, Simple) {
AssertSyntaxError(Parse("select a from b order asc"));
AssertSyntaxError(Parse("select a from b order by a limit"));

AssertIR(Parse("select a from b"), "select a from b");
AssertIR(Parse(" select a from b "), "select a from b");
AssertIR(Parse("\nselect\n a\t \tfrom \n\nb "), "select a from b");
AssertIR(Parse("select * from b"), "select * from b");
AssertIR(Parse("select a, b from c"), "select a, b from c");
AssertIR(Parse("select a, b, c from d"), "select a, b, c from d");
AssertIR(Parse("select xY_z12_3 , X00 from b"), "select xY_z12_3, X00 from b");
AssertIR(Parse("select a from b"), "select a from b where true");
AssertIR(Parse(" select a from b "), "select a from b where true");
AssertIR(Parse("\nselect\n a\t \tfrom \n\nb "), "select a from b where true");
AssertIR(Parse("select * from b"), "select * from b where true");
AssertIR(Parse("select a, b from c"), "select a, b from c where true");
AssertIR(Parse("select a, b, c from d"), "select a, b, c from d where true");
AssertIR(Parse("select xY_z12_3 , X00 from b"), "select xY_z12_3, X00 from b where true");
AssertIR(Parse("select a from b where true"), "select a from b where true");
AssertIR(Parse("select a from b where false"), "select a from b where false");
AssertIR(Parse("select a from b where true and true"), "select a from b where (and true, true)");
Expand Down Expand Up @@ -121,11 +121,11 @@ TEST(SQLParserTest, Simple) {
AssertIR(Parse("select a from b where x > 1 and y < 33"), "select a from b where (and x > 1, y < 33)");
AssertIR(Parse("select a from b where x >= 1 and y hastag \"hi\" or c <= 233"),
"select a from b where (or (and x >= 1, y hastag \"hi\"), c <= 233)");
AssertIR(Parse("select a from b limit 10"), "select a from b limit 0, 10");
AssertIR(Parse("select a from b limit 2, 3"), "select a from b limit 2, 3");
AssertIR(Parse("select a from b order by a"), "select a from b sortby a, asc");
AssertIR(Parse("select a from b order by c desc"), "select a from b sortby c, desc");
AssertIR(Parse("select a from b order by a limit 10"), "select a from b sortby a, asc limit 0, 10");
AssertIR(Parse("select a from b limit 10"), "select a from b where true limit 0, 10");
AssertIR(Parse("select a from b limit 2, 3"), "select a from b where true limit 2, 3");
AssertIR(Parse("select a from b order by a"), "select a from b where true sortby a, asc");
AssertIR(Parse("select a from b order by c desc"), "select a from b where true sortby c, desc");
AssertIR(Parse("select a from b order by a limit 10"), "select a from b where true sortby a, asc limit 0, 10");
AssertIR(Parse("select a from b where c = 1 limit 10"), "select a from b where c = 1 limit 0, 10");
AssertIR(Parse("select a from b where c = 1 and d hastag \"x\" order by e"),
"select a from b where (and c = 1, d hastag \"x\") sortby e, asc");
Expand Down

0 comments on commit 8c1b0ad

Please sign in to comment.