Skip to content

Commit 69a7cf8

Browse files
committed
Support SHOW CREATE TABLE statement
commit_hash:01d41da47d238ae4a86642bd8cdb8c245d9c503a
1 parent 50c6140 commit 69a7cf8

File tree

10 files changed

+150
-1
lines changed

10 files changed

+150
-1
lines changed

yql/essentials/sql/v1/SQLv1.g.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ sql_stmt_core:
8080
| alter_transfer_stmt
8181
| drop_transfer_stmt
8282
| alter_database_stmt
83+
| show_create_table_stmt
8384
;
8485

8586
expr:
@@ -1098,6 +1099,8 @@ alter_sequence_action:
10981099
| INCREMENT BY? integer
10991100
;
11001101

1102+
show_create_table_stmt: SHOW CREATE TABLE simple_table_ref;
1103+
11011104
// Special rules that allow to use certain keywords as identifiers.
11021105
identifier: ID_PLAIN | ID_QUOTED;
11031106
id: identifier | keyword;

yql/essentials/sql/v1/SQLv1Antlr4.g.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ sql_stmt_core:
7979
| alter_transfer_stmt
8080
| drop_transfer_stmt
8181
| alter_database_stmt
82+
| show_create_table_stmt
8283
;
8384

8485
expr:
@@ -1098,6 +1099,8 @@ alter_sequence_action:
10981099
| INCREMENT BY? integer
10991100
;
11001101

1102+
show_create_table_stmt: SHOW CREATE TABLE simple_table_ref;
1103+
11011104
// Special rules that allow to use certain keywords as identifiers.
11021105
identifier: ID_PLAIN | ID_QUOTED;
11031106
id: identifier | keyword;

yql/essentials/sql/v1/format/sql_format.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,11 @@ friend struct TStaticData;
944944
VisitAllFields(TRule_alter_sequence_stmt::GetDescriptor(), msg);
945945
}
946946

