summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/ce
diff options
context:
space:
mode:
authorTimour Katchaounov <timour.katchaounov@mongodb.com>2022-10-20 05:37:28 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-10-20 06:10:35 +0000
commit28450cf43e1c28c2bf6b75c9fdfec6ad96ad584b (patch)
tree02c72f95452324e587c5d1c463d97e3a4b4fa3db /src/mongo/db/query/ce
parent426956403eeb184097ea4e1933289fc64d590676 (diff)
downloadmongo-28450cf43e1c28c2bf6b75c9fdfec6ad96ad584b.tar.gz
SERVER-70514 Estimate number of arrays via type counter
Diffstat (limited to 'src/mongo/db/query/ce')
-rw-r--r--src/mongo/db/query/ce/array_histogram.cpp18
-rw-r--r--src/mongo/db/query/ce/array_histogram.h10
-rw-r--r--src/mongo/db/query/ce/ce_array_data_test.cpp18
-rw-r--r--src/mongo/db/query/ce/ce_generated_histograms_test.cpp9
-rw-r--r--src/mongo/db/query/ce/ce_histogram_test.cpp58
-rw-r--r--src/mongo/db/query/ce/ce_interpolation_test.cpp6
-rw-r--r--src/mongo/db/query/ce/max_diff.cpp12
-rw-r--r--src/mongo/db/query/ce/maxdiff_histogram_test.cpp17
-rw-r--r--src/mongo/db/query/ce/rand_utils.cpp7
-rw-r--r--src/mongo/db/query/ce/rand_utils.h2
10 files changed, 100 insertions, 57 deletions
diff --git a/src/mongo/db/query/ce/array_histogram.cpp b/src/mongo/db/query/ce/array_histogram.cpp
index 6050625846a..216bfa3fcc5 100644
--- a/src/mongo/db/query/ce/array_histogram.cpp
+++ b/src/mongo/db/query/ce/array_histogram.cpp
@@ -40,9 +40,11 @@ ArrayHistogram::ArrayHistogram(ScalarHistogram scalar,
ScalarHistogram arrayUnique,
ScalarHistogram arrayMin,
ScalarHistogram arrayMax,
- TypeCounts arrayTypeCounts)
+ TypeCounts arrayTypeCounts,
+ size_t emptyArrayCount)
: _scalar(std::move(scalar)),
_typeCounts(std::move(typeCounts)),
+ _emptyArrayCount(emptyArrayCount),
_arrayUnique(std::move(arrayUnique)),
_arrayMin(std::move(arrayMin)),
_arrayMax(std::move(arrayMax)),
@@ -55,6 +57,7 @@ ArrayHistogram::ArrayHistogram(ScalarHistogram scalar,
ArrayHistogram::ArrayHistogram(ScalarHistogram scalar, TypeCounts typeCounts)
: _scalar(std::move(scalar)),
_typeCounts(std::move(typeCounts)),
+ _emptyArrayCount(0),
_arrayUnique(boost::none),
_arrayMin(boost::none),
_arrayMax(boost::none),
@@ -136,17 +139,4 @@ size_t ArrayHistogram::getArrayCount() const {
return 0;
}
-size_t ArrayHistogram::getEmptyArrayCount() const {
- if (isArray()) {
- size_t nonEmptyArrayCount = _arrayMin->empty() ? 0 : _arrayMin->getCardinality();
- size_t totalArrCount = getArrayCount();
- uassert(6979502,
- "The number of empty arrays is < the total number of arrays.",
- totalArrCount >= nonEmptyArrayCount);
- size_t emptyArrCount = totalArrCount - nonEmptyArrayCount;
- return emptyArrCount;
- }
- return 0;
-}
-
} // namespace mongo::ce
diff --git a/src/mongo/db/query/ce/array_histogram.h b/src/mongo/db/query/ce/array_histogram.h
index 11feda3fbae..38ed53f28b4 100644
--- a/src/mongo/db/query/ce/array_histogram.h
+++ b/src/mongo/db/query/ce/array_histogram.h
@@ -52,7 +52,8 @@ public:
ScalarHistogram arrayUnique,
ScalarHistogram arrayMin,
ScalarHistogram arrayMax,
- TypeCounts arrayTypeCounts);
+ TypeCounts arrayTypeCounts,
+ size_t emptyArrayCount);
// ArrayHistogram is neither copy-constructible nor copy-assignable.
ArrayHistogram(const ArrayHistogram&) = delete;
@@ -76,14 +77,19 @@ public:
// The total number of arrays in the histogram's path including empty arrays.
size_t getArrayCount() const;
// The total number of empty arrays ( [] ) in the histogram's path.
- size_t getEmptyArrayCount() const;
+ size_t getEmptyArrayCount() const {
+ return _emptyArrayCount;
+ }
private:
/* ScalarHistogram fields for all paths. */
// Contains values which appeared originally as scalars on the path.
ScalarHistogram _scalar;
+ // The number of values of each type inside all arrays.
TypeCounts _typeCounts;
+ // The number of empty arrays - they are not accounted for in the histograms.
+ size_t _emptyArrayCount;
/* ScalarHistogram fields for array paths (only initialized if arrays are present). */
diff --git a/src/mongo/db/query/ce/ce_array_data_test.cpp b/src/mongo/db/query/ce/ce_array_data_test.cpp
index 90489906625..53d325641f8 100644
--- a/src/mongo/db/query/ce/ce_array_data_test.cpp
+++ b/src/mongo/db/query/ce/ce_array_data_test.cpp
@@ -159,8 +159,13 @@ TEST(EstimatorArrayDataTest, Histogram1000ArraysSmall10Buckets) {
typeCounts.insert({value::TypeTags::Array, 1000});
arrayTypeCounts.insert({value::TypeTags::NumberInt32, 3996});
- const ArrayHistogram arrHist(
- scalarHist, typeCounts, aUniqueHist, aMinHist, aMaxHist, arrayTypeCounts);
+ const ArrayHistogram arrHist(scalarHist,
+ typeCounts,
+ aUniqueHist,
+ aMinHist,
+ aMaxHist,
+ arrayTypeCounts,
+ 0 /* emptyArrayCount */);
std::vector<QuerySpec> querySet{{10, 20, 35.7, 93.0, 37.8, 39.0},
{10, 60, 103.3, 240.0, 158.0, 196.0},
@@ -245,8 +250,13 @@ TEST(EstimatorArrayDataTest, Histogram1000ArraysLarge10Buckets) {
typeCounts.insert({value::TypeTags::Array, 1000});
arrayTypeCounts.insert({value::TypeTags::NumberInt32, 8940});
- const ArrayHistogram arrHist(
- scalarHist, typeCounts, aUniqueHist, aMinHist, aMaxHist, arrayTypeCounts);
+ const ArrayHistogram arrHist(scalarHist,
+ typeCounts,
+ aUniqueHist,
+ aMinHist,
+ aMaxHist,
+ arrayTypeCounts,
+ 0 /* emptyArrayCount */);
std::vector<QuerySpec> querySet{{10, 20, 13.7, 39.0, 9.7, 26.0},
{10, 60, 41.6, 108.0, 55.7, 101.0},
diff --git a/src/mongo/db/query/ce/ce_generated_histograms_test.cpp b/src/mongo/db/query/ce/ce_generated_histograms_test.cpp
index a5daac6b4fd..c3751acc418 100644
--- a/src/mongo/db/query/ce/ce_generated_histograms_test.cpp
+++ b/src/mongo/db/query/ce/ce_generated_histograms_test.cpp
@@ -221,8 +221,13 @@ TEST(EstimatorTest, IntStrArrayEstimate) {
{value::TypeTags::Array, 293}};
TypeCounts arrayTypeCounts{{value::TypeTags::NumberInt64, 874},
{value::TypeTags::StringSmall, 340}};
- const ArrayHistogram arrHist(
- scalarHist, typeCounts, uniqueHist, minHist, maxHist, arrayTypeCounts);
+ const ArrayHistogram arrHist(scalarHist,
+ typeCounts,
+ uniqueHist,
+ minHist,
+ maxHist,
+ arrayTypeCounts,
+ 0 /* No empty arrays */);
const auto [tagLowDbl, valLowDbl] =
std::make_pair(value::TypeTags::NumberDouble,
diff --git a/src/mongo/db/query/ce/ce_histogram_test.cpp b/src/mongo/db/query/ce/ce_histogram_test.cpp
index 81c3058fd29..d2506c7bf99 100644
--- a/src/mongo/db/query/ce/ce_histogram_test.cpp
+++ b/src/mongo/db/query/ce/ce_histogram_test.cpp
@@ -121,21 +121,19 @@ std::unique_ptr<ArrayHistogram> getArrayHistogramFromData(
std::vector<TestBucket> arrayMinBuckets,
std::vector<TestBucket> arrayMaxBuckets,
TypeCounts arrayTypeCounts,
+ size_t totalArrayCount,
size_t emptyArrayCount = 0) {
auto arrayMinHist = getHistogramFromData(arrayMinBuckets);
auto arrayMaxHist = getHistogramFromData(arrayMaxBuckets);
TypeCounts typeCounts = getTypeCountsFromData(scalarBuckets);
- // The min/max histograms contain one (min or max) value from each array. Therefore the
- // total number of min/max values is equal to the number of non non-empty arrays.
- // The total number of arrays needs to account for the additional empty arrays not present
- // in histograms.
- typeCounts[value::TypeTags::Array] = arrayMinHist.getCardinality() + emptyArrayCount;
+ typeCounts[value::TypeTags::Array] = totalArrayCount;
return std::make_unique<ArrayHistogram>(getHistogramFromData(scalarBuckets),
std::move(typeCounts),
getHistogramFromData(arrayUniqueBuckets),
std::move(arrayMinHist),
std::move(arrayMaxHist),
- std::move(arrayTypeCounts));
+ std::move(arrayTypeCounts),
+ emptyArrayCount);
}
TEST(CEHistogramTest, AssertSmallMaxDiffHistogramEstimatesAtomicPredicates) {
@@ -457,8 +455,8 @@ TEST(CEHistogramTest, TestArrayHistogramOnAtomicPredicates) {
// Generate a histogram for this data:
// {a: 1}, {a: 2}, {a: [1, 2, 3, 2, 2]}, {a: [10]}, {a: [2, 3, 3, 4, 5, 5, 6]}, {a: []}
// - scalars: [1, 2]
- // - unique values: [1], [2], [1, 2, 3], [10], [2, 3, 4, 5, 6]
- // -> [1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 10]
+ // - unique values: [1, 2, 3], [10], [2, 3, 4, 5, 6]
+ // -> [1, 2, 2, 3, 3, 4, 5, 6, 10]
// - min values: [1], [10], [2] -> [1, 1, 2, 2, 10]
// - max values: [3], [10], [6] -> [1, 2, 3, 6, 10]
getArrayHistogramFromData(
@@ -487,8 +485,9 @@ TEST(CEHistogramTest, TestArrayHistogramOnAtomicPredicates) {
{Value(6), 1 /* frequency */},
{Value(10), 1 /* frequency */},
},
- {{sbe::value::TypeTags::NumberInt32, 4}}, // Array type counts.
- 1 // 1 empty array.
+ {{sbe::value::TypeTags::NumberInt32, 13}}, // Array type counts.
+ 3, // 3 arrays total.
+ 1 // 1 empty array.
));
CEHistogramTester t(collName, collCardinality, collStats);
@@ -514,20 +513,20 @@ TEST(CEHistogramTest, TestArrayHistogramOnAtomicPredicates) {
ASSERT_EQ_ELEMMATCH_CE(t, 0.0 /* CE */, 0.0 /* $elemMatch CE */, "a", "{$gt: 10}");
ASSERT_EQ_ELEMMATCH_CE(t, 1.0 /* CE */, 1.0 /* $elemMatch CE */, "a", "{$gte: 10}");
- ASSERT_EQ_ELEMMATCH_CE(t, 5.0 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$lte: 10}");
- ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$lt: 10}");
- ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$gt: 1}");
- ASSERT_EQ_ELEMMATCH_CE(t, 5.0 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$gte: 1}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 5.0 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$lte: 10}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$lt: 10}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$gt: 1}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 5.0 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$gte: 1}");
- ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$lte: 5}");
- ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$lt: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$lte: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 4.0 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$lt: 5}");
ASSERT_EQ_ELEMMATCH_CE(t, 2.0 /* CE */, 2.0 /* $elemMatch CE */, "a", "{$gt: 5}");
- ASSERT_EQ_ELEMMATCH_CE(t, 2.0 /* CE */, 2.55 /* $elemMatch CE */, "a", "{$gte: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 2.0 /* CE */, 2.40822 /* $elemMatch CE */, "a", "{$gte: 5}");
- ASSERT_EQ_ELEMMATCH_CE(t, 2.45 /* CE */, 2.55 /* $elemMatch CE */, "a", "{$gt: 2, $lt: 5}");
- ASSERT_EQ_ELEMMATCH_CE(t, 3.27 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$gte: 2, $lt: 5}");
- ASSERT_EQ_ELEMMATCH_CE(t, 2.45 /* CE */, 3.40 /* $elemMatch CE */, "a", "{$gt: 2, $lte: 5}");
- ASSERT_EQ_ELEMMATCH_CE(t, 3.27 /* CE */, 4.0 /* $elemMatch CE */, "a", "{$gte: 2, $lte: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 2.45 /* CE */, 2.40822 /* $elemMatch CE */, "a", "{$gt: 2, $lt: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 3.27 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$gte: 2, $lt: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 2.45 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$gt: 2, $lte: 5}");
+ ASSERT_EQ_ELEMMATCH_CE(t, 3.27 /* CE */, 3.0 /* $elemMatch CE */, "a", "{$gte: 2, $lte: 5}");
}
TEST(CEHistogramTest, TestArrayHistogramOnCompositePredicates) {
@@ -573,8 +572,9 @@ TEST(CEHistogramTest, TestArrayHistogramOnCompositePredicates) {
{Value(6), 35 /* frequency */},
{Value(10), 35 /* frequency */},
},
- {{sbe::value::TypeTags::NumberInt32, collCardinality}}, // Array type counts.
- 35 // 35 empty arrays
+ {{sbe::value::TypeTags::NumberInt32, 420}}, // Array type count = 3*35+5*35+1*35+3*35.
+ collCardinality, // collCardinality arrays total.
+ 35 // 35 empty arrays
));
collStats->addHistogram(
@@ -611,8 +611,9 @@ TEST(CEHistogramTest, TestArrayHistogramOnCompositePredicates) {
{Value(6), 17 /* frequency */},
{Value(10), 17 /* frequency */},
},
- {{sbe::value::TypeTags::NumberInt32, 88}}, // Array type counts.
- 20 // 20 empty arrays.
+ {{sbe::value::TypeTags::NumberInt32, 289}}, // Array type count = 3*17+5*17+6*17+3*17
+ 88, // collCardinality arrays total.
+ 20 // 20 empty arrays.
));
CEHistogramTester t(collName, collCardinality, collStats);
@@ -741,9 +742,10 @@ TEST(CEHistogramTest, TestMixedElemMatchAndNonElemMatch) {
// Array max buckets.
{Value(10), 1 /* frequency */},
},
- {{sbe::value::TypeTags::NumberInt32,
- collCardinality}} // Array type counts.
- ));
+ {{sbe::value::TypeTags::NumberInt32, 2}},
+ // Array type counts.
+ 1,
+ 0));
CEHistogramTester t(collName, collCardinality, collStats);
diff --git a/src/mongo/db/query/ce/ce_interpolation_test.cpp b/src/mongo/db/query/ce/ce_interpolation_test.cpp
index 02e4e8a3395..6d9d52b347d 100644
--- a/src/mongo/db/query/ce/ce_interpolation_test.cpp
+++ b/src/mongo/db/query/ce/ce_interpolation_test.cpp
@@ -366,7 +366,8 @@ TEST(EstimatorTest, UniformIntArrayOnlyEstimate) {
uniqueHist,
minHist,
maxHist,
- TypeCounts{});
+ TypeCounts{},
+ 0);
// Query in the middle of the domain: estimate from ArrayUnique histogram.
value::TypeTags lowTag = value::TypeTags::NumberInt64;
@@ -469,7 +470,8 @@ TEST(EstimatorTest, UniformIntMixedArrayEstimate) {
uniqueHist,
minHist,
maxHist,
- TypeCounts{{value::TypeTags::NumberInt64, 375}});
+ TypeCounts{{value::TypeTags::NumberInt64, 375}},
+ 0);
value::TypeTags lowTag = value::TypeTags::NumberInt64;
value::Value lowVal = 500;
diff --git a/src/mongo/db/query/ce/max_diff.cpp b/src/mongo/db/query/ce/max_diff.cpp
index 944d43aa4ba..7b7177c8d45 100644
--- a/src/mongo/db/query/ce/max_diff.cpp
+++ b/src/mongo/db/query/ce/max_diff.cpp
@@ -237,6 +237,7 @@ ArrayHistogram createArrayEstimator(const std::vector<SBEValue>& arrayData, size
std::vector<SBEValue> arrayUniqueData;
std::map<value::TypeTags, size_t> typeCounts;
std::map<value::TypeTags, size_t> arrayTypeCounts;
+ size_t emptyArrayCount = 0;
for (const auto& v : arrayData) {
auto tagCount = typeCounts.insert({v.getTag(), 1});
@@ -249,7 +250,13 @@ ArrayHistogram createArrayEstimator(const std::vector<SBEValue>& arrayData, size
if (v.getTag() == value::TypeTags::Array) {
std::vector<SBEValue> arrayElements;
value::Array* arr = value::getArrayView(v.getValue());
- for (size_t i = 0; i < arr->size(); i++) {
+ size_t arrSize = arr->size();
+ if (arrSize == 0) {
+ ++emptyArrayCount;
+ continue;
+ }
+
+ for (size_t i = 0; i < arrSize; i++) {
const auto [tag, val] = arr->getAt(i);
auto arrTagCount = arrayTypeCounts.insert({tag, 1});
if (!arrTagCount.second) {
@@ -319,7 +326,8 @@ ArrayHistogram createArrayEstimator(const std::vector<SBEValue>& arrayData, size
std::move(arrayUniqueHist),
std::move(arrayMinHist),
std::move(arrayMaxHist),
- std::move(arrayTypeCounts)};
+ std::move(arrayTypeCounts),
+ emptyArrayCount};
}
} // namespace mongo::ce
diff --git a/src/mongo/db/query/ce/maxdiff_histogram_test.cpp b/src/mongo/db/query/ce/maxdiff_histogram_test.cpp
index aa8ebdb3891..2f1e2185f8d 100644
--- a/src/mongo/db/query/ce/maxdiff_histogram_test.cpp
+++ b/src/mongo/db/query/ce/maxdiff_histogram_test.cpp
@@ -191,7 +191,7 @@ TEST_F(HistogramTest, MaxDiffIntArrays) {
constexpr size_t nBuckets = 10;
auto rawData = genFixedValueArray(nElems, 1.0, 0.0);
- auto arrayData = nestArrays(rawData);
+ auto arrayData = nestArrays(rawData, 0 /* No empty arrays */);
ArrayHistogram estimator = createArrayEstimator(arrayData, nBuckets);
@@ -250,5 +250,20 @@ TEST_F(HistogramTest, MaxDiffIntArrays) {
}
}
+TEST_F(HistogramTest, MaxDiffEmptyArrays) {
+ constexpr size_t nElems = 21;
+ constexpr size_t nBuckets = 5;
+ constexpr size_t emptyArrayCount = 3;
+
+ auto rawData = genFixedValueArray(nElems, 1.0, 0.0);
+ auto arrayData = nestArrays(rawData, emptyArrayCount);
+ std::cout << "Generated " << nElems << " arrayData:\n"
+ << printValueArray(arrayData) << "\n"
+ << std::flush;
+
+ ArrayHistogram arrayHist = createArrayEstimator(arrayData, nBuckets);
+ ASSERT_EQ(arrayHist.getEmptyArrayCount(), emptyArrayCount);
+}
+
} // namespace
} // namespace mongo::ce::statistics
diff --git a/src/mongo/db/query/ce/rand_utils.cpp b/src/mongo/db/query/ce/rand_utils.cpp
index 05bfcf393df..7f317904298 100644
--- a/src/mongo/db/query/ce/rand_utils.cpp
+++ b/src/mongo/db/query/ce/rand_utils.cpp
@@ -349,7 +349,7 @@ std::vector<SBEValue> genRandomValueArray(size_t nElems,
return randValues;
}
-std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input) {
+std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input, size_t emptyArrayCount) {
std::vector<SBEValue> result;
auto [arrayTag, arrayVal] = value::makeNewArray();
@@ -371,6 +371,11 @@ std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input) {
}
}
+ for (size_t i = 0; i < emptyArrayCount; ++i) {
+ auto [emptyArrayTag, emptyArrayVal] = value::makeNewArray();
+ result.emplace_back(emptyArrayTag, emptyArrayVal);
+ }
+
// It's possible that the array still contains something. If it's empty,
// we can safely release it. If not, append it to the result.
value::Array* arr = value::getArrayView(arrayVal);
diff --git a/src/mongo/db/query/ce/rand_utils.h b/src/mongo/db/query/ce/rand_utils.h
index 362d2c7eaec..9a9acad5161 100644
--- a/src/mongo/db/query/ce/rand_utils.h
+++ b/src/mongo/db/query/ce/rand_utils.h
@@ -186,6 +186,6 @@ std::vector<SBEValue> genRandomValueArray(size_t nElems,
Values contained in the result will be drawn from the input vector.
*/
-std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input);
+std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input, size_t emptyArrayCount);
} // namespace mongo::ce