aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Vasilev <ns-vasilev@ydb.tech>2024-11-28 12:34:07 +0300
committerGitHub <noreply@github.com>2024-11-28 12:34:07 +0300
commit776b371efb88d6304bf00ea98996d5395569f1b5 (patch)
tree1b31dfd87d4abeaf52ebb72a7bbc1ba2a5ef53b3
parent9b2bdb08de3df99f801c281b1c7db0e6c74fa630 (diff)
downloadydb-776b371efb88d6304bf00ea98996d5395569f1b5.tar.gz
Fix CompareRanges (#12043)
-rw-r--r--ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp8
-rw-r--r--ydb/core/scheme/scheme_ranges_ut.cpp139
-rw-r--r--ydb/core/scheme/scheme_tabledefs.h4
-rw-r--r--ydb/core/scheme/ut/ya.make1
4 files changed, 150 insertions, 2 deletions
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 22aa6837396..4268b42a403 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 00000000000..5a613290b75
--- /dev/null
+++ b/ydb/core/scheme/scheme_ranges_ut.cpp
@@ -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);
+ }
+}
+
+}
diff --git a/ydb/core/scheme/scheme_tabledefs.h b/ydb/core/scheme/scheme_tabledefs.h
index 04ad724cab4..b81c7d12119 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<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;
diff --git a/ydb/core/scheme/ut/ya.make b/ydb/core/scheme/ut/ya.make
index 0bcbd8ac8ba..97b01cdeb8d 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
)