Skip to content

Commit e64385b

Browse files
authored
Replay cached timestamp & randoms (#9403)
1 parent 360e71f commit e64385b

File tree

3 files changed

+93
-26
lines changed

3 files changed

+93
-26
lines changed

ydb/library/yql/core/services/yql_eval_expr.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,20 @@ IGraphTransformer::TStatus EvaluateExpression(const TExprNode::TPtr& input, TExp
973973
clonedArg = ctx.NewCallable(clonedArg->Pos(), "SerializeCode", { clonedArg });
974974
}
975975

976+
TString key;
976977
NYT::TNode ysonNode;
978+
if (types.QContext) {
979+
key = MakeCacheKey(*clonedArg);
980+
if (types.QContext.CanRead()) {
981+
auto item = types.QContext.GetReader()->Get({EvaluationComponent, key}).GetValueSync();
982+
if (!item) {
983+
throw yexception() << "Missing replay data";
984+
}
985+
986+
ysonNode = NYT::NodeFromYsonString(item->Value);
987+
}
988+
}
989+
977990
do {
978991
calcProvider.Clear();
979992
calcWorldRoot.Drop();
@@ -997,6 +1010,10 @@ IGraphTransformer::TStatus EvaluateExpression(const TExprNode::TPtr& input, TExp
9971010
return nullptr;
9981011
}
9991012

1013+
if (types.QContext.CanRead()) {
1014+
break;
1015+
}
1016+
10001017
IDataProvider::TFillSettings fillSettings;
10011018
auto delegatedNode = Build<TResult>(ctx, node->Pos())
10021019
.Input(clonedArg)
@@ -1031,29 +1048,13 @@ IGraphTransformer::TStatus EvaluateExpression(const TExprNode::TPtr& input, TExp
10311048

10321049
delegatedNode->SetTypeAnn(atomType);
10331050
delegatedNode->SetState(TExprNode::EState::ConstrComplete);
1034-
TString yson;
1035-
TString key;
1036-
if (types.QContext) {
1037-
key = MakeCacheKey(*clonedArg);
1038-
}
1039-
1040-
if (types.QContext.CanRead()) {
1041-
auto item = types.QContext.GetReader()->Get({EvaluationComponent, key}).GetValueSync();
1042-
if (!item) {
1043-
throw yexception() << "Missing replay data";
1044-
}
1045-
1046-
yson = item->Value;
1047-
} else {
1048-
auto& transformer = calcTransfomer ? *calcTransfomer : (*calcProvider.Get())->GetCallableExecutionTransformer();
1049-
status = SyncTransform(transformer, delegatedNode, ctx);
1050-
if (status.Level == IGraphTransformer::TStatus::Error) {
1051-
return nullptr;
1052-
}
1053-
1054-
yson = delegatedNode->GetResult().Content();
1051+
auto& transformer = calcTransfomer ? *calcTransfomer : (*calcProvider.Get())->GetCallableExecutionTransformer();
1052+
status = SyncTransform(transformer, delegatedNode, ctx);
1053+
if (status.Level == IGraphTransformer::TStatus::Error) {
1054+
return nullptr;
10551055
}
10561056

1057+
TString yson{delegatedNode->GetResult().Content()};
10571058
ysonNode = NYT::NodeFromYsonString(yson);
10581059
if (ysonNode.HasKey("FallbackProvider")) {
10591060
nextProvider = ysonNode["FallbackProvider"].AsString();

ydb/library/yql/core/ut/yql_qplayer_ut.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct TRunSettings {
5454
THashMap<TString, TString> Tables;
5555
TMaybe<TString> ParametersYson;
5656
THashMap<TString, TString> StaticFiles, DynamicFiles;
57+
bool ChangeTime = false;
5758
};
5859

5960
TUserDataTable MakeUserTables(const THashMap<TString, TString>& map) {
@@ -91,7 +92,7 @@ bool RunProgram(bool replay, const TString& query, const TQContext& qContext, co
9192
}
9293
TExprContext::TFreezeGuard freezeGuard(modulesCtx);
9394

94-
TProgramFactory factory(true, functionRegistry.Get(), 0ULL, dataProvidersInit, "ut");
95+
TProgramFactory factory(!runSettings.ChangeTime, functionRegistry.Get(), 0ULL, dataProvidersInit, "ut");
9596
factory.SetUdfResolver(NCommon::CreateSimpleUdfResolver(functionRegistry.Get()));
9697
factory.SetModules(moduleResolver);
9798

@@ -281,4 +282,11 @@ SELECT State FROM plato.WalkFolders(``, $postHandler AS PostHandler);
281282
CheckProgram(s, runSettings);
282283
});
283284
}
285+
286+
Y_UNIT_TEST(EvaluateNow) {
287+
auto s = "select EvaluateExpr(CurrentUtcDate())";
288+
TRunSettings runSettings;
289+
runSettings.ChangeTime = true;
290+
CheckProgram(s, runSettings);
291+
}
284292
}

ydb/library/yql/core/yql_type_annotation.h

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,40 @@ struct TUdfCachedInfo {
280280
bool IsStrict = false;
281281
};
282282

283+
const TString TypeAnnotationContextComponent = "TypeAnnotationContext";
284+
const TString NowKey = "Now";
285+
const TString RandomKey = "Random";
286+
const TString RandomNumberKey = "RandomNumber";
287+
const TString RandomUuidKey = "RandomUuid";
288+
289+
template <typename T>
290+
inline TString SerializeBinary(const T& value) {
291+
return TString((const char*)&value, sizeof(T));
292+
}
293+
294+
template <typename T>
295+
inline T DeserializeBinary(const TString& value) {
296+
return *(const T*)value.Data();
297+
}
298+
299+
template <typename T>
300+
inline TString GetRandomKey();
301+
302+
template <>
303+
inline TString GetRandomKey<ui64>() {
304+
return RandomNumberKey;
305+
}
306+
307+
template <>
308+
inline TString GetRandomKey<double>() {
309+
return RandomKey;
310+
}
311+
312+
template <>
313+
inline TString GetRandomKey<TGUID>() {
314+
return RandomUuidKey;
315+
}
316+
283317
struct TTypeAnnotationContext: public TThrRefBase {
284318
THashMap<TString, TIntrusivePtr<TOptimizerStatistics::TColumnStatMap>> ColumnStatisticsByTableName;
285319
THashMap<ui64, std::shared_ptr<TOptimizerStatistics>> StatisticsMap;
@@ -369,17 +403,41 @@ struct TTypeAnnotationContext: public TThrRefBase {
369403
T GetRandom() const noexcept;
370404

371405
template <typename T>
372-
T GetCachedRandom() noexcept {
406+
T GetCachedRandom() {
373407
auto& cached = std::get<std::optional<T>>(CachedRandom);
374408
if (!cached) {
375-
cached = GetRandom<T>();
409+
if (QContext.CanRead()) {
410+
auto item = QContext.GetReader()->Get({TypeAnnotationContextComponent, GetRandomKey<T>()}).GetValueSync();
411+
if (!item) {
412+
throw yexception() << "Missing replay data";
413+
}
414+
415+
cached = DeserializeBinary<T>(item->Value);
416+
} else {
417+
cached = GetRandom<T>();
418+
if (QContext.CanWrite()) {
419+
QContext.GetWriter()->Put({TypeAnnotationContextComponent, GetRandomKey<T>()}, SerializeBinary<T>(*cached)).GetValueSync();
420+
}
421+
}
376422
}
377423
return *cached;
378424
}
379425

380-
ui64 GetCachedNow() noexcept {
426+
ui64 GetCachedNow() {
381427
if (!CachedNow) {
382-
CachedNow = TimeProvider->Now().GetValue();
428+
if (QContext.CanRead()) {
429+
auto item = QContext.GetReader()->Get({TypeAnnotationContextComponent, NowKey}).GetValueSync();
430+
if (!item) {
431+
throw yexception() << "Missing replay data";
432+
}
433+
434+
CachedNow = DeserializeBinary<ui64>(item->Value);
435+
} else {
436+
CachedNow = TimeProvider->Now().GetValue();
437+
if (QContext.CanWrite()) {
438+
QContext.GetWriter()->Put({TypeAnnotationContextComponent, NowKey}, SerializeBinary<ui64>(*CachedNow)).GetValueSync();
439+
}
440+
}
383441
}
384442
return *CachedNow;
385443
}

0 commit comments

Comments
 (0)