Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support temp tables in yql #1585

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ydb/library/yql/sql/v1/SQLv1.g.in
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ object_features: object_feature | LPAREN object_feature (COMMA object_feature)*

object_type_ref: an_id_or_type;

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
create_table_stmt: CREATE (OR REPLACE)? (TABLE | TABLESTORE | EXTERNAL TABLE | TEMP TABLE) (IF NOT EXISTS)? simple_table_ref LPAREN create_table_entry (COMMA create_table_entry)* COMMA? RPAREN
table_inherits?
table_partition_by?
with_table_settings?
Expand Down
3 changes: 2 additions & 1 deletion ydb/library/yql/sql/v1/format/sql_format_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ Y_UNIT_TEST_SUITE(CheckSqlFormatter) {
")\n"
"PARTITION BY HASH (a, b, hash)\n"
"WITH (tiering = 'some');\n"},
{"create table if not exists user(user int32)", "CREATE TABLE IF NOT EXISTS user (\n\tuser int32\n);\n"}
{"create table if not exists user(user int32)", "CREATE TABLE IF NOT EXISTS user (\n\tuser int32\n);\n"},
{"create temp table user(user int32)", "CREATE TEMP TABLE user (\n\tuser int32\n);\n"}
};

TSetup setup;
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 @@ -1046,6 +1046,7 @@ namespace NSQLTranslationV1 {
TVector<TChangefeedDescription> Changefeeds;
TTableSettings TableSettings;
ETableType TableType = ETableType::Table;
bool Temporary = false;
};

struct TAlterTableParameters {
Expand Down
4 changes: 4 additions & 0 deletions ydb/library/yql/sql/v1/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,10 @@ class TCreateTableNode final: public TAstListNode {
break;
}

if (Params.Temporary) {
opts = L(opts, Q(Y(Q("temporary"))));
}

Add("block", Q(Y(
Y("let", "sink", Y("DataSink", BuildQuotedAtom(Pos, Table.Service), Scoped->WrapCluster(Table.Cluster, ctx))),
Y("let", "world", Y(TString(WriteName), "world", "sink", keys, Y("Void"), Q(opts))),
Expand Down
6 changes: 5 additions & 1 deletion ydb/library/yql/sql/v1/sql_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ bool TSqlQuery::Statement(TVector<TNodePtr>& blocks, const TRule_sql_stmt_core&

const auto& block = rule.GetBlock3();
ETableType tableType = ETableType::Table;
bool temporary = false;
if (block.HasAlt2() && block.GetAlt2().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_TABLESTORE) {
tableType = ETableType::TableStore;
} else if (block.HasAlt3() && block.GetAlt3().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_EXTERNAL) {
tableType = ETableType::ExternalTable;
} else if (block.HasAlt4() && block.GetAlt4().GetToken1().GetId() == SQLv1LexerTokens::TOKEN_TEMP) {
temporary = true;
Y_DEBUG_ABORT_UNLESS(block.GetAlt4().GetToken2().GetId() == SQLv1LexerTokens::TOKEN_TABLE);
}

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

TCreateTableParameters params{.TableType=tableType};
TCreateTableParameters params{.TableType=tableType, .Temporary=temporary};
if (!CreateTableEntry(rule.GetRule_create_table_entry7(), params)) {
return false;
}
Expand Down
17 changes: 17 additions & 0 deletions ydb/library/yql/sql/v1/sql_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,23 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
}

Y_UNIT_TEST(CreateTempTable) {
NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TEMP TABLE t (a int32, primary key(a));");
UNIT_ASSERT(res.Root);

TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
if (word == "Write!") {
UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos,
line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")) '('temporary))))__"), line);
}
};

TWordCountHive elementStat = {{TString("Write!"), 0}};
VerifyProgram(res, elementStat, verifyLine);

UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
}

Y_UNIT_TEST(CreateTableDuplicatedPkColumnsFail) {
NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32 not null, primary key(a, a));");
UNIT_ASSERT(!res.Root);
Expand Down
Loading