Skip to content

Commit 358728e

Browse files
authored
fail gracefully on SHOW CREATE TABLE targeting a view (#16423)
1 parent 5098c62 commit 358728e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

ydb/core/kqp/provider/yql_kikimr_datasource.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <yql/essentials/core/yql_expr_optimize.h>
1111
#include <yql/essentials/core/yql_expr_type_annotation.h>
12+
#include <yql/essentials/core/yql_opt_utils.h>
1213
#include <yql/essentials/providers/common/schema/expr/yql_expr_schema.h>
1314
#include <ydb/library/yql/providers/dq/expr_nodes/dqs_expr_nodes.h>
1415
#include <ydb/library/yql/dq/expr_nodes/dq_expr_nodes.h>
@@ -95,6 +96,15 @@ namespace {
9596
using namespace NKikimr;
9697
using namespace NNodes;
9798

99+
bool IsShowCreate(const TExprNode& read) {
100+
if (read.ChildrenSize() <= TKiReadTable::idx_Settings) {
101+
return false;
102+
}
103+
const auto& settings = *read.Child(TKiReadTable::idx_Settings);
104+
return HasSetting(settings, "showCreateTable")
105+
|| HasSetting(settings, "showCreateView");
106+
}
107+
98108
class TKiSourceIntentDeterminationTransformer: public TKiSourceVisitorTransformer {
99109
public:
100110
TKiSourceIntentDeterminationTransformer(TIntrusivePtr<TKikimrSessionContext> sessionCtx)
@@ -795,7 +805,7 @@ class TKikimrDataSource : public TDataProviderBase {
795805
retChildren[0] = newRead;
796806
return ctx.ChangeChildren(*node, std::move(retChildren));
797807
}
798-
} else if (tableDesc.Metadata->Kind == EKikimrTableKind::View) {
808+
} else if (tableDesc.Metadata->Kind == EKikimrTableKind::View && !IsShowCreate(*read)) {
799809
if (!SessionCtx->Config().FeatureFlags.GetEnableViews()) {
800810
ctx.AddError(TIssue(node->Pos(ctx),
801811
"Views are disabled. Please contact your system administrator to enable the feature"));

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,6 +2895,31 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
28952895
}
28962896
}
28972897

2898+
Y_UNIT_TEST(ShowCreateTableOnView) {
2899+
auto serverSettings = TKikimrSettings().SetEnableShowCreate(true);
2900+
2901+
TKikimrRunner kikimr(serverSettings);
2902+
auto db = kikimr.GetQueryClient();
2903+
auto session = db.GetSession().GetValueSync().GetSession();
2904+
2905+
{
2906+
// note: KeyValue is one of the sample tables created in KikimrRunner
2907+
auto result = session.ExecuteQuery(R"(
2908+
CREATE VIEW test_view WITH security_invoker = TRUE AS
2909+
SELECT * FROM KeyValue;
2910+
)", TTxControl::NoTx()).ExtractValueSync();
2911+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
2912+
}
2913+
2914+
{
2915+
auto result = session.ExecuteQuery(R"(
2916+
SHOW CREATE TABLE test_view;
2917+
)", TTxControl::NoTx()).ExtractValueSync();
2918+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::ABORTED, result.GetIssues().ToString());
2919+
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(), "Invalid path type");
2920+
}
2921+
}
2922+
28982923
Y_UNIT_TEST(DdlCache) {
28992924
NKikimrConfig::TAppConfig appConfig;
29002925
auto setting = NKikimrKqp::TKqpSetting();

0 commit comments

Comments
 (0)