diff options
author | Anton Korshunov <anton.korshunov@mongodb.com> | 2022-11-28 14:19:20 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-11-28 15:12:36 +0000 |
commit | b1474e5c22fd2106c1e7c6493052e8fbc450e289 (patch) | |
tree | 84c30cb1b57836a437ebd90b11632a4efbdeb86d /src | |
parent | e4e1b807a5e079dc9fff098294271b63966930e3 (diff) | |
download | mongo-b1474e5c22fd2106c1e7c6493052e8fbc450e289.tar.gz |
SERVER-71051 Make CE module less dependent on statistics module
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/db/commands/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/db/commands/analyze_cmd.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/exec/sbe/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/db/mongod_main.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/pipeline/SConscript | 4 | ||||
-rw-r--r-- | src/mongo/db/pipeline/accumulator.h | 4 | ||||
-rw-r--r-- | src/mongo/db/pipeline/accumulator_internal_construct_stats.cpp | 8 | ||||
-rw-r--r-- | src/mongo/db/query/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/query/ce/SConscript | 184 | ||||
-rw-r--r-- | src/mongo/db/query/ce/generated_histograms_test.cpp (renamed from src/mongo/db/query/ce/ce_generated_histograms_test.cpp) | 15 | ||||
-rw-r--r-- | src/mongo/db/query/ce/heuristic_dataflow_nodes_test.cpp (renamed from src/mongo/db/query/ce/ce_dataflow_nodes_test.cpp) | 20 | ||||
-rw-r--r-- | src/mongo/db/query/ce/heuristic_estimator.cpp (renamed from src/mongo/db/query/ce/ce_heuristic.cpp) | 49 | ||||
-rw-r--r-- | src/mongo/db/query/ce/heuristic_estimator.h (renamed from src/mongo/db/query/ce/ce_heuristic.h) | 14 | ||||
-rw-r--r-- | src/mongo/db/query/ce/heuristic_estimator_test.cpp (renamed from src/mongo/db/query/ce/ce_heuristic_test.cpp) | 99 | ||||
-rw-r--r-- | src/mongo/db/query/ce/hinted_estimator.cpp (renamed from src/mongo/db/query/ce/ce_hinted.cpp) | 54 | ||||
-rw-r--r-- | src/mongo/db/query/ce/hinted_estimator.h (renamed from src/mongo/db/query/ce/ce_hinted.h) | 21 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_array_data_test.cpp (renamed from src/mongo/db/query/ce/ce_array_data_test.cpp) | 15 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_edge_cases_test.cpp (renamed from src/mongo/db/query/ce/ce_edge_cases_test.cpp) | 21 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_estimator.cpp (renamed from src/mongo/db/query/ce/ce_histogram.cpp) | 87 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_estimator.h (renamed from src/mongo/db/query/ce/ce_histogram.h) | 26 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_estimator_test.cpp (renamed from src/mongo/db/query/ce/ce_histogram_test.cpp) | 25 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_interpolation_test.cpp (renamed from src/mongo/db/query/ce/ce_interpolation_test.cpp) | 15 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_predicate_estimation.cpp (renamed from src/mongo/db/query/ce/histogram_estimation.cpp) | 26 | ||||
-rw-r--r-- | src/mongo/db/query/ce/histogram_predicate_estimation.h (renamed from src/mongo/db/query/ce/histogram_estimation.h) | 20 | ||||
-rw-r--r-- | src/mongo/db/query/ce/maxdiff_histogram_test.cpp | 34 | ||||
-rw-r--r-- | src/mongo/db/query/ce/sampling_estimator.cpp (renamed from src/mongo/db/query/ce/ce_sampling.cpp) | 171 | ||||
-rw-r--r-- | src/mongo/db/query/ce/sampling_estimator.h (renamed from src/mongo/db/query/ce/ce_sampling.h) | 28 | ||||
-rw-r--r-- | src/mongo/db/query/ce/test_utils.cpp (renamed from src/mongo/db/query/ce/ce_test_utils.cpp) | 38 | ||||
-rw-r--r-- | src/mongo/db/query/ce/test_utils.h (renamed from src/mongo/db/query/ce/ce_test_utils.h) | 37 | ||||
-rw-r--r-- | src/mongo/db/query/ce_mode_parameter.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/query/ce_mode_parameter.h | 4 | ||||
-rw-r--r-- | src/mongo/db/query/ce_mode_parameter_test.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/query/cost_model/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/db/query/cost_model/cost_estimator_impl.cpp (renamed from src/mongo/db/query/cost_model/cost_estimator.cpp) | 14 | ||||
-rw-r--r-- | src/mongo/db/query/cost_model/cost_estimator_impl.h (renamed from src/mongo/db/query/cost_model/cost_estimator.h) | 4 | ||||
-rw-r--r-- | src/mongo/db/query/cost_model/cost_estimator_test.cpp | 12 | ||||
-rw-r--r-- | src/mongo/db/query/cqf_get_executor.cpp | 49 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/interfaces.h | 8 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/logical_rewriter.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/logical_rewriter.h | 4 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/memo.cpp | 13 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/memo.h | 4 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/physical_rewriter.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/cascades/physical_rewriter.h | 4 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/opt_phase_manager.cpp | 12 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/opt_phase_manager.h | 12 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/utils/ce_math.cpp | 5 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/utils/ce_math.h | 6 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/utils/unit_test_utils.cpp | 32 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/utils/unit_test_utils.h | 10 | ||||
-rw-r--r-- | src/mongo/db/query/query_knobs.idl | 2 | ||||
-rw-r--r-- | src/mongo/db/query/stats/SConscript | 123 | ||||
-rw-r--r-- | src/mongo/db/query/stats/array_histogram.cpp (renamed from src/mongo/db/query/ce/array_histogram.cpp) | 25 | ||||
-rw-r--r-- | src/mongo/db/query/stats/array_histogram.h (renamed from src/mongo/db/query/ce/array_histogram.h) | 20 | ||||
-rw-r--r-- | src/mongo/db/query/stats/collection_statistics.h (renamed from src/mongo/db/query/ce/collection_statistics.h) | 6 | ||||
-rw-r--r-- | src/mongo/db/query/stats/collection_statistics_impl.cpp (renamed from src/mongo/db/query/ce/collection_statistics_impl.cpp) | 9 | ||||
-rw-r--r-- | src/mongo/db/query/stats/collection_statistics_impl.h (renamed from src/mongo/db/query/ce/collection_statistics_impl.h) | 8 | ||||
-rw-r--r-- | src/mongo/db/query/stats/collection_statistics_mock.cpp (renamed from src/mongo/db/query/ce/collection_statistics_mock.cpp) | 6 | ||||
-rw-r--r-- | src/mongo/db/query/stats/collection_statistics_mock.h (renamed from src/mongo/db/query/ce/collection_statistics_mock.h) | 6 | ||||
-rw-r--r-- | src/mongo/db/query/stats/max_diff.cpp (renamed from src/mongo/db/query/ce/max_diff.cpp) | 180 | ||||
-rw-r--r-- | src/mongo/db/query/stats/max_diff.h (renamed from src/mongo/db/query/ce/max_diff.h) | 10 | ||||
-rw-r--r-- | src/mongo/db/query/stats/maxdiff_test_utils.cpp (renamed from src/mongo/db/query/ce/maxdiff_test_utils.cpp) | 22 | ||||
-rw-r--r-- | src/mongo/db/query/stats/maxdiff_test_utils.h (renamed from src/mongo/db/query/ce/maxdiff_test_utils.h) | 16 | ||||
-rw-r--r-- | src/mongo/db/query/stats/rand_utils.cpp (renamed from src/mongo/db/query/ce/rand_utils.cpp) | 9 | ||||
-rw-r--r-- | src/mongo/db/query/stats/rand_utils.h (renamed from src/mongo/db/query/ce/rand_utils.h) | 17 | ||||
-rw-r--r-- | src/mongo/db/query/stats/rand_utils_new.cpp (renamed from src/mongo/db/query/ce/rand_utils_new.cpp) | 9 | ||||
-rw-r--r-- | src/mongo/db/query/stats/rand_utils_new.h (renamed from src/mongo/db/query/ce/rand_utils_new.h) | 15 | ||||
-rw-r--r-- | src/mongo/db/query/stats/scalar_histogram.cpp (renamed from src/mongo/db/query/ce/scalar_histogram.cpp) | 14 | ||||
-rw-r--r-- | src/mongo/db/query/stats/scalar_histogram.h (renamed from src/mongo/db/query/ce/scalar_histogram.h) | 6 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats.idl (renamed from src/mongo/db/query/ce/stats.idl) | 0 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache.cpp (renamed from src/mongo/db/query/ce/stats_cache.cpp) | 16 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache.h (renamed from src/mongo/db/query/ce/stats_cache.h) | 11 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader.h (renamed from src/mongo/db/query/ce/stats_cache_loader.h) | 9 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_impl.cpp (renamed from src/mongo/db/query/ce/stats_cache_loader_impl.cpp) | 12 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_impl.h (renamed from src/mongo/db/query/ce/stats_cache_loader_impl.h) | 10 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_mock.cpp (renamed from src/mongo/db/query/ce/stats_cache_loader_mock.cpp) | 11 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_mock.h (renamed from src/mongo/db/query/ce/stats_cache_loader_mock.h) | 10 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_test.cpp (renamed from src/mongo/db/query/ce/stats_cache_loader_test.cpp) | 26 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_test_fixture.cpp (renamed from src/mongo/db/query/ce/stats_cache_loader_test_fixture.cpp) | 8 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_loader_test_fixture.h (renamed from src/mongo/db/query/ce/stats_cache_loader_test_fixture.h) | 6 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_cache_test.cpp (renamed from src/mongo/db/query/ce/stats_cache_test.cpp) | 10 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_catalog.cpp (renamed from src/mongo/db/query/ce/stats_catalog.cpp) | 19 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_catalog.h (renamed from src/mongo/db/query/ce/stats_catalog.h) | 13 | ||||
-rw-r--r-- | src/mongo/db/query/stats/stats_path_test.cpp (renamed from src/mongo/db/query/ce/stats_path_test.cpp) | 36 | ||||
-rw-r--r-- | src/mongo/db/query/stats/value_utils.cpp (renamed from src/mongo/db/query/ce/value_utils.cpp) | 14 | ||||
-rw-r--r-- | src/mongo/db/query/stats/value_utils.h (renamed from src/mongo/db/query/ce/value_utils.h) | 45 |
87 files changed, 941 insertions, 1071 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index acfed69144a..a33c66d3cdf 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -1520,6 +1520,7 @@ env.Library( '$BUILD_DIR/mongo/db/concurrency/exception_util', '$BUILD_DIR/mongo/db/exec/sbe/query_sbe_abt', '$BUILD_DIR/mongo/db/internal_transactions_feature_flag', + '$BUILD_DIR/mongo/db/query/ce/query_ce_heuristic', '$BUILD_DIR/mongo/db/query/ce/query_ce_histogram', '$BUILD_DIR/mongo/db/query/ce/query_ce_sampling', '$BUILD_DIR/mongo/db/query/optimizer/optimizer', @@ -2330,7 +2331,7 @@ env.Library( '$BUILD_DIR/mongo/db/change_stream_options_manager', '$BUILD_DIR/mongo/db/change_streams_cluster_parameter', '$BUILD_DIR/mongo/db/pipeline/change_stream_expired_pre_image_remover', - '$BUILD_DIR/mongo/db/query/ce/query_ce_histogram', + '$BUILD_DIR/mongo/db/query/stats/query_stats', '$BUILD_DIR/mongo/db/s/query_analysis_writer', '$BUILD_DIR/mongo/db/set_change_stream_state_coordinator', '$BUILD_DIR/mongo/idl/cluster_server_parameter', diff --git a/src/mongo/db/commands/SConscript b/src/mongo/db/commands/SConscript index 3dd709d03e7..d18125f0da1 100644 --- a/src/mongo/db/commands/SConscript +++ b/src/mongo/db/commands/SConscript @@ -360,9 +360,9 @@ env.Library( '$BUILD_DIR/mongo/db/ops/write_ops_exec', '$BUILD_DIR/mongo/db/pipeline/aggregation_request_helper', '$BUILD_DIR/mongo/db/pipeline/process_interface/mongo_process_interface', - '$BUILD_DIR/mongo/db/query/ce/query_ce_histogram', '$BUILD_DIR/mongo/db/query/command_request_response', '$BUILD_DIR/mongo/db/query/cursor_response_idl', + '$BUILD_DIR/mongo/db/query/stats/query_stats', '$BUILD_DIR/mongo/db/query/telemetry', '$BUILD_DIR/mongo/db/query_exec', '$BUILD_DIR/mongo/db/repl/replica_set_messages', diff --git a/src/mongo/db/commands/analyze_cmd.cpp b/src/mongo/db/commands/analyze_cmd.cpp index d47a3b55b41..d6a31329f1c 100644 --- a/src/mongo/db/commands/analyze_cmd.cpp +++ b/src/mongo/db/commands/analyze_cmd.cpp @@ -38,8 +38,8 @@ #include "mongo/db/namespace_string.h" #include "mongo/db/query/allowed_contexts.h" #include "mongo/db/query/analyze_command_gen.h" -#include "mongo/db/query/ce/stats_catalog.h" #include "mongo/db/query/query_feature_flags_gen.h" +#include "mongo/db/query/stats/stats_catalog.h" #include "mongo/rpc/get_status_from_command_result.h" namespace mongo { @@ -195,7 +195,7 @@ public: uassertStatusOK(getStatusFromCommandResult(analyzeResult)); // Invalidate statistics in the cache for the analyzed path - StatsCatalog& statsCatalog = StatsCatalog::get(opCtx); + stats::StatsCatalog& statsCatalog = stats::StatsCatalog::get(opCtx); uassertStatusOK(statsCatalog.invalidatePath(nss, key->toString())); } else if (sampleSize || sampleRate) { diff --git a/src/mongo/db/exec/sbe/SConscript b/src/mongo/db/exec/sbe/SConscript index fc66fc3519e..bf2d6170cf1 100644 --- a/src/mongo/db/exec/sbe/SConscript +++ b/src/mongo/db/exec/sbe/SConscript @@ -137,8 +137,7 @@ env.Library( 'abt/abt_lower.cpp', ], LIBDEPS=[ - '$BUILD_DIR/mongo/db/query/ce/query_ce_heuristic', - '$BUILD_DIR/mongo/db/query/optimizer/optimizer', + '$BUILD_DIR/mongo/db/query/optimizer/optimizer_base', 'query_sbe', 'query_sbe_stages', 'query_sbe_storage', diff --git a/src/mongo/db/mongod_main.cpp b/src/mongo/db/mongod_main.cpp index a23b35f911a..a018c618cbf 100644 --- a/src/mongo/db/mongod_main.cpp +++ b/src/mongo/db/mongod_main.cpp @@ -107,9 +107,9 @@ #include "mongo/db/periodic_runner_job_abort_expired_transactions.h" #include "mongo/db/pipeline/change_stream_expired_pre_image_remover.h" #include "mongo/db/pipeline/process_interface/replica_set_node_process_interface.h" -#include "mongo/db/query/ce/stats_cache_loader_impl.h" -#include "mongo/db/query/ce/stats_catalog.h" #include "mongo/db/query/internal_plans.h" +#include "mongo/db/query/stats/stats_cache_loader_impl.h" +#include "mongo/db/query/stats/stats_catalog.h" #include "mongo/db/read_write_concern_defaults_cache_lookup_mongod.h" #include "mongo/db/repl/drop_pending_collection_reaper.h" #include "mongo/db/repl/initial_syncer_factory.h" @@ -855,9 +855,9 @@ ExitCode _initAndListen(ServiceContext* serviceContext, int listenPort) { LogicalSessionCache::set(serviceContext, makeLogicalSessionCacheD(kind)); - auto cacheLoader = std::make_unique<StatsCacheLoaderImpl>(); - auto catalog = std::make_unique<StatsCatalog>(serviceContext, std::move(cacheLoader)); - StatsCatalog::set(serviceContext, std::move(catalog)); + auto cacheLoader = std::make_unique<stats::StatsCacheLoaderImpl>(); + auto catalog = std::make_unique<stats::StatsCatalog>(serviceContext, std::move(cacheLoader)); + stats::StatsCatalog::set(serviceContext, std::move(catalog)); if (analyze_shard_key::supportsPersistingSampledQueriesIgnoreFCV()) { analyze_shard_key::QueryAnalysisWriter::get(serviceContext).onStartup(); diff --git a/src/mongo/db/pipeline/SConscript b/src/mongo/db/pipeline/SConscript index 422eef81f39..b4843172e39 100644 --- a/src/mongo/db/pipeline/SConscript +++ b/src/mongo/db/pipeline/SConscript @@ -133,8 +133,8 @@ env.Library( ], LIBDEPS=[ '$BUILD_DIR/mongo/db/exec/document_value/document_value', - '$BUILD_DIR/mongo/db/query/ce/query_stats', '$BUILD_DIR/mongo/db/query/query_knobs', + '$BUILD_DIR/mongo/db/query/stats/query_stats', '$BUILD_DIR/mongo/db/query_expressions', '$BUILD_DIR/mongo/scripting/scripting_common', '$BUILD_DIR/mongo/util/summation', @@ -334,13 +334,13 @@ pipelineEnv.Library( '$BUILD_DIR/mongo/db/index/key_generator', '$BUILD_DIR/mongo/db/pipeline/change_stream_error_extra_info', '$BUILD_DIR/mongo/db/pipeline/lite_parsed_document_source', - '$BUILD_DIR/mongo/db/query/ce/query_stats_gen', '$BUILD_DIR/mongo/db/query/collation/collator_factory_interface', '$BUILD_DIR/mongo/db/query/collation/collator_interface', '$BUILD_DIR/mongo/db/query/cursor_response_idl', '$BUILD_DIR/mongo/db/query/datetime/date_time_support', '$BUILD_DIR/mongo/db/query/query_knobs', '$BUILD_DIR/mongo/db/query/sort_pattern', + '$BUILD_DIR/mongo/db/query/stats/stats_gen', '$BUILD_DIR/mongo/db/query/telemetry', '$BUILD_DIR/mongo/db/query_expressions', '$BUILD_DIR/mongo/db/repl/apply_ops_command_info', diff --git a/src/mongo/db/pipeline/accumulator.h b/src/mongo/db/pipeline/accumulator.h index df070ff6531..e99b9db3ec1 100644 --- a/src/mongo/db/pipeline/accumulator.h +++ b/src/mongo/db/pipeline/accumulator.h @@ -43,7 +43,7 @@ #include "mongo/db/exec/document_value/value_comparator.h" #include "mongo/db/pipeline/expression.h" #include "mongo/db/pipeline/expression_context.h" -#include "mongo/db/query/ce/value_utils.h" +#include "mongo/db/query/stats/value_utils.h" #include "mongo/stdx/unordered_set.h" #include "mongo/util/summation.h" @@ -250,7 +250,7 @@ public: private: double _count; - std::vector<ce::SBEValue> _values; + std::vector<stats::SBEValue> _values; }; class AccumulatorLast final : public AccumulatorState { diff --git a/src/mongo/db/pipeline/accumulator_internal_construct_stats.cpp b/src/mongo/db/pipeline/accumulator_internal_construct_stats.cpp index e5f2a5b0c70..bdcd67c6c24 100644 --- a/src/mongo/db/pipeline/accumulator_internal_construct_stats.cpp +++ b/src/mongo/db/pipeline/accumulator_internal_construct_stats.cpp @@ -35,8 +35,8 @@ #include "mongo/db/pipeline/accumulator.h" #include "mongo/db/pipeline/expression_context.h" #include "mongo/db/query/allowed_contexts.h" -#include "mongo/db/query/ce/max_diff.h" -#include "mongo/db/query/ce/value_utils.h" +#include "mongo/db/query/stats/max_diff.h" +#include "mongo/db/query/stats/value_utils.h" #include "mongo/logv2/log.h" @@ -71,7 +71,7 @@ void AccumulatorInternalConstructStats::processInternal(const Value& input, bool auto val = doc["val"]; LOGV2_DEBUG(6735800, 4, "Extracted document", "val"_attr = val); - _values.emplace_back(ce::SBEValue(mongo::optimizer::convertFrom(val))); + _values.emplace_back(stats::SBEValue(mongo::optimizer::convertFrom(val))); _count++; _memUsageBytes = sizeof(*this); @@ -81,7 +81,7 @@ Value AccumulatorInternalConstructStats::getValue(bool toBeMerged) { uassert(8423374, "Can not merge analyze pipelines", !toBeMerged); // Generate and serialize maxdiff histogram for scalar and array values. - auto arrayHistogram = ce::createArrayEstimator(_values, ce::ScalarHistogram::kMaxBuckets); + auto arrayHistogram = stats::createArrayEstimator(_values, stats::ScalarHistogram::kMaxBuckets); auto stats = stats::makeStatistics(_count, arrayHistogram); return Value(stats); diff --git a/src/mongo/db/query/SConscript b/src/mongo/db/query/SConscript index 02890313b21..1d3e11ccc13 100644 --- a/src/mongo/db/query/SConscript +++ b/src/mongo/db/query/SConscript @@ -11,6 +11,7 @@ env.SConscript( 'cost_model', 'datetime', 'optimizer', + 'stats', ], exports=[ 'env', diff --git a/src/mongo/db/query/ce/SConscript b/src/mongo/db/query/ce/SConscript index d1e33484f03..c027c82f743 100644 --- a/src/mongo/db/query/ce/SConscript +++ b/src/mongo/db/query/ce/SConscript @@ -7,7 +7,7 @@ env = env.Clone() env.Library( target="query_ce_heuristic", source=[ - 'ce_heuristic.cpp', + 'heuristic_estimator.cpp', ], LIBDEPS_PRIVATE=[ '$BUILD_DIR/mongo/db/query/optimizer/optimizer_memo', @@ -17,7 +17,7 @@ env.Library( env.Library( target="query_ce_hinted", source=[ - 'ce_hinted.cpp', + 'hinted_estimator.cpp', ], LIBDEPS_PRIVATE=[ '$BUILD_DIR/mongo/db/query/optimizer/optimizer_memo', @@ -28,62 +28,32 @@ env.Library( env.Library( target="query_ce_histogram", source=[ - 'ce_histogram.cpp', - 'collection_statistics_impl.cpp', - 'histogram_estimation.cpp', - 'stats_catalog.cpp', - 'stats_cache.cpp', - 'stats_cache_loader_impl.cpp', + 'histogram_estimator.cpp', + 'histogram_predicate_estimation.cpp', ], LIBDEPS_PRIVATE=[ - '$BUILD_DIR/mongo/db/dbdirectclient', - '$BUILD_DIR/mongo/db/pipeline/pipeline', + '$BUILD_DIR/mongo/db/pipeline/abt_utils', '$BUILD_DIR/mongo/db/query/optimizer/optimizer_memo', '$BUILD_DIR/mongo/db/query/optimizer/optimizer_rewrites', - '$BUILD_DIR/mongo/util/caching', - '$BUILD_DIR/mongo/util/concurrency/thread_pool', - 'query_stats', + '$BUILD_DIR/mongo/db/query/stats/stats_histograms', ], ) env.Library( target="query_ce_sampling", source=[ - 'ce_sampling.cpp', + 'sampling_estimator.cpp', ], LIBDEPS_PRIVATE=[ '$BUILD_DIR/mongo/db/exec/sbe/query_sbe_abt', - '$BUILD_DIR/mongo/db/query/optimizer/optimizer_memo', - ], -) - -env.Library( - target="query_stats", - source=[ - 'array_histogram.cpp', - 'scalar_histogram.cpp', - 'stats.idl', - 'value_utils.cpp', - ], - LIBDEPS=[ - '$BUILD_DIR/mongo/db/exec/sbe/query_sbe_values', - ], -) - -env.Library( - target="query_stats_gen", - source=[ - 'max_diff.cpp', - ], - LIBDEPS=[ - 'query_stats', + '$BUILD_DIR/mongo/db/query/optimizer/optimizer', ], ) env.Library( - target="ce_test_utils", + target="test_utils", source=[ - 'ce_test_utils.cpp', + 'test_utils.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/base', @@ -94,150 +64,79 @@ env.Library( 'query_ce_heuristic', 'query_ce_histogram', 'query_ce_sampling', - 'query_stats', ], ) env.CppUnitTest( - target="ce_histogram_test", + target="histogram_estimator_test", source=[ - "ce_histogram_test.cpp", - "collection_statistics_mock.cpp", + "histogram_estimator_test.cpp", ], LIBDEPS=[ - 'ce_test_utils', + '$BUILD_DIR/mongo/db/query/stats/stats_test_utils', + 'test_utils', ], ) env.CppUnitTest( - target="ce_interpolation_test", + target="histogram_interpolation_test", source=[ - "ce_interpolation_test.cpp", + "histogram_interpolation_test.cpp", ], LIBDEPS=[ - 'ce_test_utils', + 'test_utils', ], ) env.CppUnitTest( - target="ce_heuristic_test", + target="heuristic_estimator_test", source=[ - "ce_heuristic_test.cpp", + "heuristic_estimator_test.cpp", ], LIBDEPS=[ - 'ce_test_utils', + 'test_utils', ], ) env.CppUnitTest( - target="ce_array_data_test", + target="histogram_array_data_test", source=[ - "ce_array_data_test.cpp", + "histogram_array_data_test.cpp", ], LIBDEPS=[ - 'ce_test_utils', + '$BUILD_DIR/mongo/db/query/stats/stats_test_utils', + 'test_utils', ], ) env.CppUnitTest( - target="ce_edge_cases_test", + target="histogram_edge_cases_test", source=[ - "ce_edge_cases_test.cpp", + "histogram_edge_cases_test.cpp", ], LIBDEPS=[ - 'ce_test_utils', - 'query_stats_test_utils', + '$BUILD_DIR/mongo/db/query/stats/stats_test_utils', + 'test_utils', ], ) env.CppUnitTest( - target="ce_dataflow_nodes_test", + target="heuristic_dataflow_nodes_test", source=[ - "ce_dataflow_nodes_test.cpp", + "heuristic_dataflow_nodes_test.cpp", ], LIBDEPS=[ - 'ce_test_utils', + 'test_utils', ], ) env.CppUnitTest( - target='stats_cache_loader_test', + target="generated_histograms_test", source=[ - 'stats_cache_loader_test.cpp', - 'stats_cache_loader_test_fixture.cpp', + "generated_histograms_test.cpp", ], LIBDEPS=[ - '$BUILD_DIR/mongo/db/auth/authmocks', - '$BUILD_DIR/mongo/db/catalog/collection_crud', - '$BUILD_DIR/mongo/db/commands/test_commands_enabled', - '$BUILD_DIR/mongo/db/index_builds_coordinator_mongod', - '$BUILD_DIR/mongo/db/multitenancy', - '$BUILD_DIR/mongo/db/op_observer/op_observer', - '$BUILD_DIR/mongo/db/op_observer/op_observer_impl', - '$BUILD_DIR/mongo/db/query/datetime/date_time_support', - '$BUILD_DIR/mongo/db/query/query_test_service_context', - '$BUILD_DIR/mongo/db/query_expressions', - '$BUILD_DIR/mongo/db/repl/drop_pending_collection_reaper', - '$BUILD_DIR/mongo/db/repl/oplog', - '$BUILD_DIR/mongo/db/repl/optime', - '$BUILD_DIR/mongo/db/repl/repl_coordinator_interface', - '$BUILD_DIR/mongo/db/repl/replmocks', - '$BUILD_DIR/mongo/db/repl/storage_interface_impl', - '$BUILD_DIR/mongo/db/server_base', - '$BUILD_DIR/mongo/db/service_context', - '$BUILD_DIR/mongo/db/service_context_d_test_fixture', - '$BUILD_DIR/mongo/db/service_context_test_fixture', - '$BUILD_DIR/mongo/db/shard_role', - '$BUILD_DIR/mongo/db/storage/wiredtiger/storage_wiredtiger', - '$BUILD_DIR/mongo/db/timeseries/timeseries_options', - '$BUILD_DIR/mongo/unittest/unittest', - '$BUILD_DIR/mongo/util/clock_source_mock', - '$BUILD_DIR/mongo/util/fail_point', - '$BUILD_DIR/mongo/util/pcre_wrapper', - 'query_ce_histogram', - 'query_stats', - ], -) - -env.CppUnitTest( - target="stats_cache_test", - source=[ - "stats_cache_test.cpp", - "stats_cache_loader_mock.cpp", - ], - LIBDEPS=[ - '$BUILD_DIR/mongo/base', - '$BUILD_DIR/mongo/db/service_context', - 'ce_test_utils', - ], -) - -env.CppUnitTest( - target="stats_path_test", - source=[ - "stats_path_test.cpp", - ], - LIBDEPS=[ - '$BUILD_DIR/mongo/base', - '$BUILD_DIR/mongo/db/service_context', - 'ce_test_utils', - ], -) - -env.Library( - target="query_stats_test_utils", - source=[ - 'rand_utils.cpp', - 'rand_utils_new.cpp', - 'maxdiff_test_utils.cpp', - ], - LIBDEPS=[ - '$BUILD_DIR/mongo/base', - '$BUILD_DIR/mongo/db/exec/sbe/sbe_abt_test_util', - "$BUILD_DIR/mongo/unittest/unittest", - 'query_ce_histogram', - 'query_stats', - 'query_stats_gen', + 'test_utils', ], ) @@ -247,18 +146,7 @@ env.CppUnitTest( 'maxdiff_histogram_test.cpp', ], LIBDEPS=[ - 'ce_test_utils', - 'query_stats_test_utils', - ], -) - -env.CppUnitTest( - target="ce_generated_histograms_test", - source=[ - "ce_generated_histograms_test.cpp", - ], - LIBDEPS=[ - 'ce_test_utils', - 'query_stats_test_utils', + '$BUILD_DIR/mongo/db/query/stats/stats_test_utils', + 'test_utils', ], ) diff --git a/src/mongo/db/query/ce/ce_generated_histograms_test.cpp b/src/mongo/db/query/ce/generated_histograms_test.cpp index 93696346447..3f5ce361584 100644 --- a/src/mongo/db/query/ce/ce_generated_histograms_test.cpp +++ b/src/mongo/db/query/ce/generated_histograms_test.cpp @@ -31,15 +31,18 @@ #include <vector> #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/ce_test_utils.h" -#include "mongo/db/query/ce/histogram_estimation.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/ce/test_utils.h" +#include "mongo/db/query/stats/array_histogram.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { +namespace value = sbe::value; -using namespace sbe; +using stats::ArrayHistogram; +using stats::ScalarHistogram; +using stats::TypeCounts; constexpr double kErrorBound = 0.1; @@ -360,4 +363,4 @@ TEST(EstimatorTest, IntStrArrayEstimate) { ASSERT_APPROX_EQUAL(156.1, expectedCard, kErrorBound); } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_dataflow_nodes_test.cpp b/src/mongo/db/query/ce/heuristic_dataflow_nodes_test.cpp index 1f11472c811..7efe1a974ba 100644 --- a/src/mongo/db/query/ce/ce_dataflow_nodes_test.cpp +++ b/src/mongo/db/query/ce/heuristic_dataflow_nodes_test.cpp @@ -27,19 +27,15 @@ * it in the license file. */ -#include "mongo/db/query/ce/ce_heuristic.h" -#include "mongo/db/query/ce/ce_test_utils.h" +#include "mongo/db/query/ce/heuristic_estimator.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/optimizer/props.h" #include "mongo/db/query/optimizer/utils/unit_test_utils.h" #include "mongo/db/query/optimizer/utils/utils.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { - -using namespace optimizer; -using namespace optimizer::cascades; - constexpr double kCollCard = 1000.0; const std::string kCollName = "test"; @@ -54,16 +50,14 @@ public: DataflowCETester() : CETester(kCollName, kCollCard, kDefaultCETestPhaseSet) {} protected: - std::unique_ptr<CEInterface> getCETransport() const override { - return std::make_unique<HeuristicCE>(); + std::unique_ptr<cascades::CardinalityEstimator> getEstimator() const override { + return std::make_unique<HeuristicEstimator>(); } }; -namespace { bool isRootNodeFn(const ABT& node) { - return node.is<optimizer::RootNode>(); + return node.is<RootNode>(); } -} // namespace TEST(CEDataflowTest, EstimateTrivialNodes) { DataflowCETester t; @@ -224,4 +218,4 @@ TEST(CEDataflowTest, EstimateUnwindNode) { } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_heuristic.cpp b/src/mongo/db/query/ce/heuristic_estimator.cpp index dcdc6e698e4..88421015f39 100644 --- a/src/mongo/db/query/ce/ce_heuristic.cpp +++ b/src/mongo/db/query/ce/heuristic_estimator.cpp @@ -27,23 +27,13 @@ * it in the license file. */ -#include "mongo/db/query/ce/ce_heuristic.h" +#include "mongo/db/query/ce/heuristic_estimator.h" #include "mongo/db/query/optimizer/cascades/memo.h" #include "mongo/db/query/optimizer/utils/ce_math.h" #include "mongo/util/assert_util.h" -namespace mongo::ce { -namespace { -namespace cascades = optimizer::cascades; -namespace properties = optimizer::properties; - -using ABT = optimizer::ABT; -using CEType = optimizer::CEType; -using LogicalProps = properties::LogicalProps; -using Memo = cascades::Memo; -using Metadata = optimizer::Metadata; - +namespace mongo::optimizer::ce { // Invalid estimate - an arbitrary negative value used for initialization. constexpr SelectivityType kInvalidSel = -1.0; @@ -387,7 +377,7 @@ private: } }; -class CEHeuristicTransport { +class HeuristicTransport { public: CEType transport(const ScanNode& node, CEType /*bindResult*/) { // Default cardinality estimate. @@ -461,10 +451,10 @@ public: const SelectivityType sel = intervalSel(interval, childResult); conjSelectivities.push_back(sel); } - conjSel = ce::conjExponentialBackoff(std::move(conjSelectivities)); + conjSel = conjExponentialBackoff(std::move(conjSelectivities)); disjSelectivities.push_back(conjSel); } - disjSel = ce::disjExponentialBackoff(std::move(disjSelectivities)); + disjSel = disjExponentialBackoff(std::move(disjSelectivities)); topLevelSelectivities.push_back(disjSel); } @@ -472,9 +462,9 @@ public: return 1.0; } // The elements of the PartialSchemaRequirements map represent an implicit conjunction. - topLevelSel = ce::conjExponentialBackoff(std::move(topLevelSelectivities)); + topLevelSel = conjExponentialBackoff(std::move(topLevelSelectivities)); CEType card = std::max(topLevelSel * childResult, kMinCard); - uassert(6716602, "Invalid cardinality.", mongo::ce::validCardinality(card)); + uassert(6716602, "Invalid cardinality.", validCardinality(card)); return card; } @@ -585,27 +575,26 @@ public: } static CEType derive(const Metadata& metadata, - const Memo& memo, + const cascades::Memo& memo, const ABT::reference_type logicalNodeRef) { - CEHeuristicTransport instance(metadata, memo); + HeuristicTransport instance(metadata, memo); return algebra::transport<false>(logicalNodeRef, instance); } private: - CEHeuristicTransport(const Metadata& metadata, const Memo& memo) + HeuristicTransport(const Metadata& metadata, const cascades::Memo& memo) : _metadata(metadata), _memo(memo) {} // We don't own this. const Metadata& _metadata; - const Memo& _memo; + const cascades::Memo& _memo; }; -} // namespace - -CEType HeuristicCE::deriveCE(const Metadata& metadata, - const Memo& memo, - const LogicalProps& /*logicalProps*/, - const ABT::reference_type logicalNodeRef) const { - CEType card = CEHeuristicTransport::derive(metadata, memo, logicalNodeRef); - return card; + +CEType HeuristicEstimator::deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& /*logicalProps*/, + const ABT::reference_type logicalNodeRef) const { + return HeuristicTransport::derive(metadata, memo, logicalNodeRef); } -} // namespace mongo::ce + +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_heuristic.h b/src/mongo/db/query/ce/heuristic_estimator.h index fd6e0672c54..0cfef17d6c2 100644 --- a/src/mongo/db/query/ce/ce_heuristic.h +++ b/src/mongo/db/query/ce/heuristic_estimator.h @@ -31,19 +31,19 @@ #include "mongo/db/query/optimizer/cascades/interfaces.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { /** * Default cardinality estimation in the absence of statistics. * Relies purely on heuristics. * We currently do not use logical properties for heuristic ce. */ -class HeuristicCE : public optimizer::cascades::CEInterface { +class HeuristicEstimator : public cascades::CardinalityEstimator { public: - optimizer::CEType deriveCE(const optimizer::Metadata& metadata, - const optimizer::cascades::Memo& memo, - const optimizer::properties::LogicalProps& /*logicalProps*/, - optimizer::ABT::reference_type logicalNodeRef) const override final; + CEType deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& /*logicalProps*/, + ABT::reference_type logicalNodeRef) const override final; }; -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_heuristic_test.cpp b/src/mongo/db/query/ce/heuristic_estimator_test.cpp index c59361fb03c..f92f63edde9 100644 --- a/src/mongo/db/query/ce/ce_heuristic_test.cpp +++ b/src/mongo/db/query/ce/heuristic_estimator_test.cpp @@ -29,8 +29,8 @@ #include <string> -#include "mongo/db/query/ce/ce_heuristic.h" -#include "mongo/db/query/ce/ce_test_utils.h" +#include "mongo/db/query/ce/heuristic_estimator.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/optimizer/cascades/logical_props_derivation.h" #include "mongo/db/query/optimizer/cascades/memo.h" #include "mongo/db/query/optimizer/defs.h" @@ -42,25 +42,20 @@ #include "mongo/db/query/optimizer/utils/utils.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { - -using namespace optimizer; -using namespace optimizer::cascades; - constexpr double kCollCard = 10000.0; const std::string collName = "test"; class HeuristicCETester : public CETester { public: - HeuristicCETester( - std::string collName, - const optimizer::OptPhaseManager::PhaseSet& optPhases = kDefaultCETestPhaseSet) + HeuristicCETester(std::string collName, + const OptPhaseManager::PhaseSet& optPhases = kDefaultCETestPhaseSet) : CETester(collName, kCollCard, optPhases) {} protected: - std::unique_ptr<CEInterface> getCETransport() const override { - return std::make_unique<HeuristicCE>(); + std::unique_ptr<cascades::CardinalityEstimator> getEstimator() const override { + return std::make_unique<HeuristicEstimator>(); } }; @@ -252,8 +247,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationConjunctionWithIn) { } TEST(CEHeuristicTest, CEWithoutOptimizationOneLowBoundWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -261,8 +254,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationOneLowBoundWithoutTraverse) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -274,8 +267,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationOneLowBoundWithoutTraverse) { } TEST(CEHeuristicTest, CEWithoutOptimizationOneHighBoundWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -283,8 +274,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationOneHighBoundWithoutTraverse) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -296,8 +287,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationOneHighBoundWithoutTraverse) { } TEST(CEHeuristicTest, CEWithoutOptimizationTwoLowBoundsWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -308,8 +297,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationTwoLowBoundsWithoutTraverse) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -321,8 +310,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationTwoLowBoundsWithoutTraverse) { } TEST(CEHeuristicTest, CEWithoutOptimizationTwoHighBoundsWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -333,8 +320,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationTwoHighBoundsWithoutTraverse) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -346,8 +333,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationTwoHighBoundsWithoutTraverse) { } TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -358,8 +343,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWithoutTraverse) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -371,8 +356,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWithoutTraverse) { } TEST(CEHeuristicTest, CEWithoutOptimizationIntervalWithDifferentTypes) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -384,8 +367,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationIntervalWithDifferentTypes) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -397,8 +380,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationIntervalWithDifferentTypes) { } TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWithPathExpr) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -423,8 +404,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWithPathExpr) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -436,8 +417,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWithPathExpr) { } TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWith1Variable) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -462,8 +441,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWith1Variable) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -475,8 +454,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationClosedRangeWith1Variable) { } TEST(CEHeuristicTest, CEWithoutOptimizationOpenRangeWith1Variable) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -501,8 +478,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationOpenRangeWith1Variable) { make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -514,8 +491,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationOpenRangeWith1Variable) { } TEST(CEHeuristicTest, CEWithoutOptimizationConjunctionOfBoundsWithDifferentPaths) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -540,8 +515,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationConjunctionOfBoundsWithDifferentPaths make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -553,8 +528,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationConjunctionOfBoundsWithDifferentPaths } TEST(CEHeuristicTest, CEWithoutOptimizationDisjunctionOnSamePathWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -569,8 +542,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationDisjunctionOnSamePathWithoutTraverse) make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -582,8 +555,6 @@ TEST(CEHeuristicTest, CEWithoutOptimizationDisjunctionOnSamePathWithoutTraverse) } TEST(CEHeuristicTest, CEWithoutOptimizationDisjunctionOnDifferentPathsWithoutTraverse) { - using namespace properties; - ABT scanNode = make<ScanNode>("test", "test"); ABT filterNode = make<FilterNode>( @@ -598,8 +569,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationDisjunctionOnDifferentPathsWithoutTra make<Variable>("test")), std::move(scanNode)); - ABT rootNode = - make<RootNode>(ProjectionRequirement{ProjectionNameVector{"test"}}, std::move(filterNode)); + ABT rootNode = make<RootNode>(properties::ProjectionRequirement{ProjectionNameVector{"test"}}, + std::move(filterNode)); HeuristicCETester ht(collName, kNoOptPhaseSet); ASSERT_CE_CARD(ht, rootNode, 0.0, 0.0); @@ -611,10 +582,8 @@ TEST(CEHeuristicTest, CEWithoutOptimizationDisjunctionOnDifferentPathsWithoutTra } TEST(CEHeuristicTest, CEWithoutOptimizationEquivalentConjunctions) { - using namespace properties; - ABT rootNode1 = make<RootNode>( - ProjectionRequirement{ProjectionNameVector{"test"}}, + properties::ProjectionRequirement{ProjectionNameVector{"test"}}, make<FilterNode>( make<EvalFilter>( make<PathComposeM>( @@ -630,7 +599,7 @@ TEST(CEHeuristicTest, CEWithoutOptimizationEquivalentConjunctions) { make<ScanNode>("test", "test"))); ABT rootNode2 = make<RootNode>( - ProjectionRequirement{ProjectionNameVector{"test"}}, + properties::ProjectionRequirement{ProjectionNameVector{"test"}}, make<FilterNode>( make<EvalFilter>(make<PathGet>("a0", make<PathTraverse>(make<PathCompare>(Operations::Gt, @@ -1006,4 +975,4 @@ TEST(CEHeuristicTest, CEExists) { } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_hinted.cpp b/src/mongo/db/query/ce/hinted_estimator.cpp index 0ce71a69fe7..b27381268b8 100644 --- a/src/mongo/db/query/ce/ce_hinted.cpp +++ b/src/mongo/db/query/ce/hinted_estimator.cpp @@ -27,23 +27,15 @@ * it in the license file. */ -#include "mongo/db/query/ce/ce_hinted.h" -#include "mongo/db/query/ce/ce_heuristic.h" +#include "mongo/db/query/ce/hinted_estimator.h" -namespace mongo::ce { -namespace cascades = optimizer::cascades; -namespace properties = optimizer::properties; +#include "mongo/db/query/ce/heuristic_estimator.h" -using ABT = optimizer::ABT; -using CEType = optimizer::CEType; -using LogicalProps = properties::LogicalProps; -using Memo = cascades::Memo; -using Metadata = optimizer::Metadata; - -class CEHintedTransport { +namespace mongo::optimizer::ce { +class HintedTransport { public: CEType transport(const ABT& n, - const optimizer::SargableNode& node, + const SargableNode& node, CEType childResult, CEType /*bindsResult*/, CEType /*refsResult*/) { @@ -63,46 +55,46 @@ public: template <typename T, typename... Ts> CEType transport(const ABT& n, const T& /*node*/, Ts&&...) { - if (optimizer::canBeLogicalNode<T>()) { + if (canBeLogicalNode<T>()) { return _heuristicCE.deriveCE(_metadata, _memo, _logicalProps, n.ref()); } return 0.0; } static CEType derive(const Metadata& metadata, - const Memo& memo, + const cascades::Memo& memo, const PartialSchemaSelHints& hints, - const LogicalProps& logicalProps, + const properties::LogicalProps& logicalProps, const ABT::reference_type logicalNodeRef) { - CEHintedTransport instance(metadata, memo, logicalProps, hints); - return optimizer::algebra::transport<true>(logicalNodeRef, instance); + HintedTransport instance(metadata, memo, logicalProps, hints); + return algebra::transport<true>(logicalNodeRef, instance); } private: - CEHintedTransport(const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, - const PartialSchemaSelHints& hints) + HintedTransport(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + const PartialSchemaSelHints& hints) : _heuristicCE(), _metadata(metadata), _memo(memo), _logicalProps(logicalProps), _hints(hints) {} - HeuristicCE _heuristicCE; + HeuristicEstimator _heuristicCE; // We don't own this. const Metadata& _metadata; - const Memo& _memo; - const LogicalProps& _logicalProps; + const cascades::Memo& _memo; + const properties::LogicalProps& _logicalProps; const PartialSchemaSelHints& _hints; }; -CEType HintedCE::deriveCE(const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, - const ABT::reference_type logicalNodeRef) const { - return CEHintedTransport::derive(metadata, memo, _hints, logicalProps, logicalNodeRef); +CEType HintedEstimator::deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + const ABT::reference_type logicalNodeRef) const { + return HintedTransport::derive(metadata, memo, _hints, logicalProps, logicalNodeRef); } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_hinted.h b/src/mongo/db/query/ce/hinted_estimator.h index eacadc0ccfb..766a1a1f03c 100644 --- a/src/mongo/db/query/ce/ce_hinted.h +++ b/src/mongo/db/query/ce/hinted_estimator.h @@ -31,28 +31,27 @@ #include "mongo/db/query/optimizer/cascades/interfaces.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { -using PartialSchemaSelHints = std::map<optimizer::PartialSchemaKey, - optimizer::SelectivityType, - optimizer::PartialSchemaKeyLessComparator>; +using PartialSchemaSelHints = + std::map<PartialSchemaKey, SelectivityType, PartialSchemaKeyLessComparator>; /** * Estimation based on hints. The hints are organized in a PartialSchemaSelHints structure. * SargableNodes are estimated based on the matching PartialSchemaKeys. */ -class HintedCE : public optimizer::cascades::CEInterface { +class HintedEstimator : public cascades::CardinalityEstimator { public: - HintedCE(PartialSchemaSelHints hints) : _hints(std::move(hints)) {} + HintedEstimator(PartialSchemaSelHints hints) : _hints(std::move(hints)) {} - optimizer::CEType deriveCE(const optimizer::Metadata& metadata, - const optimizer::cascades::Memo& memo, - const optimizer::properties::LogicalProps& logicalProps, - optimizer::ABT::reference_type logicalNodeRef) const override final; + CEType deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + ABT::reference_type logicalNodeRef) const override final; private: // Selectivity hints per PartialSchemaKey. PartialSchemaSelHints _hints; }; -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_array_data_test.cpp b/src/mongo/db/query/ce/histogram_array_data_test.cpp index 587ab4b7364..7f8bb92fc51 100644 --- a/src/mongo/db/query/ce/ce_array_data_test.cpp +++ b/src/mongo/db/query/ce/histogram_array_data_test.cpp @@ -30,16 +30,19 @@ #include <vector> #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/ce_test_utils.h" -#include "mongo/db/query/ce/histogram_estimation.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/query_test_service_context.h" +#include "mongo/db/query/stats/array_histogram.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { +namespace value = sbe::value; -using namespace sbe; +using stats::ArrayHistogram; +using stats::ScalarHistogram; +using stats::TypeCounts; /** * Structure representing a range query and its estimated and actual cardinalities. @@ -292,4 +295,4 @@ TEST(EstimatorArrayDataTest, Histogram1000ArraysLarge10Buckets) { std::cout << computeRMSE(querySet, true /* isElemMatch */) << std::endl; } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_edge_cases_test.cpp b/src/mongo/db/query/ce/histogram_edge_cases_test.cpp index 4d8d84f831a..051d3134dcc 100644 --- a/src/mongo/db/query/ce/ce_edge_cases_test.cpp +++ b/src/mongo/db/query/ce/histogram_edge_cases_test.cpp @@ -28,19 +28,24 @@ */ #include "mongo/db/pipeline/abt/utils.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/ce_test_utils.h" -#include "mongo/db/query/ce/histogram_estimation.h" -#include "mongo/db/query/ce/maxdiff_test_utils.h" -#include "mongo/db/query/ce/value_utils.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/optimizer/utils/ce_math.h" #include "mongo/db/query/sbe_stage_builder_helpers.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/maxdiff_test_utils.h" +#include "mongo/db/query/stats/value_utils.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { +namespace value = sbe::value; -using namespace sbe; +using stats::ArrayHistogram; +using stats::makeInt64Value; +using stats::SBEValue; +using stats::ScalarHistogram; +using stats::TypeCounts; constexpr double kErrorBound = 0.01; @@ -999,4 +1004,4 @@ TEST(EstimatorTest, MinValueMixedHistogramFromBuckets) { ASSERT_APPROX_EQUAL(47.5, expectedCard, kErrorBound); } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_histogram.cpp b/src/mongo/db/query/ce/histogram_estimator.cpp index c456d9227b6..6978ad4a307 100644 --- a/src/mongo/db/query/ce/ce_histogram.cpp +++ b/src/mongo/db/query/ce/histogram_estimator.cpp @@ -27,50 +27,34 @@ * it in the license file. */ -#include "mongo/db/exec/sbe/abt/abt_lower.h" - -#include "mongo/db/query/ce/ce_histogram.h" -#include "mongo/db/query/ce/collection_statistics_impl.h" -#include "mongo/db/query/ce/histogram_estimation.h" +#include "mongo/db/query/ce/histogram_estimator.h" +#include "mongo/db/pipeline/abt/utils.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" #include "mongo/db/query/optimizer/utils/abt_hash.h" #include "mongo/db/query/optimizer/utils/ce_math.h" #include "mongo/db/query/optimizer/utils/memo_utils.h" -#include "mongo/db/pipeline/abt/utils.h" - -namespace mongo::ce { -namespace cascades = optimizer::cascades; -namespace properties = optimizer::properties; - -using ABT = optimizer::ABT; -using CEType = optimizer::CEType; -using LogicalProps = properties::LogicalProps; -using Memo = cascades::Memo; -using Metadata = optimizer::Metadata; - +namespace mongo::optimizer::ce { namespace { - /** * This transport combines chains of PathGets and PathTraverses into an MQL-like string path. */ class PathDescribeTransport { public: - std::string transport(const optimizer::PathTraverse& /*node*/, std::string childResult) { + std::string transport(const PathTraverse& /*node*/, std::string childResult) { return childResult; } - std::string transport(const optimizer::PathGet& node, std::string childResult) { + std::string transport(const PathGet& node, std::string childResult) { return str::stream() << node.name() << (childResult.length() > 0 ? "." : "") << childResult; } - std::string transport(const optimizer::EvalFilter& node, - std::string pathResult, - std::string inputResult) { + std::string transport(const EvalFilter& node, std::string pathResult, std::string inputResult) { return pathResult; } - std::string transport(const optimizer::PathIdentity& node) { + std::string transport(const PathIdentity& node) { return ""; } @@ -82,26 +66,24 @@ public: std::string serializePath(const ABT& path) { PathDescribeTransport pdt; - auto str = optimizer::algebra::transport<false>(path, pdt); + auto str = algebra::transport<false>(path, pdt); return str; } } // namespace -class CEHistogramTransportImpl { +class HistogramTransport { public: - CEHistogramTransportImpl(std::shared_ptr<CollectionStatistics> stats, - std::unique_ptr<cascades::CEInterface> fallbackCE) + HistogramTransport(std::shared_ptr<stats::CollectionStatistics> stats, + std::unique_ptr<cascades::CardinalityEstimator> fallbackCE) : _stats(stats), _fallbackCE(std::move(fallbackCE)), _arrayOnlyInterval(*defaultConvertPathToInterval(make<PathArr>())) {} - ~CEHistogramTransportImpl() {} - CEType transport(const ABT& n, - const optimizer::ScanNode& node, - const Memo& memo, - const LogicalProps& logicalProps, + const ScanNode& node, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, CEType /*bindResult*/) { return _stats->getCardinality(); } @@ -114,15 +96,15 @@ public: */ struct SargableConjunct { bool includeScalar; - const ce::ArrayHistogram& histogram; + const stats::ArrayHistogram& histogram; std::vector<std::reference_wrapper<const IntervalReqExpr::Node>> intervals; }; CEType transport(const ABT& n, const SargableNode& node, const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, CEType childResult, CEType /*bindsResult*/, CEType /*refsResult*/) { @@ -240,8 +222,8 @@ public: CEType transport(const ABT& n, const RootNode& node, const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, CEType childResult, CEType /*refsResult*/) { // Root node does not change cardinality. @@ -255,8 +237,8 @@ public: CEType transport(const ABT& n, const T& /*node*/, const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, Ts&&...) { if (canBeLogicalNode<T>()) { return _fallbackCE->deriveCE(metadata, memo, logicalProps, n.ref()); @@ -265,25 +247,26 @@ public: } private: - std::shared_ptr<CollectionStatistics> _stats; - std::unique_ptr<cascades::CEInterface> _fallbackCE; + std::shared_ptr<stats::CollectionStatistics> _stats; + std::unique_ptr<cascades::CardinalityEstimator> _fallbackCE; // This is a special interval indicating that we expect to use $elemMatch semantics when // estimating the current path. const IntervalReqExpr::Node _arrayOnlyInterval; }; -CEHistogramTransport::CEHistogramTransport(std::shared_ptr<CollectionStatistics> stats, - std::unique_ptr<cascades::CEInterface> fallbackCE) - : _impl(std::make_unique<CEHistogramTransportImpl>(stats, std::move(fallbackCE))) {} +HistogramEstimator::HistogramEstimator(std::shared_ptr<stats::CollectionStatistics> stats, + std::unique_ptr<cascades::CardinalityEstimator> fallbackCE) + : _transport(std::make_unique<HistogramTransport>(stats, std::move(fallbackCE))) {} -CEHistogramTransport::~CEHistogramTransport() {} +HistogramEstimator::~HistogramEstimator() {} -CEType CEHistogramTransport::deriveCE(const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, - const ABT::reference_type logicalNodeRef) const { - return algebra::transport<true>(logicalNodeRef, *this->_impl, metadata, memo, logicalProps); +CEType HistogramEstimator::deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + const ABT::reference_type logicalNodeRef) const { + return algebra::transport<true>( + logicalNodeRef, *this->_transport, metadata, memo, logicalProps); } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_histogram.h b/src/mongo/db/query/ce/histogram_estimator.h index 1823bc211a5..ebcf008bdd3 100644 --- a/src/mongo/db/query/ce/ce_histogram.h +++ b/src/mongo/db/query/ce/histogram_estimator.h @@ -29,26 +29,26 @@ #pragma once -#include "mongo/db/query/ce/collection_statistics_impl.h" #include "mongo/db/query/optimizer/cascades/interfaces.h" +#include "mongo/db/query/stats/collection_statistics.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { -class CEHistogramTransportImpl; +class HistogramTransport; -class CEHistogramTransport : public optimizer::cascades::CEInterface { +class HistogramEstimator : public cascades::CardinalityEstimator { public: - CEHistogramTransport(std::shared_ptr<CollectionStatistics> stats, - std::unique_ptr<optimizer::cascades::CEInterface> fallbackCE); - ~CEHistogramTransport(); + HistogramEstimator(std::shared_ptr<stats::CollectionStatistics> stats, + std::unique_ptr<cascades::CardinalityEstimator> fallbackCE); + ~HistogramEstimator(); - optimizer::CEType deriveCE(const optimizer::Metadata& metadata, - const optimizer::cascades::Memo& memo, - const optimizer::properties::LogicalProps& logicalProps, - optimizer::ABT::reference_type logicalNodeRef) const final; + CEType deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + ABT::reference_type logicalNodeRef) const final; private: - std::unique_ptr<CEHistogramTransportImpl> _impl; + std::unique_ptr<HistogramTransport> _transport; }; -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_histogram_test.cpp b/src/mongo/db/query/ce/histogram_estimator_test.cpp index 3267ce4d89f..bdc7d95dea4 100644 --- a/src/mongo/db/query/ce/ce_histogram_test.cpp +++ b/src/mongo/db/query/ce/histogram_estimator_test.cpp @@ -27,19 +27,24 @@ * it in the license file. */ -#include "mongo/db/query/ce/ce_histogram.h" -#include "mongo/db/query/ce/ce_test_utils.h" -#include "mongo/db/query/ce/collection_statistics_mock.h" -#include "mongo/db/query/ce/histogram_estimation.h" +#include "mongo/db/query/ce/histogram_estimator.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/optimizer/utils/unit_test_utils.h" #include "mongo/db/query/sbe_stage_builder_helpers.h" +#include "mongo/db/query/stats/collection_statistics_mock.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { +namespace value = sbe::value; -using namespace optimizer; -using namespace cascades; +using stats::ArrayHistogram; +using stats::Bucket; +using stats::CollectionStatistics; +using stats::CollectionStatisticsMock; +using stats::ScalarHistogram; +using stats::TypeCounts; std::string collName("test"); @@ -53,9 +58,9 @@ public: } protected: - std::unique_ptr<CEInterface> getCETransport() const override { + std::unique_ptr<cascades::CardinalityEstimator> getEstimator() const override { // making a copy of CollecitonStatistics to override - return std::make_unique<CEHistogramTransport>(_stats, makeHeuristicCE()); + return std::make_unique<HistogramEstimator>(_stats, makeHeuristicCE()); } private: @@ -1153,4 +1158,4 @@ TEST(CEHistogramTest, TestFallbackForNonConstIntervals) { ASSERT_EQ(estInterval(intervalEqNonConst), -1.0); } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_interpolation_test.cpp b/src/mongo/db/query/ce/histogram_interpolation_test.cpp index 6d9d52b347d..4ad9d38b4e0 100644 --- a/src/mongo/db/query/ce/ce_interpolation_test.cpp +++ b/src/mongo/db/query/ce/histogram_interpolation_test.cpp @@ -27,16 +27,19 @@ * it in the license file. */ -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/ce_test_utils.h" -#include "mongo/db/query/ce/histogram_estimation.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/sbe_stage_builder_helpers.h" +#include "mongo/db/query/stats/array_histogram.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { namespace { +namespace value = sbe::value; -using namespace sbe; +using stats::ArrayHistogram; +using stats::ScalarHistogram; +using stats::TypeCounts; TEST(EstimatorTest, ManualHistogram) { std::vector<BucketData> data{{0, 1.0, 1.0, 1.0}, @@ -502,4 +505,4 @@ TEST(EstimatorTest, UniformIntMixedArrayEstimate) { } } // namespace -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/histogram_estimation.cpp b/src/mongo/db/query/ce/histogram_predicate_estimation.cpp index cd1e52219c2..25d1658807d 100644 --- a/src/mongo/db/query/ce/histogram_estimation.cpp +++ b/src/mongo/db/query/ce/histogram_predicate_estimation.cpp @@ -27,18 +27,26 @@ * it in the license file. */ -#include "mongo/db/query/ce/histogram_estimation.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" + #include "mongo/db/exec/sbe/abt/abt_lower.h" #include "mongo/db/pipeline/abt/utils.h" -#include "mongo/db/query/ce/value_utils.h" + #include "mongo/db/query/optimizer/rewrites/const_eval.h" #include "mongo/db/query/optimizer/syntax/expr.h" #include "mongo/db/query/optimizer/utils/ce_math.h" #include "mongo/db/query/optimizer/utils/interval_utils.h" +#include "mongo/db/query/stats/value_utils.h" + +namespace mongo::optimizer::ce { +namespace value = sbe::value; -namespace mongo::ce { -using namespace sbe; -using namespace optimizer; +using stats::ArrayHistogram; +using stats::Bucket; +using stats::compareValues; +using stats::sameTypeBracket; +using stats::ScalarHistogram; +using stats::valueToDouble; std::pair<value::TypeTags, value::Value> getConstTypeVal(const ABT& abt) { const auto* constant = abt.cast<Constant>(); @@ -416,7 +424,7 @@ double estimateCardRange(const ArrayHistogram& ah, return result; } -double estimateIntervalCardinality(const ce::ArrayHistogram& ah, +double estimateIntervalCardinality(const ArrayHistogram& ah, const IntervalRequirement& interval, CEType childResult, bool includeScalar) { @@ -429,7 +437,7 @@ double estimateIntervalCardinality(const ce::ArrayHistogram& ah, } auto [tag, val] = *maybeConstBound; - if (canEstimateTypeViaHistogram(tag)) { + if (stats::canEstimateTypeViaHistogram(tag)) { return estimateCardEq(ah, tag, val, includeScalar); } @@ -456,7 +464,7 @@ double estimateIntervalCardinality(const ce::ArrayHistogram& ah, // Check if we estimated this interval using histograms. One of the tags may not be of a type we // know how to estimate using histograms; however, it should still be possible to estimate the // interval if the other one is of the appropriate type. - if (canEstimateTypeViaHistogram(lowTag) || canEstimateTypeViaHistogram(highTag)) { + if (stats::canEstimateTypeViaHistogram(lowTag) || stats::canEstimateTypeViaHistogram(highTag)) { return estimateCardRange(ah, lowBound.isInclusive(), lowTag, @@ -485,4 +493,4 @@ double estimateIntervalCardinality(const ce::ArrayHistogram& ah, return 0.0; } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/histogram_estimation.h b/src/mongo/db/query/ce/histogram_predicate_estimation.h index f0291b42cd8..763f6c13a5e 100644 --- a/src/mongo/db/query/ce/histogram_estimation.h +++ b/src/mongo/db/query/ce/histogram_predicate_estimation.h @@ -29,11 +29,11 @@ #pragma once -#include "mongo/db/query/ce/array_histogram.h" #include "mongo/db/query/optimizer/defs.h" #include "mongo/db/query/optimizer/index_bounds.h" +#include "mongo/db/query/stats/array_histogram.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { constexpr double kInvalidEstimate = -1.0; @@ -59,13 +59,13 @@ struct EstimationResult { /** * Returns cumulative total statistics for a histogram. */ -EstimationResult getTotals(const ScalarHistogram& h); +EstimationResult getTotals(const stats::ScalarHistogram& h); /** * Compute an estimate for a given value and estimation type. Use linear interpolation for values * that fall inside of histogram buckets. */ -EstimationResult estimate(const ScalarHistogram& h, +EstimationResult estimate(const stats::ScalarHistogram& h, sbe::value::TypeTags tag, sbe::value::Value val, EstimationType type); @@ -74,16 +74,16 @@ EstimationResult estimate(const ScalarHistogram& h, * Given an array histogram, an interval, and the input cardinality, estimates the cardinality of * the interval. */ -double estimateIntervalCardinality(const ArrayHistogram& estimator, - const optimizer::IntervalRequirement& interval, - optimizer::CEType inputCardinality, +double estimateIntervalCardinality(const stats::ArrayHistogram& estimator, + const IntervalRequirement& interval, + CEType inputCardinality, bool includeScalar); /** * Estimates the cardinality of an equality predicate given an ArrayHistogram and an SBE value and * type tag pair. */ -double estimateCardEq(const ArrayHistogram& ah, +double estimateCardEq(const stats::ArrayHistogram& ah, sbe::value::TypeTags tag, sbe::value::Value val, bool includeScalar); @@ -93,7 +93,7 @@ double estimateCardEq(const ArrayHistogram& ah, * Set 'includeScalar' to true to indicate whether or not the provided range should include no-array * values. The other fields define the range of the estimation. */ -double estimateCardRange(const ArrayHistogram& ah, +double estimateCardRange(const stats::ArrayHistogram& ah, bool lowInclusive, sbe::value::TypeTags tagLow, sbe::value::Value valLow, @@ -103,4 +103,4 @@ double estimateCardRange(const ArrayHistogram& ah, bool includeScalar, EstimationAlgo estAlgo = EstimationAlgo::HistogramV2); -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/maxdiff_histogram_test.cpp b/src/mongo/db/query/ce/maxdiff_histogram_test.cpp index 2f1e2185f8d..80364fea0bb 100644 --- a/src/mongo/db/query/ce/maxdiff_histogram_test.cpp +++ b/src/mongo/db/query/ce/maxdiff_histogram_test.cpp @@ -27,30 +27,36 @@ * it in the license file. */ -#include <string> -#include <vector> - #include "mongo/db/concurrency/lock_state.h" #include "mongo/db/exec/sbe/abt/sbe_abt_test_util.h" #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/ce_test_utils.h" -#include "mongo/db/query/ce/histogram_estimation.h" -#include "mongo/db/query/ce/max_diff.h" -#include "mongo/db/query/ce/maxdiff_test_utils.h" -#include "mongo/db/query/ce/rand_utils.h" -#include "mongo/db/query/ce/rand_utils_new.h" -#include "mongo/db/query/ce/scalar_histogram.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/query/optimizer/utils/unit_test_utils.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/max_diff.h" +#include "mongo/db/query/stats/maxdiff_test_utils.h" +#include "mongo/db/query/stats/rand_utils.h" +#include "mongo/db/query/stats/rand_utils_new.h" +#include "mongo/db/query/stats/scalar_histogram.h" #include "mongo/logv2/log_component.h" #include "mongo/logv2/log_component_settings.h" #include "mongo/logv2/log_severity.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce::statistics { +namespace mongo::optimizer::ce { namespace { +namespace value = sbe::value; + +using stats::ArrayHistogram; +using stats::Bucket; +using stats::DataDistribution; +using stats::genFixedValueArray; +using stats::getDataDistribution; +using stats::makeHistogram; +using stats::makeInt64Value; +using stats::ScalarHistogram; -using namespace sbe; const double kTolerance = 0.001; class HistogramTest : public ServiceContextTest {}; @@ -266,4 +272,4 @@ TEST_F(HistogramTest, MaxDiffEmptyArrays) { } } // namespace -} // namespace mongo::ce::statistics +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_sampling.cpp b/src/mongo/db/query/ce/sampling_estimator.cpp index ce31ae842e2..85fac93b0a2 100644 --- a/src/mongo/db/query/ce/ce_sampling.cpp +++ b/src/mongo/db/query/ce/sampling_estimator.cpp @@ -27,7 +27,7 @@ * it in the license file. */ -#include "mongo/db/query/ce/ce_sampling.h" +#include "mongo/db/query/ce/sampling_estimator.h" #include "mongo/db/exec/sbe/abt/abt_lower.h" #include "mongo/db/query/cqf_command_utils.h" @@ -40,62 +40,45 @@ #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery -namespace mongo::ce { -namespace cascades = optimizer::cascades; -namespace properties = optimizer::properties; - -using ABT = optimizer::ABT; -using CEType = optimizer::CEType; -using LogicalProps = properties::LogicalProps; -using OptPhaseManager = optimizer::OptPhaseManager; -using Memo = cascades::Memo; -using Metadata = optimizer::Metadata; - +namespace mongo::optimizer::ce { class SamplingPlanExtractor { public: - SamplingPlanExtractor(const Memo& memo, + SamplingPlanExtractor(const cascades::Memo& memo, const OptPhaseManager& phaseManager, const size_t sampleSize) : _memo(memo), _sampleSize(sampleSize), _phaseManager(phaseManager) {} - void transport(ABT& n, const optimizer::MemoLogicalDelegatorNode& node) { + void transport(ABT& n, const MemoLogicalDelegatorNode& node) { n = extract(_memo.getLogicalNodes(node.getGroupId()).front()); } - void transport(ABT& n, const optimizer::ScanNode& /*node*/, ABT& /*binder*/) { + void transport(ABT& n, const ScanNode& /*node*/, ABT& /*binder*/) { // We will lower the scan node in a sampling context here. // TODO: for now just return the documents in random order. - n = optimizer::make<optimizer::LimitSkipNode>( - properties::LimitSkipRequirement(_sampleSize, 0), std::move(n)); + n = make<LimitSkipNode>(properties::LimitSkipRequirement(_sampleSize, 0), std::move(n)); } - void transport(ABT& n, - const optimizer::FilterNode& /*node*/, - ABT& childResult, - ABT& /*exprResult*/) { + void transport(ABT& n, const FilterNode& /*node*/, ABT& childResult, ABT& /*exprResult*/) { // Skip over filters. n = childResult; } void transport(ABT& /*n*/, - const optimizer::EvaluationNode& /*node*/, + const EvaluationNode& /*node*/, ABT& /*childResult*/, ABT& /*exprResult*/) { // Keep Eval nodes. } - void transport( - ABT& n, const optimizer::SargableNode& node, ABT& childResult, ABT& refs, ABT& binds) { + void transport(ABT& n, const SargableNode& node, ABT& childResult, ABT& refs, ABT& binds) { ABT result = childResult; // Retain only output bindings without applying filters. for (const auto& [key, req] : node.getReqMap()) { if (const auto& boundProjName = req.getBoundProjectionName()) { - optimizer::lowerPartialSchemaRequirement( + lowerPartialSchemaRequirement( key, - optimizer::PartialSchemaRequirement{ - boundProjName, - optimizer::IntervalReqExpr::makeSingularDNF(), - req.getIsPerfOnly()}, + PartialSchemaRequirement{ + boundProjName, IntervalReqExpr::makeSingularDNF(), req.getIsPerfOnly()}, result, _phaseManager.getPathToInterval()); } @@ -103,47 +86,47 @@ public: std::swap(n, result); } - void transport(ABT& n, const optimizer::CollationNode& /*node*/, ABT& childResult, ABT& refs) { + void transport(ABT& n, const CollationNode& /*node*/, ABT& childResult, ABT& refs) { // Skip over collation nodes. n = childResult; } template <typename T, typename... Ts> void transport(ABT& /*n*/, const T& /*node*/, Ts&&...) { - if constexpr (std::is_base_of_v<optimizer::Node, T>) { + if constexpr (std::is_base_of_v<Node, T>) { uasserted(6624242, "Should not be seeing other types of nodes here."); } } ABT extract(ABT node) { - optimizer::algebra::transport<true>(node, *this); + algebra::transport<true>(node, *this); return node; } private: - const Memo& _memo; + const cascades::Memo& _memo; const size_t _sampleSize; const OptPhaseManager& _phaseManager; }; -class CESamplingTransportImpl { +class SamplingTransport { static constexpr size_t kMaxSampleSize = 1000; public: - CESamplingTransportImpl(OperationContext* opCtx, - OptPhaseManager phaseManager, - const int64_t numRecords, - std::unique_ptr<cascades::CEInterface> fallbackCE) + SamplingTransport(OperationContext* opCtx, + OptPhaseManager phaseManager, + const int64_t numRecords, + std::unique_ptr<cascades::CardinalityEstimator> fallbackCE) : _phaseManager(std::move(phaseManager)), _opCtx(opCtx), _sampleSize(std::min<int64_t>(numRecords, kMaxSampleSize)), _fallbackCE(std::move(fallbackCE)) {} CEType transport(const ABT& n, - const optimizer::FilterNode& node, + const FilterNode& node, const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, CEType childResult, CEType /*exprResult*/) { if (!properties::hasProperty<properties::IndexingAvailability>(logicalProps)) { @@ -152,17 +135,16 @@ public: SamplingPlanExtractor planExtractor(memo, _phaseManager, _sampleSize); // Create a plan with all eval nodes so far and the filter last. - ABT abtTree = - optimizer::make<optimizer::FilterNode>(node.getFilter(), planExtractor.extract(n)); + ABT abtTree = make<FilterNode>(node.getFilter(), planExtractor.extract(n)); return estimateFilterCE(metadata, memo, logicalProps, n, std::move(abtTree), childResult); } CEType transport(const ABT& n, - const optimizer::SargableNode& node, + const SargableNode& node, const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, CEType childResult, CEType /*bindResult*/, CEType /*refsResult*/) { @@ -188,12 +170,12 @@ public: // Lower requirement without an output binding. lowerPartialSchemaRequirement( key, - optimizer::PartialSchemaRequirement{boost::none /*boundProjectionName*/, - req.getIntervals(), - req.getIsPerfOnly()}, + PartialSchemaRequirement{boost::none /*boundProjectionName*/, + req.getIntervals(), + req.getIsPerfOnly()}, lowered, _phaseManager.getPathToInterval()); - uassert(6624243, "Expected a filter node", lowered.is<optimizer::FilterNode>()); + uassert(6624243, "Expected a filter node", lowered.is<FilterNode>()); result = estimateFilterCE(metadata, memo, logicalProps, n, std::move(lowered), result); } @@ -209,27 +191,26 @@ public: CEType transport(const ABT& n, const T& /*node*/, const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, Ts&&...) { - if (optimizer::canBeLogicalNode<T>()) { + if (canBeLogicalNode<T>()) { return _fallbackCE->deriveCE(metadata, memo, logicalProps, n.ref()); } return 0.0; } CEType derive(const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, const ABT::reference_type logicalNodeRef) { - return optimizer::algebra::transport<true>( - logicalNodeRef, *this, metadata, memo, logicalProps); + return algebra::transport<true>(logicalNodeRef, *this, metadata, memo, logicalProps); } private: CEType estimateFilterCE(const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, const ABT& n, ABT abtTree, CEType childResult) { @@ -253,39 +234,38 @@ private: return selectivity * childResult; } - std::pair<bool, optimizer::SelectivityType> estimateSelectivity(ABT abtTree) { + std::pair<bool, SelectivityType> estimateSelectivity(ABT abtTree) { // Add a group by to count number of documents. - const optimizer::ProjectionName sampleSumProjection = "sum"; - abtTree = optimizer::make<optimizer::GroupByNode>( - optimizer::ProjectionNameVector{}, - optimizer::ProjectionNameVector{sampleSumProjection}, - optimizer::makeSeq(optimizer::make<optimizer::FunctionCall>( - "$sum", makeSeq(optimizer::Constant::int64(1)))), - std::move(abtTree)); - abtTree = optimizer::make<optimizer::RootNode>( - properties::ProjectionRequirement{optimizer::ProjectionNameVector{sampleSumProjection}}, + const ProjectionName sampleSumProjection = "sum"; + abtTree = + make<GroupByNode>(ProjectionNameVector{}, + ProjectionNameVector{sampleSumProjection}, + makeSeq(make<FunctionCall>("$sum", makeSeq(Constant::int64(1)))), + std::move(abtTree)); + abtTree = make<RootNode>( + properties::ProjectionRequirement{ProjectionNameVector{sampleSumProjection}}, std::move(abtTree)); OPTIMIZER_DEBUG_LOG(6264806, 5, "Estimate selectivity ABT", - "explain"_attr = optimizer::ExplainGenerator::explainV2(abtTree)); + "explain"_attr = ExplainGenerator::explainV2(abtTree)); _phaseManager.optimize(abtTree); - auto env = optimizer::VariableEnvironment::build(abtTree); - optimizer::SlotVarMap slotMap; + auto env = VariableEnvironment::build(abtTree); + SlotVarMap slotMap; boost::optional<sbe::value::SlotId> ridSlot; sbe::value::SlotIdGenerator ids; - optimizer::SBENodeLowering g{env, - slotMap, - ridSlot, - ids, - _phaseManager.getMetadata(), - _phaseManager.getNodeToGroupPropsMap(), - _phaseManager.getRIDProjections(), - true /*randomScan*/}; + SBENodeLowering g{env, + slotMap, + ridSlot, + ids, + _phaseManager.getMetadata(), + _phaseManager.getNodeToGroupPropsMap(), + _phaseManager.getRIDProjections(), + true /*randomScan*/}; auto sbePlan = g.optimize(abtTree); tassert(6624261, "Unexpected rid slot", !ridSlot); @@ -320,7 +300,7 @@ private: struct NodeRefHash { size_t operator()(const ABT& node) const { - return optimizer::ABTHashGenerator::generate(node); + return ABTHashGenerator::generate(node); } }; @@ -331,8 +311,7 @@ private: }; // Cache a logical node reference to computed selectivity. Used for Filter and Sargable nodes. - optimizer::opt::unordered_map<ABT, optimizer::SelectivityType, NodeRefHash, NodeRefCompare> - _selectivityCacheMap; + opt::unordered_map<ABT, SelectivityType, NodeRefHash, NodeRefCompare> _selectivityCacheMap; OptPhaseManager _phaseManager; @@ -340,23 +319,23 @@ private: OperationContext* _opCtx; const int64_t _sampleSize; - std::unique_ptr<cascades::CEInterface> _fallbackCE; + std::unique_ptr<cascades::CardinalityEstimator> _fallbackCE; }; -CESamplingTransport::CESamplingTransport(OperationContext* opCtx, - OptPhaseManager phaseManager, - const int64_t numRecords, - std::unique_ptr<cascades::CEInterface> fallbackCE) - : _impl(std::make_unique<CESamplingTransportImpl>( +SamplingEstimator::SamplingEstimator(OperationContext* opCtx, + OptPhaseManager phaseManager, + const int64_t numRecords, + std::unique_ptr<cascades::CardinalityEstimator> fallbackCE) + : _transport(std::make_unique<SamplingTransport>( opCtx, std::move(phaseManager), numRecords, std::move(fallbackCE))) {} -CESamplingTransport::~CESamplingTransport() {} +SamplingEstimator::~SamplingEstimator() {} -CEType CESamplingTransport::deriveCE(const Metadata& metadata, - const Memo& memo, - const LogicalProps& logicalProps, - const ABT::reference_type logicalNodeRef) const { - return _impl->derive(metadata, memo, logicalProps, logicalNodeRef); +CEType SamplingEstimator::deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + const ABT::reference_type logicalNodeRef) const { + return _transport->derive(metadata, memo, logicalProps, logicalNodeRef); } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_sampling.h b/src/mongo/db/query/ce/sampling_estimator.h index 9e13abb5d13..cf9d0973a39 100644 --- a/src/mongo/db/query/ce/ce_sampling.h +++ b/src/mongo/db/query/ce/sampling_estimator.h @@ -32,25 +32,25 @@ #include "mongo/db/query/optimizer/cascades/interfaces.h" #include "mongo/db/query/optimizer/opt_phase_manager.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { -class CESamplingTransportImpl; +class SamplingTransport; -class CESamplingTransport : public optimizer::cascades::CEInterface { +class SamplingEstimator : public cascades::CardinalityEstimator { public: - CESamplingTransport(OperationContext* opCtx, - optimizer::OptPhaseManager phaseManager, - int64_t numRecords, - std::unique_ptr<optimizer::cascades::CEInterface> fallbackCE); - ~CESamplingTransport(); + SamplingEstimator(OperationContext* opCtx, + OptPhaseManager phaseManager, + int64_t numRecords, + std::unique_ptr<cascades::CardinalityEstimator> fallbackCE); + ~SamplingEstimator(); - optimizer::CEType deriveCE(const optimizer::Metadata& metadata, - const optimizer::cascades::Memo& memo, - const optimizer::properties::LogicalProps& logicalProps, - optimizer::ABT::reference_type logicalNodeRef) const final; + CEType deriveCE(const Metadata& metadata, + const cascades::Memo& memo, + const properties::LogicalProps& logicalProps, + ABT::reference_type logicalNodeRef) const final; private: - std::unique_ptr<CESamplingTransportImpl> _impl; + std::unique_ptr<SamplingTransport> _transport; }; -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_test_utils.cpp b/src/mongo/db/query/ce/test_utils.cpp index 5212c48ab00..55bf1645f12 100644 --- a/src/mongo/db/query/ce/ce_test_utils.cpp +++ b/src/mongo/db/query/ce/test_utils.cpp @@ -27,9 +27,7 @@ * it in the license file. */ -#include <cstddef> - -#include "mongo/db/query/ce/ce_test_utils.h" +#include "mongo/db/query/ce/test_utils.h" #include "mongo/db/pipeline/abt/utils.h" #include "mongo/db/query/optimizer/explain.h" @@ -41,25 +39,23 @@ #include "mongo/db/query/sbe_stage_builder_helpers.h" #include "mongo/unittest/unittest.h" -namespace mongo::ce { - -using namespace optimizer; -using namespace cascades; +namespace mongo::optimizer::ce { +namespace value = sbe::value; CETester::CETester(std::string collName, double collCard, - const optimizer::OptPhaseManager::PhaseSet& optPhases) + const OptPhaseManager::PhaseSet& optPhases) : _optPhases(optPhases), _hints(), _metadata({}), _collName(collName) { addCollection(collName, collCard); } -optimizer::CEType CETester::getMatchCE(const std::string& queryPredicate, - std::function<bool(const ABT&)> nodePredicate) const { +CEType CETester::getMatchCE(const std::string& queryPredicate, + std::function<bool(const ABT&)> nodePredicate) const { return getCE("[{$match: " + queryPredicate + "}]", nodePredicate); } -optimizer::CEType CETester::getCE(const std::string& pipeline, - std::function<bool(const ABT&)> nodePredicate) const { +CEType CETester::getCE(const std::string& pipeline, + std::function<bool(const ABT&)> nodePredicate) const { if constexpr (kCETestLogOnly) { std::cout << "\n\nQuery: " << pipeline << "\n"; } @@ -71,7 +67,7 @@ optimizer::CEType CETester::getCE(const std::string& pipeline, return getCE(abt, nodePredicate); } -optimizer::CEType CETester::getCE(ABT& abt, std::function<bool(const ABT&)> nodePredicate) const { +CEType CETester::getCE(ABT& abt, std::function<bool(const ABT&)> nodePredicate) const { if constexpr (kCETestLogOnly) { std::cout << ExplainGenerator::explainV2(abt) << std::endl; } @@ -80,9 +76,9 @@ optimizer::CEType CETester::getCE(ABT& abt, std::function<bool(const ABT&)> node _prefixId, false /*requireRID*/, _metadata, - getCETransport(), + getEstimator(), makeHeuristicCE(), - makeCosting(), + makeCostEstimator(), defaultConvertPathToInterval, ConstEval::constFold, DebugInfo::kDefaultForTests, @@ -94,7 +90,7 @@ optimizer::CEType CETester::getCE(ABT& abt, std::function<bool(const ABT&)> node std::cout << ExplainGenerator::explainMemo(memo) << std::endl; } - auto cht = getCETransport(); + auto cht = getEstimator(); // If we are running no optimization phases, we are ensuring that we get the correct estimate on // the original ABT (usually testing the CE for FilterNodes). The memo won't have any groups for @@ -186,9 +182,9 @@ void CETester::addCollection(std::string collName, numRecords)); } -ScalarHistogram createHistogram(const std::vector<BucketData>& data) { +stats::ScalarHistogram createHistogram(const std::vector<BucketData>& data) { value::Array bounds; - std::vector<Bucket> buckets; + std::vector<stats::Bucket> buckets; double cumulativeFreq = 0.0; double cumulativeNDV = 0.0; @@ -207,10 +203,12 @@ ScalarHistogram createHistogram(const std::vector<BucketData>& data) { return {std::move(bounds), std::move(buckets)}; } -double estimateIntValCard(const ScalarHistogram& hist, const int v, const EstimationType type) { +double estimateIntValCard(const stats::ScalarHistogram& hist, + const int v, + const EstimationType type) { const auto [tag, val] = std::make_pair(value::TypeTags::NumberInt64, value::bitcastFrom<int64_t>(v)); return estimate(hist, tag, val, type).card; }; -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce/ce_test_utils.h b/src/mongo/db/query/ce/test_utils.h index 0894501bc2a..1f84fe9a1a8 100644 --- a/src/mongo/db/query/ce/ce_test_utils.h +++ b/src/mongo/db/query/ce/test_utils.h @@ -29,30 +29,12 @@ #pragma once -#include <cstddef> -#include <sys/types.h> - -#include "mongo/db/query/ce/histogram_estimation.h" -#include "mongo/db/query/ce/scalar_histogram.h" +#include "mongo/db/query/ce/histogram_predicate_estimation.h" #include "mongo/db/query/optimizer/cascades/interfaces.h" #include "mongo/db/query/optimizer/opt_phase_manager.h" +#include "mongo/db/query/stats/scalar_histogram.h" -namespace mongo { - -namespace optimizer { -namespace cascades { - -// Forward declaration. -class CEInterface; - -} // namespace cascades -} // namespace optimizer - -namespace ce { - -using namespace optimizer; -using namespace sbe; - +namespace mongo::optimizer::ce { // Enable this flag to log all estimates, and let all tests pass. constexpr bool kCETestLogOnly = false; @@ -199,9 +181,10 @@ public: protected: /** - * Subclasses need to override this method to initialize the transports they are testing. + * Subclasses need to override this method to initialize the cardinality estimators they are + * testing. */ - virtual std::unique_ptr<cascades::CEInterface> getCETransport() const = 0; + virtual std::unique_ptr<cascades::CardinalityEstimator> getEstimator() const = 0; private: /** @@ -242,9 +225,7 @@ struct BucketData { : BucketData(Value(v), equalFreq, rangeFreq, ndv) {} }; -ScalarHistogram createHistogram(const std::vector<BucketData>& data); - -double estimateIntValCard(const ScalarHistogram& hist, int v, EstimationType type); +stats::ScalarHistogram createHistogram(const std::vector<BucketData>& data); -} // namespace ce -} // namespace mongo +double estimateIntValCard(const stats::ScalarHistogram& hist, int v, EstimationType type); +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce_mode_parameter.cpp b/src/mongo/db/query/ce_mode_parameter.cpp index 2099c65ab1d..f54831bf626 100644 --- a/src/mongo/db/query/ce_mode_parameter.cpp +++ b/src/mongo/db/query/ce_mode_parameter.cpp @@ -30,11 +30,11 @@ #include "mongo/db/query/ce_mode_parameter.h" #include "mongo/db/query/query_knobs_gen.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { Status validateCEMode(const std::string& value, const boost::optional<TenantId>&) { if (value == kHeuristic || value == kHistogram || value == kSampling) { return Status::OK(); } return Status(ErrorCodes::Error{6695700}, "Invalid cardinality estimation mode."); } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce_mode_parameter.h b/src/mongo/db/query/ce_mode_parameter.h index e103b82e927..f253c60e5fd 100644 --- a/src/mongo/db/query/ce_mode_parameter.h +++ b/src/mongo/db/query/ce_mode_parameter.h @@ -34,7 +34,7 @@ #include "mongo/base/status.h" #include "mongo/db/tenant_id.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { /** * Defines cardinality estimation modes. @@ -45,4 +45,4 @@ const std::string kSampling = "sampling"; Status validateCEMode(const std::string& value, const boost::optional<TenantId>&); -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/ce_mode_parameter_test.cpp b/src/mongo/db/query/ce_mode_parameter_test.cpp index 9769127e5b3..011c5f210d3 100644 --- a/src/mongo/db/query/ce_mode_parameter_test.cpp +++ b/src/mongo/db/query/ce_mode_parameter_test.cpp @@ -31,7 +31,7 @@ #include "mongo/unittest/unittest.h" -namespace mongo::ce { +namespace mongo::optimizer::ce { TEST(CEModeParameterTest, ValidatesValidCEModes) { ASSERT_OK(validateCEMode("heuristic", boost::none)); @@ -44,4 +44,4 @@ TEST(CEModeParameterTest, RejectsInvalidCEModes) { ASSERT_NOT_OK(validateCEMode("", boost::none)); } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/cost_model/SConscript b/src/mongo/db/query/cost_model/SConscript index d8ce836096a..44649d53978 100644 --- a/src/mongo/db/query/cost_model/SConscript +++ b/src/mongo/db/query/cost_model/SConscript @@ -7,7 +7,7 @@ env = env.Clone() env.Library( target="query_cost_model", source=[ - 'cost_estimator.cpp', + 'cost_estimator_impl.cpp', 'cost_model.idl', 'cost_model_manager.cpp', 'cost_model_utils.cpp', diff --git a/src/mongo/db/query/cost_model/cost_estimator.cpp b/src/mongo/db/query/cost_model/cost_estimator_impl.cpp index 3dea08ebd1f..55d2bf6ab5c 100644 --- a/src/mongo/db/query/cost_model/cost_estimator.cpp +++ b/src/mongo/db/query/cost_model/cost_estimator_impl.cpp @@ -27,7 +27,7 @@ * it in the license file. */ -#include "mongo/db/query/cost_model/cost_estimator.h" +#include "mongo/db/query/cost_model/cost_estimator_impl.h" #include "mongo/db/query/optimizer/defs.h" @@ -404,12 +404,12 @@ private: }; } // namespace -CostAndCE CostEstimator::deriveCost(const Metadata& metadata, - const Memo& memo, - const PhysProps& physProps, - const ABT::reference_type physNodeRef, - const ChildPropsType& childProps, - const NodeCEMap& nodeCEMap) const { +CostAndCE CostEstimatorImpl::deriveCost(const Metadata& metadata, + const Memo& memo, + const PhysProps& physProps, + const ABT::reference_type physNodeRef, + const ChildPropsType& childProps, + const NodeCEMap& nodeCEMap) const { const CostAndCEInternal result = CostDerivation::derive( metadata, memo, physProps, physNodeRef, childProps, nodeCEMap, _coefficients); return {CostType::fromDouble(result._cost), result._ce}; diff --git a/src/mongo/db/query/cost_model/cost_estimator.h b/src/mongo/db/query/cost_model/cost_estimator_impl.h index 763351324b4..0ed094c02a9 100644 --- a/src/mongo/db/query/cost_model/cost_estimator.h +++ b/src/mongo/db/query/cost_model/cost_estimator_impl.h @@ -37,9 +37,9 @@ namespace mongo::cost_model { /** * Default costing for physical nodes with logical delegator (not-yet-optimized) inputs. */ -class CostEstimator : public optimizer::cascades::CostingInterface { +class CostEstimatorImpl : public optimizer::cascades::CostEstimator { public: - CostEstimator(CostModelCoefficients coefficicients) + CostEstimatorImpl(CostModelCoefficients coefficicients) : _coefficients{std::move(coefficicients)} {} optimizer::CostAndCE deriveCost(const optimizer::Metadata& metadata, diff --git a/src/mongo/db/query/cost_model/cost_estimator_test.cpp b/src/mongo/db/query/cost_model/cost_estimator_test.cpp index 420da954b06..5b68139a72e 100644 --- a/src/mongo/db/query/cost_model/cost_estimator_test.cpp +++ b/src/mongo/db/query/cost_model/cost_estimator_test.cpp @@ -27,7 +27,7 @@ * it in the license file. */ -#include "mongo/db/query/cost_model/cost_estimator.h" +#include "mongo/db/query/cost_model/cost_estimator_impl.h" #include "mongo/db/query/cost_model/cost_model_gen.h" #include "mongo/db/query/cost_model/cost_model_utils.h" #include "mongo/db/query/optimizer/cascades/memo.h" @@ -48,7 +48,7 @@ TEST(CostEstimatorTest, PhysicalScanCost) { costModel.setScanStartupCost(startupCost); costModel.setScanIncrementalCost(scanCost); - CostEstimator costEstimator{costModel}; + CostEstimatorImpl costEstimator{costModel}; optimizer::Metadata metadata{{}}; optimizer::cascades::Memo memo{}; @@ -94,7 +94,7 @@ TEST(CostEstimatorTest, PhysicalScanCostWithAdjustedCE) { costModel.setScanStartupCost(startupCost); costModel.setScanIncrementalCost(scanCost); - CostEstimator costEstimator{costModel}; + CostEstimatorImpl costEstimator{costModel}; optimizer::Metadata metadata{{}}; optimizer::cascades::Memo memo{}; @@ -130,7 +130,7 @@ TEST(CostEstimatorTest, IndexScanCost) { costModel.setIndexScanStartupCost(startupCost); costModel.setIndexScanIncrementalCost(indexScanCost); - CostEstimator costEstimator{costModel}; + CostEstimatorImpl costEstimator{costModel}; optimizer::Metadata metadata{{}}; optimizer::cascades::Memo memo{}; @@ -173,7 +173,7 @@ TEST(CostEstimatorTest, FilterAndEvaluationCost) { costModel.setEvalIncrementalCost(evalCost); costModel.setEvalStartupCost(startupCost); - CostEstimator costEstimator{costModel}; + CostEstimatorImpl costEstimator{costModel}; optimizer::Metadata metadata{{}}; optimizer::cascades::Memo memo{}; @@ -273,7 +273,7 @@ TEST(CostEstimatorTest, MergeJoinCost) { nodeCEMap[evalNodeRight.cast<optimizer::Node>()] = ce; - CostEstimator costEstimator{costModel}; + CostEstimatorImpl costEstimator{costModel}; optimizer::Metadata metadata{{}}; optimizer::cascades::Memo memo{}; diff --git a/src/mongo/db/query/cqf_get_executor.cpp b/src/mongo/db/query/cqf_get_executor.cpp index 642a8ad6322..be762fc0b9e 100644 --- a/src/mongo/db/query/cqf_get_executor.cpp +++ b/src/mongo/db/query/cqf_get_executor.cpp @@ -35,12 +35,11 @@ #include "mongo/db/pipeline/abt/document_source_visitor.h" #include "mongo/db/pipeline/abt/match_expression_visitor.h" #include "mongo/db/pipeline/abt/utils.h" -#include "mongo/db/query/ce/ce_heuristic.h" -#include "mongo/db/query/ce/ce_histogram.h" -#include "mongo/db/query/ce/ce_sampling.h" -#include "mongo/db/query/ce/collection_statistics_impl.h" +#include "mongo/db/query/ce/heuristic_estimator.h" +#include "mongo/db/query/ce/histogram_estimator.h" +#include "mongo/db/query/ce/sampling_estimator.h" #include "mongo/db/query/ce_mode_parameter.h" -#include "mongo/db/query/cost_model/cost_estimator.h" +#include "mongo/db/query/cost_model/cost_estimator_impl.h" #include "mongo/db/query/cost_model/cost_model_gen.h" #include "mongo/db/query/cost_model/cost_model_manager.h" #include "mongo/db/query/cost_model/on_coefficients_change_updater_impl.h" @@ -54,6 +53,7 @@ #include "mongo/db/query/query_knobs_gen.h" #include "mongo/db/query/query_planner_params.h" #include "mongo/db/query/sbe_stage_builder.h" +#include "mongo/db/query/stats/collection_statistics_impl.h" #include "mongo/db/query/yield_policy_callbacks_impl.h" #include "mongo/logv2/log.h" #include "mongo/logv2/log_attr.h" @@ -65,7 +65,10 @@ MONGO_FAIL_POINT_DEFINE(failConstructingBonsaiExecutor); namespace mongo { using namespace optimizer; -using cost_model::CostEstimator; +using ce::HeuristicEstimator; +using ce::HistogramEstimator; +using ce::SamplingEstimator; +using cost_model::CostEstimatorImpl; using cost_model::CostModelManager; static opt::unordered_map<std::string, optimizer::IndexDefinition> buildIndexSpecsOptimizer( @@ -582,9 +585,9 @@ static OptPhaseManager createPhaseManager(const CEMode mode, prefixId, false /*requireRID*/, std::move(metadataForSampling), - std::make_unique<ce::HeuristicCE>(), - std::make_unique<ce::HeuristicCE>(), - std::make_unique<CostEstimator>(costModel), + std::make_unique<HeuristicEstimator>(), + std::make_unique<HeuristicEstimator>(), + std::make_unique<CostEstimatorImpl>(costModel), defaultConvertPathToInterval, constFold, DebugInfo::kDefaultForProd, @@ -593,12 +596,12 @@ static OptPhaseManager createPhaseManager(const CEMode mode, prefixId, requireRID, std::move(metadata), - std::make_unique<ce::CESamplingTransport>(opCtx, - std::move(phaseManagerForSampling), - collectionSize, - std::make_unique<ce::HeuristicCE>()), - std::make_unique<ce::HeuristicCE>(), - std::make_unique<CostEstimator>(costModel), + std::make_unique<SamplingEstimator>(opCtx, + std::move(phaseManagerForSampling), + collectionSize, + std::make_unique<HeuristicEstimator>()), + std::make_unique<HeuristicEstimator>(), + std::make_unique<CostEstimatorImpl>(costModel), defaultConvertPathToInterval, constFold, DebugInfo::kDefaultForProd, @@ -610,11 +613,11 @@ static OptPhaseManager createPhaseManager(const CEMode mode, prefixId, requireRID, std::move(metadata), - std::make_unique<ce::CEHistogramTransport>( - std::make_shared<ce::CollectionStatisticsImpl>(collectionSize, nss), - std::make_unique<ce::HeuristicCE>()), - std::make_unique<ce::HeuristicCE>(), - std::make_unique<CostEstimator>(costModel), + std::make_unique<HistogramEstimator>( + std::make_shared<stats::CollectionStatisticsImpl>(collectionSize, nss), + std::make_unique<HeuristicEstimator>()), + std::make_unique<HeuristicEstimator>(), + std::make_unique<CostEstimatorImpl>(costModel), defaultConvertPathToInterval, constFold, DebugInfo::kDefaultForProd, @@ -625,9 +628,9 @@ static OptPhaseManager createPhaseManager(const CEMode mode, prefixId, requireRID, std::move(metadata), - std::make_unique<ce::HeuristicCE>(), - std::make_unique<ce::HeuristicCE>(), - std::make_unique<CostEstimator>(costModel), + std::make_unique<HeuristicEstimator>(), + std::make_unique<HeuristicEstimator>(), + std::make_unique<CostEstimatorImpl>(costModel), defaultConvertPathToInterval, constFold, DebugInfo::kDefaultForProd, diff --git a/src/mongo/db/query/optimizer/cascades/interfaces.h b/src/mongo/db/query/optimizer/cascades/interfaces.h index 2eb2f801b0b..63d20fe9845 100644 --- a/src/mongo/db/query/optimizer/cascades/interfaces.h +++ b/src/mongo/db/query/optimizer/cascades/interfaces.h @@ -56,9 +56,9 @@ public: /** * Interface for deriving CE for a newly added logical node in a new memo group. */ -class CEInterface { +class CardinalityEstimator { public: - virtual ~CEInterface() = default; + virtual ~CardinalityEstimator() = default; virtual CEType deriveCE(const Metadata& metadata, const Memo& memo, @@ -69,9 +69,9 @@ public: /** * Interface for deriving costs and adjusted CE (based on physical props) for a physical node. */ -class CostingInterface { +class CostEstimator { public: - virtual ~CostingInterface() = default; + virtual ~CostEstimator() = default; virtual CostAndCE deriveCost(const Metadata& metadata, const Memo& memo, const properties::PhysProps& physProps, diff --git a/src/mongo/db/query/optimizer/cascades/logical_rewriter.cpp b/src/mongo/db/query/optimizer/cascades/logical_rewriter.cpp index 7f527d394cf..f1192426a1d 100644 --- a/src/mongo/db/query/optimizer/cascades/logical_rewriter.cpp +++ b/src/mongo/db/query/optimizer/cascades/logical_rewriter.cpp @@ -88,7 +88,7 @@ LogicalRewriter::LogicalRewriter(const Metadata& metadata, const PathToIntervalFn& pathToInterval, const ConstFoldFn& constFold, const LogicalPropsInterface& logicalPropsDerivation, - const CEInterface& ceDerivation) + const CardinalityEstimator& cardinalityEstimator) : _activeRewriteSet(std::move(rewriteSet)), _groupsPending(), _metadata(metadata), @@ -99,7 +99,7 @@ LogicalRewriter::LogicalRewriter(const Metadata& metadata, _pathToInterval(pathToInterval), _constFold(constFold), _logicalPropsDerivation(logicalPropsDerivation), - _ceDerivation(ceDerivation) { + _cardinalityEstimator(cardinalityEstimator) { initializeRewrites(); if (_activeRewriteSet.count(LogicalRewriteType::SargableSplit) > 0) { @@ -132,7 +132,7 @@ std::pair<GroupIdType, NodeIdSet> LogicalRewriter::addNode(const ABT& node, } const GroupIdType resultGroupId = _memo.integrate( - Memo::Context{&_metadata, &_debugInfo, &_logicalPropsDerivation, &_ceDerivation}, + Memo::Context{&_metadata, &_debugInfo, &_logicalPropsDerivation, &_cardinalityEstimator}, node, std::move(targetGroupMap), insertNodeIds, diff --git a/src/mongo/db/query/optimizer/cascades/logical_rewriter.h b/src/mongo/db/query/optimizer/cascades/logical_rewriter.h index 0af43abae90..5ed180a222b 100644 --- a/src/mongo/db/query/optimizer/cascades/logical_rewriter.h +++ b/src/mongo/db/query/optimizer/cascades/logical_rewriter.h @@ -61,7 +61,7 @@ public: const PathToIntervalFn& pathToInterval, const ConstFoldFn& constFold, const LogicalPropsInterface& logicalPropsDerivation, - const CEInterface& ceDerivation); + const CardinalityEstimator& cardinalityEstimator); // This is a transient structure. We do not allow copying or moving. LogicalRewriter() = delete; @@ -130,7 +130,7 @@ private: const PathToIntervalFn& _pathToInterval; const ConstFoldFn& _constFold; const LogicalPropsInterface& _logicalPropsDerivation; - const CEInterface& _ceDerivation; + const CardinalityEstimator& _cardinalityEstimator; RewriteFnMap _rewriteMap; diff --git a/src/mongo/db/query/optimizer/cascades/memo.cpp b/src/mongo/db/query/optimizer/cascades/memo.cpp index 12f43bb2d9d..f847b1df58b 100644 --- a/src/mongo/db/query/optimizer/cascades/memo.cpp +++ b/src/mongo/db/query/optimizer/cascades/memo.cpp @@ -535,15 +535,15 @@ private: Memo::Context::Context(const Metadata* metadata, const DebugInfo* debugInfo, const LogicalPropsInterface* logicalPropsDerivation, - const CEInterface* ceDerivation) + const CardinalityEstimator* cardinalityEstimator) : _metadata(metadata), _debugInfo(debugInfo), _logicalPropsDerivation(logicalPropsDerivation), - _ceDerivation(ceDerivation) { + _cardinalityEstimator(cardinalityEstimator) { invariant(_metadata != nullptr); invariant(_debugInfo != nullptr); invariant(_logicalPropsDerivation != nullptr); - invariant(_ceDerivation != nullptr); + invariant(_cardinalityEstimator != nullptr); } size_t Memo::GroupIdVectorHash::operator()(const Memo::GroupIdVector& v) const { @@ -617,7 +617,8 @@ void Memo::estimateCE(const Context& ctx, const GroupIdType groupId) { ctx._logicalPropsDerivation->deriveProps(*ctx._metadata, nodeRef, nullptr, this, groupId); props.merge(logicalProps); - const CEType estimate = ctx._ceDerivation->deriveCE(*ctx._metadata, *this, props, nodeRef); + const CEType estimate = + ctx._cardinalityEstimator->deriveCE(*ctx._metadata, *this, props, nodeRef); auto ceProp = properties::CardinalityEstimate(estimate); if (auto sargablePtr = nodeRef.cast<SargableNode>(); sargablePtr != nullptr) { @@ -630,8 +631,8 @@ void Memo::estimateCE(const Context& ctx, const GroupIdType groupId) { ScanParams{}, sargablePtr->getTarget(), sargablePtr->getChild()); - const CEType singularEst = - ctx._ceDerivation->deriveCE(*ctx._metadata, *this, props, singularReq.ref()); + const CEType singularEst = ctx._cardinalityEstimator->deriveCE( + *ctx._metadata, *this, props, singularReq.ref()); partialSchemaKeyCE.emplace_back(key, singularEst); } } diff --git a/src/mongo/db/query/optimizer/cascades/memo.h b/src/mongo/db/query/optimizer/cascades/memo.h index c2c2b6a2d93..93321bce7e1 100644 --- a/src/mongo/db/query/optimizer/cascades/memo.h +++ b/src/mongo/db/query/optimizer/cascades/memo.h @@ -127,13 +127,13 @@ public: Context(const Metadata* metadata, const DebugInfo* debugInfo, const LogicalPropsInterface* logicalPropsDerivation, - const CEInterface* ceDerivation); + const CardinalityEstimator* cardinalityEstimator); // None of those should be null. const Metadata* _metadata; const DebugInfo* _debugInfo; const LogicalPropsInterface* _logicalPropsDerivation; - const CEInterface* _ceDerivation; + const CardinalityEstimator* _cardinalityEstimator; }; struct Stats { diff --git a/src/mongo/db/query/optimizer/cascades/physical_rewriter.cpp b/src/mongo/db/query/optimizer/cascades/physical_rewriter.cpp index c0b3423ab0c..bdbe168e146 100644 --- a/src/mongo/db/query/optimizer/cascades/physical_rewriter.cpp +++ b/src/mongo/db/query/optimizer/cascades/physical_rewriter.cpp @@ -110,13 +110,13 @@ PhysicalRewriter::PhysicalRewriter(const Metadata& metadata, const DebugInfo& debugInfo, const QueryHints& hints, const RIDProjectionsMap& ridProjections, - const CostingInterface& costDerivation, + const CostEstimator& costEstimator, const PathToIntervalFn& pathToInterval, std::unique_ptr<LogicalRewriter>& logicalRewriter) : _metadata(metadata), _memo(memo), _rootGroupId(rootGroupId), - _costDerivation(costDerivation), + _costEstimator(costEstimator), _debugInfo(debugInfo), _hints(hints), _ridProjections(ridProjections), @@ -149,7 +149,7 @@ void PhysicalRewriter::costAndRetainBestNode(std::unique_ptr<ABT> node, const GroupIdType groupId, PrefixId& prefixId, PhysOptimizationResult& bestResult) { - const CostAndCE nodeCostAndCE = _costDerivation.deriveCost( + const CostAndCE nodeCostAndCE = _costEstimator.deriveCost( _metadata, _memo, bestResult._physProps, node->ref(), childProps, nodeCEMap); const CostType nodeCost = nodeCostAndCE._cost; uassert(6624056, "Must get non-infinity cost for physical node.", !nodeCost.isInfinite()); diff --git a/src/mongo/db/query/optimizer/cascades/physical_rewriter.h b/src/mongo/db/query/optimizer/cascades/physical_rewriter.h index a2fb936442e..50bc831dc46 100644 --- a/src/mongo/db/query/optimizer/cascades/physical_rewriter.h +++ b/src/mongo/db/query/optimizer/cascades/physical_rewriter.h @@ -58,7 +58,7 @@ public: const DebugInfo& debugInfo, const QueryHints& hints, const RIDProjectionsMap& ridProjections, - const CostingInterface& costDerivation, + const CostEstimator& costEstimator, const PathToIntervalFn& pathToInterval, std::unique_ptr<LogicalRewriter>& logicalRewriter); @@ -96,7 +96,7 @@ private: const Metadata& _metadata; Memo& _memo; const GroupIdType _rootGroupId; - const CostingInterface& _costDerivation; + const CostEstimator& _costEstimator; const DebugInfo& _debugInfo; const QueryHints& _hints; const RIDProjectionsMap& _ridProjections; diff --git a/src/mongo/db/query/optimizer/opt_phase_manager.cpp b/src/mongo/db/query/optimizer/opt_phase_manager.cpp index 79e6b34201a..2b710a1b2ca 100644 --- a/src/mongo/db/query/optimizer/opt_phase_manager.cpp +++ b/src/mongo/db/query/optimizer/opt_phase_manager.cpp @@ -49,9 +49,9 @@ OptPhaseManager::OptPhaseManager(OptPhaseManager::PhaseSet phaseSet, PrefixId& prefixId, const bool requireRID, Metadata metadata, - std::unique_ptr<CEInterface> explorationCE, - std::unique_ptr<CEInterface> substitutionCE, - std::unique_ptr<CostingInterface> costDerivation, + std::unique_ptr<CardinalityEstimator> explorationCE, + std::unique_ptr<CardinalityEstimator> substitutionCE, + std::unique_ptr<CostEstimator> costEstimator, PathToIntervalFn pathToInterval, ConstFoldFn constFold, DebugInfo debugInfo, @@ -64,14 +64,14 @@ OptPhaseManager::OptPhaseManager(OptPhaseManager::PhaseSet phaseSet, _logicalPropsDerivation(std::make_unique<DefaultLogicalPropsDerivation>()), _explorationCE(std::move(explorationCE)), _substitutionCE(std::move(substitutionCE)), - _costDerivation(std::move(costDerivation)), + _costEstimator(std::move(costEstimator)), _pathToInterval(std::move(pathToInterval)), _constFold(std::move(constFold)), _physicalNodeId(), _requireRID(requireRID), _ridProjections(), _prefixId(prefixId) { - uassert(6624093, "Cost derivation is null", _costDerivation); + uassert(6624093, "Cost derivation is null", _costEstimator); uassert(7088900, "Exploration CE is null", _explorationCE); uassert(7088901, "Substitution CE is null", _substitutionCE); @@ -224,7 +224,7 @@ void OptPhaseManager::runMemoPhysicalRewrite(const OptPhase phase, _debugInfo, _hints, _ridProjections, - *_costDerivation, + *_costEstimator, _pathToInterval, logicalRewriter); diff --git a/src/mongo/db/query/optimizer/opt_phase_manager.h b/src/mongo/db/query/optimizer/opt_phase_manager.h index 4c6e8c431ae..c1219361f55 100644 --- a/src/mongo/db/query/optimizer/opt_phase_manager.h +++ b/src/mongo/db/query/optimizer/opt_phase_manager.h @@ -79,9 +79,9 @@ public: PrefixId& prefixId, bool requireRID, Metadata metadata, - std::unique_ptr<CEInterface> explorationCE, - std::unique_ptr<CEInterface> substitutionCE, - std::unique_ptr<CostingInterface> costDerivation, + std::unique_ptr<CardinalityEstimator> explorationCE, + std::unique_ptr<CardinalityEstimator> substitutionCE, + std::unique_ptr<CostEstimator> costEstimator, PathToIntervalFn pathToInterval, ConstFoldFn constFold, DebugInfo debugInfo, @@ -172,7 +172,7 @@ private: /** * Cardinality estimation implementation to be used during the exploraton phase.. */ - std::unique_ptr<CEInterface> _explorationCE; + std::unique_ptr<CardinalityEstimator> _explorationCE; /** * Cardinality estimation implementation to be used during the substitution phase. @@ -181,12 +181,12 @@ private: * alternatives. Since some CE implementations are expensive (sampling), we let the caller pass * a different one for this phase. */ - std::unique_ptr<CEInterface> _substitutionCE; + std::unique_ptr<CardinalityEstimator> _substitutionCE; /** * Cost derivation implementation. */ - std::unique_ptr<CostingInterface> _costDerivation; + std::unique_ptr<CostEstimator> _costEstimator; /** * Path ABT node to index bounds converter implementation. diff --git a/src/mongo/db/query/optimizer/utils/ce_math.cpp b/src/mongo/db/query/optimizer/utils/ce_math.cpp index 936686354ac..4eaa9372c4e 100644 --- a/src/mongo/db/query/optimizer/utils/ce_math.cpp +++ b/src/mongo/db/query/optimizer/utils/ce_math.cpp @@ -34,8 +34,7 @@ #include "mongo/db/query/optimizer/utils/ce_math.h" #include "mongo/util/assert_util.h" -namespace mongo::ce { - +namespace mongo::optimizer::ce { bool validSelectivity(SelectivityType sel) { return (sel >= 0.0 && sel <= 1.0); } @@ -82,4 +81,4 @@ SelectivityType disjExponentialBackoff(std::vector<SelectivityType> disjSelectiv } return 1.0 - sel; } -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/optimizer/utils/ce_math.h b/src/mongo/db/query/optimizer/utils/ce_math.h index a29a7d1f34b..44b87b14904 100644 --- a/src/mongo/db/query/optimizer/utils/ce_math.h +++ b/src/mongo/db/query/optimizer/utils/ce_math.h @@ -34,9 +34,7 @@ #include "mongo/db/query/optimizer/defs.h" -namespace mongo::ce { - -using namespace mongo::optimizer; +namespace mongo::optimizer::ce { // Default cardinality when actual collection cardinality is unknown. // Mostly used by unit tests. @@ -68,4 +66,4 @@ SelectivityType conjExponentialBackoff(std::vector<SelectivityType> conjSelectiv * exponential backoff. */ SelectivityType disjExponentialBackoff(std::vector<SelectivityType> disjSelectivities); -} // namespace mongo::ce +} // namespace mongo::optimizer::ce diff --git a/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp b/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp index f4b3ba349e5..e261c5ac537 100644 --- a/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp +++ b/src/mongo/db/query/optimizer/utils/unit_test_utils.cpp @@ -32,9 +32,9 @@ #include <fstream> #include "mongo/db/pipeline/abt/utils.h" -#include "mongo/db/query/ce/ce_heuristic.h" -#include "mongo/db/query/ce/ce_hinted.h" -#include "mongo/db/query/cost_model/cost_estimator.h" +#include "mongo/db/query/ce/heuristic_estimator.h" +#include "mongo/db/query/ce/hinted_estimator.h" +#include "mongo/db/query/cost_model/cost_estimator_impl.h" #include "mongo/db/query/cost_model/cost_model_manager.h" #include "mongo/db/query/optimizer/explain.h" #include "mongo/db/query/optimizer/metadata.h" @@ -246,16 +246,16 @@ IndexDefinition makeCompositeIndexDefinition(std::vector<TestIndexField> indexFi return IndexDefinition{std::move(idxCollSpec), isMultiKey}; } -std::unique_ptr<CEInterface> makeHeuristicCE() { - return std::make_unique<ce::HeuristicCE>(); +std::unique_ptr<CardinalityEstimator> makeHeuristicCE() { + return std::make_unique<ce::HeuristicEstimator>(); } -std::unique_ptr<CEInterface> makeHintedCE(ce::PartialSchemaSelHints hints) { - return std::make_unique<ce::HintedCE>(std::move(hints)); +std::unique_ptr<CardinalityEstimator> makeHintedCE(ce::PartialSchemaSelHints hints) { + return std::make_unique<ce::HintedEstimator>(std::move(hints)); } -std::unique_ptr<CostingInterface> makeCosting() { - return std::make_unique<cost_model::CostEstimator>( +std::unique_ptr<CostEstimator> makeCostEstimator() { + return std::make_unique<cost_model::CostEstimatorImpl>( cost_model::CostModelManager::getDefaultCoefficients()); } @@ -270,7 +270,7 @@ OptPhaseManager makePhaseManager(OptPhaseManager::PhaseSet phaseSet, std::move(metadata), makeHeuristicCE(), // primary CE makeHeuristicCE(), // substitution phase CE, same as primary - makeCosting(), + makeCostEstimator(), defaultConvertPathToInterval, ConstEval::constFold, std::move(debugInfo), @@ -280,16 +280,16 @@ OptPhaseManager makePhaseManager(OptPhaseManager::PhaseSet phaseSet, OptPhaseManager makePhaseManager(OptPhaseManager::PhaseSet phaseSet, PrefixId& prefixId, Metadata metadata, - std::unique_ptr<CEInterface> ceDerivation, + std::unique_ptr<CardinalityEstimator> ce, DebugInfo debugInfo, QueryHints queryHints) { return OptPhaseManager{std::move(phaseSet), prefixId, false /*requireRID*/, std::move(metadata), - std::move(ceDerivation), // primary CE - makeHeuristicCE(), // substitution phase CE - makeCosting(), + std::move(ce), // primary CE + makeHeuristicCE(), // substitution phase CE + makeCostEstimator(), defaultConvertPathToInterval, ConstEval::constFold, std::move(debugInfo), @@ -308,7 +308,7 @@ OptPhaseManager makePhaseManager(OptPhaseManager::PhaseSet phaseSet, std::move(metadata), makeHeuristicCE(), // primary CE makeHeuristicCE(), // substitution phase CE, same as primary - std::make_unique<cost_model::CostEstimator>(coefs), + std::make_unique<cost_model::CostEstimatorImpl>(coefs), defaultConvertPathToInterval, ConstEval::constFold, std::move(debugInfo), @@ -326,7 +326,7 @@ OptPhaseManager makePhaseManagerRequireRID(OptPhaseManager::PhaseSet phaseSet, std::move(metadata), makeHeuristicCE(), // primary CE makeHeuristicCE(), // substitution phase CE, same as primary - makeCosting(), + makeCostEstimator(), defaultConvertPathToInterval, ConstEval::constFold, std::move(debugInfo), diff --git a/src/mongo/db/query/optimizer/utils/unit_test_utils.h b/src/mongo/db/query/optimizer/utils/unit_test_utils.h index 953b8a07781..a1e6549e52f 100644 --- a/src/mongo/db/query/optimizer/utils/unit_test_utils.h +++ b/src/mongo/db/query/optimizer/utils/unit_test_utils.h @@ -30,7 +30,7 @@ #pragma once #include "mongo/db/bson/dotted_path_support.h" -#include "mongo/db/query/ce/ce_hinted.h" +#include "mongo/db/query/ce/hinted_estimator.h" #include "mongo/db/query/cost_model/cost_model_gen.h" #include "mongo/db/query/optimizer/defs.h" #include "mongo/db/query/optimizer/explain.h" @@ -168,17 +168,17 @@ IndexDefinition makeCompositeIndexDefinition(std::vector<TestIndexField> indexFi /** * A factory function to create a heuristic-based cardinality estimator. */ -std::unique_ptr<CEInterface> makeHeuristicCE(); +std::unique_ptr<CardinalityEstimator> makeHeuristicCE(); /** * A factory function to create a hint-based cardinality estimator. */ -std::unique_ptr<CEInterface> makeHintedCE(ce::PartialSchemaSelHints hints); +std::unique_ptr<CardinalityEstimator> makeHintedCE(ce::PartialSchemaSelHints hints); /** * A convenience factory function to create costing. */ -std::unique_ptr<CostingInterface> makeCosting(); +std::unique_ptr<CostEstimator> makeCostEstimator(); /** * A convenience factory function to create OptPhaseManager for unit tests. @@ -195,7 +195,7 @@ OptPhaseManager makePhaseManager(OptPhaseManager::PhaseSet phaseSet, OptPhaseManager makePhaseManager(OptPhaseManager::PhaseSet phaseSet, PrefixId& prefixId, Metadata metadata, - std::unique_ptr<CEInterface> ceDerivation, + std::unique_ptr<CardinalityEstimator> ce, DebugInfo debugInfo, QueryHints queryHints = {}); diff --git a/src/mongo/db/query/query_knobs.idl b/src/mongo/db/query/query_knobs.idl index 6b10ec2e9fd..af84c505323 100644 --- a/src/mongo/db/query/query_knobs.idl +++ b/src/mongo/db/query/query_knobs.idl @@ -742,7 +742,7 @@ server_parameters: cpp_vartype: std::string default: sampling validator: - callback: ce::validateCEMode + callback: optimizer::ce::validateCEMode internalCascadesOptimizerDisableScan: description: "Disable full collection scans in the Cascades optimizer." diff --git a/src/mongo/db/query/stats/SConscript b/src/mongo/db/query/stats/SConscript new file mode 100644 index 00000000000..1ef2b61f00e --- /dev/null +++ b/src/mongo/db/query/stats/SConscript @@ -0,0 +1,123 @@ +# -*- mode: python -*- + +Import("env") + +env = env.Clone() + +env.Library( + target="query_stats", + source=[ + 'collection_statistics_impl.cpp', + 'stats_catalog.cpp', + 'stats_cache.cpp', + 'stats_cache_loader_impl.cpp', + ], + LIBDEPS_PRIVATE=[ + '$BUILD_DIR/mongo/db/dbdirectclient', + '$BUILD_DIR/mongo/util/caching', + '$BUILD_DIR/mongo/util/concurrency/thread_pool', + 'stats_histograms', + ], +) + +env.Library( + target="stats_histograms", + source=[ + 'array_histogram.cpp', + 'scalar_histogram.cpp', + 'stats.idl', + 'value_utils.cpp', + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/db/exec/sbe/query_sbe_values', + ], +) + +env.Library( + target="stats_gen", + source=[ + 'max_diff.cpp', + ], + LIBDEPS=[ + 'stats_histograms', + ], +) + +env.CppUnitTest( + target='stats_cache_loader_test', + source=[ + 'stats_cache_loader_test.cpp', + 'stats_cache_loader_test_fixture.cpp', + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/db/auth/authmocks', + '$BUILD_DIR/mongo/db/catalog/collection_crud', + '$BUILD_DIR/mongo/db/commands/test_commands_enabled', + '$BUILD_DIR/mongo/db/index_builds_coordinator_mongod', + '$BUILD_DIR/mongo/db/multitenancy', + '$BUILD_DIR/mongo/db/op_observer/op_observer', + '$BUILD_DIR/mongo/db/op_observer/op_observer_impl', + '$BUILD_DIR/mongo/db/query/datetime/date_time_support', + '$BUILD_DIR/mongo/db/query/query_test_service_context', + '$BUILD_DIR/mongo/db/query_expressions', + '$BUILD_DIR/mongo/db/repl/drop_pending_collection_reaper', + '$BUILD_DIR/mongo/db/repl/oplog', + '$BUILD_DIR/mongo/db/repl/optime', + '$BUILD_DIR/mongo/db/repl/repl_coordinator_interface', + '$BUILD_DIR/mongo/db/repl/replmocks', + '$BUILD_DIR/mongo/db/repl/storage_interface_impl', + '$BUILD_DIR/mongo/db/server_base', + '$BUILD_DIR/mongo/db/service_context', + '$BUILD_DIR/mongo/db/service_context_d_test_fixture', + '$BUILD_DIR/mongo/db/service_context_test_fixture', + '$BUILD_DIR/mongo/db/shard_role', + '$BUILD_DIR/mongo/db/storage/wiredtiger/storage_wiredtiger', + '$BUILD_DIR/mongo/db/timeseries/timeseries_options', + '$BUILD_DIR/mongo/unittest/unittest', + '$BUILD_DIR/mongo/util/clock_source_mock', + '$BUILD_DIR/mongo/util/fail_point', + '$BUILD_DIR/mongo/util/pcre_wrapper', + ], +) + +env.CppUnitTest( + target="stats_cache_test", + source=[ + "stats_cache_test.cpp", + "stats_cache_loader_mock.cpp", + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/db/service_context', + 'stats_test_utils', + ], +) + +env.CppUnitTest( + target="stats_path_test", + source=[ + "stats_path_test.cpp", + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/db/service_context', + 'stats_test_utils', + ], +) + +env.Library( + target="stats_test_utils", + source=[ + 'collection_statistics_mock.cpp', + 'rand_utils.cpp', + 'rand_utils_new.cpp', + 'maxdiff_test_utils.cpp', + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/db/exec/sbe/sbe_abt_test_util', + "$BUILD_DIR/mongo/unittest/unittest", + 'stats_gen', + 'stats_histograms', + ], +) diff --git a/src/mongo/db/query/ce/array_histogram.cpp b/src/mongo/db/query/stats/array_histogram.cpp index 66ab117e60d..ccf11bf02d2 100644 --- a/src/mongo/db/query/ce/array_histogram.cpp +++ b/src/mongo/db/query/stats/array_histogram.cpp @@ -27,13 +27,11 @@ * it in the license file. */ -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/value_utils.h" - -namespace mongo { -namespace ce { -using namespace sbe; +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/value_utils.h" +namespace mongo::stats { +namespace { TypeCounts mapStatsTypeCountToTypeCounts(std::vector<TypeTag> tc) { TypeCounts out; for (const auto& t : tc) { @@ -41,6 +39,7 @@ TypeCounts mapStatsTypeCountToTypeCounts(std::vector<TypeTag> tc) { } return out; } +} // namespace ArrayHistogram::ArrayHistogram() : ArrayHistogram(ScalarHistogram(), {}) {} @@ -152,7 +151,7 @@ const TypeCounts& ArrayHistogram::getArrayTypeCounts() const { double ArrayHistogram::getArrayCount() const { if (isArray()) { - auto findArray = _typeCounts.find(value::TypeTags::Array); + auto findArray = _typeCounts.find(sbe::value::TypeTags::Array); uassert(6979504, "Histogram with array data must have a total array count.", findArray != _typeCounts.end()); @@ -177,7 +176,7 @@ BSONObj ArrayHistogram::serialize() const { BSONArrayBuilder typeCountBuilder(histogramBuilder.subarrayStart("typeCount")); const auto& typeCounts = getTypeCounts(); for (const auto& [sbeType, count] : typeCounts) { - auto typeCount = BSON("typeName" << ce::serialize(sbeType) << "count" << count); + auto typeCount = BSON("typeName" << stats::serialize(sbeType) << "count" << count); typeCountBuilder.append(typeCount); } typeCountBuilder.doneFast(); @@ -190,11 +189,8 @@ BSONObj ArrayHistogram::serialize() const { histogramBuilder.doneFast(); return histogramBuilder.obj(); } -} // namespace ce -// TODO: update this once SERVER-71051 is done. -namespace stats { -BSONObj makeStatistics(double documents, const ce::ArrayHistogram& arrayHistogram) { +BSONObj makeStatistics(double documents, const ArrayHistogram& arrayHistogram) { BSONObjBuilder builder; builder.appendNumber("documents", documents); builder.appendElements(arrayHistogram.serialize()); @@ -202,13 +198,12 @@ BSONObj makeStatistics(double documents, const ce::ArrayHistogram& arrayHistogra return builder.obj(); } -BSONObj makeStatsPath(StringData path, double documents, const ce::ArrayHistogram& arrayHistogram) { +BSONObj makeStatsPath(StringData path, double documents, const ArrayHistogram& arrayHistogram) { BSONObjBuilder builder; builder.append("_id", path); builder.append("statistics", makeStatistics(documents, arrayHistogram)); builder.doneFast(); return builder.obj(); } -} // namespace stats -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/array_histogram.h b/src/mongo/db/query/stats/array_histogram.h index 2ce33d330b5..9a80feae423 100644 --- a/src/mongo/db/query/ce/array_histogram.h +++ b/src/mongo/db/query/stats/array_histogram.h @@ -32,12 +32,10 @@ #include <map> #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/scalar_histogram.h" -#include "mongo/db/query/ce/stats_gen.h" - -namespace mongo { -namespace ce { +#include "mongo/db/query/stats/scalar_histogram.h" +#include "mongo/db/query/stats/stats_gen.h" +namespace mongo::stats { using TypeCounts = std::map<sbe::value::TypeTags, double>; class ArrayHistogram { @@ -131,18 +129,14 @@ private: // The number of values of each type inside all arrays. boost::optional<TypeCounts> _arrayTypeCounts; }; -} // namespace ce -// TODO: update this once SERVER-71051 is done. -namespace stats { + /** * Returns an owned BSON Object representing data matching mongo::Statistics IDL. */ -BSONObj makeStatistics(double documents, const ce::ArrayHistogram& arrayHistogram); +BSONObj makeStatistics(double documents, const ArrayHistogram& arrayHistogram); /** * Returns an owned BSON Object representing data matching mongo::StatsPath IDL. */ -BSONObj makeStatsPath(StringData path, double documents, const ce::ArrayHistogram& arrayHistogram); -} // namespace stats - -} // namespace mongo +BSONObj makeStatsPath(StringData path, double documents, const ArrayHistogram& arrayHistogram); +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/collection_statistics.h b/src/mongo/db/query/stats/collection_statistics.h index 5949215b448..22e48663a61 100644 --- a/src/mongo/db/query/ce/collection_statistics.h +++ b/src/mongo/db/query/stats/collection_statistics.h @@ -30,9 +30,9 @@ #pragma once #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/array_histogram.h" +#include "mongo/db/query/stats/array_histogram.h" -namespace mongo::ce { +namespace mongo::stats { using Histograms = std::map<std::string, std::shared_ptr<ArrayHistogram>>; @@ -57,4 +57,4 @@ public: virtual ~CollectionStatistics() = default; }; -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/collection_statistics_impl.cpp b/src/mongo/db/query/stats/collection_statistics_impl.cpp index 7bf6b4e7a11..b03829b3f1d 100644 --- a/src/mongo/db/query/ce/collection_statistics_impl.cpp +++ b/src/mongo/db/query/stats/collection_statistics_impl.cpp @@ -27,11 +27,12 @@ * it in the license file. */ -#include "mongo/db/query/ce/collection_statistics_impl.h" +#include "mongo/db/query/stats/collection_statistics_impl.h" + #include "mongo/db/client.h" -#include "mongo/db/query/ce/stats_catalog.h" +#include "mongo/db/query/stats/stats_catalog.h" -namespace mongo::ce { +namespace mongo::stats { CollectionStatisticsImpl::CollectionStatisticsImpl(double cardinality, const NamespaceString& nss) : _cardinality{cardinality}, _histograms{}, _nss{nss} {}; @@ -68,4 +69,4 @@ const ArrayHistogram* CollectionStatisticsImpl::getHistogram(const std::string& } } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/collection_statistics_impl.h b/src/mongo/db/query/stats/collection_statistics_impl.h index 11b2c9630ce..19c9612382f 100644 --- a/src/mongo/db/query/ce/collection_statistics_impl.h +++ b/src/mongo/db/query/stats/collection_statistics_impl.h @@ -29,10 +29,10 @@ #pragma once -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/collection_statistics.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/collection_statistics.h" -namespace mongo::ce { +namespace mongo::stats { using Histograms = std::map<std::string, std::shared_ptr<ArrayHistogram>>; @@ -64,4 +64,4 @@ private: const NamespaceString _nss; }; -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/collection_statistics_mock.cpp b/src/mongo/db/query/stats/collection_statistics_mock.cpp index d8faa285e20..39b2f65e527 100644 --- a/src/mongo/db/query/ce/collection_statistics_mock.cpp +++ b/src/mongo/db/query/stats/collection_statistics_mock.cpp @@ -27,9 +27,9 @@ * it in the license file. */ -#include "mongo/db/query/ce/collection_statistics_mock.h" +#include "mongo/db/query/stats/collection_statistics_mock.h" -namespace mongo::ce { +namespace mongo::stats { CollectionStatisticsMock::CollectionStatisticsMock(double cardinality) : _cardinality{cardinality}, _histograms{} {}; @@ -50,4 +50,4 @@ const ArrayHistogram* CollectionStatisticsMock::getHistogram(const std::string& return nullptr; } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/collection_statistics_mock.h b/src/mongo/db/query/stats/collection_statistics_mock.h index a93964cd701..04fee5ff69c 100644 --- a/src/mongo/db/query/ce/collection_statistics_mock.h +++ b/src/mongo/db/query/stats/collection_statistics_mock.h @@ -30,9 +30,9 @@ #pragma once #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/collection_statistics.h" +#include "mongo/db/query/stats/collection_statistics.h" -namespace mongo::ce { +namespace mongo::stats { class CollectionStatisticsMock : public CollectionStatistics { public: @@ -61,4 +61,4 @@ private: mutable Histograms _histograms; }; -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/max_diff.cpp b/src/mongo/db/query/stats/max_diff.cpp index 3c265620771..9203b3d8321 100644 --- a/src/mongo/db/query/ce/max_diff.cpp +++ b/src/mongo/db/query/stats/max_diff.cpp @@ -27,6 +27,8 @@ * it in the license file. */ +#include "mongo/db/query/stats/max_diff.h" + #include <algorithm> #include <cmath> #include <cstddef> @@ -41,12 +43,12 @@ #include "mongo/bson/bsontypes.h" #include "mongo/db/exec/sbe/values/bson.h" #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/max_diff.h" -#include "mongo/db/query/ce/value_utils.h" +#include "mongo/db/query/stats/value_utils.h" #include "mongo/util/assert_util.h" - -namespace mongo::ce { +namespace mongo::stats { +namespace { +namespace value = sbe::value; std::string printDistribution(const DataDistribution& distr, size_t nElems) { std::ostringstream os; @@ -56,10 +58,10 @@ std::string printDistribution(const DataDistribution& distr, size_t nElems) { return os.str(); } -static double valueSpread(value::TypeTags tag1, - value::Value val1, - value::TypeTags tag2, - value::Value val2) { +double valueSpread(value::TypeTags tag1, + value::Value val1, + value::TypeTags tag2, + value::Value val2) { double doubleVal1 = valueToDouble(tag1, val1); double doubleVal2 = valueToDouble(tag2, val2); uassert(6660502, @@ -68,6 +70,86 @@ static double valueSpread(value::TypeTags tag1, return doubleVal2 - doubleVal1; } +// TODO: This doesn't seem right -- it looks like we're sorting on the frequency, +// not the difference between buckets +std::vector<ValFreq> generateTopKBuckets(const DataDistribution& dataDistrib, size_t numBuckets) { + struct AreaComparator { + bool operator()(const ValFreq& a, const ValFreq& b) const { + return a._normArea > b._normArea; + } + }; + std::priority_queue<ValFreq, std::vector<ValFreq>, AreaComparator> pq; + + for (const auto& valFreq : dataDistrib._freq) { + if (pq.size() < numBuckets) { + pq.emplace(valFreq); + } else if (AreaComparator()(valFreq, pq.top())) { + pq.pop(); + pq.emplace(valFreq); + } + } + + std::vector<ValFreq> result; + while (!pq.empty()) { + result.push_back(pq.top()); + pq.pop(); + } + + std::sort(result.begin(), result.end(), [](const ValFreq& a, const ValFreq& b) { + return a._idx < b._idx; + }); + + return result; +} + +/** + * Helper for getting the input for constructing an array histogram for an array estimator using the + * values in an array. For each value in `arrayElements`, update the min, max, and unique value + * vectors. These will be used to generate the corresponding histograms for array values. + */ +void updateMinMaxUniqArrayVals(std::vector<SBEValue>& arrayElements, + std::vector<SBEValue>& arrayMinData, + std::vector<SBEValue>& arrayMaxData, + std::vector<SBEValue>& arrayUniqueData) { + + if (arrayElements.size() == 0) { + return; + } + + sortValueVector(arrayElements); + + // Emit values for arrayMin and arrayMax histograms. + { + boost::optional<SBEValue> prev; + for (const auto& element : arrayElements) { + if (!prev) { + arrayMinData.push_back(element); + } else if (!sameTypeClass(prev->getTag(), element.getTag())) { + arrayMaxData.push_back(*prev); + arrayMinData.push_back(element); + } + prev = element; + } + if (prev) { + arrayMaxData.push_back(*prev); + } + } + + // Emit values for arrayUnique histogram. + { + boost::optional<SBEValue> prev; + for (const auto& element : arrayElements) { + if (!prev || + compareValues( + prev->getTag(), prev->getValue(), element.getTag(), element.getValue()) < 0) { + arrayUniqueData.push_back(element); + prev = element; + } + } + } +} +} // namespace + DataDistribution getDataDistribution(const std::vector<SBEValue>& sortedInput) { if (sortedInput.empty()) { return {}; @@ -152,39 +234,6 @@ DataDistribution getDataDistribution(const std::vector<SBEValue>& sortedInput) { return result; } -// TODO: This doesn't seem right -- it looks like we're sorting on the frequency, -// not the difference between buckets -static std::vector<ValFreq> generateTopKBuckets(const DataDistribution& dataDistrib, - size_t numBuckets) { - struct AreaComparator { - bool operator()(const ValFreq& a, const ValFreq& b) const { - return a._normArea > b._normArea; - } - }; - std::priority_queue<ValFreq, std::vector<ValFreq>, AreaComparator> pq; - - for (const auto& valFreq : dataDistrib._freq) { - if (pq.size() < numBuckets) { - pq.emplace(valFreq); - } else if (AreaComparator()(valFreq, pq.top())) { - pq.pop(); - pq.emplace(valFreq); - } - } - - std::vector<ValFreq> result; - while (!pq.empty()) { - result.push_back(pq.top()); - pq.pop(); - } - - std::sort(result.begin(), result.end(), [](const ValFreq& a, const ValFreq& b) { - return a._idx < b._idx; - }); - - return result; -} - ScalarHistogram genMaxDiffHistogram(const DataDistribution& dataDistrib, size_t numBuckets) { if (dataDistrib._freq.empty()) { return {}; @@ -230,53 +279,6 @@ ScalarHistogram genMaxDiffHistogram(const DataDistribution& dataDistrib, size_t return {std::move(bounds), std::move(buckets)}; } -/** - * Helper for getting the input for constructing an array histogram for an array estimator using the - * values in an array. For each value in `arrayElements`, update the min, max, and unique value - * vectors. These will be used to generate the corresponding histograms for array values. - */ -void updateMinMaxUniqArrayVals(std::vector<SBEValue>& arrayElements, - std::vector<SBEValue>& arrayMinData, - std::vector<SBEValue>& arrayMaxData, - std::vector<SBEValue>& arrayUniqueData) { - - if (arrayElements.size() == 0) { - return; - } - - sortValueVector(arrayElements); - - // Emit values for arrayMin and arrayMax histograms. - { - boost::optional<SBEValue> prev; - for (const auto& element : arrayElements) { - if (!prev) { - arrayMinData.push_back(element); - } else if (!sameTypeClass(prev->getTag(), element.getTag())) { - arrayMaxData.push_back(*prev); - arrayMinData.push_back(element); - } - prev = element; - } - if (prev) { - arrayMaxData.push_back(*prev); - } - } - - // Emit values for arrayUnique histogram. - { - boost::optional<SBEValue> prev; - for (const auto& element : arrayElements) { - if (!prev || - compareValues( - prev->getTag(), prev->getValue(), element.getTag(), element.getValue()) < 0) { - arrayUniqueData.push_back(element); - prev = element; - } - } - } -} - ArrayHistogram createArrayEstimator(const std::vector<SBEValue>& arrayData, size_t nBuckets) { // Values that will be used as inputs to histogram generation code. std::vector<SBEValue> scalarData; @@ -373,4 +375,4 @@ ArrayHistogram createArrayEstimator(const std::vector<SBEValue>& arrayData, size falseCount}; } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/max_diff.h b/src/mongo/db/query/stats/max_diff.h index ab69f7001eb..147cb35af8d 100644 --- a/src/mongo/db/query/ce/max_diff.h +++ b/src/mongo/db/query/stats/max_diff.h @@ -33,11 +33,11 @@ #include <vector> #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/scalar_histogram.h" -#include "mongo/db/query/ce/value_utils.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/scalar_histogram.h" +#include "mongo/db/query/stats/value_utils.h" -namespace mongo::ce { +namespace mongo::stats { struct ValFreq { ValFreq(size_t idx, size_t freq) : _idx(idx), _freq(freq), _area(-1.0), _normArea(-1) {} @@ -79,4 +79,4 @@ ScalarHistogram genMaxDiffHistogram(const DataDistribution& dataDistrib, size_t */ ArrayHistogram createArrayEstimator(const std::vector<SBEValue>& arrayData, size_t nBuckets); -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/maxdiff_test_utils.cpp b/src/mongo/db/query/stats/maxdiff_test_utils.cpp index b27cbb6ec93..cb0e66dc285 100644 --- a/src/mongo/db/query/ce/maxdiff_test_utils.cpp +++ b/src/mongo/db/query/stats/maxdiff_test_utils.cpp @@ -27,25 +27,25 @@ * it in the license file. */ -#include "mongo/db/query/ce/maxdiff_test_utils.h" +#include "mongo/db/query/stats/maxdiff_test_utils.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/max_diff.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/max_diff.h" -namespace mongo::ce { +namespace mongo::stats { static std::vector<std::string> convertToJSON(const std::vector<SBEValue>& input) { std::vector<std::string> result; for (size_t i = 0; i < input.size(); i++) { - const auto [objTag, objVal] = value::makeNewObject(); - value::ValueGuard vg(objTag, objVal); + const auto [objTag, objVal] = sbe::value::makeNewObject(); + sbe::value::ValueGuard vg(objTag, objVal); const auto [tag, val] = input[i].get(); // Copy the value because objVal owns its value, and the ValueGuard releases not only // objVal, but also its Value (in the case below - copyVal). - const auto [copyTag, copyVal] = value::copyValue(tag, val); - value::getObjectView(objVal)->push_back("a", copyTag, copyVal); + const auto [copyTag, copyVal] = sbe::value::copyValue(tag, val); + sbe::value::getObjectView(objVal)->push_back("a", copyTag, copyVal); std::ostringstream os; os << std::make_pair(objTag, objVal); @@ -61,9 +61,9 @@ size_t getActualCard(OperationContext* opCtx, return mongo::optimizer::runPipeline(opCtx, query, convertToJSON(input)).size(); } -std::string makeMatchExpr(const SBEValue& val, EstimationType cmpOp) { +std::string makeMatchExpr(const SBEValue& val, optimizer::ce::EstimationType cmpOp) { std::stringstream matchExpr; - std::string cmpOpName = estimationTypeName.at(cmpOp); + std::string cmpOpName = optimizer::ce::estimationTypeName.at(cmpOp); matchExpr << "[{$match: {a: {$" << cmpOpName << ": " << val.get() << "}}}]"; return matchExpr.str(); } @@ -117,4 +117,4 @@ std::string plotArrayEstimator(const ArrayHistogram& estimator, const std::strin return os.str(); } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/maxdiff_test_utils.h b/src/mongo/db/query/stats/maxdiff_test_utils.h index 4ea1244da02..a34f7dd41ee 100644 --- a/src/mongo/db/query/ce/maxdiff_test_utils.h +++ b/src/mongo/db/query/stats/maxdiff_test_utils.h @@ -32,16 +32,14 @@ #include <string> #include <vector> - #include "mongo/db/exec/sbe/abt/sbe_abt_test_util.h" #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/histogram_estimation.h" -#include "mongo/db/query/ce/scalar_histogram.h" -#include "mongo/db/query/ce/value_utils.h" - -namespace mongo::ce { +#include "mongo/db/query/ce/histogram_predicate_estimation.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/scalar_histogram.h" +#include "mongo/db/query/stats/value_utils.h" -class ArrayHistogram; +namespace mongo::stats { /** Given a list of SBE values and a query, create a collection containing the data, @@ -55,7 +53,7 @@ size_t getActualCard(OperationContext* opCtx, Given a value and a comparison operator, generate a match expression reflecting x cmpOp val. */ -std::string makeMatchExpr(const SBEValue& val, EstimationType cmpOp); +std::string makeMatchExpr(const SBEValue& val, optimizer::ce::EstimationType cmpOp); /** Given a vector of values, create a histogram reflection the distribution of the vector @@ -73,4 +71,4 @@ std::string printValueArray(const std::vector<SBEValue>& values); */ std::string plotArrayEstimator(const ArrayHistogram& estimator, const std::string& header); -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/rand_utils.cpp b/src/mongo/db/query/stats/rand_utils.cpp index 7f317904298..ff66272a681 100644 --- a/src/mongo/db/query/ce/rand_utils.cpp +++ b/src/mongo/db/query/stats/rand_utils.cpp @@ -27,17 +27,18 @@ * it in the license file. */ +#include "mongo/db/query/stats/rand_utils.h" + #include <algorithm> #include <cstddef> #include <string> #include <vector> -#include "mongo/db/query/ce/rand_utils.h" - #include "mongo/db/exec/sbe/values/value.h" #include "mongo/util/assert_util.h" -namespace mongo::ce { +namespace mongo::stats { +namespace value = sbe::value; const std::string DatasetDescriptor::_alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -388,4 +389,4 @@ std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input, size_t empt return result; } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/rand_utils.h b/src/mongo/db/query/stats/rand_utils.h index 9a9acad5161..89e4741fd2a 100644 --- a/src/mongo/db/query/ce/rand_utils.h +++ b/src/mongo/db/query/stats/rand_utils.h @@ -32,14 +32,11 @@ #include <random> #include <vector> -#include "value_utils.h" - -namespace mongo::ce { - -class SBEValue; +#include "mongo/db/query/stats/value_utils.h" +namespace mongo::stats { // A simple histogram describing the distribution of values of each data type. -using DataTypeDistribution = std::map<value::TypeTags, double>; +using DataTypeDistribution = std::map<sbe::value::TypeTags, double>; /** Describes the distribution of a dataset according to type and weight. Other ctor parameters @@ -67,7 +64,7 @@ public: private: // Select a random value data type. - value::TypeTags getRandDataType() { + sbe::value::TypeTags getRandDataType() { double key = _uniformRandProbability(_gen); return (*_dataTypeDistribution.upper_bound(key)).second; } @@ -82,7 +79,7 @@ private: void fillRandomArraySet(); private: - using InternalDataTypeDistribution = std::map<double, value::TypeTags>; + using InternalDataTypeDistribution = std::map<double, sbe::value::TypeTags>; /* * General distribution charecteristics. */ @@ -140,7 +137,7 @@ private: std::shared_ptr<DatasetDescriptor> _nestedDataDescriptor; // Generator of random indexes into the set of arrays '_arraySet'. std::uniform_int_distribution<size_t> _uniformArrIdxDist; -}; // namespace mongo::ce +}; /** Generate a pseudorandom string of length n @@ -188,4 +185,4 @@ std::vector<SBEValue> genRandomValueArray(size_t nElems, */ std::vector<SBEValue> nestArrays(const std::vector<SBEValue>& input, size_t emptyArrayCount); -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/rand_utils_new.cpp b/src/mongo/db/query/stats/rand_utils_new.cpp index 038e69dde04..a8e8fab3bb8 100644 --- a/src/mongo/db/query/ce/rand_utils_new.cpp +++ b/src/mongo/db/query/stats/rand_utils_new.cpp @@ -27,18 +27,19 @@ * it in the license file. */ +#include "mongo/db/query/stats/rand_utils_new.h" + #include <algorithm> #include <cstddef> #include <string> #include <unordered_map> #include <vector> -#include "mongo/db/query/ce/rand_utils_new.h" - #include "mongo/db/exec/sbe/values/value.h" #include "mongo/util/assert_util.h" -namespace mongo::ce { +namespace mongo::stats { +namespace value = sbe::value; const std::string StrDistribution::_alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -246,4 +247,4 @@ std::vector<SBEValue> DatasetDescriptorNew::genRandomDataset(size_t nElems) { } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/rand_utils_new.h b/src/mongo/db/query/stats/rand_utils_new.h index 0420b990dce..be77578fc28 100644 --- a/src/mongo/db/query/ce/rand_utils_new.h +++ b/src/mongo/db/query/stats/rand_utils_new.h @@ -32,11 +32,10 @@ #include <random> #include <vector> -#include "value_utils.h" +#include "mongo/db/query/stats/value_utils.h" -namespace mongo::ce { +namespace mongo::stats { -class SBEValue; class DatasetDescriptorNew; /** @@ -180,7 +179,7 @@ private: class DataTypeDistrNew { public: DataTypeDistrNew(MixedDistributionDescriptor distrDescriptor, - value::TypeTags tag, + sbe::value::TypeTags tag, double weight, size_t ndv, double nullsRatio = 0.0) @@ -211,7 +210,7 @@ public: /** * Generate a single random value, and store it in 'randValueArray' array. */ - void generate(value::Array* randValueArray, std::mt19937_64& gen); + void generate(sbe::value::Array* randValueArray, std::mt19937_64& gen); /** * Custom equality comparison for storage in sets. There can be only datatype in a set. @@ -220,7 +219,7 @@ public: return this->_tag == d._tag; } - value::TypeTags tag() const { + sbe::value::TypeTags tag() const { return _tag; } @@ -230,7 +229,7 @@ public: protected: MixedDistributionDescriptor _mixedDistrDescriptor; - value::TypeTags _tag; + sbe::value::TypeTags _tag; // Weight that determines the probability of a value of this type. const double _weight; const size_t _ndv; @@ -351,4 +350,4 @@ private: friend class ArrDistribution; }; -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/scalar_histogram.cpp b/src/mongo/db/query/stats/scalar_histogram.cpp index 604af42da13..87ab175fe83 100644 --- a/src/mongo/db/query/ce/scalar_histogram.cpp +++ b/src/mongo/db/query/stats/scalar_histogram.cpp @@ -27,14 +27,12 @@ * it in the license file. */ -#include "mongo/db/query/ce/scalar_histogram.h" +#include "mongo/db/query/stats/scalar_histogram.h" + #include "mongo/db/exec/sbe/values/bson.h" #include "mongo/db/exec/sbe/values/value.h" -namespace mongo::ce { - -using namespace sbe; - +namespace mongo::stats { Bucket::Bucket( double equalFreq, double rangeFreq, double cumulativeFreq, double ndv, double cumulativeNDV) : _equalFreq(equalFreq), @@ -95,7 +93,7 @@ ScalarHistogram::ScalarHistogram(const StatsHistogram& histogram) { } } -ScalarHistogram::ScalarHistogram(value::Array bounds, std::vector<Bucket> buckets) +ScalarHistogram::ScalarHistogram(sbe::value::Array bounds, std::vector<Bucket> buckets) : _bounds(std::move(bounds)), _buckets(std::move(buckets)) { uassert(6695707, "Invalid sizes", bounds.size() == buckets.size()); } @@ -162,7 +160,7 @@ std::string ScalarHistogram::dump() const { return os.str(); } -const value::Array& ScalarHistogram::getBounds() const { +const sbe::value::Array& ScalarHistogram::getBounds() const { return _bounds; } @@ -191,4 +189,4 @@ BSONObj ScalarHistogram::serialize() const { return histogramBuilder.obj(); } -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/scalar_histogram.h b/src/mongo/db/query/stats/scalar_histogram.h index c368e0dd50b..0473f369af1 100644 --- a/src/mongo/db/query/ce/scalar_histogram.h +++ b/src/mongo/db/query/stats/scalar_histogram.h @@ -34,9 +34,9 @@ #include <vector> #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/stats_gen.h" +#include "mongo/db/query/stats/stats_gen.h" -namespace mongo::ce { +namespace mongo::stats { /** * Statistics related to a single ScalarHistogram bucket. The boundary value is kept in a separate @@ -117,4 +117,4 @@ private: std::vector<Bucket> _buckets; }; -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats.idl b/src/mongo/db/query/stats/stats.idl index eb6220d45b9..eb6220d45b9 100644 --- a/src/mongo/db/query/ce/stats.idl +++ b/src/mongo/db/query/stats/stats.idl diff --git a/src/mongo/db/query/ce/stats_cache.cpp b/src/mongo/db/query/stats/stats_cache.cpp index 2fb2be400a6..dfe5a43890e 100644 --- a/src/mongo/db/query/ce/stats_cache.cpp +++ b/src/mongo/db/query/stats/stats_cache.cpp @@ -27,26 +27,18 @@ * it in the license file. */ +#include "mongo/db/query/stats/stats_cache.h" -#include "mongo/platform/basic.h" - -#include "mongo/db/query/ce/stats_cache.h" - -#include "mongo/db/query/ce/collection_statistics.h" +#include "mongo/db/query/stats/collection_statistics.h" #include "mongo/util/read_through_cache.h" #include "mongo/logv2/log.h" #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery - -namespace mongo { -using namespace mongo::ce; - +namespace mongo::stats { namespace { - const auto statsCacheDecoration = ServiceContext::declareDecoration<std::unique_ptr<StatsCache>>(); - } // namespace StatsCache::StatsCache(ServiceContext* service, @@ -79,4 +71,4 @@ StatsCache::LookupResult StatsCache::_lookupStats(OperationContext* opCtx, } } -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache.h b/src/mongo/db/query/stats/stats_cache.h index 65b5bcd19b4..37d3d238a4d 100644 --- a/src/mongo/db/query/ce/stats_cache.h +++ b/src/mongo/db/query/stats/stats_cache.h @@ -31,15 +31,12 @@ #include "mongo/base/string_data.h" #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/collection_statistics.h" -#include "mongo/db/query/ce/stats_cache_loader.h" +#include "mongo/db/query/stats/collection_statistics.h" +#include "mongo/db/query/stats/stats_cache_loader.h" #include "mongo/util/concurrency/thread_pool.h" #include "mongo/util/read_through_cache.h" -namespace mongo { - -using namespace mongo::ce; - +namespace mongo::stats { using StatsCacheType = ReadThroughCache<StatsPathString, StatsCacheVal>; using StatsCacheValueHandle = StatsCacheType::ValueHandle; @@ -81,4 +78,4 @@ private: std::unique_ptr<StatsCacheLoader> _statsCacheLoader; }; -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader.h b/src/mongo/db/query/stats/stats_cache_loader.h index a6ba3935c43..7bad4b64304 100644 --- a/src/mongo/db/query/ce/stats_cache_loader.h +++ b/src/mongo/db/query/stats/stats_cache_loader.h @@ -30,13 +30,10 @@ #pragma once #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/array_histogram.h" +#include "mongo/db/query/stats/array_histogram.h" #include "mongo/stdx/thread.h" -namespace mongo { - -using namespace mongo::ce; - +namespace mongo::stats { using StatsPathString = std::pair<NamespaceString, std::string>; using StatsCacheVal = std::shared_ptr<ArrayHistogram>; @@ -58,4 +55,4 @@ public: static constexpr StringData kStatsPrefix = "system.statistics"_sd; }; -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_impl.cpp b/src/mongo/db/query/stats/stats_cache_loader_impl.cpp index bd4d54c4e17..e41912eafc0 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_impl.cpp +++ b/src/mongo/db/query/stats/stats_cache_loader_impl.cpp @@ -27,21 +27,17 @@ * it in the license file. */ - -#include "mongo/platform/basic.h" - -#include "mongo/db/query/ce/stats_cache_loader_impl.h" +#include "mongo/db/query/stats/stats_cache_loader_impl.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/stats_gen.h" +#include "mongo/db/query/stats/stats_gen.h" #include "mongo/logv2/log.h" #include "mongo/stdx/thread.h" #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery -namespace mongo { - +namespace mongo::stats { SemiFuture<StatsCacheVal> StatsCacheLoaderImpl::getStats(OperationContext* opCtx, const StatsPathString& statsPath) { @@ -83,4 +79,4 @@ SemiFuture<StatsCacheVal> StatsCacheLoaderImpl::getStats(OperationContext* opCtx MONGO_UNREACHABLE } -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_impl.h b/src/mongo/db/query/stats/stats_cache_loader_impl.h index b461d1d51c6..979a1009acb 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_impl.h +++ b/src/mongo/db/query/stats/stats_cache_loader_impl.h @@ -30,13 +30,11 @@ #pragma once #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/collection_statistics.h" -#include "mongo/db/query/ce/stats_cache_loader.h" +#include "mongo/db/query/stats/collection_statistics.h" +#include "mongo/db/query/stats/stats_cache_loader.h" #include "mongo/stdx/thread.h" -namespace mongo { - -using namespace mongo::ce; +namespace mongo::stats { class StatsCacheLoaderImpl : public StatsCacheLoader { public: @@ -44,4 +42,4 @@ public: const StatsPathString& statsPath) override; }; -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_mock.cpp b/src/mongo/db/query/stats/stats_cache_loader_mock.cpp index ddf343bd026..c190d61c312 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_mock.cpp +++ b/src/mongo/db/query/stats/stats_cache_loader_mock.cpp @@ -27,16 +27,13 @@ * it in the license file. */ - -#include "mongo/platform/basic.h" - -#include "mongo/db/query/ce/stats_cache_loader_mock.h" +#include "mongo/db/query/stats/stats_cache_loader_mock.h" #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/collection_statistics.h" +#include "mongo/db/query/stats/collection_statistics.h" #include "mongo/stdx/thread.h" -namespace mongo { +namespace mongo::stats { const Status StatsCacheLoaderMock::kInternalErrorStatus = { ErrorCodes::InternalError, "Stats cache loader received unexpected request"}; @@ -50,4 +47,4 @@ SemiFuture<StatsCacheVal> StatsCacheLoaderMock::getStats(OperationContext* opCtx void StatsCacheLoaderMock::setStatsReturnValueForTest(StatusWith<StatsCacheVal> swStats) { _swStatsReturnValueForTest = std::move(swStats); } -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_mock.h b/src/mongo/db/query/stats/stats_cache_loader_mock.h index 0b105d5858a..9951bcfd2ca 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_mock.h +++ b/src/mongo/db/query/stats/stats_cache_loader_mock.h @@ -30,13 +30,11 @@ #pragma once #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/collection_statistics.h" -#include "mongo/db/query/ce/stats_cache_loader.h" +#include "mongo/db/query/stats/collection_statistics.h" +#include "mongo/db/query/stats/stats_cache_loader.h" #include "mongo/stdx/thread.h" -namespace mongo { - -using namespace mongo::ce; +namespace mongo::stats { class StatsCacheLoaderMock : public StatsCacheLoader { public: @@ -51,4 +49,4 @@ private: StatusWith<StatsCacheVal> _swStatsReturnValueForTest{kInternalErrorStatus}; }; -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_test.cpp b/src/mongo/db/query/stats/stats_cache_loader_test.cpp index 9fc003e524a..a22e6dd9044 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_test.cpp +++ b/src/mongo/db/query/stats/stats_cache_loader_test.cpp @@ -30,15 +30,15 @@ #include "mongo/bson/oid.h" #include "mongo/db/catalog/collection_write_path.h" #include "mongo/db/db_raii.h" -#include "mongo/db/query/ce/scalar_histogram.h" -#include "mongo/db/query/ce/stats_cache_loader_impl.h" -#include "mongo/db/query/ce/stats_cache_loader_test_fixture.h" -#include "mongo/db/query/ce/stats_gen.h" +#include "mongo/db/query/stats/scalar_histogram.h" +#include "mongo/db/query/stats/stats_cache_loader_impl.h" +#include "mongo/db/query/stats/stats_cache_loader_test_fixture.h" +#include "mongo/db/query/stats/stats_gen.h" #include "mongo/unittest/unittest.h" #include "mongo/util/assert_util.h" #include "mongo/util/fail_point.h" -namespace mongo { +namespace mongo::stats { namespace { class StatsCacheLoaderTest : public StatsCacheLoaderTestFixture { @@ -62,10 +62,10 @@ TEST_F(StatsCacheLoaderTest, VerifyStatsLoad) { constexpr double trueCount = 12.0; constexpr double falseCount = 16.0; constexpr double numDocs = doubleCount + trueCount + falseCount; - std::vector<ce::Bucket> buckets{ - ce::Bucket{1.0, 0.0, 1.0, 0.0, 1.0}, - ce::Bucket{2.0, 5.0, 8.0, 1.0, 2.0}, - ce::Bucket{3.0, 4.0, 15.0, 2.0, 6.0}, + std::vector<Bucket> buckets{ + Bucket{1.0, 0.0, 1.0, 0.0, 1.0}, + Bucket{2.0, 5.0, 8.0, 1.0, 2.0}, + Bucket{3.0, 4.0, 15.0, 2.0, 6.0}, }; // Initialize histogram bounds. @@ -77,12 +77,12 @@ TEST_F(StatsCacheLoaderTest, VerifyStatsLoad) { bounds->push_back(sbe::value::TypeTags::NumberDouble, 3.0); // Create a scalar histogram. - ce::TypeCounts tc{ + TypeCounts tc{ {sbe::value::TypeTags::NumberDouble, doubleCount}, {sbe::value::TypeTags::Boolean, trueCount + falseCount}, }; - ce::ScalarHistogram sh(*bounds, buckets); - ce::ArrayHistogram ah(sh, tc, trueCount, falseCount); + ScalarHistogram sh(*bounds, buckets); + ArrayHistogram ah(sh, tc, trueCount, falseCount); auto expectedSerialized = ah.serialize(); // Serialize histogram into a stats path. @@ -113,4 +113,4 @@ TEST_F(StatsCacheLoaderTest, VerifyStatsLoad) { } } // namespace -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_test_fixture.cpp b/src/mongo/db/query/stats/stats_cache_loader_test_fixture.cpp index 20510a19203..1e353196b83 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_test_fixture.cpp +++ b/src/mongo/db/query/stats/stats_cache_loader_test_fixture.cpp @@ -27,17 +27,15 @@ * it in the license file. */ -#include "mongo/platform/basic.h" +#include "mongo/db/query/stats/stats_cache_loader_test_fixture.h" #include <memory> -#include "mongo/db/query/ce/stats_cache_loader_test_fixture.h" - #include "mongo/db/repl/replication_coordinator_mock.h" #include "mongo/db/repl/storage_interface_impl.h" #include "mongo/db/service_context_d_test_fixture.h" -namespace mongo { +namespace mongo::stats { void StatsCacheLoaderTestFixture::setUp() { // Set up mongod. @@ -73,4 +71,4 @@ repl::StorageInterface* StatsCacheLoaderTestFixture::storageInterface() { return _storage.get(); } -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_loader_test_fixture.h b/src/mongo/db/query/stats/stats_cache_loader_test_fixture.h index 6ffc992b9f8..6c7d502fdf1 100644 --- a/src/mongo/db/query/ce/stats_cache_loader_test_fixture.h +++ b/src/mongo/db/query/stats/stats_cache_loader_test_fixture.h @@ -30,11 +30,11 @@ #pragma once #include "mongo/db/operation_context.h" -#include "mongo/db/query/ce/stats_cache_loader.h" +#include "mongo/db/query/stats/stats_cache_loader.h" #include "mongo/db/repl/storage_interface_impl.h" #include "mongo/db/service_context_d_test_fixture.h" -namespace mongo { +namespace mongo::stats { /** * Sets up and provides a repl::StorageInterface and OperationContext. @@ -57,4 +57,4 @@ private: std::unique_ptr<repl::StorageInterface> _storage; }; -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_cache_test.cpp b/src/mongo/db/query/stats/stats_cache_test.cpp index 4e92a9ea2ca..b95dc2c3bd8 100644 --- a/src/mongo/db/query/ce/stats_cache_test.cpp +++ b/src/mongo/db/query/stats/stats_cache_test.cpp @@ -27,15 +27,13 @@ * it in the license file. */ -#include "mongo/platform/basic.h" - #include <string> #include "mongo/db/client.h" #include "mongo/db/concurrency/locker_noop_service_context_test_fixture.h" #include "mongo/db/operation_context.h" -#include "mongo/db/query/ce/stats_cache.h" -#include "mongo/db/query/ce/stats_cache_loader_mock.h" +#include "mongo/db/query/stats/stats_cache.h" +#include "mongo/db/query/stats/stats_cache_loader_mock.h" #include "mongo/unittest/barrier.h" #include "mongo/unittest/unittest.h" #include "mongo/util/concurrency/thread_pool.h" @@ -44,7 +42,7 @@ #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault -namespace mongo { +namespace mongo::stats { namespace { using unittest::assertGet; @@ -130,4 +128,4 @@ TEST_F(StatsCacheTest, LoadStats) { */ } // namespace -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_catalog.cpp b/src/mongo/db/query/stats/stats_catalog.cpp index d8b65d09e72..99891f1dc4c 100644 --- a/src/mongo/db/query/ce/stats_catalog.cpp +++ b/src/mongo/db/query/stats/stats_catalog.cpp @@ -27,28 +27,21 @@ * it in the license file. */ +#include "mongo/db/query/stats/stats_catalog.h" -#include "mongo/platform/basic.h" - -#include "mongo/db/query/ce/stats_cache.h" -#include "mongo/db/query/ce/stats_catalog.h" - -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/collection_statistics.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/collection_statistics.h" +#include "mongo/db/query/stats/stats_cache.h" #include "mongo/util/read_through_cache.h" #include "mongo/logv2/log.h" #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery -namespace mongo { -using namespace mongo::ce; - +namespace mongo::stats { namespace { - const auto statsCatalogDecoration = ServiceContext::declareDecoration<std::unique_ptr<StatsCatalog>>(); - } // namespace StatsCatalog::StatsCatalog(ServiceContext* service, @@ -112,4 +105,4 @@ Status StatsCatalog::invalidatePath(const NamespaceString& nss, const std::strin return ex.toStatus(); } } -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_catalog.h b/src/mongo/db/query/stats/stats_catalog.h index efd53178c94..e86b4562b8e 100644 --- a/src/mongo/db/query/ce/stats_catalog.h +++ b/src/mongo/db/query/stats/stats_catalog.h @@ -31,15 +31,12 @@ #include "mongo/base/string_data.h" #include "mongo/db/namespace_string.h" -#include "mongo/db/query/ce/collection_statistics.h" -#include "mongo/db/query/ce/stats_cache.h" -#include "mongo/db/query/ce/stats_cache_loader.h" +#include "mongo/db/query/stats/collection_statistics.h" +#include "mongo/db/query/stats/stats_cache.h" +#include "mongo/db/query/stats/stats_cache_loader.h" #include "mongo/util/concurrency/thread_pool.h" -namespace mongo { - -using namespace mongo::ce; - +namespace mongo::stats { /** * This class owns statsCache and manages executor lifetime. */ @@ -77,4 +74,4 @@ private: StatsCache _statsCache; }; -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/stats_path_test.cpp b/src/mongo/db/query/stats/stats_path_test.cpp index c4ed743e987..3e3afe50b32 100644 --- a/src/mongo/db/query/ce/stats_path_test.cpp +++ b/src/mongo/db/query/stats/stats_path_test.cpp @@ -27,18 +27,16 @@ * it in the license file. */ -#include "mongo/platform/basic.h" - #include "mongo/bson/bsonobjbuilder.h" #include "mongo/db/exec/sbe/values/bson.h" #include "mongo/db/exec/sbe/values/value.h" -#include "mongo/db/query/ce/array_histogram.h" -#include "mongo/db/query/ce/scalar_histogram.h" -#include "mongo/db/query/ce/stats_gen.h" +#include "mongo/db/query/stats/array_histogram.h" +#include "mongo/db/query/stats/scalar_histogram.h" +#include "mongo/db/query/stats/stats_gen.h" #include "mongo/unittest/unittest.h" #include "mongo/util/assert_util.h" -namespace mongo { +namespace mongo::stats { namespace { IDLParserContext ctx("StatsPath"); @@ -48,7 +46,7 @@ IDLParserContext ctx("StatsPath"); */ TEST(StatsPath, BasicValidStatsBucketDouble) { // Create & parse StatsBucket. - auto serializedBucket = ce::Bucket{3.0, 4.0, 15.0, 2.0, 6.0}.serialize(); + auto serializedBucket = Bucket{3.0, 4.0, 15.0, 2.0, 6.0}.serialize(); auto parsedBucket = StatsBucket::parse(ctx, serializedBucket); // Round-trip conversion. @@ -65,10 +63,10 @@ TEST(StatsPath, BasicValidStatsPath) { constexpr double trueCount = 12.0; constexpr double falseCount = 16.0; constexpr double numDocs = doubleCount + trueCount + falseCount; - std::vector<ce::Bucket> buckets{ - ce::Bucket{1.0, 0.0, 1.0, 0.0, 1.0}, - ce::Bucket{2.0, 5.0, 8.0, 1.0, 2.0}, - ce::Bucket{3.0, 4.0, 15.0, 2.0, 6.0}, + std::vector<Bucket> buckets{ + Bucket{1.0, 0.0, 1.0, 0.0, 1.0}, + Bucket{2.0, 5.0, 8.0, 1.0, 2.0}, + Bucket{3.0, 4.0, 15.0, 2.0, 6.0}, }; // Initialize histogram bounds. @@ -80,12 +78,12 @@ TEST(StatsPath, BasicValidStatsPath) { bounds->push_back(sbe::value::TypeTags::NumberDouble, 3.0); // Create a scalar histogram. - ce::TypeCounts tc{ + TypeCounts tc{ {sbe::value::TypeTags::NumberDouble, doubleCount}, {sbe::value::TypeTags::Boolean, trueCount + falseCount}, }; - ce::ScalarHistogram sh(*bounds, buckets); - ce::ArrayHistogram ah(sh, tc, trueCount, falseCount); + ScalarHistogram sh(*bounds, buckets); + ArrayHistogram ah(sh, tc, trueCount, falseCount); // Serialize to BSON. auto serializedPath = stats::makeStatsPath("somePath", numDocs, ah); @@ -104,7 +102,7 @@ TEST(StatsPath, BasicValidStatsPath) { TEST(StatsPath, BasicValidEmptyStatsPath) { // Initialize histogram buckets. constexpr double numDocs = 0.0; - std::vector<ce::Bucket> buckets; + std::vector<Bucket> buckets; // Initialize histogram bounds. auto [boundsTag, boundsVal] = sbe::value::makeNewArray(); @@ -112,9 +110,9 @@ TEST(StatsPath, BasicValidEmptyStatsPath) { auto bounds = sbe::value::getArrayView(boundsVal); // Create an empty scalar histogram. - ce::TypeCounts tc; - ce::ScalarHistogram sh(*bounds, buckets); - ce::ArrayHistogram ah(sh, tc); + TypeCounts tc; + ScalarHistogram sh(*bounds, buckets); + ArrayHistogram ah(sh, tc); // Serialize to BSON. auto serializedPath = stats::makeStatsPath("someEmptyPath", numDocs, ah); @@ -128,4 +126,4 @@ TEST(StatsPath, BasicValidEmptyStatsPath) { } } // namespace -} // namespace mongo +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/value_utils.cpp b/src/mongo/db/query/stats/value_utils.cpp index 46e3f143b16..5af0f1c248c 100644 --- a/src/mongo/db/query/ce/value_utils.cpp +++ b/src/mongo/db/query/stats/value_utils.cpp @@ -27,14 +27,12 @@ * it in the license file. */ -#include "mongo/db/query/ce/value_utils.h" +#include "mongo/db/query/stats/value_utils.h" -#include "mongo/db/query/ce/max_diff.h" -#include "mongo/db/query/ce/scalar_histogram.h" +#include "mongo/db/query/stats/scalar_histogram.h" -namespace mongo::ce { - -using namespace sbe; +namespace mongo::stats { +namespace value = sbe::value; SBEValue::SBEValue(value::TypeTags tag, value::Value val) : _tag(tag), _val(val) {} @@ -249,6 +247,6 @@ value::TypeTags deserialize(const std::string& name) { // Trying to deserialize any other types should result in an error. uasserted(6660600, str::stream() << "String " << name << " is not convertable to SBE type tag."); -} // namespace mongo::ce +} -} // namespace mongo::ce +} // namespace mongo::stats diff --git a/src/mongo/db/query/ce/value_utils.h b/src/mongo/db/query/stats/value_utils.h index 0191b4e2c26..d79417ea724 100644 --- a/src/mongo/db/query/ce/value_utils.h +++ b/src/mongo/db/query/stats/value_utils.h @@ -31,18 +31,15 @@ #include "mongo/db/exec/sbe/values/value.h" -namespace mongo::ce { - -using namespace sbe; - +namespace mongo::stats { /** Container object for SBE value/tag pairs. Supplied values are owned by this object and are released on destruction */ class SBEValue { public: - SBEValue(value::TypeTags tag, value::Value val); - SBEValue(std::pair<value::TypeTags, value::Value> v); + SBEValue(sbe::value::TypeTags tag, sbe::value::Value val); + SBEValue(std::pair<sbe::value::TypeTags, sbe::value::Value> v); ~SBEValue(); SBEValue(const SBEValue& other); @@ -51,36 +48,36 @@ public: SBEValue& operator=(const SBEValue& other); SBEValue& operator=(SBEValue&& other); - std::pair<value::TypeTags, value::Value> get() const; - value::TypeTags getTag() const; - value::Value getValue() const; + std::pair<sbe::value::TypeTags, sbe::value::Value> get() const; + sbe::value::TypeTags getTag() const; + sbe::value::Value getValue() const; private: - value::TypeTags _tag; - value::Value _val; + sbe::value::TypeTags _tag; + sbe::value::Value _val; }; /** Generate an SBE Value pair that represents the supplied int with type Int64 */ -std::pair<value::TypeTags, value::Value> makeInt64Value(int v); +std::pair<sbe::value::TypeTags, sbe::value::Value> makeInt64Value(int v); /** Generate an SBE Value pair representing a BSON null value */ -std::pair<value::TypeTags, value::Value> makeNullValue(); +std::pair<sbe::value::TypeTags, sbe::value::Value> makeNullValue(); /** Do the supplied type tags represent the same BSON type? */ -bool sameTypeClass(value::TypeTags tag1, value::TypeTags tag2); +bool sameTypeClass(sbe::value::TypeTags tag1, sbe::value::TypeTags tag2); /** Do the supplied type tags represent the same BSON type? TODO: This may be the same as sameTypeClass. @timourk? */ -bool sameTypeBracket(value::TypeTags tag1, value::TypeTags tag2); +bool sameTypeBracket(sbe::value::TypeTags tag1, sbe::value::TypeTags tag2); /** Compare a pair of SBE values. @@ -90,10 +87,10 @@ bool sameTypeBracket(value::TypeTags tag1, value::TypeTags tag2); 0 if val1 == val2 in BSON order >0 if val1 > val2 in BSON order */ -int32_t compareValues(value::TypeTags tag1, - value::Value val1, - value::TypeTags tag2, - value::Value val2); +int32_t compareValues(sbe::value::TypeTags tag1, + sbe::value::Value val1, + sbe::value::TypeTags tag2, + sbe::value::Value val2); /** Sort a vector of values in place in BSON order @@ -104,7 +101,7 @@ void sortValueVector(std::vector<SBEValue>& sortVector); Convert a value of any supported type into a double according to some metric. This metric will be consistent with ordering in the type. */ -double valueToDouble(value::TypeTags tag, value::Value val); +double valueToDouble(sbe::value::TypeTags tag, sbe::value::Value val); /** * Returns true for types that can be estimated via histograms, and false for types that need type @@ -112,12 +109,12 @@ double valueToDouble(value::TypeTags tag, value::Value val); * * NOTE: This should be kept in sync with 'valueToDouble' above. */ -bool canEstimateTypeViaHistogram(value::TypeTags tag); +bool canEstimateTypeViaHistogram(sbe::value::TypeTags tag); /** * Serialize/Deserialize a TypeTag to a string for TypeCount storage in the stats collection. */ -std::string serialize(value::TypeTags tag); -value::TypeTags deserialize(const std::string& name); +std::string serialize(sbe::value::TypeTags tag); +sbe::value::TypeTags deserialize(const std::string& name); -} // namespace mongo::ce +} // namespace mongo::stats |