Skip to content

Commit

Permalink
Fix CompareRanges (#12043)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvas0 authored Nov 28, 2024
1 parent 9b2bdb0 commit 776b371
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
8 changes: 8 additions & 0 deletions ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5221,6 +5221,14 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
CompareYson(R"([[6u]])", FormatResultSetYson(result.GetResultSet(0)));
}

{
auto result = client.ExecuteQuery(R"(
SELECT COUNT(*) FROM `/Root/DataShard` WHERE Col1 = "y";
)", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
CompareYson(R"([[2u]])", FormatResultSetYson(result.GetResultSet(0)));
}
}

Y_UNIT_TEST(ReadManyShardsRange) {
Expand Down
139 changes: 139 additions & 0 deletions ydb/core/scheme/scheme_ranges_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include <ydb/core/scheme/scheme_tabledefs.h>

#include <ydb/core/scheme_types/scheme_types.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/vector.h>

namespace NKikimr {

Y_UNIT_TEST_SUITE(SchemeRanges) {

TVector<NScheme::TTypeInfo> MakeTypes(size_t keysCount) {
TVector<NScheme::TTypeInfo> types;
types.reserve(keysCount);
for (size_t i = 0; i < keysCount; ++i) {
types.push_back(NScheme::TTypeInfo(NScheme::NTypeIds::Uint32));
}
return types;
}

TCell MakeUi32(ui32 key) {
return TCell::Make(ui32(key));
}

TCell MakeNull() {
return TCell();
}

Y_UNIT_TEST(RangesBorders) {
auto types = MakeTypes(1);

for (ui32 flags = 0; flags < (1 << 4); ++flags) {
TVector<TCell> firstLeft = {MakeUi32(1)};
TVector<TCell> firstRight = {MakeUi32(10)};
TVector<TCell> secondLeft = {MakeUi32(1)};
TVector<TCell> sedondRight = {MakeUi32(10)};
TTableRange first(firstLeft, ((flags >> 0) & 1), firstRight, ((flags >> 1) & 1));
TTableRange second(secondLeft, ((flags >> 2) & 1), sedondRight, ((flags >> 3) & 1));
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), 0);
}

TVector<TCell> firstLeft = {MakeUi32(1)};
TVector<TCell> firstRight = {MakeUi32(10)};
TVector<TCell> secondLeft = {MakeUi32(10)};
TVector<TCell> sedondRight = {MakeUi32(100)};

{
TTableRange first(firstLeft, true, firstRight, true);
TTableRange second(secondLeft, true, sedondRight, true);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), 0);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 0);
}

{
TTableRange first(firstLeft, true, firstRight, false);
TTableRange second(secondLeft, true, sedondRight, true);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), -1);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 1);
}

{
TTableRange first(firstLeft, true, firstRight, true);
TTableRange second(secondLeft, false, sedondRight, true);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), -1);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 1);
}

{
TTableRange first(firstLeft, true, firstRight, false);
TTableRange second(secondLeft, false, sedondRight, true);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), -1);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 1);
}

{
TTableRange first(firstLeft, false, firstRight, false);
TTableRange second(secondLeft, false, sedondRight, false);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), -1);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 1);
}

{
TTableRange first(firstLeft, false, firstRight, true);
TTableRange second(secondLeft, false, sedondRight, false);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), -1);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 1);
}

{
TTableRange first(firstLeft, false, firstRight, false);
TTableRange second(secondLeft, true, sedondRight, false);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), -1);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 1);
}

{
TTableRange first(firstLeft, false, firstRight, true);
TTableRange second(secondLeft, true, sedondRight, false);
UNIT_ASSERT_EQUAL(CompareRanges(first, second, types), 0);
UNIT_ASSERT_EQUAL(CompareRanges(second, first, types), 0);
}
}

Y_UNIT_TEST(CmpBorders) {
auto types = MakeTypes(1);

TVector<TCell> b1 = {MakeUi32(1)};
TVector<TCell> b10 = {MakeUi32(10)};
TVector<TCell> b100 = {MakeUi32(100)};

for (ui32 flags = 0; flags < (1 << 2); ++flags) {
UNIT_ASSERT_EQUAL((CompareBorders<false, true>(b1, b10, ((flags >> 0) & 1), ((flags >> 1) & 1), types)), -1);
UNIT_ASSERT_EQUAL((CompareBorders<true, false>(b100, b10, ((flags >> 0) & 1), ((flags >> 1) & 1), types)), 1);
}

UNIT_ASSERT_EQUAL((CompareBorders<true, true>(b10, b10, true, true, types)), 0);
UNIT_ASSERT_EQUAL((CompareBorders<true, true>(b10, b10, false, true, types)), -1);
UNIT_ASSERT_EQUAL((CompareBorders<true, true>(b10, b10, true, false, types)), 1);
UNIT_ASSERT_EQUAL((CompareBorders<true, true>(b10, b10, false, false, types)), 0);

UNIT_ASSERT_EQUAL((CompareBorders<false, false>(b10, b10, true, true, types)), 0);
UNIT_ASSERT_EQUAL((CompareBorders<false, false>(b10, b10, false, true, types)), 1);
UNIT_ASSERT_EQUAL((CompareBorders<false, false>(b10, b10, true, false, types)), -1);
UNIT_ASSERT_EQUAL((CompareBorders<false, false>(b10, b10, false, false, types)), 0);

UNIT_ASSERT_EQUAL((CompareBorders<true, false>(b10, b10, true, true, types)), 0);
UNIT_ASSERT_EQUAL((CompareBorders<true, false>(b10, b10, false, true, types)), -1);
UNIT_ASSERT_EQUAL((CompareBorders<true, false>(b10, b10, true, false, types)), -1);
UNIT_ASSERT_EQUAL((CompareBorders<true, false>(b10, b10, false, false, types)), -1);

UNIT_ASSERT_EQUAL((CompareBorders<false, true>(b10, b10, true, true, types)), 0);
UNIT_ASSERT_EQUAL((CompareBorders<false, true>(b10, b10, false, true, types)), 1);
UNIT_ASSERT_EQUAL((CompareBorders<false, true>(b10, b10, true, false, types)), 1);
UNIT_ASSERT_EQUAL((CompareBorders<false, true>(b10, b10, false, false, types)), 1);
}
}

}
4 changes: 2 additions & 2 deletions ydb/core/scheme/scheme_tabledefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ inline int CompareRanges(const TTableRange& rangeX, const TTableRange& rangeY,
Y_ABORT_UNLESS(!rangeX.Point);
Y_ABORT_UNLESS(!rangeY.Point);

int xStart_yEnd = CompareBorders<true, false>(
int xStart_yEnd = CompareBorders<false, true>(
rangeX.From, rangeY.To, rangeX.InclusiveFrom, rangeY.InclusiveTo, types);
if (xStart_yEnd > 0)
return 1;

int xEnd_yStart = CompareBorders<false, true>(
int xEnd_yStart = CompareBorders<true, false>(
rangeX.To, rangeY.From, rangeX.InclusiveTo, rangeY.InclusiveFrom, types);
if (xEnd_yStart < 0)
return -1;
Expand Down
1 change: 1 addition & 0 deletions ydb/core/scheme/ut/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PEERDIR(

SRCS(
scheme_borders_ut.cpp
scheme_ranges_ut.cpp
scheme_tablecell_ut.cpp
)

Expand Down

0 comments on commit 776b371

Please sign in to comment.