Skip to content

Commit

Permalink
Vector index USING syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
azevaykin committed Jul 13, 2024
1 parent 2f41631 commit 71e6bd1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
8 changes: 4 additions & 4 deletions ydb/library/yql/sql/v1/SQLv1.g.in
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,13 @@ table_index: INDEX an_id table_index_type
ON LPAREN an_id_schema (COMMA an_id_schema)* RPAREN
(COVER LPAREN an_id_schema (COMMA an_id_schema)* RPAREN)?;

table_index_type:
global_index
| local_index
;
table_index_type: (global_index | local_index) (USING index_subtype)?;

global_index: GLOBAL UNIQUE? (SYNC | ASYNC)?;
local_index: LOCAL;

index_subtype: VECTOR_KMEANS_TREE;

changefeed: CHANGEFEED an_id WITH LPAREN changefeed_settings RPAREN;
changefeed_settings: changefeed_settings_entry (COMMA changefeed_settings_entry)*;
changefeed_settings_entry: an_id EQUALS changefeed_setting_value;
Expand Down Expand Up @@ -1963,6 +1962,7 @@ USING: U S I N G;
VACUUM: V A C U U M;
VALUES: V A L U E S;
VARIANT: V A R I A N T;
VECTOR_KMEANS_TREE: V E C T O R '_' K M E A N S '_' T R E E;
VIEW: V I E W;
VIRTUAL: V I R T U A L;
WHEN: W H E N;
Expand Down
1 change: 1 addition & 0 deletions ydb/library/yql/sql/v1/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,7 @@ namespace NSQLTranslationV1 {
GlobalSync,
GlobalAsync,
GlobalSyncUnique,
GlobalVectorKmeansTree
};

TIndexDescription(const TIdentifier& name, EType type = EType::GlobalSync)
Expand Down
2 changes: 2 additions & 0 deletions ydb/library/yql/sql/v1/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ static INode::TPtr CreateIndexType(TIndexDescription::EType type, const INode& n
return node.Q("asyncGlobal");
case TIndexDescription::EType::GlobalSyncUnique:
return node.Q("syncGlobalUnique");
case TIndexDescription::EType::GlobalVectorKmeansTree:
return node.Q("globalVectorKmeansTree");
}
}

