Skip to content

Commit 53ce1d6

Browse files
committed
Support temp tables in yql (ydb-platform#1589)
* Initial commit * Fixes
1 parent 8ba73d4 commit 53ce1d6

File tree

7 files changed

+179
-3
lines changed

7 files changed

+179
-3
lines changed

ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp

+131
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,137 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
860860
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
861861
}
862862

863+
Y_UNIT_TEST(CreateTempTable) {
864+
NKikimrConfig::TAppConfig appConfig;
865+
appConfig.MutableTableServiceConfig()->SetEnablePreparedDdl(true);
866+
auto setting = NKikimrKqp::TKqpSetting();
867+
auto serverSettings = TKikimrSettings()
868+
.SetAppConfig(appConfig)
869+
.SetKqpSettings({setting});
870+
TKikimrRunner kikimr(
871+
serverSettings.SetWithSampleTables(false).SetEnableTempTables(true));
872+
auto clientConfig = NGRpcProxy::TGRpcClientConfig(kikimr.GetEndpoint());
873+
auto client = kikimr.GetQueryClient();
874+
{
875+
auto session = client.GetSession().GetValueSync().GetSession();
876+
auto id = session.GetId();
877+
878+
const auto queryCreate = Q_(R"(
879+
--!syntax_v1
880+
CREATE TEMP TABLE Temp (
881+
Key Uint64 NOT NULL,
882+
Value String,
883+
PRIMARY KEY (Key)
884+
);)");
885+
886+
auto resultCreate = session.ExecuteQuery(queryCreate, NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync();
887+
UNIT_ASSERT_C(resultCreate.IsSuccess(), resultCreate.GetIssues().ToString());
888+
889+
const auto querySelect = Q_(R"(
890+
--!syntax_v1
891+
SELECT * FROM Temp;
892+
)");
893+
894+
auto resultSelect = session.ExecuteQuery(
895+
querySelect, NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
896+
UNIT_ASSERT_C(resultSelect.IsSuccess(), resultSelect.GetIssues().ToString());
897+
898+
bool allDoneOk = true;
899+
NTestHelpers::CheckDelete(clientConfig, id, Ydb::StatusIds::SUCCESS, allDoneOk);
900+
901+
UNIT_ASSERT(allDoneOk);
902+
}
903+
904+
{
905+
const auto querySelect = Q_(R"(
906+
--!syntax_v1
907+
SELECT * FROM Temp;
908+
)");
909+
910+
auto resultSelect = client.ExecuteQuery(
911+
querySelect, NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
912+
UNIT_ASSERT(!resultSelect.IsSuccess());
913+
}
914+
}
915+
916+
Y_UNIT_TEST(TempTablesDrop) {
917+
NKikimrConfig::TAppConfig appConfig;
918+
appConfig.MutableTableServiceConfig()->SetEnablePreparedDdl(true);
919+
auto setting = NKikimrKqp::TKqpSetting();
920+
auto serverSettings = TKikimrSettings()
921+
.SetAppConfig(appConfig)
922+
.SetKqpSettings({setting});
923+
TKikimrRunner kikimr(
924+
serverSettings.SetWithSampleTables(false).SetEnableTempTables(true));
925+
auto clientConfig = NGRpcProxy::TGRpcClientConfig(kikimr.GetEndpoint());
926+
auto client = kikimr.GetQueryClient();
927+
928+
auto session = client.GetSession().GetValueSync().GetSession();
929+
auto id = session.GetId();
930+
931+
const auto queryCreate = Q_(R"(
932+
--!syntax_v1
933+
CREATE TEMPORARY TABLE Temp (
934+
Key Uint64 NOT NULL,
935+
Value String,
936+
PRIMARY KEY (Key)
937+
);)");
938+
939+
auto resultCreate = session.ExecuteQuery(queryCreate, NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync();
940+
UNIT_ASSERT_C(resultCreate.IsSuccess(), resultCreate.GetIssues().ToString());
941+
942+
{
943+
const auto querySelect = Q_(R"(
944+
--!syntax_v1
945+
SELECT * FROM Temp;
946+
)");
947+
948+
auto resultSelect = session.ExecuteQuery(
949+
querySelect, NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
950+
UNIT_ASSERT_C(resultSelect.IsSuccess(), resultSelect.GetIssues().ToString());
951+
}
952+
953+
const auto queryDrop = Q_(R"(
954+
--!syntax_v1
955+
DROP TABLE Temp;
956+
)");
957+
958+
auto resultDrop = session.ExecuteQuery(
959+
queryDrop, NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync();
960+
UNIT_ASSERT_C(resultDrop.IsSuccess(), resultDrop.GetIssues().ToString());
961+
962+
{
963+
const auto querySelect = Q_(R"(
964+
--!syntax_v1
965+
SELECT * FROM Temp;
966+
)");
967+
968+
auto resultSelect = session.ExecuteQuery(
969+
querySelect, NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
970+
UNIT_ASSERT(!resultSelect.IsSuccess());
971+
}
972+
973+
bool allDoneOk = true;
974+
NTestHelpers::CheckDelete(clientConfig, id, Ydb::StatusIds::SUCCESS, allDoneOk);
975+
976+
UNIT_ASSERT(allDoneOk);
977+
978+
auto sessionAnother = client.GetSession().GetValueSync().GetSession();
979+
auto idAnother = sessionAnother.GetId();
980+
UNIT_ASSERT(id != idAnother);
981+
982+
{
983+
const auto querySelect = Q_(R"(
984+
--!syntax_v1
985+
SELECT * FROM Temp;
986+
)");
987+
988+
auto resultSelect = sessionAnother.ExecuteQuery(
989+
querySelect, NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
990+
UNIT_ASSERT(!resultSelect.IsSuccess());
991+
}
992+
}
993+
863994
Y_UNIT_TEST(DdlGroup) {
864995
NKikimrConfig::TAppConfig appConfig;
865996
appConfig.MutableTableServiceConfig()->SetEnablePreparedDdl(true);

ydb/library/yql/sql/v1/SQLv1.g.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ object_features: object_feature | LPAREN object_feature (COMMA object_feature)*
623623

624624
object_type_ref: an_id_or_type;
625625

626-
create_table_stmt: CREATE (OR REPLACE)? (TABLE | TABLESTORE | EXTERNAL TABLE) (IF NOT EXISTS)? simple_table_ref LPAREN create_table_entry (COMMA create_table_entry)* COMMA? RPAREN
626+
create_table_stmt: CREATE (OR REPLACE)? (TABLE | TABLESTORE | EXTERNAL TABLE | TEMP TABLE | TEMPORARY TABLE) (IF NOT EXISTS)? simple_table_ref LPAREN create_table_entry (COMMA create_table_entry)* COMMA? RPAREN
627627
table_inherits?
628628
table_partition_by?
629629
with_table_settings?

ydb/library/yql/sql/v1/format/sql_format_ut.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ Y_UNIT_TEST_SUITE(CheckSqlFormatter) {
307307
")\n"
308308
"PARTITION BY HASH (a, b, hash)\n"
309309
"WITH (tiering = 'some');\n"},
310-
{"create table if not exists user(user int32)", "CREATE TABLE IF NOT EXISTS user (\n\tuser int32\n);\n"}
310+
{"create table if not exists user(user int32)", "CREATE TABLE IF NOT EXISTS user (\n\tuser int32\n);\n"},
311+
{"create temp table user(user int32)", "CREATE TEMP TABLE user (\n\tuser int32\n);\n"},
312+
{"create temporary table user(user int32)", "CREATE TEMPORARY TABLE user (\n\tuser int32\n);\n"}
311313
};
312314

313315
TSetup setup;

ydb/library/yql/sql/v1/node.h

+1
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ namespace NSQLTranslationV1 {
10461046
TVector<TChangefeedDescription> Changefeeds;
10471047
TTableSettings TableSettings;
10481048
ETableType TableType = ETableType::Table;
1049+
bool Temporary = false;
10491050
};
10501051

10511052
struct TAlterTableParameters {

ydb/library/yql/sql/v1/query.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,10 @@ class TCreateTableNode final: public TAstListNode {
11401140
break;
11411141
}
11421142

1143+
if (Params.Temporary) {
1144+
opts = L(opts, Q(Y(Q("temporary"))));
1145+
}
1146+
11431147
Add("block", Q(Y(
11441148
Y("let", "sink", Y("DataSink", BuildQuotedAtom(Pos, Table.Service), Scoped->WrapCluster(Table.Cluster, ctx))),
11451149
Y("let", "world", Y(TString(WriteName), "world", "sink", keys, Y("Void"), Q(opts))),

ydb/library/yql/sql/v1/sql_query.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,14 @@ bool TSqlQuery::Statement(TVector<TNodePtr>& blocks, const TRule_sql_stmt_core&
166166

167167
const auto& block = rule.GetBlock3();
168168
ETableType tableType = ETableType::Table;
169+
bool temporary = false;
169170
if (block.HasAlt2() && block.GetAlt2().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_TABLESTORE) {
170171
tableType = ETableType::TableStore;
171172
} else if (block.HasAlt3() && block.GetAlt3().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_EXTERNAL) {
172173
tableType = ETableType::ExternalTable;
174+
} else if (block.HasAlt4() && block.GetAlt4().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_TEMP ||
175+
block.HasAlt5() && block.GetAlt5().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_TEMPORARY) {
176+
temporary = true;
173177
}
174178

175179
bool existingOk = false;
@@ -193,7 +197,7 @@ bool TSqlQuery::Statement(TVector<TNodePtr>& blocks, const TRule_sql_stmt_core&
193197
return false;
194198
}
195199

196-
TCreateTableParameters params{.TableType=tableType};
200+
TCreateTableParameters params{.TableType=tableType, .Temporary=temporary};
197201
if (!CreateTableEntry(rule.GetRule_create_table_entry7(), params)) {
198202
return false;
199203
}

ydb/library/yql/sql/v1/sql_ut.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,40 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
10071007
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
10081008
}
10091009

1010+
Y_UNIT_TEST(CreateTempTable) {
1011+
NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TEMP TABLE t (a int32, primary key(a));");
1012+
UNIT_ASSERT(res.Root);
1013+
1014+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
1015+
if (word == "Write!") {
1016+
UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos,
1017+
line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")) '('temporary))))__"), line);
1018+
}
1019+
};
1020+
1021+
TWordCountHive elementStat = {{TString("Write!"), 0}};
1022+
VerifyProgram(res, elementStat, verifyLine);
1023+
1024+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
1025+
}
1026+
1027+
Y_UNIT_TEST(CreateTemporaryTable) {
1028+
NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TEMPORARY TABLE t (a int32, primary key(a));");
1029+
UNIT_ASSERT(res.Root);
1030+
1031+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
1032+
if (word == "Write!") {
1033+
UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos,
1034+
line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")) '('temporary))))__"), line);
1035+
}
1036+
};
1037+
1038+
TWordCountHive elementStat = {{TString("Write!"), 0}};
1039+
VerifyProgram(res, elementStat, verifyLine);
1040+
1041+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
1042+
}
1043+
10101044
Y_UNIT_TEST(CreateTableDuplicatedPkColumnsFail) {
10111045
NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32 not null, primary key(a, a));");
10121046
UNIT_ASSERT(!res.Root);

0 commit comments

Comments
 (0)