Skip to content

Commit 08da06d

Browse files
authored
ensure reads are sequential (#11280)
1 parent 2d120ed commit 08da06d

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
@@ -59,6 +59,18 @@ TMaybeNode<TCoLambda> ExtractTopSortKeySelector(TExprBase node, const NYql::TPar
5959
return {};
6060
}
6161

62+
bool IsIdLambda(TExprBase body) {
63+
if (auto cond = body.Maybe<TCoConditionalValueBase>()) {
64+
if (auto boolLit = cond.Cast().Predicate().Maybe<TCoBool>()) {
65+
return boolLit.Literal().Cast().Value() == "true" && cond.Value().Maybe<TCoArgument>();
66+
}
67+
}
68+
if (body.Maybe<TCoArgument>()) {
69+
return true;
70+
}
71+
return false;
72+
}
73+
6274
} // namespace
6375

6476
TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext& kqpCtx,
@@ -156,7 +168,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
156168
const NYql::TKikimrTableDescription & tableDesc) -> TIndexComparisonKey
157169
{
158170
return std::make_tuple(
159-
keySelector.IsValid() && IsSortKeyPrimary(keySelector.Cast(), tableDesc),
171+
keySelector.IsValid() && IsSortKeyPrimary(keySelector.Cast(), tableDesc) && IsIdLambda(TCoLambda(buildResult.PrunedLambda).Body()),
160172
buildResult.PointPrefixLen >= descriptionKeyColumns,
161173
buildResult.PointPrefixLen >= descriptionKeyColumns ? 0 : buildResult.PointPrefixLen,
162174
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
@@ -4020,24 +4020,111 @@ Y_UNIT_TEST_SUITE(KqpNewEngine) {
40204020
Y_UNIT_TEST(AutoChooseIndexOrderByLimit) {
40214021
TKikimrSettings settings;
40224022
NKikimrConfig::TAppConfig appConfig;
4023-
appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_POINTS);
4023+
appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_MAX_USED_PREFIX);
40244024
settings.SetAppConfig(appConfig);
40254025

40264026
TKikimrRunner kikimr(settings);
40274027

40284028
auto db = kikimr.GetTableClient();
40294029
auto session = db.CreateSession().GetValueSync().GetSession();
4030-
CreateSampleTablesWithIndex(session);
4030+
{
4031+
auto session = db.CreateSession().GetValueSync().GetSession();
4032+
AssertSuccessResult(session.ExecuteSchemeQuery(R"(
4033+
--!syntax_v1
4034+
CREATE TABLE `/Root/ComplexKey` (
4035+
Key1 Int32,
4036+
Key2 Int32,
4037+
Key3 Int32,
4038+
Value Int32,
4039+
PRIMARY KEY (Key1, Key2, Key3),
4040+
INDEX Index GLOBAL ON (Key2)
4041+
);
4042+
)").GetValueSync());
4043+
4044+
auto result2 = session.ExecuteDataQuery(R"(
4045+
REPLACE INTO `/Root/ComplexKey` (Key1, Key2, Key3, Value) VALUES
4046+
(1, 1, 101, 1),
4047+
(2, 2, 102, 1),
4048+
(2, 2, 103, 3),
4049+
(3, 3, 103, 2);
4050+
)", TTxControl::BeginTx().CommitTx()).GetValueSync();
4051+
UNIT_ASSERT_C(result2.IsSuccess(), result2.GetIssues().ToString());
4052+
}
4053+
4054+
NYdb::NTable::TExecDataQuerySettings querySettings;
4055+
querySettings.CollectQueryStats(ECollectQueryStatsMode::Profile);
4056+
4057+
{
4058+
auto result = session.ExecuteDataQuery(R"(
4059+
--!syntax_v1
4060+
SELECT Key1, Key2, Key3 FROM `/Root/ComplexKey`
4061+
WHERE Key1 = 2 and Key2 = 2;
4062+
)", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync();
4063+
AssertSuccessResult(result);
4064+
AssertTableReads(result, "/Root/ComplexKey/Index/indexImplTable", 2);
4065+
}
4066+
4067+
{
4068+
auto result = session.ExecuteDataQuery(R"(
4069+
--!syntax_v1
4070+
SELECT Key1, Key2, Key3 FROM `/Root/ComplexKey`
4071+
WHERE Key1 = 2 and Key2 = 2
4072+
ORDER BY Key1 DESC
4073+
LIMIT 1;
4074+
)", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync();
4075+
AssertSuccessResult(result);
4076+
AssertTableReads(result, "/Root/ComplexKey/Index/indexImplTable", 0);
4077+
}
4078+
}
4079+
4080+
Y_UNIT_TEST(AutoChooseIndexOrderByLambda) {
4081+
TKikimrSettings settings;
4082+
NKikimrConfig::TAppConfig appConfig;
4083+
appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_MAX_USED_PREFIX);
4084+
settings.SetAppConfig(appConfig);
4085+
4086+
TKikimrRunner kikimr(settings);
4087+
4088+
auto db = kikimr.GetTableClient();
4089+
auto session = db.CreateSession().GetValueSync().GetSession();
4090+
{
4091+
auto session = db.CreateSession().GetValueSync().GetSession();
4092+
AssertSuccessResult(session.ExecuteSchemeQuery(R"(
4093+
--!syntax_v1
4094+
CREATE TABLE `/Root/ComplexKey` (
4095+
Key Int32,
4096+
Fk Int32,
4097+
Value String,
4098+
PRIMARY KEY (Key, Fk),
4099+
INDEX Index GLOBAL ON (Value)
4100+
);
4101+
)").GetValueSync());
4102+
4103+
auto result2 = session.ExecuteDataQuery(R"(
4104+
REPLACE INTO `/Root/ComplexKey` (Key, Fk, Value) VALUES
4105+
(null, null, "NullValue"),
4106+
(1, 101, "Value1"),
4107+
(2, 102, "Value1"),
4108+
(2, 103, "Value3"),
4109+
(3, 103, "Value2"),
4110+
(4, 104, "Value2"),
4111+
(5, 105, "Value3");
4112+
)", TTxControl::BeginTx().CommitTx()).GetValueSync();
4113+
UNIT_ASSERT_C(result2.IsSuccess(), result2.GetIssues().ToString());
4114+
}
40314115

40324116
NYdb::NTable::TExecDataQuerySettings querySettings;
40334117
querySettings.CollectQueryStats(ECollectQueryStatsMode::Profile);
40344118

40354119
auto result = session.ExecuteDataQuery(R"(
40364120
--!syntax_v1
4037-
SELECT Fk, Key FROM `/Root/SecondaryKeys` WHERE Fk = 1 ORDER BY Key DESC LIMIT 1;
4121+
SELECT Key, Fk, Value FROM `/Root/ComplexKey`
4122+
WHERE Key = 2
4123+
ORDER BY Value DESC
4124+
LIMIT 1;
40384125
)", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync();
40394126
AssertSuccessResult(result);
4040-
AssertTableReads(result, "/Root/SecondaryKeys/Index/indexImplTable", 0);
4127+
AssertTableReads(result, "/Root/ComplexKey", 2);
40414128
}
40424129

40434130
Y_UNIT_TEST(MultipleBroadcastJoin) {

0 commit comments

Comments
 (0)