Skip to content

Commit a3559d6

Browse files
UgnineSirdisrobot-piglet
authored andcommitted
Fix parsing INDEX GLOBAL (SYNC|ASYNC)
* Changelog entry Type: fix Component: yql Fix parsing unique index type. There was an error that, if not specified explicitly (SYNC or ASYNC), index type remained with the default value GLOBAL SYNC, despite that it was explicitly specified as UNIQUE in query. #17885 --- Pull Request resolved: ytsaurus/ytsaurus#1287 commit_hash:60de1b723fb1229f2197087fe21db67c72ccbd13
1 parent 0a389f0 commit a3559d6

File tree

2 files changed

+124
-10
lines changed

2 files changed

+124
-10
lines changed

yql/essentials/sql/v1/sql_translation.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -661,24 +661,30 @@ bool TSqlTranslation::CreateTableIndex(const TRule_table_index& node, TVector<TI
661661
if (globalIndex.HasBlock2()) {
662662
uniqIndex = true;
663663
}
664+
bool sync = true;
664665
if (globalIndex.HasBlock3()) {
665666
const TString token = to_lower(Ctx.Token(globalIndex.GetBlock3().GetToken1()));
666667
if (token == "sync") {
667-
if (uniqIndex) {
668-
indexes.back().Type = TIndexDescription::EType::GlobalSyncUnique;
669-
} else {
670-
indexes.back().Type = TIndexDescription::EType::GlobalSync;
671-
}
668+
sync = true;
672669
} else if (token == "async") {
673-
if (uniqIndex) {
674-
AltNotImplemented("unique", indexType);
675-
return false;
676-
}
677-
indexes.back().Type = TIndexDescription::EType::GlobalAsync;
670+
sync = false;
678671
} else {
679672
Y_ABORT("You should change implementation according to grammar changes");
680673
}
681674
}
675+
if (sync) {
676+
if (uniqIndex) {
677+
indexes.back().Type = TIndexDescription::EType::GlobalSyncUnique;
678+
} else {
679+
indexes.back().Type = TIndexDescription::EType::GlobalSync;
680+
}
681+
} else {
682+
if (uniqIndex) {
683+
AltNotImplemented("unique", indexType);
684+
return false;
685+
}
686+
indexes.back().Type = TIndexDescription::EType::GlobalAsync;
687+
}
682688
}
683689
break;
684690
// "LOCAL"

yql/essentials/sql/v1/sql_ut_common.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,6 +2957,114 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
29572957
#endif
29582958
}
29592959

