Skip to content

Commit 2bcae57

Browse files
Merge 621daf0 into 4d68f9b
2 parents 4d68f9b + 621daf0 commit 2bcae57

File tree

13 files changed

+74
-53
lines changed

13 files changed

+74
-53
lines changed

ydb/core/kqp/provider/yql_kikimr_exec.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ class TKiSourceCallableExecutionTransformer : public TAsyncCallbackTransformer<T
764764
lambda = NDq::BuildProgram(
765765
programLambda, *paramsType, compiler, SessionCtx->Query().QueryData->GetAllocState()->TypeEnv,
766766
*SessionCtx->Query().QueryData->GetAllocState()->HolderFactory.GetFunctionRegistry(),
767-
ctx, fakeReads);
767+
ctx, fakeReads, {});
768768

769769
NKikimr::NMiniKQL::TProgramBuilder programBuilder(SessionCtx->Query().QueryData->GetAllocState()->TypeEnv,
770770
*SessionCtx->Query().QueryData->GetAllocState()->HolderFactory.GetFunctionRegistry());

ydb/core/kqp/query_compiler/kqp_query_compiler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ class TKqpQueryCompiler : public IKqpQueryCompiler {
778778

779779
auto paramsType = CollectParameters(stage, ctx);
780780
auto programBytecode = NDq::BuildProgram(stage.Program(), *paramsType, *KqlCompiler, TypeEnv, FuncRegistry,
781-
ctx, {});
781+
ctx, {}, {});
782782

783783
auto& programProto = *stageProto.MutableProgram();
784784
programProto.SetRuntimeVersion(NYql::NDqProto::ERuntimeVersion::RUNTIME_VERSION_YQL_1_0);

ydb/library/yql/dq/tasks/dq_task_program.cpp

+36-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,41 @@
22

33
#include <ydb/library/yql/core/yql_expr_optimize.h>
44
#include <ydb/library/yql/minikql/mkql_node_serialization.h>
5+
#include <ydb/library/yql/minikql/mkql_runtime_version.h>
56
#include <ydb/library/yql/providers/common/mkql/yql_type_mkql.h>
67

