Skip to content

Commit ccdcf6b

Browse files
committed
Added parsing validations
1 parent cd072fe commit ccdcf6b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

ydb/core/fq/libs/row_dispatcher/json_filter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,13 @@ class TJsonFilter::TImpl {
279279
} else if (columnType == "Optional<Json>") {
280280
columnType = "Optional<String>";
281281
}
282-
str << "CAST(" << columnNames[i] << " as " << columnType << ") as " << columnNames[i] << ((i != columnNames.size() - 1) ? "," : "");
282+
283+
if (columnType.StartsWith("Optional")) {
284+
str << "IF(" << columnNames[i] << " IS NOT NULL, Unwrap(CAST(" << columnNames[i] << " as " << columnType << ")), NULL)";
285+
} else {
286+
str << "Unwrap(CAST(" << columnNames[i] << " as " << columnType << "))";
287+
}
288+
str << " as " << columnNames[i] << ((i != columnNames.size() - 1) ? "," : "");
283289
}
284290
str << " FROM Input;\n";
285291
str << "$filtered = SELECT * FROM $fields " << whereFilter << ";\n";

ydb/core/fq/libs/row_dispatcher/ut/json_filter_ut.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Y_UNIT_TEST_SUITE(TJsonFilterTests) {
7979
Filter->Push({6}, {{"101"}, {"hello2"}});
8080
UNIT_ASSERT_VALUES_EQUAL(1, result.size());
8181
UNIT_ASSERT_VALUES_EQUAL(R"({"a1":"hello2","a2":101})", result[6]);
82+
UNIT_ASSERT_EXCEPTION_CONTAINS(Filter->Push({7}, {{"102"}, {std::string_view()}}), yexception, "Failed to unwrap empty optional");
83+
UNIT_ASSERT_EXCEPTION_CONTAINS(Filter->Push({8}, {{"str"}, {"hello3"}}), yexception, "Failed to unwrap empty optional");
8284
}
8385

8486
Y_UNIT_TEST_F(ManyValues, TFixture) {
@@ -98,18 +100,19 @@ Y_UNIT_TEST_SUITE(TJsonFilterTests) {
98100
Y_UNIT_TEST_F(NullValues, TFixture) {
99101
TMap<ui64, TString> result;
100102
MakeFilter(
101-
{"a1"},
102-
{"Optional<String>"},
103+
{"a1", "a2"},
104+
{"Optional<UInt64>", "String"},
103105
"where a1 is null",
104106
[&](ui64 offset, const TString& json) {
105107
result[offset] = json;
106108
});
107-
Filter->Push({5}, {{std::string_view()}});
109+
Filter->Push({5}, {{std::string_view()}, {"str"}});
108110
UNIT_ASSERT_VALUES_EQUAL(1, result.size());
109-
UNIT_ASSERT_VALUES_EQUAL(R"({"a1":null})", result[5]);
111+
UNIT_ASSERT_VALUES_EQUAL(R"({"a1":null,"a2":"str"})", result[5]);
112+
UNIT_ASSERT_EXCEPTION_CONTAINS(Filter->Push({5}, {{"hello1"}, {"str"}}), yexception, "Failed to unwrap empty optional");
110113
}
111114

112-
Y_UNIT_TEST_F(ThrowExceptionByError, TFixture) {
115+
Y_UNIT_TEST_F(ThrowExceptionByError, TFixture) {
113116
MakeFilter(
114117
{"a1", "a2"},
115118
{"String", "UInt64"},
@@ -120,4 +123,3 @@ Y_UNIT_TEST_SUITE(TJsonFilterTests) {
120123
}
121124

122125
}
123-

0 commit comments

Comments
 (0)