Files
mongo/jstests/libs/query/cbr_utils.js
karlbozdogan 878d1f1c63 SERVER-117209 Expose CBR related query knobs for IFR (#46526)
GitOrigin-RevId: c6f10cb35025385fe61348109a78d1996ed3e08c
2026-02-05 10:11:21 +00:00

119 lines
3.6 KiB
JavaScript

/**
* Utilities for writing tests for the cost-based ranker (CBR).
*/
// Round the given number to the nearest 0.1
function round(num) {
return Math.round(num * 10) / 10;
}
// Compare two cardinality estimates with approximate equality. This is done so that our tests can
// be robust to floating point rounding differences but detect large changes in estimation
// indicating a real change in estimation behavior.
export function ceEqual(lhs, rhs) {
return Math.abs(round(lhs) - round(rhs)) < 0.1;
}
function planEstimateTypeIs(plan, type) {
return plan.estimatesMetadata.ceSource === type;
}
// Return true if the plan was estimated with a histogam estimation source. Return false otherwise.
export function planEstimatedWithHistogram(plan) {
return planEstimateTypeIs(plan, "Histogram");
}
/**
* Returns whether the given plan was costed or not.
*/
export function isPlanCosted(plan) {
return plan.hasOwnProperty("costEstimate");
}
/**
* Assert the given plan was not costed. This is used as an indicator to whether CBR used its
* fallback to multiplanning for this plan.
*/
export function assertPlanNotCosted(plan) {
assert(!isPlanCosted(plan), plan);
}
/**
* Assert the given plan was costed.
*/
export function assertPlanCosted(plan) {
assert(isPlanCosted(plan), plan);
}
export function getPlanRankerMode(db) {
if (db !== null) {
const getParam = db.adminCommand({
getParameter: 1,
featureFlagCostBasedRanker: 1,
internalQueryCBRCEMode: 1,
});
return !getParam.featureFlagCostBasedRanker?.value ? "multiPlanning" : getParam.internalQueryCBRCEMode;
} else {
return TestData.setParameters.planRankerMode ? TestData.setParameters.planRankerMode : "multiPlanning";
}
}
export function getAutomaticCEPlanRankingStrategy(db) {
if (db !== null) {
const getParam = db.adminCommand({
getParameter: 1,
automaticCEPlanRankingStrategy: 1,
});
return getParam.hasOwnProperty("automaticCEPlanRankingStrategy")
? getParam.automaticCEPlanRankingStrategy
: "HistogramCEWithHeuristicFallback";
} else {
return TestData.setParameters.automaticCEPlanRankingStrategy
? TestData.setParameters.automaticCEPlanRankingStrategy
: "HistogramCEWithHeuristicFallback";
}
}
export function getMultiplanningBatchSize() {
const result = db.adminCommand({
getParameter: 1,
internalQueryPlanEvaluationMaxResults: 1,
});
if (result.ok === 1) {
return result.internalQueryPlanEvaluationMaxResults;
} else {
throw new Error("Failed to retrieve multiplanning batch size: " + JSON.stringify(result));
}
}
export function getCBRConfig(db) {
const config = assert.commandWorked(
db.adminCommand({
getParameter: 1,
featureFlagCostBasedRanker: 1,
internalQueryCBRCEMode: 1,
automaticCEPlanRankingStrategy: 1,
}),
);
return {
featureFlagCostBasedRanker: config.featureFlagCostBasedRanker.value,
internalQueryCBRCEMode: config.internalQueryCBRCEMode,
automaticCEPlanRankingStrategy: config.automaticCEPlanRankingStrategy,
};
}
export function restoreCBRConfig(db, config) {
assert.commandWorked(
db.adminCommand({
setParameter: 1,
featureFlagCostBasedRanker: config.featureFlagCostBasedRanker,
internalQueryCBRCEMode: config.internalQueryCBRCEMode,
automaticCEPlanRankingStrategy: config.automaticCEPlanRankingStrategy,
}),
);
}