Skip to content

[24-3-analytics] CTAS & Sinks fixes #10326

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

Merged
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
4 changes: 2 additions & 2 deletions .github/config/muted_ya.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ ydb/core/kqp/ut/tx KqpLocksTricky.TestNoLocksIssue+withSink
ydb/core/kqp/ut/tx KqpSnapshotRead.ReadOnlyTxWithIndexCommitsOnConcurrentWrite+withSink
ydb/core/kqp/ut/tx KqpSinkTx.InvalidateOnError
ydb/core/kqp/ut/tx KqpSinkMvcc.ReadWriteTxFailsOnConcurrentWrite3
ydb/core/kqp/ut/tx KqpSinkMvcc.OltpNamedStatement
ydb/core/kqp/ut/tx KqpSinkMvcc.OlapNamedStatement
ydb/core/kqp/ut/tx KqpSinkMvcc.OlapMultiSinks
ydb/core/kqp/ut/tx KqpSinkMvcc.OltpMultiSinks
ydb/core/kqp/ut/tx KqpLocks.MixedTxFail
ydb/core/kqp/ut/query KqpLimits.QueryReplySize
ydb/core/kqp/ut/query KqpQuery.QueryTimeout
ydb/core/kqp/ut/service KqpQueryService.TableSink_OlapRWQueries
Expand All @@ -37,6 +36,7 @@ ydb/core/kqp/ut/scheme [44/50]*
ydb/core/kqp/ut/service KqpQueryService.ExecuteQueryPgTableSelect
ydb/core/kqp/ut/service KqpQueryService.QueryOnClosedSession
ydb/core/kqp/ut/service KqpQueryService.TableSink_OltpUpdate
ydb/core/kqp/ut/service KqpQueryService.TableSink_Htap*
ydb/core/kqp/ut/service KqpService.CloseSessionsWithLoad
ydb/core/kqp/ut/service [38/50]*
ydb/core/persqueue/ut [37/40] chunk chunk
Expand Down
106 changes: 94 additions & 12 deletions ydb/core/kqp/opt/kqp_opt_build_txs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,20 @@ class TKqpBuildTxsTransformer : public TSyncTransformerBase {
}

if (!query.Effects().Empty()) {
auto tx = BuildTx(query.Effects().Ptr(), ctx, /* isPrecompute */ false);
if (!tx) {
return TStatus::Error;
}
auto collectedEffects = CollectEffects(query.Effects(), ctx);

if (!CheckEffectsTx(tx.Cast(), query, ctx)) {
return TStatus::Error;
}
for (auto& effects : collectedEffects) {
auto tx = BuildTx(effects.Ptr(), ctx, /* isPrecompute */ false);
if (!tx) {
return TStatus::Error;
}

BuildCtx->PhysicalTxs.emplace_back(tx.Cast());
if (!CheckEffectsTx(tx.Cast(), effects, ctx)) {
return TStatus::Error;
}

BuildCtx->PhysicalTxs.emplace_back(tx.Cast());
}
}

return TStatus::Ok;
Expand All @@ -581,8 +585,86 @@ class TKqpBuildTxsTransformer : public TSyncTransformerBase {
}