Expand Down
26 changes: 21 additions & 5 deletions ydb/library/yql/sql/v1/sql_translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,11 @@ bool PureColumnOrNamedListStr(const TRule_pure_column_or_named_list& node, TTran
bool CreateTableIndex(const TRule_table_index& node, TTranslation& ctx, TVector<TIndexDescription>& indexes) {
indexes.emplace_back(IdEx(node.GetRule_an_id2(), ctx));

const auto& indexType = node.GetRule_table_index_type3();
const auto& indexType = node.GetRule_table_index_type3().GetBlock1();
switch (indexType.Alt_case()) {
case TRule_table_index_type::kAltTableIndexType1: {
auto globalIndex = indexType.GetAlt_table_index_type1().GetRule_global_index1();
// "GLOBAL"
case TRule_table_index_type_TBlock1::kAlt1: {
auto globalIndex = indexType.GetAlt1().GetRule_global_index1();
bool uniqIndex = false;
if (globalIndex.HasBlock2()) {
uniqIndex = true;
Expand All @@ -658,13 +659,28 @@ bool CreateTableIndex(const TRule_table_index& node, TTranslation& ctx, TVector<
}
}
break;
case TRule_table_index_type::kAltTableIndexType2:
// "LOCAL"
case TRule_table_index_type_TBlock1::kAlt2:
ctx.AltNotImplemented("local", indexType);
return false;
case TRule_table_index_type::ALT_NOT_SET:
case TRule_table_index_type_TBlock1::ALT_NOT_SET:
Y_ABORT("You should change implementation according to grammar changes");
}

if (node.GetRule_table_index_type3().HasBlock2()) {
const TString subType = to_lower(ctx.Token(node.GetRule_table_index_type3().GetBlock2().GetRule_index_subtype2().GetToken1())) ;
if (subType == "vector_kmeans_tree") {
if (indexes.back().Type != TIndexDescription::EType::GlobalSync) {
ctx.Error() << "VECTOR_KMEANS_TREE index can only be GLOBAL [SYNC]";
return false;
}

indexes.back().Type = TIndexDescription::EType::GlobalVectorKmeansTree;
} else {
Y_ABORT("You should change implementation according to grammar changes");
}
}

if (node.HasBlock4()) {
ctx.AltNotImplemented("with", indexType);
return false;
Expand Down
25 changes: 15 additions & 10 deletions ydb/library/yql/sql/v1/sql_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
"HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST", "LOCAL",
"NOT", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "RETURNING", "ROLLUP",
"SELECT", "SET", "STREAM", "STRUCT", "SYMMETRIC", "TAGGED", "TUPLE", "UNBOUNDED",
"UNION", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
"UNION", "VECTOR_KMEANS_TREE", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -95,7 +95,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
"HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST", "LOCAL",
"NOT", "NULL", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "RETURNING", "ROLLUP",
"SELECT", "SET", "STRUCT", "SYMMETRIC", "TAGGED", "TRUE", "TUPLE", "UNBOUNDED",
"UNION", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
"UNION", "VECTOR_KMEANS_TREE", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -110,7 +110,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
auto failed = ValidateTokens({
"ANY", "AUTOMAP", "CALLABLE", "COLUMN", "DICT", "ENUM", "ERASE", "FALSE", "FLOW",
"GLOBAL", "LIST", "OPTIONAL", "REPEATABLE", "RESOURCE",
"SET", "STREAM", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT"
"SET", "STREAM", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT", "VECTOR_KMEANS_TREE"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -124,7 +124,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
Y_UNIT_TEST(TokensAsColumnAlias) {
auto failed = ValidateTokens({
"AUTOMAP", "FALSE",
"GLOBAL", "REPEATABLE", "TRUE"
"GLOBAL", "REPEATABLE", "TRUE", "VECTOR_KMEANS_TREE"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -138,7 +138,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
Y_UNIT_TEST(TokensAsTableName) { //id_table_or_type
auto failed = ValidateTokens({
"ANY", "AUTOMAP", "COLUMN", "ERASE", "FALSE",
"GLOBAL", "REPEATABLE", "STREAM", "TRUE"
"GLOBAL", "REPEATABLE", "STREAM", "TRUE", "VECTOR_KMEANS_TREE"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -153,7 +153,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
auto failed = ValidateTokens({
"AUTOMAP", "CALLABLE", "DICT", "ENUM","FALSE", "FLOW",
"GLOBAL", "LIST", "OPTIONAL", "REPEATABLE", "RESOURCE",
"SET", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT"
"SET", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT", "VECTOR_KMEANS_TREE"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -168,7 +168,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
auto failed = ValidateTokens({
"AUTOMAP", "CALLABLE", "COLUMNS", "DICT", "ENUM", "FALSE", "FLOW",
"GLOBAL", "LIST", "OPTIONAL", "REPEATABLE", "RESOURCE",
"SCHEMA", "SET", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT"
"SCHEMA", "SET", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT", "VECTOR_KMEANS_TREE"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -182,7 +182,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
Y_UNIT_TEST(TokensAsWindow) { //id_window
auto failed = ValidateTokens({
"AUTOMAP", "CALLABLE", "DICT", "ENUM", "FALSE", "FLOW", "GLOBAL", "GROUPS", "LIST", "OPTIONAL",
"RANGE", "REPEATABLE", "RESOURCE", "ROWS", "SET", "STRUCT", "TAGGED" ,"TRUE", "TUPLE", "VARIANT"
"RANGE", "REPEATABLE", "RESOURCE", "ROWS", "SET", "STRUCT", "TAGGED" ,"TRUE", "TUPLE", "VARIANT", "VECTOR_KMEANS_TREE"
},
[](const TString& token){
TStringBuilder req;
Expand All @@ -201,7 +201,7 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
"HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST", "LOCAL",
"NOT", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "RETURNING", "ROLLUP",
"SELECT", "SET", "STREAM", "STRUCT", "SYMMETRIC", "TAGGED", "TUPLE", "UNBOUNDED",
"UNION", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
"UNION", "VARIANT", "VECTOR_KMEANS_TREE", "WHEN", "WHERE", "WINDOW", "WITHOUT"
},
[](const TString& token){
TStringBuilder req;
Expand Down Expand Up @@ -2494,7 +2494,12 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {

Y_UNIT_TEST(AlterTableAddIndexWithIsNotSupported) {
ExpectFailWithError("USE plato; ALTER TABLE table ADD INDEX idx LOCAL WITH (a=b, c=d, e=f) ON (col)",
"<main>:1:40: Error: local: alternative is not implemented yet: 725:7: local_index\n");
"<main>:1:40: Error: local: alternative is not implemented yet: 723:35: local_index\n");
}

Y_UNIT_TEST(AlterTableAddVectorIndex) {
const auto result = SqlToYql("USE plato; ALTER TABLE table ADD INDEX idx GLOBAL USING VECTOR_KMEANS_TREE ON (col) COVER (col)");
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
}

Y_UNIT_TEST(AlterTableAlterIndexSetPartitioningIsCorrect) {
Expand Down

0 comments on commit 71e6bd1

Please sign in to comment.