aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzverevgeny <zverevgeny@ydb.tech>2023-10-03 20:34:45 +0300
committerzverevgeny <zverevgeny@ydb.tech>2023-10-03 21:11:40 +0300
commit634606e640934e21f073af4b7c9e94a50187153e (patch)
tree0474b02f4d77fb4414deca8b21221089a0668b8c
parent3a63610601211a234bbe414d4cadcc06b19019f5 (diff)
downloadydb-634606e640934e21f073af4b7c9e94a50187153e.tar.gz
YQL-16784 check vars in pattern
-rw-r--r--ydb/library/yql/sql/v1/sql_match_recognize.cpp8
-rw-r--r--ydb/library/yql/sql/v1/sql_match_recognize_ut.cpp16
-rw-r--r--ydb/library/yql/tests/sql/suites/match_recognize/test_type-streaming.sql2
-rw-r--r--ydb/library/yql/tests/sql/suites/match_recognize/test_type.sql2
4 files changed, 25 insertions, 3 deletions
diff --git a/ydb/library/yql/sql/v1/sql_match_recognize.cpp b/ydb/library/yql/sql/v1/sql_match_recognize.cpp
index 357c251c5d..a4b9aaabb1 100644
--- a/ydb/library/yql/sql/v1/sql_match_recognize.cpp
+++ b/ydb/library/yql/sql/v1/sql_match_recognize.cpp
@@ -106,6 +106,14 @@ TMatchRecognizeBuilderPtr TSqlMatchRecognizeClause::CreateBuilder(const NSQLv1Ge
const auto& definitions = ParseDefinitions(commonSyntax.GetRule_row_pattern_definition_list9());
const auto& definitionsPos = TokenPosition(commonSyntax.GetToken8());
+ const auto& rowPatternVariables = GetPatternVars(pattern);
+ for (const auto& [callable, name]: definitions) {
+ if (!rowPatternVariables.contains(name)) {
+ Ctx.Error(callable->GetPos()) << "ROW PATTERN VARIABLE " << name << " is defined, but not mentioned in the PATTERN";
+ return {};
+ }
+ }
+
return new TMatchRecognizeBuilder{
pos,
std::pair{partitionsPos, std::move(partitioners)},
diff --git a/ydb/library/yql/sql/v1/sql_match_recognize_ut.cpp b/ydb/library/yql/sql/v1/sql_match_recognize_ut.cpp
index 9d99445ab6..30d715eeca 100644
--- a/ydb/library/yql/sql/v1/sql_match_recognize_ut.cpp
+++ b/ydb/library/yql/sql/v1/sql_match_recognize_ut.cpp
@@ -141,7 +141,7 @@ FROM Input MATCH_RECOGNIZE(
MEASURES
Last(Q.dt) as T,
First(Y.key) as Key
- PATTERN ( A )
+ PATTERN ( Y Q )
DEFINE Y as true
)
)";
@@ -649,6 +649,20 @@ FROM Input MATCH_RECOGNIZE(
UNIT_ASSERT(IsLambda(defines->GetChild(6), 3));
}
+ Y_UNIT_TEST(AbsentRowPatternVariableInDefines) {
+ auto getStatement = [](const TString &var) {
+ return TStringBuilder() << R"(
+USE plato;
+SELECT *
+FROM Input MATCH_RECOGNIZE(
+PATTERN ( Q )
+DEFINE
+)" << var << " AS TRUE )";
+ };
+ UNIT_ASSERT(MatchRecognizeSqlToYql(getStatement("Q")).IsOk());
+ UNIT_ASSERT(!MatchRecognizeSqlToYql(getStatement("Y")).IsOk());
+ }
+
Y_UNIT_TEST(CheckRequiredNavigationFunction) {
TString stmtPrefix = R"(
USE plato;
diff --git a/ydb/library/yql/tests/sql/suites/match_recognize/test_type-streaming.sql b/ydb/library/yql/tests/sql/suites/match_recognize/test_type-streaming.sql
index 567d3ec880..d742cae284 100644
--- a/ydb/library/yql/tests/sql/suites/match_recognize/test_type-streaming.sql
+++ b/ydb/library/yql/tests/sql/suites/match_recognize/test_type-streaming.sql
@@ -24,7 +24,7 @@ select * from AS_TABLE($data) MATCH_RECOGNIZE(
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW
PATTERN (
- A
+ Q
)
DEFINE
Q as TRUE
diff --git a/ydb/library/yql/tests/sql/suites/match_recognize/test_type.sql b/ydb/library/yql/tests/sql/suites/match_recognize/test_type.sql
index 8488e45fc8..1a5cdeaaf6 100644
--- a/ydb/library/yql/tests/sql/suites/match_recognize/test_type.sql
+++ b/ydb/library/yql/tests/sql/suites/match_recognize/test_type.sql
@@ -23,7 +23,7 @@ select * from AS_TABLE($data) MATCH_RECOGNIZE(
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW
PATTERN (
- A
+ Q
)
DEFINE
Q as TRUE