private:
bool HasTableEffects(const TKqlQuery& query) const {
for (const TExprBase& effect : query.Effects()) {
TVector<TExprList> CollectEffects(const TExprList& list, TExprContext& ctx) {
struct TEffectsInfo {
enum class EType {
KQP_EFFECT,
KQP_SINK,
EXTERNAL_SINK,
};

EType Type;
THashSet<TStringBuf> TablesPathIds;
TVector<TExprNode::TPtr> Exprs;
};
TVector<TEffectsInfo> effectsInfos;

for (const auto& expr : list) {
if (auto sinkEffect = expr.Maybe<TKqpSinkEffect>()) {
const size_t sinkIndex = FromString(TStringBuf(sinkEffect.Cast().SinkIndex()));
const auto stage = sinkEffect.Cast().Stage().Maybe<TDqStageBase>();
YQL_ENSURE(stage);
YQL_ENSURE(stage.Cast().Outputs());
const auto outputs = stage.Cast().Outputs().Cast();
YQL_ENSURE(sinkIndex < outputs.Size());
const auto sink = outputs.Item(sinkIndex).Maybe<TDqSink>();
YQL_ENSURE(sink);

const auto sinkSettings = sink.Cast().Settings().Maybe<TKqpTableSinkSettings>();
if (!sinkSettings) {
// External writes always use their own physical transaction.
effectsInfos.emplace_back();
effectsInfos.back().Type = TEffectsInfo::EType::EXTERNAL_SINK;
effectsInfos.back().Exprs.push_back(expr.Ptr());
} else {
// Two table sinks can't be executed in one physical transaction if they write into one table.
const TStringBuf tablePathId = sinkSettings.Cast().Table().PathId().Value();

auto it = std::find_if(
std::begin(effectsInfos),
std::end(effectsInfos),
[&tablePathId](const auto& effectsInfo) {
return effectsInfo.Type == TEffectsInfo::EType::KQP_SINK
&& !effectsInfo.TablesPathIds.contains(tablePathId);
});
if (it == std::end(effectsInfos)) {
effectsInfos.emplace_back();
it = std::prev(std::end(effectsInfos));
it->Type = TEffectsInfo::EType::KQP_SINK;
}
it->TablesPathIds.insert(tablePathId);
it->Exprs.push_back(expr.Ptr());
}
} else {
// Table effects are executed all in one physical transaction.
auto it = std::find_if(
std::begin(effectsInfos),
std::end(effectsInfos),
[](const auto& effectsInfo) { return effectsInfo.Type == TEffectsInfo::EType::KQP_EFFECT; });
if (it == std::end(effectsInfos)) {
effectsInfos.emplace_back();
it = std::prev(std::end(effectsInfos));
it->Type = TEffectsInfo::EType::KQP_EFFECT;
}
it->Exprs.push_back(expr.Ptr());
}
}

TVector<TExprList> results;

for (const auto& effects : effectsInfos) {
auto builder = Build<TExprList>(ctx, list.Pos());
for (const auto& expr : effects.Exprs) {
builder.Add(expr);
}
results.push_back(builder.Done());
}

return results;
}

bool HasTableEffects(const TExprList& effectsList) const {
for (const TExprBase& effect : effectsList) {
if (auto maybeSinkEffect = effect.Maybe<TKqpSinkEffect>()) {
// (KqpSinkEffect (DqStage (... ((DqSink '0 (DataSink '"kikimr") ...)))) '0)
auto sinkEffect = maybeSinkEffect.Cast();
Expand All @@ -608,7 +690,7 @@ class TKqpBuildTxsTransformer : public TSyncTransformerBase {
return false;
}

bool CheckEffectsTx(TKqpPhysicalTx tx, const TKqlQuery& query, TExprContext& ctx) const {
bool CheckEffectsTx(TKqpPhysicalTx tx, const TExprList& effectsList, TExprContext& ctx) const {
TMaybeNode<TExprBase> blackistedNode;
VisitExpr(tx.Ptr(), [&blackistedNode](const TExprNode::TPtr& exprNode) {
if (blackistedNode) {
Expand All @@ -630,7 +712,7 @@ class TKqpBuildTxsTransformer : public TSyncTransformerBase {
return true;
});

if (blackistedNode && HasTableEffects(query)) {
if (blackistedNode && HasTableEffects(effectsList)) {
ctx.AddError(TIssue(ctx.GetPosition(blackistedNode.Cast().Pos()), TStringBuilder()
<< "Callable not expected in effects tx: " << blackistedNode.Cast<TCallable>().CallableName()));
return false;
Expand Down
3 changes: 2 additions & 1 deletion ydb/core/kqp/session_actor/kqp_session_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ class TKqpSessionActor : public TActorBootstrapped<TKqpSessionActor> {
QueryState->TxCtx->HasOlapTable |= ::NKikimr::NKqp::HasOlapTableReadInTx(phyQuery) || ::NKikimr::NKqp::HasOlapTableWriteInTx(phyQuery);
QueryState->TxCtx->HasOltpTable |= ::NKikimr::NKqp::HasOltpTableReadInTx(phyQuery) || ::NKikimr::NKqp::HasOltpTableWriteInTx(phyQuery);
QueryState->TxCtx->HasTableWrite |= ::NKikimr::NKqp::HasOlapTableWriteInTx(phyQuery) || ::NKikimr::NKqp::HasOltpTableWriteInTx(phyQuery);
if (QueryState->TxCtx->HasOlapTable && QueryState->TxCtx->HasOltpTable && QueryState->TxCtx->HasTableWrite && !Settings.TableService.GetEnableHtapTx()) {
if (QueryState->TxCtx->HasOlapTable && QueryState->TxCtx->HasOltpTable && QueryState->TxCtx->HasTableWrite
&& !Settings.TableService.GetEnableHtapTx() && !QueryState->IsSplitted()) {
ReplyQueryError(Ydb::StatusIds::PRECONDITION_FAILED,
"Write transactions between column and row tables are disabled at current time.");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,7 @@ Y_UNIT_TEST_SUITE(KqpFederatedQuery) {
auto db = kikimr->GetQueryClient();
auto resultFuture = db.ExecuteQuery(sql, NYdb::NQuery::TTxControl::BeginTx().CommitTx());
resultFuture.Wait();
UNIT_ASSERT_C(!resultFuture.GetValueSync().IsSuccess(), resultFuture.GetValueSync().GetIssues().ToString());
UNIT_ASSERT_STRING_CONTAINS(resultFuture.GetValueSync().GetIssues().ToString(), "Callable not expected in effects tx: Unwrap");
UNIT_ASSERT_C(resultFuture.GetValueSync().IsSuccess(), resultFuture.GetValueSync().GetIssues().ToString());
}
}

Expand Down
Loading
Loading