Skip to content

Commit 0e176bd

Browse files
authored
Initial implementation of facade qplayer tests (#4005)
1 parent 9a710f7 commit 0e176bd

File tree

6 files changed

+138
-4
lines changed

6 files changed

+138
-4
lines changed

ydb/library/yql/core/facade/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PEERDIR(
1818
ydb/library/yql/core/url_lister/interface
1919
ydb/library/yql/core/url_preprocessing/interface
2020
ydb/library/yql/core/credentials
21+
ydb/library/yql/core/qplayer/storage/interface
2122
ydb/library/yql/sql
2223
ydb/library/yql/utils/log
2324
ydb/library/yql/core

ydb/library/yql/core/facade/yql_facade.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace {
4444

4545
const size_t DEFAULT_AST_BUF_SIZE = 1024;
4646
const size_t DEFAULT_PLAN_BUF_SIZE = 1024;
47+
const TString FacadeComponent = "Facade";
48+
const TString SourceCodeLabel = "SourceCode";
4749

4850
class TUrlLoader : public IUrlLoader {
4951
public:
@@ -311,6 +313,12 @@ TProgram::~TProgram() {
311313
}
312314
}
313315

316+
void TProgram::SetQContext(const TQContext& qContext) {
317+
YQL_PROFILE_FUNC(TRACE);
318+
YQL_ENSURE(SourceSyntax_ == ESourceSyntax::Unknown);
319+
QContext_ = qContext;
320+
}
321+
314322
void TProgram::ConfigureYsonResultFormat(NYson::EYsonFormat format) {
315323
ResultFormat_ = format;
316324
OutputFormat_ = format;
@@ -468,12 +476,24 @@ void TProgram::AddUserDataTable(const TUserDataTable& userDataTable) {
468476
}
469477
}
470478

479+
void TProgram::HandleSourceCode(TString& sourceCode) {
480+
if (QContext_.CanWrite()) {
481+
QContext_.GetWriter()->Put({FacadeComponent, SourceCodeLabel}, sourceCode).GetValueSync();
482+
} else if (QContext_.CanRead()) {
483+
auto loaded = QContext_.GetReader()->Get({FacadeComponent, SourceCodeLabel}).GetValueSync();
484+
Y_ENSURE(loaded.Defined(), "No source code");
485+
sourceCode = loaded->Value;
486+
}
487+
}
488+
471489
bool TProgram::ParseYql() {
472490
YQL_PROFILE_FUNC(TRACE);
473491
YQL_ENSURE(SourceSyntax_ == ESourceSyntax::Unknown);
474492
SourceSyntax_ = ESourceSyntax::Yql;
475493
SyntaxVersion_ = 1;
476-
return FillParseResult(ParseAst(SourceCode_));
494+
auto sourceCode = SourceCode_;
495+
HandleSourceCode(sourceCode);
496+
return FillParseResult(ParseAst(sourceCode));
477497
}
478498

479499
bool TProgram::ParseSql() {
@@ -495,7 +515,9 @@ bool TProgram::ParseSql(const NSQLTranslation::TTranslationSettings& settings)
495515
SourceSyntax_ = ESourceSyntax::Sql;
496516
SyntaxVersion_ = settings.SyntaxVersion;
497517
NYql::TWarningRules warningRules;
498-
return FillParseResult(SqlToYql(SourceCode_, settings, &warningRules), &warningRules);
518+
auto sourceCode = SourceCode_;
519+
HandleSourceCode(sourceCode);
520+
return FillParseResult(SqlToYql(sourceCode, settings, &warningRules), &warningRules);
499521
}
500522

501523
bool TProgram::Compile(const TString& username, bool skipLibraries) {

ydb/library/yql/core/facade/yql_facade.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <ydb/library/yql/core/url_preprocessing/interface/url_preprocessing.h>
99
#include <ydb/library/yql/core/yql_type_annotation.h>
1010
#include <ydb/library/yql/core/yql_user_data.h>
11+
#include <ydb/library/yql/core/qplayer/storage/interface/yql_qstorage.h>
1112
#include <ydb/library/yql/providers/config/yql_config_provider.h>
1213
#include <ydb/library/yql/providers/result/provider/yql_result_provider.h>
1314
#include <ydb/library/yql/public/issue/yql_issue.h>
@@ -106,6 +107,8 @@ class TProgram: public TThrRefBase, private TNonCopyable
106107
public:
107108
~TProgram();
108109

110+
void SetQContext(const TQContext& qContext);
111+
109112
void AddCredentials(const TVector<std::pair<TString, TCredential>>& credentials);
110113
void ClearCredentials();
111114

@@ -386,6 +389,7 @@ class TProgram: public TThrRefBase, private TNonCopyable
386389

387390
private:
388391
std::optional<bool> CheckFallbackIssues(const TIssues& issues);
392+
void HandleSourceCode(TString& sourceCode);
389393

390394
const NKikimr::NMiniKQL::IFunctionRegistry* FunctionRegistry_;
391395
const TIntrusivePtr<IRandomProvider> RandomProvider_;
@@ -445,6 +449,8 @@ class TProgram: public TThrRefBase, private TNonCopyable
445449
const EHiddenMode HiddenMode_ = EHiddenMode::Disable;
446450
THiddenQueryAborter AbortHidden_ = [](){};
447451
TMaybe<TString> LineageStr_;
452+
453+
TQContext QContext_;
448454
};
449455

450456
} // namspace NYql

ydb/library/yql/core/qplayer/storage/interface/yql_qstorage.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class TQContext {
9696
: Writer_(writer)
9797
{}
9898

99+
TQContext(const TQContext&) = default;
100+
TQContext& operator=(const TQContext&) = default;
101+
99102
bool CanRead() const {
100103
return Reader_ != nullptr;
101104
}
@@ -113,8 +116,8 @@ class TQContext {
113116
}
114117

115118
private:
116-
const IQReaderPtr Reader_;
117-
const IQWriterPtr Writer_;
119+
IQReaderPtr Reader_;
120+
IQWriterPtr Writer_;
118121
};
119122

120123
}

ydb/library/yql/core/ut/ya.make

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SRCS(
1111
yql_library_compiler_ut.cpp
1212
yql_opt_utils_ut.cpp
1313
yql_udf_index_ut.cpp
14+
yql_qplayer_ut.cpp
1415
)
1516

1617
PEERDIR(
@@ -19,6 +20,7 @@ PEERDIR(
1920
ydb/library/yql/core
2021
ydb/library/yql/core/facade
2122
ydb/library/yql/core/services
23+
ydb/library/yql/core/qplayer/storage/memory
2224
ydb/library/yql/public/udf
2325
ydb/library/yql/public/udf/service/exception_policy
2426
ydb/library/yql/core/type_ann
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <ydb/library/yql/minikql/invoke_builtins/mkql_builtins.h>
2+
#include <ydb/library/yql/providers/yt/provider/yql_yt_provider.h>
3+
#include <ydb/library/yql/providers/yt/gateway/file/yql_yt_file.h>
4+
#include <ydb/library/yql/providers/yt/gateway/file/yql_yt_file_services.h>
5+
#include <ydb/library/yql/core/facade/yql_facade.h>
6+
#include <ydb/library/yql/core/qplayer/storage/memory/yql_qstorage_memory.h>
7+
8+
#include <library/cpp/testing/unittest/registar.h>
9+
10+
#include <util/system/user.h>
11+
12+
using namespace NYql;
13+
14+
bool RunProgram(const TString& sourceCode, const TQContext& qContext, bool isSql) {
15+
auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry(NKikimr::NMiniKQL::CreateBuiltinRegistry());
16+
auto yqlNativeServices = NFile::TYtFileServices::Make(functionRegistry.Get(), {}, {}, "");
17+
auto ytGateway = CreateYtFileGateway(yqlNativeServices);
18+
19+
TVector<TDataProviderInitializer> dataProvidersInit;
20+
dataProvidersInit.push_back(GetYtNativeDataProviderInitializer(ytGateway));
21+
TProgramFactory factory(true, functionRegistry.Get(), 0ULL, dataProvidersInit, "ut");
22+
23+
TProgramPtr program = factory.Create("-stdin-", sourceCode);
24+
program->SetQContext(qContext);
25+
if (isSql) {
26+
if (!program->ParseSql()) {
27+
program->PrintErrorsTo(Cerr);
28+
return false;
29+
}
30+
} else if (!program->ParseYql()) {
31+
program->PrintErrorsTo(Cerr);
32+
return false;
33+
}
34+
35+
if (!program->Compile(GetUsername())) {
36+
program->PrintErrorsTo(Cerr);
37+
return false;
38+
}
39+
40+
TProgram::TStatus status = program->Optimize(GetUsername());
41+
if (status == TProgram::TStatus::Error) {
42+
program->PrintErrorsTo(Cerr);
43+
}
44+
45+
return status == TProgram::TStatus::Ok;
46+
}
47+
48+
void CheckProgram(const TString& sql, bool isSql = true) {
49+
auto qStorage = MakeMemoryQStorage();
50+
TQContext savingCtx(qStorage->MakeWriter("foo"));
51+
UNIT_ASSERT(RunProgram(sql, savingCtx, isSql));
52+
savingCtx.GetWriter()->Commit().GetValueSync();
53+
TQContext loadingCtx(qStorage->MakeReader("foo"));
54+
UNIT_ASSERT(RunProgram("", loadingCtx, isSql));
55+
}
56+
57+
Y_UNIT_TEST_SUITE(QPlayerTests) {
58+
Y_UNIT_TEST(SimpleYql) {
59+
auto s = R"(
60+
(
61+
(let world (block '(
62+
(let output (block '(
63+
(let select (block '(
64+
(let core (AsList (AsStruct)))
65+
(let core (PersistableRepr (block '(
66+
(let projectCoreType (TypeOf core))
67+
(let core (OrderedSqlProject core '((SqlProjectItem projectCoreType '"" (lambda '(row) (block '(
68+
(let res (Int32 '"1"))
69+
(return res)
70+
))) '('('autoName))))))
71+
(return core)
72+
))))
73+
(return core)
74+
)))
75+
(return select)
76+
)))
77+
(let output (Unordered output))
78+
(let world (block '(
79+
(let result_sink (DataSink 'result))
80+
(let world (Write! world result_sink (Key) output '('('type) '('autoref) '('columns '('('auto))) '('unordered))))
81+
(return (Commit! world result_sink))
82+
)))
83+
(return world)
84+
)))
85+
(let world (block '(
86+
(let world (CommitAll! world))
87+
(return world)
88+
)))
89+
(return world)
90+
)
91+
)";
92+
93+
CheckProgram(s, false);
94+
}
95+
96+
Y_UNIT_TEST(SimpleSql) {
97+
auto s = "select 1";
98+
CheckProgram(s);
99+
}
100+
}

0 commit comments

Comments
 (0)