947+
void VisitShowCreateTable(const TRule_show_create_table_stmt& msg) {
948+
NewLine();
949+
VisitAllFields(TRule_show_create_table_stmt::GetDescriptor(), msg);
950+
}
951+
947952
void VisitIntoTable(const TRule_into_table_stmt& msg) {
948953
NewLine();
949954
VisitAllFields(TRule_into_table_stmt::GetDescriptor(), msg);
@@ -3021,6 +3026,7 @@ TStaticData::TStaticData()
30213026
{TRule_restore_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRestore)},
30223027
{TRule_alter_sequence_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterSequence)},
30233028
{TRule_alter_database_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterDatabase)},
3029+
{TRule_show_create_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitShowCreateTable)},
30243030
})
30253031
, ObfuscatingVisitDispatch({
30263032
{TToken::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitToken)},

yql/essentials/sql/v1/format/sql_format_ut.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ Y_UNIT_TEST(AlterSequence) {
144144
setup.Run(cases);
145145
}
146146

147+
Y_UNIT_TEST(ShowCreateTable) {
148+
TCases cases = {
149+
{"use plato;show create table user;","USE plato;\n\nSHOW CREATE TABLE user;\n"},
150+
};
151+
152+
TSetup setup;
153+
setup.Run(cases);
154+
}
147155

148156
Y_UNIT_TEST(Use) {
149157
TCases cases = {

yql/essentials/sql/v1/query.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,80 @@ TNodePtr BuildAnalyze(TPosition pos, const TString& service, const TDeferredAtom
36723672
return new TAnalyzeNode(pos, service, cluster, params, scoped);
36733673
}
36743674

3675+
class TShowCreateNode final : public TAstListNode {
3676+
public:
3677+
TShowCreateNode(TPosition pos, const TTableRef& tr, TScopedStatePtr scoped)
3678+
: TAstListNode(pos)
3679+
, Table(tr)
3680+
, Scoped(scoped)
3681+
, FakeSource(BuildFakeSource(pos))
3682+
{
3683+
Scoped->UseCluster(Table.Service, Table.Cluster);
3684+
}
3685+
3686+
bool DoInit(TContext& ctx, ISource* src) override {
3687+
if (Table.Options) {
3688+
if (!Table.Options->Init(ctx, src)) {
3689+
return false;
3690+
}
3691+
Table.Options = L(Table.Options, Q(Y(Q("showCreateTable"))));
3692+
} else {
3693+
Table.Options = Y(Q(Y(Q("showCreateTable"))));
3694+
}
3695+
3696+
bool asRef = ctx.PragmaRefSelect;
3697+
bool asAutoRef = true;
3698+
if (ctx.PragmaSampleSelect) {
3699+
asRef = false;
3700+
asAutoRef = false;
3701+
}
3702+
3703+
auto settings = Y(Q(Y(Q("type"))));
3704+
if (asRef) {
3705+
settings = L(settings, Q(Y(Q("ref"))));
3706+
} else if (asAutoRef) {
3707+
settings = L(settings, Q(Y(Q("autoref"))));
3708+
}
3709+
3710+
TNodePtr node(BuildInputTables(Pos, {Table}, false, Scoped));
3711+
if (!node->Init(ctx, src)) {
3712+
return false;
3713+
}
3714+
3715+
auto source = BuildTableSource(TPosition(ctx.Pos()), Table);
3716+
if (!source) {
3717+
return false;
3718+
}
3719+
auto output = source->Build(ctx);
3720+
if (!output) {
3721+
return false;
3722+
}
3723+
node = L(node, Y("let", "output", output));
3724+
3725+
auto writeResult(BuildWriteResult(Pos, "output", settings));
3726+
if (!writeResult->Init(ctx, src)) {
3727+
return false;
3728+
}
3729+
node = L(node, Y("let", "world", writeResult));
3730+
node = L(node, Y("return", "world"));
3731+
Add("block", Q(node));
3732+
3733+
return TAstListNode::DoInit(ctx, FakeSource.Get());
3734+
}
3735+
3736+
TPtr DoClone() const final {
3737+
return {};
3738+
}
3739+
private:
3740+
TTableRef Table;
3741+
TScopedStatePtr Scoped;
3742+
TSourcePtr FakeSource;
3743+
};
3744+
3745+
TNodePtr BuildShowCreate(TPosition pos, const TTableRef& tr, TScopedStatePtr scoped) {
3746+
return new TShowCreateNode(pos, tr, scoped);
3747+
}
3748+
36753749
class TBaseBackupCollectionNode
36763750
: public TAstListNode
36773751
, public TObjectOperatorContext

yql/essentials/sql/v1/source.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ namespace NSQLTranslationV1 {
317317
TNodePtr BuildWriteTable(TPosition pos, const TString& label, const TTableRef& table, EWriteColumnMode mode, TNodePtr options,
318318
TScopedStatePtr scoped);
319319
TNodePtr BuildAnalyze(TPosition pos, const TString& service, const TDeferredAtom& cluster, const TAnalyzeParams& params, TScopedStatePtr scoped);
320+
TNodePtr BuildShowCreate(TPosition pos, const TTableRef& table, TScopedStatePtr scoped);
320321
TNodePtr BuildAlterSequence(TPosition pos, const TString& service, const TDeferredAtom& cluster, const TString& id, const TSequenceParameters& params, TScopedStatePtr scoped);
321322
TSourcePtr TryMakeSourceFromExpression(TPosition pos, TContext& ctx, const TString& currService, const TDeferredAtom& currCluster,
322323
TNodePtr node, const TString& view = {});

yql/essentials/sql/v1/sql.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ bool NeedUseForAllStatements(const TRule_sql_stmt_core::AltCase& subquery) {
182182
case TRule_sql_stmt_core::kAltSqlStmtCore59: // alter transfer
183183
case TRule_sql_stmt_core::kAltSqlStmtCore60: // drop transfer
184184
case TRule_sql_stmt_core::kAltSqlStmtCore61: // alter database
185+
case TRule_sql_stmt_core::kAltSqlStmtCore62: // show create table
185186
return false;
186187
}
187188
}

yql/essentials/sql/v1/sql_query.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,19 @@ bool TSqlQuery::Statement(TVector<TNodePtr>& blocks, const TRule_sql_stmt_core&
19491949
AddStatementToBlocks(blocks, stmt);
19501950
break;
19511951
}
1952+
case TRule_sql_stmt_core::kAltSqlStmtCore62: {
1953+
// show_create_table_stmt: SHOW CREATE TABLE table_ref
1954+
Ctx.BodyPart();
1955+
const auto& rule = core.GetAlt_sql_stmt_core62().GetRule_show_create_table_stmt1();
1956+
1957+
TTableRef tr;
1958+
if (!SimpleTableRefImpl(rule.GetRule_simple_table_ref4(), tr)) {
1959+
return false;
1960+
}
1961+
1962+
AddStatementToBlocks(blocks, BuildShowCreate(Ctx.Pos(), tr, Ctx.Scoped));
1963+
break;
1964+
}
19521965
case TRule_sql_stmt_core::ALT_NOT_SET:
19531966
Ctx.IncrementMonCounter("sql_errors", "UnknownStatement" + internalStatementName);
19541967
AltNotImplemented("sql_stmt_core", core);

yql/essentials/sql/v1/sql_ut.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,6 +2946,26 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
29462946
}
29472947
}
29482948

2949+
Y_UNIT_TEST(ShowCreateTable) {
2950+
NYql::TAstParseResult res = SqlToYql(R"(
2951+
USE plato;
2952+
SHOW CREATE TABLE user;
2953+
)");
2954+
UNIT_ASSERT(res.Root);
2955+
2956+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
2957+
if (word == "Read") {
2958+
UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("showCreateTable"));
2959+
}
2960+
};
2961+
2962+
TWordCountHive elementStat = {{TString("Read"), 0}, {TString("showCreateTable"), 0}};
2963+
VerifyProgram(res, elementStat, verifyLine);
2964+
2965+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read"]);
2966+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["showCreateTable"]);
2967+
}
2968+
29492969
Y_UNIT_TEST(OptionalAliases) {
29502970
UNIT_ASSERT(SqlToYql("USE plato; SELECT foo FROM (SELECT key foo FROM Input);").IsOk());
29512971
UNIT_ASSERT(SqlToYql("USE plato; SELECT a.x FROM Input1 a JOIN Input2 b ON a.key = b.key;").IsOk());

yql/essentials/sql/v1/sql_ut_antlr4.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,6 +3064,26 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
30643064
}
30653065
}
30663066

