diff --git a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp index f5f07bdb598..59902a6a115 100644 --- a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp +++ b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp @@ -274,7 +274,8 @@ void MergeTreeWhereOptimizer::analyzeImpl(Conditions & res, const RPNBuilderTree cond.selectivity = estimator.estimateSelectivity(node); - LOG_DEBUG(log, "Condition {} has selectivity {}", node.getASTNode()->dumpTree(), cond.selectivity); + if (node.getASTNode() != nullptr) + LOG_DEBUG(log, "Condition {} has selectivity {}", node.getASTNode()->dumpTree(), cond.selectivity); } if (where_optimizer_context.move_primary_key_columns_to_end_of_prewhere) diff --git a/src/Storages/Statistic/Statistic.cpp b/src/Storages/Statistic/Statistic.cpp index 9b7ca3ab92d..7a909f599a9 100644 --- a/src/Storages/Statistic/Statistic.cpp +++ b/src/Storages/Statistic/Statistic.cpp @@ -104,11 +104,19 @@ Float64 ConditionEstimator::estimateSelectivity(const RPNBuilderTreeNode & node) return default_unknown_cond_factor; } auto it = column_estimators.find(col.value()); + + /// If there the estimator of the column is not found or there are no data at all, + /// we use dummy estimation. + bool dummy = total_count == 0; ColumnEstimator estimator; if (it != column_estimators.end()) { estimator = it->second; } + else + { + dummy = true; + } auto [op, val] = extractBinaryOp(node, col.value()); if (op == "equals") { @@ -119,10 +127,14 @@ Float64 ConditionEstimator::estimateSelectivity(const RPNBuilderTreeNode & node) } else if (op == "less" || op == "lessThan") { + if (dummy) + return default_normal_cond_factor; return estimator.estimateLess(val) / total_count; } else if (op == "greater" || op == "greaterThan") { + if (dummy) + return default_normal_cond_factor; return estimator.estimateGreater(val) / total_count; } else @@ -144,8 +156,6 @@ void MergeTreeStatisticFactory::registerCreator(StatisticType stat_type, Creator MergeTreeStatisticFactory::MergeTreeStatisticFactory() { registerCreator(TDigest, TDigestCreator); - - ///registerCreator("cm_sketch", CMSketchCreator); } MergeTreeStatisticFactory & MergeTreeStatisticFactory::instance()