78
namespace NYql::NDq {
89

910
using namespace NKikimr::NMiniKQL;
1011
using namespace NYql::NNodes;
1112

13+
14+
class TSpillingTransformProvider {
15+
public:
16+
17+
TSpillingTransformProvider(const TSpillingSettings& spillingSettings): SpillingSettings(spillingSettings){};
18+
19+
TCallableVisitFunc operator()(TInternName name) {
20+
if (RuntimeVersion >= 50U && SpillingSettings.EnableSpillingInGraceJoin && (name == "GraceJoin" || name == "GraceSelfJoin")) {
21+
return [name](NKikimr::NMiniKQL::TCallable& callable, const TTypeEnvironment& env) {
22+
TCallableBuilder callableBuilder(env,
23+
TStringBuilder() << callable.GetType()->GetName() << "WithSpilling",
24+
callable.GetType()->GetReturnType(), false);
25+
for (ui32 i = 0; i < callable.GetInputsCount(); ++i) {
26+
callableBuilder.Add(callable.GetInput(i));
27+
}
28+
return TRuntimeNode(callableBuilder.Build(), false);
29+
};
30+
}
31+
32+
return TCallableVisitFunc();
33+
}
34+
35+
private:
36+
37+
TSpillingSettings SpillingSettings;
38+
};
39+
1240
const TStructExprType* CollectParameters(NNodes::TCoLambda program, TExprContext& ctx) {
1341
TVector<const TItemExprType*> memberTypes;
1442

@@ -27,7 +55,7 @@ const TStructExprType* CollectParameters(NNodes::TCoLambda program, TExprContext
2755

2856
TString BuildProgram(NNodes::TCoLambda program, const TStructExprType& paramsType,
2957
const NCommon::IMkqlCallableCompiler& compiler, const TTypeEnvironment& typeEnv,
30-
const IFunctionRegistry& funcRegistry, TExprContext& exprCtx, const TVector<TExprBase>& reads)
58+
const IFunctionRegistry& funcRegistry, TExprContext& exprCtx, const TVector<TExprBase>& reads, const TSpillingSettings& spillingSettings)
3159
{
3260
TProgramBuilder pgmBuilder(typeEnv, funcRegistry);
3361

@@ -49,6 +77,13 @@ TString BuildProgram(NNodes::TCoLambda program, const TStructExprType& paramsTyp
4977

5078
TRuntimeNode rootNode = MkqlBuildExpr(program.Body().Ref(), ctx);
5179

80+
TExploringNodeVisitor explorer;
81+
if (spillingSettings) {
82+
explorer.Walk(rootNode.GetNode(), typeEnv);
83+
bool wereChanges = false;
84+
rootNode = SinglePassVisitCallables(rootNode, explorer, TSpillingTransformProvider(spillingSettings), typeEnv, true, wereChanges);
85+
}
86+
5287
TStructLiteralBuilder structBuilder(typeEnv);
5388
structBuilder.Add("Program", rootNode);
5489
structBuilder.Add("Inputs", pgmBuilder.NewTuple(inputNodes));
@@ -64,7 +99,6 @@ TString BuildProgram(NNodes::TCoLambda program, const TStructExprType& paramsTyp
6499

65100
auto programNode = structBuilder.Build();
66101

67-
TExploringNodeVisitor explorer;
68102
explorer.Walk(programNode, typeEnv);
69103
ui32 uniqueId = 0;
70104
for (auto& node : explorer.GetNodes()) {

ydb/library/yql/dq/tasks/dq_task_program.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@
99

1010
namespace NYql::NDq {
1111

12+
struct TSpillingSettings {
13+
operator bool() const {
14+
return EnableSpillingInGraceJoin;
15+
}
16+
17+
bool EnableSpillingInGraceJoin = false;
18+
};
19+
1220
const TStructExprType* CollectParameters(NNodes::TCoLambda program, TExprContext& ctx);
1321

1422
TString BuildProgram(NNodes::TCoLambda program, const TStructExprType& paramsType,
1523
const NCommon::IMkqlCallableCompiler& compiler, const NKikimr::NMiniKQL::TTypeEnvironment& typeEnv,
1624
const NKikimr::NMiniKQL::IFunctionRegistry& funcRegistry, TExprContext& exprCtx,
17-
const TVector<NNodes::TExprBase>& reads);
25+
const TVector<NNodes::TExprBase>& reads, const TSpillingSettings& spillingSettings);
1826

1927
} // namespace NYql::NDq

ydb/library/yql/minikql/comp_nodes/mkql_factory.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ struct TCallableComputationNodeBuilderFuncMapFiller {
234234
{"JoinDict", &WrapJoinDict},
235235
{"GraceJoin", &WrapGraceJoin},
236236
{"GraceSelfJoin", &WrapGraceSelfJoin},
237-
{"GraceJoinWithSpilling", &WrapGraceJoinWithSpilling},
238-
{"GraceSelfJoinWithSpilling", &WrapGraceSelfJoinWithSpilling},
237+
{"GraceJoinWithSpilling", &WrapGraceJoin},
238+
{"GraceSelfJoinWithSpilling", &WrapGraceSelfJoin},
239239
{"MapJoinCore", &WrapMapJoinCore},
240240
{"CommonJoinCore", &WrapCommonJoinCore},
241241
{"CombineCore", &WrapCombineCore},

ydb/library/yql/minikql/comp_nodes/mkql_grace_join.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -1171,28 +1171,15 @@ IComputationNode* WrapGraceJoinCommon(TCallable& callable, const TComputationNod
11711171
IComputationNode* WrapGraceJoin(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
11721172
MKQL_ENSURE(callable.GetInputsCount() == 8, "Expected 8 args");
11731173

1174-
return WrapGraceJoinCommon(callable, ctx, false, false);
1174+
return WrapGraceJoinCommon(callable, ctx, false, callable.IsSpillingSupported());
11751175
}
11761176

11771177
IComputationNode* WrapGraceSelfJoin(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
11781178
MKQL_ENSURE(callable.GetInputsCount() == 7, "Expected 7 args");
11791179

1180-
return WrapGraceJoinCommon(callable, ctx, true, false);
1180+
return WrapGraceJoinCommon(callable, ctx, true, callable.IsSpillingSupported());
11811181
}
11821182

1183-
IComputationNode* WrapGraceJoinWithSpilling(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
1184-
MKQL_ENSURE(callable.GetInputsCount() == 8, "Expected 8 args");
1185-
1186-
return WrapGraceJoinCommon(callable, ctx, false, true);
1187-
}
1188-
1189-
IComputationNode* WrapGraceSelfJoinWithSpilling(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
1190-
MKQL_ENSURE(callable.GetInputsCount() == 7, "Expected 7 args");
1191-
1192-
return WrapGraceJoinCommon(callable, ctx, true, true);
1193-
}
1194-
1195-
11961183
}
11971184

11981185
}

ydb/library/yql/minikql/mkql_node.h

+4
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,10 @@ friend class TNode;
10751075
UniqueId = uniqueId;
10761076
}
10771077

1078+
bool IsSpillingSupported() const {
1079+
return TStringBuf(GetType()->GetName()).EndsWith("WithSpilling"_sb);
1080+
}
1081+
10781082
private:
10791083
TCallable(ui32 inputsCount, TRuntimeNode* inputs, TCallableType* type, bool validate = true);
10801084
TCallable(TRuntimeNode result, TCallableType* type, bool validate = true);

ydb/library/yql/minikql/mkql_program_builder.cpp

-22
Original file line numberDiff line numberDiff line change
@@ -2160,28 +2160,6 @@ TRuntimeNode TProgramBuilder::GraceSelfJoin(TRuntimeNode flowLeft, EJoinKind jo
21602160
return GraceJoinCommon(__func__, flowLeft, {}, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings);
21612161
}
21622162

2163-
TRuntimeNode TProgramBuilder::GraceJoinWithSpilling(TRuntimeNode flowLeft, TRuntimeNode flowRight, EJoinKind joinKind,
2164-
const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
2165-
const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings ) {
2166-
2167-
if constexpr (RuntimeVersion < 50U) {
2168-
THROW yexception() << "Runtime version (" << RuntimeVersion << ") too old for " << __func__;
2169-
}
2170-
2171-
return GraceJoinCommon(__func__, flowLeft, flowRight, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings);
2172-
}
2173-
2174-
TRuntimeNode TProgramBuilder::GraceSelfJoinWithSpilling(TRuntimeNode flowLeft, EJoinKind joinKind,
2175-
const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
2176-
const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings ) {
2177-
2178-
if constexpr (RuntimeVersion < 50U) {
2179-
THROW yexception() << "Runtime version (" << RuntimeVersion << ") too old for " << __func__;
2180-
}
2181-
2182-
return GraceJoinCommon(__func__, flowLeft, {}, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings);
2183-
}
2184-
21852163
TRuntimeNode TProgramBuilder::ToSortedDict(TRuntimeNode list, bool all, const TUnaryLambda& keySelector,
21862164
const TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint) {
21872165
return ToDict(list, all, keySelector, payloadSelector, __func__, isCompact, itemsCountHint);

ydb/library/yql/providers/common/mkql/yql_provider_mkql.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -1704,13 +1704,6 @@ TMkqlCommonCallableCompiler::TShared::TShared() {
17041704

17051705
const auto returnType = BuildType(node, *node.GetTypeAnn(), ctx.ProgramBuilder);
17061706

1707-
// TODO: use PRAGMA
1708-
bool IsSpillingAllowed = false;
1709-
if (RuntimeVersion >= 50U && IsSpillingAllowed) {
1710-
return selfJoin
1711-
? ctx.ProgramBuilder.GraceSelfJoinWithSpilling(flowLeft, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings)
1712-
: ctx.ProgramBuilder.GraceJoinWithSpilling(flowLeft, flowRight, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings);
1713-
}
17141707
return selfJoin
17151708
? ctx.ProgramBuilder.GraceSelfJoin(flowLeft, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings)
17161709
: ctx.ProgramBuilder.GraceJoin(flowLeft, flowRight, joinKind, leftKeyColumns, rightKeyColumns, leftRenames, rightRenames, returnType, anyJoinSettings);

ydb/library/yql/providers/dq/common/yql_dq_settings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ TDqConfiguration::TDqConfiguration() {
9898

9999
REGISTER_SETTING(*this, _MaxAttachmentsSize);
100100
REGISTER_SETTING(*this, DisableCheckpoints);
101+
REGISTER_SETTING(*this, EnabledSpillingNodes).Parser([](const TString& v) { return FromString<EEnabledSpillingNodes>(v); });;
101102
}
102103

103104
} // namespace NYql

ydb/library/yql/providers/dq/common/yql_dq_settings.h

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ struct TDqSettings {
2828
File /* "file" */,
2929
};
3030

31+
enum class EEnabledSpillingNodes {
32+
None,
33+
OnlyGraceJoin,
34+
All,
35+
};
36+
3137
struct TDefault {
3238
static constexpr ui32 MaxTasksPerStage = 20U;
3339
static constexpr ui32 MaxTasksPerOperation = 70U;
@@ -60,6 +66,7 @@ struct TDqSettings {
6066
static constexpr ui32 MaxDPccpDPTableSize = 16400U;
6167
static constexpr ui64 MaxAttachmentsSize = 2_GB;
6268
static constexpr bool SplitStageOnDqReplicate = true;
69+
static constexpr EEnabledSpillingNodes EnabledSpillingNodes = EEnabledSpillingNodes::None;
6370
};
6471

6572
using TPtr = std::shared_ptr<TDqSettings>;
@@ -131,6 +138,8 @@ struct TDqSettings {
131138
NCommon::TConfSetting<bool, false> DisableLLVMForBlockStages;
132139
NCommon::TConfSetting<bool, false> SplitStageOnDqReplicate;
133140

141+
NCommon::TConfSetting<EEnabledSpillingNodes, false> EnabledSpillingNodes;
142+
134143
NCommon::TConfSetting<ui64, false> _MaxAttachmentsSize;
135144
NCommon::TConfSetting<bool, false> DisableCheckpoints;
136145

@@ -215,6 +224,11 @@ struct TDqSettings {
215224
return SpillingEngine.Get().GetOrElse(TDqSettings::TDefault::SpillingEngine) != ESpillingEngine::Disable;
216225
}
217226

227+
bool IsSpillingInGraceJoinEnabled() const {
228+
auto enabledNodes = EnabledSpillingNodes.Get().GetOrElse(TDqSettings::TDefault::EnabledSpillingNodes);
229+
return IsSpillingEnabled() && (enabledNodes == EEnabledSpillingNodes::OnlyGraceJoin || enabledNodes == EEnabledSpillingNodes::All);
230+
}
231+
218232
bool IsDqReplicateEnabled(const TTypeAnnotationContext& typesCtx) const {
219233
return EnableDqReplicate.Get().GetOrElse(
220234
typesCtx.BlockEngineMode != EBlockEngineMode::Disable || TDqSettings::TDefault::EnableDqReplicate);

ydb/library/yql/providers/dq/planner/execution_planner.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,11 @@ namespace NYql::NDqs {
676676
Y_ABORT_UNLESS(false);
677677
}
678678
*/
679+
TSpillingSettings spillingSettings{Settings->IsSpillingInGraceJoinEnabled()};
679680
StagePrograms[stageInfo.first] = std::make_tuple(
680681
NDq::BuildProgram(
681682
stage.Program(), *paramsType, compiler, typeEnv, *FunctionRegistry,
682-
ExprContext, fakeReads),
683+
ExprContext, fakeReads, spillingSettings),
683684
stageId, publicId);
684685
}
685686
}

ydb/library/yql/providers/dq/provider/exec/yql_dq_exectransformer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,10 @@ class TDqExecTransformer: public TExecTransformerBase, TCounters
760760

761761
TVector<TExprBase> fakeReads;
762762
auto paramsType = NDq::CollectParameters(programLambda, ctx);
763+
NDq::TSpillingSettings spillingSettings{State->Settings->IsSpillingInGraceJoinEnabled()};
763764
*lambda = NDq::BuildProgram(
764765
programLambda, *paramsType, compiler, typeEnv, *State->FunctionRegistry,
765-
ctx, fakeReads);
766+
ctx, fakeReads, spillingSettings);
766767
}
767768

768769
auto block = MeasureBlock("RuntimeNodeVisitor");

0 commit comments

Comments
 (0)