Apache DataFusion SQL Query Engine
fix: use null counts to estimate IS NULL and IS NOT NULL (#25648)
Kumar Ujjawalc120619dec70
diff --git a/datafusion/physical-plan/src/filter.rs b/datafusion/physical-plan/src/filter.rsindex f756b2fa1b..037cf65008 100644--- a/datafusion/physical-plan/src/filter.rs+++ b/datafusion/physical-plan/src/filter.rs@@ -348,6 +348,10 @@ impl FilterExec {/// When interval analysis applies, min/max are also tightened to the/// surviving value range.///+ /// A bare `IS NULL` or `IS NOT NULL` check uses the column's null count+ /// when available. The result is exact only when the input row count and+ /// null count are both exact.+ ////// A contradictory predicate (e.g. `a = 1 AND a = 2`) yields zero rows and/// empty-column statistics.pub(crate) fn statistics_helper(@@ -356,6 +360,18 @@ impl FilterExec {predicate: &Arc<dyn PhysicalExpr>,default_selectivity: u8,) -> Result<Statistics> {+ let null_check = null_check_column(predicate);+ if let Some((column, is_null)) = null_check+ && let Some(num_rows) = null_check_num_rows(&input_stats, column, is_null)+ {+ return Ok(null_check_statistics(+ input_stats,+ column,+ is_null,+ num_rows,+ ));+ }+let (eq_columns, is_infeasible) = collect_equality_columns(predicate);let input_num_rows = input_stats.num_rows;@@ -366,7 +382,7 @@ impl FilterExec {// expresses that.let match_limit = unique_match_limit(predicate, &input_stats);- let (selectivity, num_rows, column_statistics) = if is_infeasible {+ let (selectivity, num_rows, mut column_statistics) = if is_infeasible {// Contradictory predicate: no rows survive.let cs = vec![empty_column_statistics(); input_stats.column_statistics.len()];(0.0, Precision::Exact(0), cs)@@ -443,6 +459,12 @@ impl FilterExec {};let total_byte_size =scale_byte_size_at_rows(input_total_byte_size, selectivity, num_rows);+ if let Some((column, true)) = null_check+ && let Some(column_stats) = column_statistics.get_mut(column)+ {+ // These properties hold even when the input null count is unknown.+ set_all_null_column_statistics(column_stats, num_rows);+ }Ok(Statistics {num_rows,@@ -1493,6 +1515,95 @@ pub(crate) fn null_check_column(Some((arg.downcast_ref::<Column>()?.index(), is_null))}+/// Estimates the rows that pass a bare null check on `column` from the+/// column's null count. Checks on expressions or compound predicates still use+/// the usual filter estimation.+fn null_check_num_rows(+ input_stats: &Statistics,+ column: usize,+ is_null: bool,+) -> Option<Precision<usize>> {+ let mut null_count = input_stats.column_statistics.get(column)?.null_count;+ if !matches!(input_stats.num_rows, Precision::Exact(_)) {+ // Keep the filtered row estimate conservative when input rows are uncertain.+ null_count = null_count.to_inexact();+ }+ if let (Some(nulls), Some(rows)) =+ (null_count.get_value(), input_stats.num_rows.get_value())+ && nulls > rows+ {+ // Estimates can disagree, but filtering cannot add rows.+ null_count = input_stats.num_rows.to_inexact();+ }+ let num_rows = if input_stats.num_rows == Precision::Exact(0) {+ Precision::Exact(0)+ } else if is_null {+ null_count+ } else {+ input_stats.num_rows.sub(&null_count)+ };+ num_rows.get_value().is_some().then_some(num_rows)+}++/// Builds the statistics of a bare null check on `column` that keeps+/// `num_rows` of the input rows.+fn null_check_statistics(+ input_stats: Statistics,+ column: usize,+ is_null: bool,+ num_rows: Precision<usize>,+) -> Statistics {+ let input_rows = input_stats.num_rows;+ let mut stats = input_stats.to_inexact();+ stats.num_rows = num_rows;+ if num_rows == Precision::Exact(0) {+ stats.total_byte_size = Precision::Exact(0);+ stats.column_statistics.fill(empty_column_statistics());+ return stats;+ }++ let selectivity = if num_rows.get_value() == Some(&0) {+ Some(0.0)+ } else if let (Some(&rows), Some(&input_rows)) =+ (num_rows.get_value(), input_rows.get_value())+ && input_rows > 0+ {+ Some(rows as f64 / input_rows as f64)+ } else {+ None+ };+ let scale_bytes = |bytes: Precision<usize>| {+ selectivity.map_or(Precision::Absent, |selectivity| {+ bytes.with_estimated_selectivity(selectivity)+ })+ };+ stats.total_byte_size = scale_bytes(stats.total_byte_size);+ for column_stats in &mut stats.column_statistics {+ column_stats.null_count = cap_at_rows(column_stats.null_count, num_rows);+ column_stats.distinct_count = cap_at_rows(column_stats.distinct_count, num_rows);+ column_stats.byte_size = scale_bytes(column_stats.byte_size);+ }+ let column_stats = &mut stats.column_statistics[column];+ if is_null {+ set_all_null_column_statistics(column_stats, num_rows);+ } else {+ column_stats.null_count = Precision::Exact(0);+ }+ stats+}++/// Every row passing `IS NULL` is null, regardless of how rows were estimated.+fn set_all_null_column_statistics(+ column_stats: &mut ColumnStatistics,+ num_rows: Precision<usize>,+) {+ column_stats.null_count = num_rows;+ column_stats.distinct_count = Precision::Exact(0);+ column_stats.min_value = Precision::Absent;+ column_stats.max_value = Precision::Absent;+ column_stats.sum_value = Precision::Absent;+}+/// Column statistics of an exactly empty output: no nulls, distinct values or/// bytes, and no value bounds.fn empty_column_statistics() -> ColumnStatistics {@@ -4664,6 +4775,179 @@ mod tests {Ok(())}+ #[tokio::test]+ async fn test_filter_statistics_null_checks() -> Result<()> {+ use Precision::{Absent, Exact, Inexact};++ // A null count gives an exact result only with an exact row count.+ let cases = [+ (Exact(100), Exact(30), [Exact(30), Exact(70)]),+ (Exact(100), Inexact(30), [Inexact(30), Inexact(70)]),+ (Inexact(100), Exact(30), [Inexact(30), Inexact(70)]),+ (Inexact(100), Inexact(30), [Inexact(30), Inexact(70)]),+ (Exact(100), Exact(0), [Exact(0), Exact(100)]),+ (Exact(100), Exact(100), [Exact(100), Exact(0)]),+ (Exact(0), Exact(0), [Exact(0), Exact(0)]),+ (Exact(0), Inexact(0), [Exact(0), Exact(0)]),+ (Exact(0), Absent, [Exact(0), Exact(0)]),+ (Exact(100), Absent, [Inexact(40), Inexact(40)]),+ (Absent, Exact(0), [Inexact(0), Absent]),+ (Absent, Exact(30), [Inexact(30), Absent]),+ (Absent, Inexact(30), [Inexact(30), Absent]),+ (Absent, Absent, [Absent, Absent]),+ (Exact(100), Inexact(120), [Inexact(100), Inexact(0)]),+ ];++ for data_type in [DataType::Int64, DataType::Utf8, DataType::Decimal128(10, 2)] {+ let schema = Schema::new(vec![Field::new("a", data_type, true)]);+ for (num_rows, null_count, expected) in cases {+ let input: Arc<dyn ExecutionPlan> = Arc::new(StatisticsExec::new(+ Statistics {+ num_rows,+ total_byte_size: Absent,+ column_statistics: vec![ColumnStatistics {+ null_count,+ ..Default::default()+ }],+ },+ schema.clone(),+ ));+ let predicates = [+ is_null(col("a", &schema)?)?,+ is_not_null(col("a", &schema)?)?,+ ];+ for (predicate, expected_rows) in predicates.into_iter().zip(expected) {+ let filter = FilterExecBuilder::new(predicate, Arc::clone(&input))+ .with_default_selectivity(40)+ .build()?;+ let stats = StatisticsContext::new()+ .compute(&filter, &StatisticsArgs::new())?;+ assert_eq!(+ stats.num_rows,+ expected_rows,+ "{} with rows={num_rows:?}, nulls={null_count:?}",+ filter.predicate()+ );+ }+ }+ }+ Ok(())+ }++ #[tokio::test]+ async fn test_filter_statistics_null_check_column_stats() -> Result<()> {+ use Precision::{Absent, Exact, Inexact};++ let schema = Schema::new(vec![+ Field::new("a", DataType::Int32, true),+ Field::new("b", DataType::Int32, true),+ ]);+ for num_rows in [Exact(100), Absent] {+ let input: Arc<dyn ExecutionPlan> = Arc::new(StatisticsExec::new(+ Statistics {+ num_rows,+ total_byte_size: Exact(800),+ column_statistics: vec![+ ColumnStatistics {+ null_count: Exact(30),+ distinct_count: Exact(50),+ min_value: Exact(ScalarValue::Int32(Some(1))),+ max_value: Exact(ScalarValue::Int32(Some(50))),+ sum_value: Exact(ScalarValue::Int64(Some(1000))),+ byte_size: Exact(400),+ };+ 2+ ],+ },+ schema.clone(),+ ));+ let predicate = is_null(col("a", &schema)?)?;+ let filter = FilterExecBuilder::new(predicate, input)+ .apply_projection(Some(vec![1, 0]))?+ .build()?;+ let stats =+ StatisticsContext::new().compute(&filter, &StatisticsArgs::new())?;++ // Without an exact input row count, the null count is an estimate.+ let rows = if num_rows == Absent {+ Inexact(30)+ } else {+ Exact(30)+ };+ assert_eq!(stats.num_rows, rows);+ // The projection moves the checked column to index 1.+ let checked = &stats.column_statistics[1];+ assert_eq!(checked.null_count, rows);+ assert_eq!(checked.distinct_count, Exact(0));+ assert_eq!(checked.min_value, Absent);+ assert_eq!(checked.max_value, Absent);+ assert_eq!(checked.sum_value, Absent);+ assert_eq!(stats.column_statistics[0].null_count, Inexact(30));+ assert_eq!(stats.column_statistics[0].distinct_count, Inexact(30));+ let (total_bytes, column_bytes) = if num_rows == Absent {+ // The null count determines rows, but not the fraction of input bytes.+ (Absent, Absent)+ } else {+ (Inexact(240), Inexact(120))+ };+ assert_eq!(stats.total_byte_size, total_bytes);+ assert_eq!(checked.byte_size, column_bytes);+ assert_eq!(stats.column_statistics[0].byte_size, column_bytes);+ }+ Ok(())+ }++ #[tokio::test]+ async fn test_filter_statistics_is_not_null_column_stats() -> Result<()> {+ use Precision::{Exact, Inexact};++ let schema = Schema::new(vec![+ Field::new("a", DataType::Int32, true),+ Field::new("b", DataType::Int32, true),+ ]);+ let input: Arc<dyn ExecutionPlan> = Arc::new(StatisticsExec::new(+ Statistics {+ num_rows: Inexact(100),+ total_byte_size: Exact(800),+ column_statistics: vec![+ ColumnStatistics {+ null_count: Inexact(50),+ distinct_count: Exact(80),+ min_value: Exact(ScalarValue::Int32(Some(1))),+ byte_size: Exact(400),+ ..Default::default()+ },+ ColumnStatistics {+ null_count: Inexact(60),+ distinct_count: Inexact(90),+ byte_size: Exact(400),+ ..Default::default()+ },+ ],+ },+ schema.clone(),+ ));+ let predicate = is_not_null(col("a", &schema)?)?;+ let filter = FilterExecBuilder::new(predicate, input).build()?;+ let stats = StatisticsContext::new().compute(&filter, &StatisticsArgs::new())?;++ // 100 rows minus 50 nulls, where the default selectivity gives 20 rows.+ assert_eq!(stats.num_rows, Inexact(50));+ assert_eq!(stats.total_byte_size, Inexact(400));+ // The surviving null count is exactly zero even though the input counts+ // and the resulting row count are estimates.+ let checked = &stats.column_statistics[0];+ assert_eq!(checked.null_count, Exact(0));+ assert_eq!(checked.distinct_count, Inexact(50));+ assert_eq!(checked.min_value, Inexact(ScalarValue::Int32(Some(1))));+ assert_eq!(checked.byte_size, Inexact(200));+ let other = &stats.column_statistics[1];+ assert_eq!(other.null_count, Inexact(50));+ assert_eq!(other.distinct_count, Inexact(50));+ assert_eq!(other.byte_size, Inexact(200));+ Ok(())+ }+#[test]fn test_string_selectivity_non_string_literal() {let schema =@@ -4983,6 +5267,88 @@ mod tests {Ok(())}+ #[tokio::test]+ async fn test_filter_statistics_null_checks_match_execution() -> Result<()> {+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Int32, true),+ Field::new("b", DataType::Int32, true),+ ]));+ for values in [+ vec![Some(1), None, Some(3), Some(4)],+ vec![None, None],+ vec![Some(1), Some(2)],+ vec![],+ ] {+ let batch = RecordBatch::try_new(+ Arc::clone(&schema),+ vec![+ Arc::new(Int32Array::from(values.clone())),+ Arc::new(Int32Array::from(values)),+ ],+ )?;+ for partitions in [1, 2] {+ let input: Arc<dyn ExecutionPlan> = test::TestMemoryExec::try_new_exec(+ &vec![vec![batch.clone()]; partitions],+ Arc::clone(&schema),+ None,+ )?;+ for predicate in [+ is_null(col("a", &schema)?)?,+ is_not_null(col("a", &schema)?)?,+ ] {+ for fetch in [None, Some(0), Some(1)] {+ let filter: Arc<dyn ExecutionPlan> = Arc::new(+ FilterExecBuilder::new(+ Arc::clone(&predicate),+ Arc::clone(&input),+ )+ .with_fetch(fetch)+ .build()?,+ );+ let stats = StatisticsContext::new()+ .compute(filter.as_ref(), &StatisticsArgs::new())?;+ let batches = crate::execution_plan::collect(+ filter,+ Arc::new(TaskContext::default()),+ )+ .await?;+ let rows = batches.iter().map(RecordBatch::num_rows).sum();+ if fetch.is_none() || stats.num_rows.is_exact() == Some(true) {+ assert_eq!(stats.num_rows, Precision::Exact(rows));+ } else {+ // Each partition stops at the fetch, so the total+ // over several partitions is only an upper bound.+ assert!(+ matches!(stats.num_rows, Precision::Inexact(n) if n >= rows)+ );+ }+ let checked = &stats.column_statistics[0];+ if predicate.downcast_ref::<IsNullExpr>().is_some() {+ assert_eq!(checked.null_count, stats.num_rows);+ assert_eq!(checked.distinct_count, Precision::Exact(0));+ } else {+ assert_eq!(checked.null_count, Precision::Exact(0));+ }+ for column in &stats.column_statistics {+ assert!(+ column.null_count.get_value().unwrap()+ <= stats.num_rows.get_value().unwrap()+ );+ }+ if stats.num_rows == Precision::Exact(0) {+ assert_eq!(stats.total_byte_size, Precision::Exact(0));+ let column = &stats.column_statistics[0];+ assert_eq!(column.null_count, Precision::Exact(0));+ assert_eq!(column.distinct_count, Precision::Exact(0));+ assert_eq!(column.byte_size, Precision::Exact(0));+ }+ }+ }+ }+ }+ Ok(())+ }+#[tokio::test]async fn test_filter_statistics_is_not_null_rejects_nulls() -> Result<()> {let schema = Schema::new(vec![Field::new("name", DataType::Utf8, true)]);@@ -4991,7 +5357,7 @@ mod tests {num_rows: Precision::Inexact(100),total_byte_size: Precision::Inexact(1000),column_statistics: vec![ColumnStatistics {- null_count: Precision::Inexact(80),+ null_count: Precision::Absent,distinct_count: Precision::Inexact(60),byte_size: Precision::Exact(1000),..Default::default()@@ -5001,8 +5367,8 @@ mod tests {));// `name IS NOT NULL` keeps only non-null rows, so the surviving null- // count is exactly zero. Utf8 interval analysis is unsupported, so this- // also exercises the default-selectivity path.+ // count is exactly zero. Without an input null count, the check uses+ // the default selectivity.let predicate: Arc<dyn PhysicalExpr> = is_not_null(col("name", &schema)?)?;let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);diff --git a/datafusion/physical-plan/src/operator_statistics/mod.rs b/datafusion/physical-plan/src/operator_statistics/mod.rsindex 32e2244ac3..2ccf9f1765 100644--- a/datafusion/physical-plan/src/operator_statistics/mod.rs+++ b/datafusion/physical-plan/src/operator_statistics/mod.rs@@ -100,7 +100,7 @@ use datafusion_common::{Result, Statistics};use crate::ExecutionPlan;use crate::aggregates::{AggregateExec, AggregateMode};use crate::execution_plan::CardinalityEffect;-use crate::filter::FilterExec;+use crate::filter::{FilterExec, null_check_column};use crate::joins::{CrossJoinExec, HashJoinExec, JoinOnRef, SortMergeJoinExec};use crate::limit::{GlobalLimitExec, LocalLimitExec};use crate::projection::ProjectionExec;@@ -643,8 +643,9 @@ fn computed_with_row_count(/// child stats), this provider receives enhanced child stats that may include/// NDV overrides injected at the scan level. It applies the same selectivity/// estimation logic as `FilterExec::statistics_helper`, then additionally-/// adjusts each column's `distinct_count` using [`ndv_after_selectivity`] based-/// on the computed selectivity ratio.+/// adjusts `distinct_count` using [`ndv_after_selectivity`] based on the+/// computed selectivity ratio, except when all non-null values survive or the+/// distinct count is exactly zero.#[deprecated(since = "56.0.0",note = "duplicates `FilterExec::statistics_from_inputs` and adds a distinct count adjustment that belongs in the operator, see https://github.com/apache/datafusion/issues/26052; without a matching provider the statistics walk uses the operator's own estimate"@@ -680,6 +681,13 @@ impl StatisticsProvider for FilterStatisticsProvider {// TODO: pass filter.expression_analyzer_registry() once #21122 lands)?;+ // `IS NOT NULL` keeps every non-null value of its column, so the+ // helper's distinct count for that column stands.+ let all_values_kept = match null_check_column(filter.predicate()) {+ Some((column, false)) => Some(column),+ _ => None,+ };+// Adjust distinct_count for each column using the selectivity ratio// via the probabilistic survival model from// ndv_after_selectivity to account for rows removed by the filter.@@ -689,7 +697,14 @@ impl StatisticsProvider for FilterStatisticsProvider {&& filtered_rows < orig_rows{let selectivity = filtered_rows as f64 / orig_rows as f64;- for col_stat in &mut stats.column_statistics {+ for (idx, col_stat) in stats.column_statistics.iter_mut().enumerate() {+ // Skip the `IS NOT NULL` column, whose values all survive, and+ // an exact zero, which cannot shrink.+ if all_values_kept == Some(idx)+ || col_stat.distinct_count == Precision::Exact(0)+ {+ continue;+ }if let Some(&ndv) = col_stat.distinct_count.get_value() {let adjusted = ndv_after_selectivity(ndv, orig_rows, selectivity);col_stat.distinct_count = Precision::Inexact(adjusted);@@ -1886,6 +1901,95 @@ mod tests {Ok(())}+ #[test]+ fn test_filter_is_null_column_stats_with_missing_counts() -> Result<()> {+ use crate::filter::FilterExecBuilder;+ use Precision::{Absent, Exact};+ use datafusion_physical_expr::expressions::is_null;++ let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)]));+ let registry =+ StatisticsRegistry::with_providers(vec![Arc::new(FilterStatisticsProvider)]);+ for num_rows in [Exact(100), Absent] {+ for null_count in [Exact(30), Absent] {+ let source: Arc<dyn ExecutionPlan> =+ Arc::new(MockSourceExec::with_column_stats(+ Arc::clone(&schema),+ num_rows,+ vec![ColumnStatistics {+ null_count,+ distinct_count: Exact(50),+ min_value: Exact(ScalarValue::Int32(Some(1))),+ max_value: Exact(ScalarValue::Int32(Some(50))),+ sum_value: Exact(ScalarValue::Int64(Some(1000))),+ ..Default::default()+ }],+ ));+ for fetch in [None, Some(10)] {+ let filter = FilterExecBuilder::new(+ is_null(col("a", &schema)?)?,+ Arc::clone(&source),+ )+ .with_fetch(fetch)+ .build()?;+ let stats = compute(®istry, &filter)?;+ let column = &stats.base.column_statistics[0];+ assert_eq!(column.distinct_count, Exact(0));+ assert_eq!(column.null_count, stats.base.num_rows);+ assert_eq!(column.min_value, Absent);+ assert_eq!(column.max_value, Absent);+ assert_eq!(column.sum_value, Absent);+ }+ }+ }+ Ok(())+ }++ #[test]+ fn test_filter_is_not_null_preserves_distinct_count() -> Result<()> {+ use crate::filter::FilterExecBuilder;+ use Precision::{Exact, Inexact};+ use datafusion_physical_expr::expressions::is_not_null;++ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Int32, true),+ Field::new("b", DataType::Int32, true),+ ]));+ let registry =+ StatisticsRegistry::with_providers(vec![Arc::new(FilterStatisticsProvider)]);+ let source: Arc<dyn ExecutionPlan> = Arc::new(MockSourceExec::with_column_stats(+ Arc::clone(&schema),+ Exact(100),+ vec![+ ColumnStatistics {+ null_count: Exact(30),+ distinct_count: Exact(50),+ ..Default::default()+ };+ 2+ ],+ ));+ for (fetch, rows, distinct) in [+ (None, Exact(70), Inexact(50)),+ (Some(10), Exact(10), Inexact(10)),+ ] {+ let filter = FilterExecBuilder::new(+ is_not_null(col("a", &schema)?)?,+ Arc::clone(&source),+ )+ .with_fetch(fetch)+ .apply_projection(Some(vec![1, 0]))?+ .build()?;+ let stats = compute(®istry, &filter)?;+ assert_eq!(stats.base.num_rows, rows);+ // Only NULLs are removed from a; the fetch can further reduce its NDV.+ let checked = &stats.base.column_statistics[1];+ assert_eq!(checked.distinct_count, distinct);+ assert_eq!(checked.null_count, Exact(0));+ }+ Ok(())+ }+#[test]fn test_filter_provider_applies_fetch() -> Result<()> {use crate::filter::FilterExecBuilder;diff --git a/datafusion/sqllogictest/test_files/explain.slt b/datafusion/sqllogictest/test_files/explain.sltindex 9dabea746c..9b0984c129 100644--- a/datafusion/sqllogictest/test_files/explain.slt+++ b/datafusion/sqllogictest/test_files/explain.slt@@ -283,6 +283,61 @@ physical_plan01)ProjectionExec: expr=[CAST(a@0 AS Int64) as cast_statistics.a], statistics=[Rows=Exact(2), Bytes=Exact(16), [(Col[0]: Null=Exact(1))]]02)--DataSourceExec: partitions=1, partition_sizes=[1], statistics=[Rows=Exact(2), Bytes=Exact(176), [(Col[0]: Null=Exact(1))]]+# Null checks use the input null count instead of the default selectivity.+statement ok+CREATE TABLE null_check_statistics(a INT) AS VALUES (1), (NULL), (2), (3), (NULL);++query TT+EXPLAIN SELECT a FROM null_check_statistics WHERE a IS NULL;+----+physical_plan+01)FilterExec: a@0 IS NULL, statistics=[Rows=Exact(2), Bytes=Inexact(77), [(Col[0]: Null=Exact(2) Distinct=Exact(0))]]+02)--DataSourceExec: partitions=1, partition_sizes=[1], statistics=[Rows=Exact(5), Bytes=Exact(192), [(Col[0]: Null=Exact(2))]]++query TT+EXPLAIN SELECT a FROM null_check_statistics WHERE a IS NOT NULL;+----+physical_plan+01)FilterExec: a@0 IS NOT NULL, statistics=[Rows=Exact(3), Bytes=Inexact(116), [(Col[0]: Null=Exact(0))]]+02)--DataSourceExec: partitions=1, partition_sizes=[1], statistics=[Rows=Exact(5), Bytes=Exact(192), [(Col[0]: Null=Exact(2))]]++query II+SELECT COUNT(*), COUNT(a) FROM null_check_statistics WHERE a IS NULL;+----+2 0++query II+SELECT COUNT(*), COUNT(a) FROM null_check_statistics WHERE a IS NOT NULL;+----+3 3++# An outer join adds NULLs that the input null counts do not include, so a null+# check above it must not give an exact row count.+statement ok+CREATE TABLE null_check_join(a INT) AS VALUES (1);++query I+SELECT COUNT(*) FROM null_check_statistics t LEFT JOIN null_check_join j ON t.a = j.a WHERE j.a IS NULL;+----+4++query I+SELECT COUNT(*) FROM null_check_statistics t FULL JOIN null_check_join j ON t.a = j.a WHERE j.a IS NULL;+----+4++# The padded column's null count includes the estimated unmatched rows, so the+# filter does not estimate zero rows. The preserved key keeps its NULLs.+query TT+EXPLAIN SELECT t.a FROM null_check_statistics t LEFT JOIN null_check_join j ON t.a = j.a WHERE j.a IS NULL;+----+physical_plan+01)FilterExec: a@1 IS NULL, projection=[a@0], statistics=[Rows=Inexact(4), Bytes=Absent, [(Col[0]: Null=Inexact(2))]]+02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, statistics=[Rows=Inexact(5), Bytes=Absent, [(Col[0]: Null=Inexact(2)),(Col[1]: Null=Inexact(4))]]+03)----HashJoinExec: mode=CollectLeft, join_type=Right, on=[(a@0, a@0)], projection=[a@1, a@0], statistics=[Rows=Inexact(5), Bytes=Absent, [(Col[0]: Null=Inexact(2)),(Col[1]: Null=Inexact(4))]]+04)------DataSourceExec: partitions=1, partition_sizes=[1], statistics=[Rows=Exact(1), Bytes=Exact(112), [(Col[0]: Null=Exact(0))]]+05)------DataSourceExec: partitions=1, partition_sizes=[1], statistics=[Rows=Exact(5), Bytes=Exact(192), [(Col[0]: Null=Exact(2))]]+# A TopK that preserves partitioning keeps up to `fetch` rows from each of its# partitions, so its estimate must count all of them, while the merge above it# keeps the global limit.