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

Use empty parsing context to build view's select in CREATE VIEW statement #2049

Merged
merged 1 commit into from
Feb 19, 2024
Merged
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
18 changes: 18 additions & 0 deletions ydb/core/kqp/ut/view/view_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ Y_UNIT_TEST_SUITE(TCreateAndDropViewTest) {
UNIT_ASSERT(dropResult.GetIssues().ToString().Contains("Error: Path does not exist"));
}
}

Y_UNIT_TEST(ContextPollution) {
TKikimrRunner kikimr(TKikimrSettings().SetWithSampleTables(false));
EnableViewsFeatureFlag(kikimr);
auto session = kikimr.GetTableClient().CreateSession().GetValueSync().GetSession();

ExecuteDataDefinitionQuery(session, R"(
CREATE VIEW InnerView WITH (security_invoker = TRUE) AS SELECT 1;
)");
ExecuteDataDefinitionQuery(session, R"(
CREATE VIEW OuterView WITH (security_invoker = TRUE) AS SELECT * FROM InnerView;
)");

ExecuteDataDefinitionQuery(session, R"(
DROP VIEW OuterView;
CREATE VIEW OuterView WITH (security_invoker = TRUE) AS SELECT * FROM InnerView;
)");
}
}

Y_UNIT_TEST_SUITE(TSelectFromViewTest) {
Expand Down
43 changes: 31 additions & 12 deletions ydb/library/yql/sql/v1/sql_translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ TString CollectTokens(const TRule_select_stmt& selectStatement) {
return tokenCollector.Tokens;
}

NSQLTranslation::TTranslationSettings CreateViewTranslationSettings(const NSQLTranslation::TTranslationSettings& base) {
NSQLTranslation::TTranslationSettings settings;

settings.ClusterMapping = base.ClusterMapping;
settings.Mode = NSQLTranslation::ESqlMode::LIMITED_VIEW;

return settings;
}

TNodePtr BuildViewSelect(const TRule_select_stmt& query, TContext& ctx) {
const auto viewTranslationSettings = CreateViewTranslationSettings(ctx.Settings);
TContext viewParsingContext(viewTranslationSettings, {}, ctx.Issues);
TSqlSelect select(viewParsingContext, viewTranslationSettings.Mode);
TPosition pos;
auto source = select.Build(query, pos);
if (!source) {
return nullptr;
}
return BuildSelectResult(
pos,
std::move(source),
false,
false,
viewParsingContext.Scoped
);
}

}

namespace NSQLTranslationV1 {
Expand Down Expand Up @@ -4493,19 +4520,11 @@ bool TSqlTranslation::ParseViewQuery(std::map<TString, TDeferredAtom>& features,
const TString queryText = CollectTokens(query);
features["query_text"] = {Ctx.Pos(), queryText};

{
TSqlSelect select(Ctx, Mode);
TPosition pos;
auto source = select.Build(query, pos);
if (!source) {
return false;
}
features["query_ast"] = {BuildSelectResult(pos,
std::move(source),
false,
false,
Ctx.Scoped), Ctx};
const auto viewSelect = BuildViewSelect(query, Ctx);
if (!viewSelect) {
return false;
}
features["query_ast"] = {viewSelect, Ctx};

return true;
}
Expand Down
Loading