Skip to content

Commit 41fa965

Browse files
committed
Convert array related features to SQL
Signed-off-by: Kunlin Yu <yukunlin@syriusrobotics.com>
1 parent db07ee2 commit 41fa965

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

include/cql2cpp/sql_converter.h

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ class SqlConverter {
5454
converters[BinCompPred][Equal] = [](auto n, auto c) -> std::string {
5555
return c.at(0) + " = " + c.at(1);
5656
};
57-
converters[Function][NullOp] = [](const AstNodePtr n, auto c) -> std::string {
58-
return std::get<std::string>(n->children().front()->origin_value()) + "(" + c.at(1) + ")";
57+
converters[Function][NullOp] = [](const AstNodePtr n,
58+
auto c) -> std::string {
59+
return std::get<std::string>(n->children().front()->origin_value()) +
60+
"(" + c.at(1) + ")";
5961
};
6062
converters[ArgumentList][NullOp] = [](auto n, auto c) -> std::string {
6163
std::stringstream ss;
@@ -197,6 +199,83 @@ class SqlConverter {
197199
converters[IsBetweenPred][NotBetween] = [](auto n, auto c) -> std::string {
198200
return c.at(0) + " NOT BETWEEN " + c.at(1) + " AND " + c.at(2);
199201
};
202+
converters[Array][NullOp] = [](auto n, auto c) -> std::string {
203+
std::stringstream ss;
204+
ss << "VALUES ";
205+
for (size_t i = 0; i < c.size(); i++) {
206+
ss << "(" << c.at(i) << ")";
207+
if (i != c.size() - 1) ss << ",";
208+
}
209+
return ss.str();
210+
};
211+
converters[ArrayPred][A_Equals] = [](auto n, auto c) -> std::string {
212+
std::string lhs = c.at(0);
213+
std::string rhs = c.at(1);
214+
std::string not_null = "";
215+
216+
if (n->children().at(0)->type() == NodeType::PropertyName) {
217+
not_null += lhs + " NOT NULL";
218+
lhs = "SELECT value FROM json_each(" + lhs + ")";
219+
}
220+
if (n->children().at(1)->type() == NodeType::PropertyName) {
221+
if (not not_null.empty()) not_null += " AND ";
222+
not_null += rhs + " NOT NULL";
223+
rhs = "SELECT value FROM json_each(" + rhs + ")";
224+
}
225+
226+
if (not not_null.empty()) not_null += " AND";
227+
228+
return not_null + " NOT EXISTS (" + lhs + " EXCEPT " + rhs + ")" +
229+
" AND NOT EXISTS (" + rhs + " EXCEPT " + lhs + ")";
230+
};
231+
converters[ArrayPred][A_Contains] = [](auto n, auto c) -> std::string {
232+
std::string lhs = c.at(1);
233+
std::string rhs = c.at(0);
234+
std::string not_null = "";
235+
236+
if (n->children().at(1)->type() == NodeType::PropertyName) {
237+
not_null += lhs + " NOT NULL";
238+
lhs = "SELECT value FROM json_each(" + lhs + ")";
239+
}
240+
if (n->children().at(0)->type() == NodeType::PropertyName) {
241+
if (not not_null.empty()) not_null += " AND ";
242+
not_null += rhs + " NOT NULL";
243+
rhs = "SELECT value FROM json_each(" + rhs + ")";
244+
}
245+
246+
if (not not_null.empty()) not_null += " AND";
247+
248+
return not_null + " NOT EXISTS (" + lhs + " EXCEPT " + rhs + ")";
249+
};
250+
converters[ArrayPred][A_ContainedBy] = [](auto n, auto c) -> std::string {
251+
std::string lhs = c.at(0);
252+
std::string rhs = c.at(1);
253+
std::string not_null = "";
254+
255+
if (n->children().at(0)->type() == NodeType::PropertyName) {
256+
not_null += lhs + " NOT NULL";
257+
lhs = "SELECT value FROM json_each(" + lhs + ")";
258+
}
259+
if (n->children().at(1)->type() == NodeType::PropertyName) {
260+
if (not not_null.empty()) not_null += " AND ";
261+
not_null += rhs + " NOT NULL";
262+
rhs = "SELECT value FROM json_each(" + rhs + ")";
263+
}
264+
265+
if (not not_null.empty()) not_null += " AND";
266+
267+
return not_null + " NOT EXISTS (" + lhs + " EXCEPT " + rhs + ")";
268+
};
269+
converters[ArrayPred][A_Overlaps] = [](const AstNodePtr n,
270+
auto c) -> std::string {
271+
std::string lhs = c.at(0);
272+
std::string rhs = c.at(1);
273+
if (n->children().at(0)->type() == NodeType::PropertyName)
274+
lhs = "SELECT value FROM json_each(" + lhs + ")";
275+
if (n->children().at(1)->type() == NodeType::PropertyName)
276+
rhs = "SELECT value FROM json_each(" + rhs + ")";
277+
return "EXISTS (" + lhs + " INTERSECT " + rhs + ")";
278+
};
200279
}
201280

202281
void Register(

test/test_sql.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ class SqlTest : public testing::Test {
147147
// TEST_F(SqlTest, example71) { EXPECT_TRUE(Convert(case_name_)); } // CASEI
148148
// TEST_F(SqlTest, example86) { EXPECT_TRUE(Convert(case_name_)); } // CASEI
149149

150-
// TEST_F(SqlTest, clause7_15) { EXPECT_TRUE(Convert(case_name_)); } // array
151-
// TEST_F(SqlTest, example79) { EXPECT_TRUE(Convert(case_name_)); } // array
152-
// TEST_F(SqlTest, example80) { EXPECT_TRUE(Convert(case_name_)); } // array
153-
// TEST_F(SqlTest, example81) { EXPECT_TRUE(Convert(case_name_)); } // array
154-
155150
TEST_F(SqlTest, clause6_02a) { EXPECT_TRUE(Convert(case_name_)); }
156151
TEST_F(SqlTest, clause6_02c) { EXPECT_TRUE(Convert(case_name_)); }
157152
TEST_F(SqlTest, clause6_03) { EXPECT_TRUE(Convert(case_name_)); }
@@ -161,6 +156,7 @@ TEST_F(SqlTest, clause7_03a) { EXPECT_TRUE(Convert(case_name_)); }
161156
TEST_F(SqlTest, clause7_03b) { EXPECT_TRUE(Convert(case_name_)); }
162157
TEST_F(SqlTest, clause7_07) { EXPECT_TRUE(Convert(case_name_)); }
163158
TEST_F(SqlTest, clause7_10) { EXPECT_TRUE(Convert(case_name_)); }
159+
TEST_F(SqlTest, clause7_15) { EXPECT_TRUE(Convert(case_name_)); }
164160
TEST_F(SqlTest, clause7_16) { EXPECT_TRUE(Convert(case_name_)); }
165161
TEST_F(SqlTest, clause7_19) { EXPECT_TRUE(Convert(case_name_)); }
166162
TEST_F(SqlTest, example01) { EXPECT_TRUE(Convert(case_name_)); }
@@ -224,6 +220,9 @@ TEST_F(SqlTest, example75) { EXPECT_TRUE(Convert(case_name_)); }
224220
TEST_F(SqlTest, example76) { EXPECT_TRUE(Convert(case_name_)); }
225221
TEST_F(SqlTest, example77) { EXPECT_TRUE(Convert(case_name_)); }
226222
TEST_F(SqlTest, example78) { EXPECT_TRUE(Convert(case_name_)); }
223+
TEST_F(SqlTest, example79) { EXPECT_TRUE(Convert(case_name_)); }
224+
TEST_F(SqlTest, example80) { EXPECT_TRUE(Convert(case_name_)); }
225+
TEST_F(SqlTest, example81) { EXPECT_TRUE(Convert(case_name_)); }
227226
TEST_F(SqlTest, example83) { EXPECT_TRUE(Convert(case_name_)); }
228227
TEST_F(SqlTest, example84) { EXPECT_TRUE(Convert(case_name_)); }
229228
TEST_F(SqlTest, example85) { EXPECT_TRUE(Convert(case_name_)); }

0 commit comments

Comments
 (0)