@@ -43,15 +43,22 @@ TString GetStatic(const TString& filePath) {
43
43
return buffer.str ();
44
44
}
45
45
46
- void CreateTables (TSession session, const TString& schemaPath, bool useColumnStore) {
46
+ void CreateTables (NYdb::NQuery:: TSession session, const TString& schemaPath, bool useColumnStore) {
47
47
std::string query = GetStatic (schemaPath);
48
48
49
49
if (useColumnStore) {
50
50
std::regex pattern (R"( CREATE TABLE [^\(]+ \([^;]*\))" , std::regex::multiline);
51
51
query = std::regex_replace (query, pattern, " $& WITH (STORE = COLUMN, AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 16);" );
52
52
}
53
53
54
- auto res = session.ExecuteSchemeQuery (TString (query)).GetValueSync ();
54
+ auto res = session.ExecuteQuery (TString (query), NYdb::NQuery::TTxControl::NoTx ()).GetValueSync ();
55
+ res.GetIssues ().PrintTo (Cerr);
56
+ UNIT_ASSERT (res.IsSuccess ());
57
+ }
58
+
59
+ void CreateView (NYdb::NQuery::TSession session, const TString& viewPath) {
60
+ std::string query = GetStatic (viewPath);
61
+ auto res = session.ExecuteQuery (TString (query), NYdb::NQuery::TTxControl::NoTx ()).GetValueSync ();
55
62
res.GetIssues ().PrintTo (Cerr);
56
63
UNIT_ASSERT (res.IsSuccess ());
57
64
}
@@ -69,7 +76,7 @@ TString GetPrettyJSON(const NJson::TJsonValue& json) {
69
76
* A basic join order test. We define 5 tables sharing the same
70
77
* key attribute and construct various full clique join queries
71
78
*/
72
- static void CreateSampleTable (TSession session, bool useColumnStore) {
79
+ static void CreateSampleTable (NYdb::NQuery:: TSession session, bool useColumnStore) {
73
80
CreateTables (session, " schema/rstuv.sql" , useColumnStore);
74
81
75
82
CreateTables (session, " schema/tpch.sql" , useColumnStore);
@@ -79,6 +86,8 @@ static void CreateSampleTable(TSession session, bool useColumnStore) {
79
86
CreateTables (session, " schema/tpcc.sql" , useColumnStore);
80
87
81
88
CreateTables (session, " schema/lookupbug.sql" , useColumnStore);
89
+
90
+ CreateView (session, " view/tpch_random_join_view.sql" );
82
91
}
83
92
84
93
static TKikimrRunner GetKikimrWithJoinSettings (bool useStreamLookupJoin = false , TString stats = " " , bool useCBO = true ){
@@ -96,8 +105,11 @@ static TKikimrRunner GetKikimrWithJoinSettings(bool useStreamLookupJoin = false,
96
105
appConfig.MutableTableServiceConfig ()->SetEnableKqpDataQueryStreamIdxLookupJoin (useStreamLookupJoin);
97
106
appConfig.MutableTableServiceConfig ()->SetEnableConstantFolding (true );
98
107
appConfig.MutableTableServiceConfig ()->SetCompileTimeoutMs (TDuration::Minutes (10 ).MilliSeconds ());
108
+ appConfig.MutableFeatureFlags ()->SetEnableViews (true );
99
109
if (!useCBO) {
100
110
appConfig.MutableTableServiceConfig ()->SetDefaultCostBasedOptimizationLevel (0 );
111
+ } else {
112
+ appConfig.MutableTableServiceConfig ()->SetDefaultCostBasedOptimizationLevel (4 );
101
113
}
102
114
103
115
auto serverSettings = TKikimrSettings ().SetAppConfig (appConfig);
@@ -125,7 +137,7 @@ class TChainTester {
125
137
TChainTester (size_t chainSize)
126
138
: Kikimr(GetKikimrWithJoinSettings(false , GetStats(chainSize)))
127
139
, TableClient(Kikimr.GetTableClient())
128
- , Session(TableClient.CreateSession ().GetValueSync().GetSession())
140
+ , Session(TableClient.GetSession ().GetValueSync().GetSession())
129
141
, ChainSize(chainSize)
130
142
{}
131
143
@@ -198,47 +210,65 @@ class TChainTester {
198
210
199
211
void ExplainJoinOrderTestDataQueryWithStats (const TString& queryPath, const TString& statsPath, bool useStreamLookupJoin, bool useColumnStore, bool useCBO = true ) {
200
212
auto kikimr = GetKikimrWithJoinSettings (useStreamLookupJoin, GetStatic (statsPath), useCBO);
201
- auto db = kikimr.GetTableClient ();
202
- auto session = db.CreateSession ().GetValueSync ().GetSession ();
213
+ kikimr.GetTestServer ().GetRuntime ()->GetAppData (0 ).FeatureFlags .SetEnableViews (true );
214
+ auto db = kikimr.GetQueryClient ();
215
+ auto session = db.GetSession ().GetValueSync ().GetSession ();
203
216
204
217
CreateSampleTable (session, useColumnStore);
205
218
206
219
/* join with parameters */
207
220
{
208
221
const TString query = GetStatic (queryPath);
209
222
210
- auto result = session.ExplainDataQuery (query).ExtractValueSync ();
223
+ auto result =
224
+ session.ExecuteQuery (
225
+ query,
226
+ NYdb::NQuery::TTxControl::NoTx (),
227
+ NYdb::NQuery::TExecuteQuerySettings ().ExecMode (NQuery::EExecMode::Explain)
228
+ ).ExtractValueSync ();
211
229
result.GetIssues ().PrintTo (Cerr);
212
230
UNIT_ASSERT_VALUES_EQUAL (result.GetStatus (), EStatus::SUCCESS);
213
- PrintPlan (result.GetPlan ());
231
+ PrintPlan (* result.GetStats ()-> GetPlan ());
214
232
}
215
233
}
216
234
217
235
void TestOlapEstimationRowsCorrectness (const TString& queryPath, const TString& statsPath) {
218
236
auto kikimr = GetKikimrWithJoinSettings (false , GetStatic (statsPath));
219
- auto db = kikimr.GetTableClient ();
220
- auto session = db.CreateSession ().GetValueSync ().GetSession ();
237
+ kikimr.GetTestServer ().GetRuntime ()->GetAppData (0 ).FeatureFlags .SetEnableViews (true );
238
+ auto db = kikimr.GetQueryClient ();
239
+ auto session = db.GetSession ().GetValueSync ().GetSession ();
221
240
222
241
CreateSampleTable (session, true );
223
242
224
243
const TString actualQuery = GetStatic (queryPath);
225
244
TString actualPlan;
226
245
{
227
- auto result = session.ExplainDataQuery (actualQuery).ExtractValueSync ();
246
+ auto result =
247
+ session.ExecuteQuery (
248
+ actualQuery,
249
+ NYdb::NQuery::TTxControl::NoTx (),
250
+ NYdb::NQuery::TExecuteQuerySettings ().ExecMode (NQuery::EExecMode::Explain)
251
+ ).ExtractValueSync ();
252
+
253
+ result.GetIssues ().PrintTo (Cerr);
228
254
UNIT_ASSERT_VALUES_EQUAL (result.GetStatus (), EStatus::SUCCESS);
229
- actualPlan = result.GetPlan ();
255
+ actualPlan = * result.GetStats ()-> GetPlan ();
230
256
PrintPlan (actualPlan);
231
- Cout << result.GetAst () << Endl;
257
+ Cout << result.GetStats ()-> GetAst () << Endl;
232
258
}
233
259
234
260
const TString expectedQuery = R"( PRAGMA kikimr.OptEnableOlapPushdown = "false";)" " \n " + actualQuery;
235
261
TString expectedPlan;
236
262
{
237
- auto result = session.ExplainDataQuery (expectedQuery).ExtractValueSync ();
263
+ auto result = session.ExecuteQuery (
264
+ expectedQuery,
265
+ NYdb::NQuery::TTxControl::NoTx (),
266
+ NYdb::NQuery::TExecuteQuerySettings ().ExecMode (NQuery::EExecMode::Explain)
267
+ ).ExtractValueSync ();
238
268
UNIT_ASSERT_VALUES_EQUAL (result.GetStatus (), EStatus::SUCCESS);
239
- expectedPlan = result.GetPlan ();
269
+ expectedPlan = * result.GetStats ()-> GetPlan ();
240
270
PrintPlan (expectedPlan);
241
- Cout << result.GetAst () << Endl;
271
+ Cout << result.GetStats ()-> GetAst () << Endl;
242
272
}
243
273
244
274
auto expectedDetailedPlan = GetDetailedJoinOrder (actualPlan, {.IncludeFilters = true , .IncludeOptimizerEstimation = true , .IncludeTables = false });
@@ -298,9 +328,9 @@ Y_UNIT_TEST_SUITE(OlapEstimationRowsCorrectness) {
298
328
TestOlapEstimationRowsCorrectness (" queries/tpcds78.sql" , " stats/tpcds1000s.json" );
299
329
}
300
330
301
- Y_UNIT_TEST (TPCDS87) {
302
- TestOlapEstimationRowsCorrectness (" queries/tpcds87.sql" , " stats/tpcds1000s.json" );
303
- }
331
+ // Y_UNIT_TEST(TPCDS87) {
332
+ // TestOlapEstimationRowsCorrectness("queries/tpcds87.sql", "stats/tpcds1000s.json");
333
+ // }
304
334
305
335
// Y_UNIT_TEST(TPCDS88) { // ???
306
336
// TestOlapEstimationRowsCorrectness("queries/tpcds88.sql", "stats/tpcds1000s.json");
@@ -334,39 +364,50 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
334
364
335
365
TString ExecuteJoinOrderTestDataQueryWithStats (const TString& queryPath, const TString& statsPath, bool useStreamLookupJoin, bool useColumnStore, bool useCBO = true ) {
336
366
auto kikimr = GetKikimrWithJoinSettings (useStreamLookupJoin, GetStatic (statsPath), useCBO);
337
- auto db = kikimr.GetTableClient ();
338
- auto session = db.CreateSession ().GetValueSync ().GetSession ();
367
+ kikimr.GetTestServer ().GetRuntime ()->GetAppData (0 ).FeatureFlags .SetEnableViews (true );
368
+ auto db = kikimr.GetQueryClient ();
369
+ auto session = db.GetSession ().GetValueSync ().GetSession ();
339
370
340
371
CreateSampleTable (session, useColumnStore);
341
372
342
373
/* join with parameters */
343
374
{
344
375
const TString query = GetStatic (queryPath);
345
376
346
- auto execRes = db. StreamExecuteScanQuery (query, TStreamExecScanQuerySettings (). Explain ( true )).ExtractValueSync ();
377
+ auto execRes = session. ExecuteQuery (query, NYdb::NQuery::TTxControl::NoTx ( )).ExtractValueSync ();
347
378
execRes.GetIssues ().PrintTo (Cerr);
348
379
UNIT_ASSERT_VALUES_EQUAL (execRes.GetStatus (), EStatus::SUCCESS);
349
- auto plan = CollectStreamResult (execRes).PlanJson ;
350
- PrintPlan (plan.GetRef ());
351
- return plan.GetRef ();
380
+
381
+ auto explainRes = session.ExecuteQuery (query, NYdb::NQuery::TTxControl::NoTx (), NYdb::NQuery::TExecuteQuerySettings ().ExecMode (NQuery::EExecMode::Explain)).ExtractValueSync ();
382
+ explainRes.GetIssues ().PrintTo (Cerr);
383
+ UNIT_ASSERT_VALUES_EQUAL (explainRes.GetStatus (), EStatus::SUCCESS);
384
+
385
+ PrintPlan (*explainRes.GetStats ()->GetPlan ());
386
+ return *explainRes.GetStats ()->GetPlan ();
352
387
}
353
388
}
354
389
355
390
void CheckJoinCardinality (const TString& queryPath, const TString& statsPath, const TString& joinKind, double card, bool useStreamLookupJoin, bool useColumnStore) {
356
391
auto kikimr = GetKikimrWithJoinSettings (useStreamLookupJoin, GetStatic (statsPath));
357
- auto db = kikimr.GetTableClient ();
358
- auto session = db.CreateSession ().GetValueSync ().GetSession ();
392
+ kikimr.GetTestServer ().GetRuntime ()->GetAppData (0 ).FeatureFlags .SetEnableViews (true );
393
+ auto db = kikimr.GetQueryClient ();
394
+ auto session = db.GetSession ().GetValueSync ().GetSession ();
359
395
360
396
CreateSampleTable (session, useColumnStore);
361
397
362
398
/* join with parameters */
363
399
{
364
400
const TString query = GetStatic (queryPath);
365
401
366
- auto result = session.ExplainDataQuery (query).ExtractValueSync ();
367
- PrintPlan (result.GetPlan ());
402
+ auto result =
403
+ session.ExecuteQuery (
404
+ query,
405
+ NYdb::NQuery::TTxControl::NoTx (),
406
+ NYdb::NQuery::TExecuteQuerySettings ().ExecMode (NQuery::EExecMode::Explain)
407
+ ).ExtractValueSync ();
408
+ PrintPlan (*result.GetStats ()->GetPlan ());
368
409
NJson::TJsonValue plan;
369
- NJson::ReadJsonTree (result.GetPlan (), &plan, true );
410
+ NJson::ReadJsonTree (* result.GetStats ()-> GetPlan (), &plan, true );
370
411
371
412
if (!useStreamLookupJoin) {
372
413
auto joinNode = FindPlanNodeByKv (plan.GetMapSafe ().at (" SimplifiedPlan" ), " Node Type" , joinKind);
@@ -438,6 +479,10 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
438
479
ExecuteJoinOrderTestDataQueryWithStats (" queries/datetime_constant_fold.sql" , " stats/basic.json" , StreamLookupJoin, ColumnStore);
439
480
}
440
481
482
+ Y_UNIT_TEST_XOR_OR_BOTH_FALSE (TPCHRandomJoinViewJustWorks, StreamLookupJoin, ColumnStore) {
483
+ ExecuteJoinOrderTestDataQueryWithStats (" queries/tpch_random_join_view_just_works.sql" , " stats/tpch1000s.json" , StreamLookupJoin, ColumnStore);
484
+ }
485
+
441
486
Y_UNIT_TEST_XOR_OR_BOTH_FALSE (TPCH3, StreamLookupJoin, ColumnStore) {
442
487
ExecuteJoinOrderTestDataQueryWithStats (" queries/tpch3.sql" , " stats/tpch1000s.json" , StreamLookupJoin, ColumnStore);
443
488
}
@@ -462,14 +507,6 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
462
507
ExecuteJoinOrderTestDataQueryWithStats (" queries/tpch21.sql" , " stats/tpch1000s.json" , StreamLookupJoin, ColumnStore);
463
508
}
464
509
465
- Y_UNIT_TEST_XOR_OR_BOTH_FALSE (TPCDS9, StreamLookupJoin, ColumnStore) {
466
- ExecuteJoinOrderTestDataQueryWithStats (" queries/tpcds9.sql" , " stats/tpcds1000s.json" , StreamLookupJoin, ColumnStore);
467
- }
468
-
469
- Y_UNIT_TEST_XOR_OR_BOTH_FALSE (TPCDS9_SMALL, StreamLookupJoin, ColumnStore) {
470
- ExecuteJoinOrderTestDataQueryWithStats (" queries/tpcds9_small.sql" , " stats/tpcds1000s.json" , StreamLookupJoin, ColumnStore);
471
- }
472
-
473
510
Y_UNIT_TEST_XOR_OR_BOTH_FALSE (TPCDS16, StreamLookupJoin, ColumnStore) {
474
511
ExecuteJoinOrderTestDataQueryWithStats (" queries/tpcds16.sql" , " stats/tpcds1000s.json" , StreamLookupJoin, ColumnStore);
475
512
}
@@ -609,19 +646,25 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
609
646
void CanonizedJoinOrderTest (const TString& queryPath, const TString& statsPath, TString correctJoinOrderPath, bool useStreamLookupJoin, bool useColumnStore
610
647
) {
611
648
auto kikimr = GetKikimrWithJoinSettings (useStreamLookupJoin, GetStatic (statsPath));
612
- auto db = kikimr.GetTableClient ();
613
- auto session = db.CreateSession ().GetValueSync ().GetSession ();
649
+ kikimr.GetTestServer ().GetRuntime ()->GetAppData (0 ).FeatureFlags .SetEnableViews (true );
650
+ auto db = kikimr.GetQueryClient ();
651
+ auto session = db.GetSession ().GetValueSync ().GetSession ();
614
652
615
653
CreateSampleTable (session, useColumnStore);
616
654
617
655
/* join with parameters */
618
656
{
619
657
const TString query = GetStatic (queryPath);
620
658
621
- auto result = session.ExplainDataQuery (query).ExtractValueSync ();
659
+ auto result =
660
+ session.ExecuteQuery (
661
+ query,
662
+ NYdb::NQuery::TTxControl::NoTx (),
663
+ NYdb::NQuery::TExecuteQuerySettings ().ExecMode (NQuery::EExecMode::Explain)
664
+ ).ExtractValueSync ();
622
665
623
666
result.GetIssues ().PrintTo (Cerr);
624
- PrintPlan (result.GetPlan ());
667
+ PrintPlan (* result.GetStats ()-> GetPlan ());
625
668
UNIT_ASSERT_VALUES_EQUAL (result.GetStatus (), EStatus::SUCCESS);
626
669
627
670
if (useStreamLookupJoin) {
@@ -632,7 +675,7 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
632
675
correctJoinOrderPath = correctJoinOrderPath.substr (0 , correctJoinOrderPath.find (" .json" )) + " _column_store.json" ;
633
676
}
634
677
635
- auto currentJoinOrder = GetPrettyJSON (GetDetailedJoinOrder (result.GetPlan ()));
678
+ auto currentJoinOrder = GetPrettyJSON (GetDetailedJoinOrder (* result.GetStats ()-> GetPlan ()));
636
679
637
680
/* to canonize the tests use --test-param CANONIZE_JOIN_ORDER_TESTS=TRUE */
638
681
TString canonize = GetTestParam (" CANONIZE_JOIN_ORDER_TESTS" ); canonize.to_lower ();
@@ -643,9 +686,9 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
643
686
}
644
687
645
688
TString ref = GetStatic (correctJoinOrderPath);
646
- Cout << " actual\n " << GetJoinOrder (result.GetPlan ()).GetStringRobust () << Endl;
689
+ Cout << " actual\n " << GetJoinOrder (* result.GetStats ()-> GetPlan ()).GetStringRobust () << Endl;
647
690
Cout << " expected\n " << GetJoinOrderFromDetailedJoinOrder (ref).GetStringRobust () << Endl;
648
- UNIT_ASSERT (JoinOrderAndAlgosMatch (result.GetPlan (), ref));
691
+ UNIT_ASSERT (JoinOrderAndAlgosMatch (* result.GetStats ()-> GetPlan (), ref));
649
692
}
650
693
}
651
694
0 commit comments