3067+
Y_UNIT_TEST(ShowCreateTable) {
3068+
NYql::TAstParseResult res = SqlToYql(R"(
3069+
USE plato;
3070+
SHOW CREATE TABLE user;
3071+
)");
3072+
UNIT_ASSERT(res.Root);
3073+
3074+
TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
3075+
if (word == "Read") {
3076+
UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("showCreateTable"));
3077+
}
3078+
};
3079+
3080+
TWordCountHive elementStat = {{TString("Read"), 0}, {TString("showCreateTable"), 0}};
3081+
VerifyProgram(res, elementStat, verifyLine);
3082+
3083+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read"]);
3084+
UNIT_ASSERT_VALUES_EQUAL(1, elementStat["showCreateTable"]);
3085+
}
3086+
30673087
Y_UNIT_TEST(OptionalAliases) {
30683088
UNIT_ASSERT(SqlToYql("USE plato; SELECT foo FROM (SELECT key foo FROM Input);").IsOk());
30693089
UNIT_ASSERT(SqlToYql("USE plato; SELECT a.x FROM Input1 a JOIN Input2 b ON a.key = b.key;").IsOk());
@@ -5972,7 +5992,7 @@ Y_UNIT_TEST_SUITE(AnsiIdentsNegative) {
59725992
"*/ select 1;";
59735993
res = SqlToYql(req);
59745994
UNIT_ASSERT(!res.Root);
5975-
UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:0: Error: mismatched input '*' expecting {';', '(', '$', ALTER, ANALYZE, BACKUP, BATCH, COMMIT, CREATE, DECLARE, DEFINE, DELETE, DISCARD, DO, DROP, EVALUATE, EXPLAIN, EXPORT, FOR, FROM, GRANT, IF, IMPORT, INSERT, PARALLEL, PRAGMA, PROCESS, REDUCE, REPLACE, RESTORE, REVOKE, ROLLBACK, SELECT, UPDATE, UPSERT, USE, VALUES}\n");
5995+
UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:0: Error: mismatched input '*' expecting {';', '(', '$', ALTER, ANALYZE, BACKUP, BATCH, COMMIT, CREATE, DECLARE, DEFINE, DELETE, DISCARD, DO, DROP, EVALUATE, EXPLAIN, EXPORT, FOR, FROM, GRANT, IF, IMPORT, INSERT, PARALLEL, PRAGMA, PROCESS, REDUCE, REPLACE, RESTORE, REVOKE, ROLLBACK, SELECT, SHOW, UPDATE, UPSERT, USE, VALUES}\n");
59765996
res = SqlToYqlWithAnsiLexer(req);
59775997
UNIT_ASSERT(res.Root);
59785998
}

0 commit comments

Comments
 (0)