Skip to content

Commit 4d45fe1

Browse files
authored
ensure reads are sequential (#11280) (#11317)
1 parent 731f584 commit 4d45fe1

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

ydb/core/kqp/opt/logical/kqp_opt_log_ranges_predext.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ TMaybeNode<TCoLambda> ExtractTopSortKeySelector(TExprBase node, const NYql::TPar
193193
return {};
194194
}
195195

196+
bool IsIdLambda(TExprBase body) {
197+
if (auto cond = body.Maybe<TCoConditionalValueBase>()) {
198+
if (auto boolLit = cond.Cast().Predicate().Maybe<TCoBool>()) {
199+
return boolLit.Literal().Cast().Value() == "true" && cond.Value().Maybe<TCoArgument>();
200+
}
201+
}
202+
if (body.Maybe<TCoArgument>()) {
203+
return true;
204+
}
205+
return false;
206+
}
207+
196208
} // namespace
197209

198210
TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext& kqpCtx,
@@ -305,7 +317,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
305317
const NYql::TKikimrTableDescription & tableDesc) -> TIndexComparisonKey
306318
{
307319
return std::make_tuple(
308-
keySelector.IsValid() && IsSortKeyPrimary(keySelector.Cast(), tableDesc),
320+
keySelector.IsValid() && IsSortKeyPrimary(keySelector.Cast(), tableDesc) && IsIdLambda(TCoLambda(buildResult.PrunedLambda).Body()),
309321
buildResult.PointPrefixLen >= descriptionKeyColumns,
310322
buildResult.PointPrefixLen >= descriptionKeyColumns ? 0 : buildResult.PointPrefixLen,
311323
buildResult.UsedPrefixLen >= descriptionKeyColumns,

ydb/core/kqp/ut/opt/kqp_ne_ut.cpp

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,24 +4062,111 @@ Y_UNIT_TEST_SUITE(KqpNewEngine) {
40624062
Y_UNIT_TEST(AutoChooseIndexOrderByLimit) {
40634063
TKikimrSettings settings;
40644064
NKikimrConfig::TAppConfig appConfig;
4065-
appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_POINTS);
4065+
appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_MAX_USED_PREFIX);
40664066
settings.SetAppConfig(appConfig);
40674067

40684068
TKikimrRunner kikimr(settings);
40694069

40704070
auto db = kikimr.GetTableClient();
40714071
auto session = db.CreateSession().GetValueSync().GetSession();
4072-
CreateSampleTablesWithIndex(session);
4072+
{
4073+
auto session = db.CreateSession().GetValueSync().GetSession();
4074+
AssertSuccessResult(session.ExecuteSchemeQuery(R"(
4075+
--!syntax_v1
4076+
CREATE TABLE `/Root/ComplexKey` (
4077+
Key1 Int32,
4078+
Key2 Int32,
4079+
Key3 Int32,
4080+
Value Int32,
4081+
PRIMARY KEY (Key1, Key2, Key3),
4082+
INDEX Index GLOBAL ON (Key2)
4083+
);
4084+
)").GetValueSync());
4085+
4086+
auto result2 = session.ExecuteDataQuery(R"(
4087+
REPLACE INTO `/Root/ComplexKey` (Key1, Key2, Key3, Value) VALUES
4088+
(1, 1, 101, 1),
4089+
(2, 2, 102, 1),
4090+
(2, 2, 103, 3),
4091+
(3, 3, 103, 2);
4092+
)", TTxControl::BeginTx().CommitTx()).GetValueSync();
4093+
UNIT_ASSERT_C(result2.IsSuccess(), result2.GetIssues().ToString());
4094+
}
4095+
4096+
NYdb::NTable::TExecDataQuerySettings querySettings;
4097+
querySettings.CollectQueryStats(ECollectQueryStatsMode::Profile);
4098+
4099+
{
4100+
auto result = session.ExecuteDataQuery(R"(
4101+
--!syntax_v1
4102+
SELECT Key1, Key2, Key3 FROM `/Root/ComplexKey`
4103+
WHERE Key1 = 2 and Key2 = 2;
4104+
)", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync();
4105+
AssertSuccessResult(result);
4106+
AssertTableReads(result, "/Root/ComplexKey/Index/indexImplTable", 2);
4107+
}
4108+
4109+
{
4110+
auto result = session.ExecuteDataQuery(R"(
4111+
--!syntax_v1
4112+
SELECT Key1, Key2, Key3 FROM `/Root/ComplexKey`
4113+
WHERE Key1 = 2 and Key2 = 2
4114+
ORDER BY Key1 DESC
4115+
LIMIT 1;
4116+
)", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync();
4117+
AssertSuccessResult(result);
4118+
AssertTableReads(result, "/Root/ComplexKey/Index/indexImplTable", 0);
4119+
}
4120+
}
4121+
4122+
Y_UNIT_TEST(AutoChooseIndexOrderByLambda) {
4123+
TKikimrSettings settings;
4124+
NKikimrConfig::TAppConfig appConfig;
4125+
appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_MAX_USED_PREFIX);
4126+
settings.SetAppConfig(appConfig);
4127+
4128+
TKikimrRunner kikimr(settings);
4129+
4130+
auto db = kikimr.GetTableClient();
4131+
auto session = db.CreateSession().GetValueSync().GetSession();
4132+
{
4133+
auto session = db.CreateSession().GetValueSync().GetSession();
4134+
AssertSuccessResult(session.ExecuteSchemeQuery(R"(
4135+
--!syntax_v1
4136+
CREATE TABLE `/Root/ComplexKey` (
4137+
Key Int32,
4138+
Fk Int32,
4139+
Value String,
4140+
PRIMARY KEY (Key, Fk),
4141+
INDEX Index GLOBAL ON (Value)
4142+
);
4143+
)").GetValueSync());
4144+
4145+
auto result2 = session.ExecuteDataQuery(R"(
4146+
REPLACE INTO `/Root/ComplexKey` (Key, Fk, Value) VALUES
4147+
(null, null, "NullValue"),
4148+
(1, 101, "Value1"),
4149+
(2, 102, "Value1"),
4150+
(2, 103, "Value3"),
4151+
(3, 103, "Value2"),
4152+
(4, 104, "Value2"),
4153+
(5, 105, "Value3");
4154+
)", TTxControl::BeginTx().CommitTx()).GetValueSync();
4155+
UNIT_ASSERT_C(result2.IsSuccess(), result2.GetIssues().ToString());
4156+
}
40734157

40744158
NYdb::NTable::TExecDataQuerySettings querySettings;
40754159
querySettings.CollectQueryStats(ECollectQueryStatsMode::Profile);
40764160

40774161
auto result = session.ExecuteDataQuery(R"(
40784162
--!syntax_v1
4079-
SELECT Fk, Key FROM `/Root/SecondaryKeys` WHERE Fk = 1 ORDER BY Key DESC LIMIT 1;
4163+
SELECT Key, Fk, Value FROM `/Root/ComplexKey`
4164+
WHERE Key = 2
4165+
ORDER BY Value DESC
4166+
LIMIT 1;
40804167
)", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync();
40814168
AssertSuccessResult(result);
4082-
AssertTableReads(result, "/Root/SecondaryKeys/Index/indexImplTable", 0);
4169+
AssertTableReads(result, "/Root/ComplexKey", 2);
40834170
}
40844171

40854172
Y_UNIT_TEST(MultipleBroadcastJoin) {

0 commit comments

Comments
 (0)