From 776b371efb88d6304bf00ea98996d5395569f1b5 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Thu, 28 Nov 2024 12:34:07 +0300 Subject: [PATCH] Fix CompareRanges (#12043) --- ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp | 8 + ydb/core/scheme/scheme_ranges_ut.cpp | 139 ++++++++++++++++++ ydb/core/scheme/scheme_tabledefs.h | 4 +- ydb/core/scheme/ut/ya.make | 1 + 4 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 ydb/core/scheme/scheme_ranges_ut.cpp diff --git a/ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp b/ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp index 22aa68373965..4268b42a4031 100644 --- a/ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp +++ b/ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp @@ -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) { diff --git a/ydb/core/scheme/scheme_ranges_ut.cpp b/ydb/core/scheme/scheme_ranges_ut.cpp new file mode 100644 index 000000000000..5a613290b754 --- /dev/null +++ b/ydb/core/scheme/scheme_ranges_ut.cpp @@ -0,0 +1,139 @@ +#include + +#include + +#include + +#include + +namespace NKikimr { + +Y_UNIT_TEST_SUITE(SchemeRanges) { + + TVector MakeTypes(size_t keysCount) { + TVector 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 firstLeft = {MakeUi32(1)}; + TVector firstRight = {MakeUi32(10)}; + TVector secondLeft = {MakeUi32(1)}; + TVector 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 firstLeft = {MakeUi32(1)}; + TVector firstRight = {MakeUi32(10)}; + TVector secondLeft = {MakeUi32(10)}; + TVector 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 b1 = {MakeUi32(1)}; + TVector b10 = {MakeUi32(10)}; + TVector b100 = {MakeUi32(100)}; + + for (ui32 flags = 0; flags < (1 << 2); ++flags) { + UNIT_ASSERT_EQUAL((CompareBorders(b1, b10, ((flags >> 0) & 1), ((flags >> 1) & 1), types)), -1); + UNIT_ASSERT_EQUAL((CompareBorders(b100, b10, ((flags >> 0) & 1), ((flags >> 1) & 1), types)), 1); + } + + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, true, types)), 0); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, true, types)), -1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, false, types)), 1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, false, types)), 0); + + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, true, types)), 0); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, true, types)), 1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, false, types)), -1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, false, types)), 0); + + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, true, types)), 0); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, true, types)), -1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, false, types)), -1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, false, types)), -1); + + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, true, types)), 0); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, true, types)), 1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, true, false, types)), 1); + UNIT_ASSERT_EQUAL((CompareBorders(b10, b10, false, false, types)), 1); + } +} + +} diff --git a/ydb/core/scheme/scheme_tabledefs.h b/ydb/core/scheme/scheme_tabledefs.h index 04ad724cab4b..b81c7d121190 100644 --- a/ydb/core/scheme/scheme_tabledefs.h +++ b/ydb/core/scheme/scheme_tabledefs.h @@ -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( + int xStart_yEnd = CompareBorders( rangeX.From, rangeY.To, rangeX.InclusiveFrom, rangeY.InclusiveTo, types); if (xStart_yEnd > 0) return 1; - int xEnd_yStart = CompareBorders( + int xEnd_yStart = CompareBorders( rangeX.To, rangeY.From, rangeX.InclusiveTo, rangeY.InclusiveFrom, types); if (xEnd_yStart < 0) return -1; diff --git a/ydb/core/scheme/ut/ya.make b/ydb/core/scheme/ut/ya.make index 0bcbd8ac8ba3..97b01cdeb8d9 100644 --- a/ydb/core/scheme/ut/ya.make +++ b/ydb/core/scheme/ut/ya.make @@ -12,6 +12,7 @@ PEERDIR( SRCS( scheme_borders_ut.cpp + scheme_ranges_ut.cpp scheme_tablecell_ut.cpp )