2960+
Y_UNIT_TEST(CreateTableAddIndexGlobalUnique) {
2961+
NYql::TAstParseResult result = SqlToYql(R"sql(USE plato;
2962+
CREATE TABLE table (
2963+
pk INT32 NOT NULL,
2964+
col String,
2965+
INDEX idx GLOBAL UNIQUE ON(col),
2966+
PRIMARY KEY (pk))
2967+
)sql");
2968+
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
2969+
UNIT_ASSERT(result.Root);
2970+
2971+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
2972+
Y_UNUSED(word);
2973+
UNIT_ASSERT_STRING_CONTAINS(line, R"('indexType 'syncGlobalUnique)");
2974+
};
2975+
2976+
TWordCountHive elementStat({TString("\'indexName \'\"idx\"")});
2977+
VerifyProgram(result, elementStat, verifyLine);
2978+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["\'indexName \'\"idx\""]);
2979+
}
2980+
2981+
Y_UNIT_TEST(CreateTableAddIndexGlobalUniqueSync) {
2982+
NYql::TAstParseResult result = SqlToYql(R"sql(USE plato;
2983+
CREATE TABLE table (
2984+
pk INT32 NOT NULL,
2985+
col String,
2986+
INDEX idx GLOBAL UNIQUE SYNC ON(col),
2987+
PRIMARY KEY (pk))
2988+
)sql");
2989+
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
2990+
UNIT_ASSERT(result.Root);
2991+
2992+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
2993+
Y_UNUSED(word);
2994+
UNIT_ASSERT_STRING_CONTAINS(line, R"('indexType 'syncGlobalUnique)");
2995+
};
2996+
2997+
TWordCountHive elementStat({TString("\'indexName \'\"idx\"")});
2998+
VerifyProgram(result, elementStat, verifyLine);
2999+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["\'indexName \'\"idx\""]);
3000+
}
3001+
3002+
Y_UNIT_TEST(CreateTableAddIndexGlobalUniqueAsync) {
3003+
#if ANTLR_VER == 3
3004+
ExpectFailWithFuzzyError(R"sql(USE plato;
3005+
CREATE TABLE table (
3006+
pk INT32 NOT NULL,
3007+
col String,
3008+
INDEX idx GLOBAL UNIQUE ASYNC ON(col),
3009+
PRIMARY KEY (pk))
3010+
)sql",
3011+
"<main>:5:41: Error: unique: alternative is not implemented yet: \\d+:\\d+: global_index\\n");
3012+
#else
3013+
ExpectFailWithError(R"sql(USE plato;
3014+
CREATE TABLE table (
3015+
pk INT32 NOT NULL,
3016+
col String,
3017+
INDEX idx GLOBAL UNIQUE ASYNC ON(col),
3018+
PRIMARY KEY (pk))
3019+
)sql",
3020+
"<main>:5:41: Error: unique: alternative is not implemented yet: \n");
3021+
#endif
3022+
}
3023+
3024+
Y_UNIT_TEST(AlterTableAddIndexGlobalUnique) {
3025+
NYql::TAstParseResult result = SqlToYql(R"sql(USE plato;
3026+
ALTER TABLE table ADD INDEX idx GLOBAL UNIQUE ON(col))sql");
3027+
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
3028+
UNIT_ASSERT(result.Root);
3029+
3030+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
3031+
Y_UNUSED(word);
3032+
UNIT_ASSERT_STRING_CONTAINS(line, R"('indexType 'syncGlobalUnique)");
3033+
};
3034+
3035+
TWordCountHive elementStat({TString("\'indexName \'\"idx\"")});
3036+
VerifyProgram(result, elementStat, verifyLine);
3037+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["\'indexName \'\"idx\""]);
3038+
}
3039+
3040+
Y_UNIT_TEST(AlterTableAddIndexGlobalUniqueSync) {
3041+
NYql::TAstParseResult result = SqlToYql(R"sql(USE plato;
3042+
ALTER TABLE table ADD INDEX idx GLOBAL UNIQUE SYNC ON(col))sql");
3043+
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
3044+
UNIT_ASSERT(result.Root);
3045+
3046+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
3047+
Y_UNUSED(word);
3048+
UNIT_ASSERT_STRING_CONTAINS(line, R"('indexType 'syncGlobalUnique)");
3049+
};
3050+
3051+
TWordCountHive elementStat({TString("\'indexName \'\"idx\"")});
3052+
VerifyProgram(result, elementStat, verifyLine);
3053+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["\'indexName \'\"idx\""]);
3054+
}
3055+
3056+
Y_UNIT_TEST(AlterTableAddIndexGlobalUniqueAsync) {
3057+
#if ANTLR_VER == 3
3058+
ExpectFailWithFuzzyError(R"sql(USE plato;
3059+
ALTER TABLE table ADD INDEX idx GLOBAL UNIQUE ASYNC ON(col))sql",
3060+
"<main>:2:59: Error: unique: alternative is not implemented yet: \\d+:\\d+: global_index\\n");
3061+
#else
3062+
ExpectFailWithError(R"sql(USE plato;
3063+
ALTER TABLE table ADD INDEX idx GLOBAL UNIQUE ASYNC ON(col))sql",
3064+
"<main>:2:59: Error: unique: alternative is not implemented yet: \n");
3065+
#endif
3066+
}
3067+
29603068
Y_UNIT_TEST(CreateTableAddIndexVector) {
29613069
const auto result = SqlToYql(R"(USE plato;
29623070
CREATE TABLE table (

0 commit comments

Comments
 (0)