|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + |
| 11 | +//! End-to-end test that the optimizer-derived `StatisticsRequest`s |
| 12 | +//! reach a custom `TableProvider`'s `scan_with_args`. |
| 13 | +
|
| 14 | +use std::sync::{Arc, Mutex}; |
| 15 | + |
| 16 | +use arrow::array::{Int64Array, RecordBatch}; |
| 17 | +use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; |
| 18 | +use async_trait::async_trait; |
| 19 | +use datafusion::catalog::{ScanArgs, ScanResult, Session, TableProvider}; |
| 20 | +use datafusion::datasource::TableType; |
| 21 | +use datafusion::datasource::memory::MemorySourceConfig; |
| 22 | +use datafusion::execution::context::SessionContext; |
| 23 | +use datafusion::logical_expr::Expr; |
| 24 | +use datafusion::physical_plan::ExecutionPlan; |
| 25 | +use datafusion_common::Result; |
| 26 | +use datafusion_expr_common::statistics::StatisticsRequest; |
| 27 | + |
| 28 | +/// A `TableProvider` that records the last `statistics_requests` it was |
| 29 | +/// asked for, so the test can assert what reached it. |
| 30 | +#[derive(Debug)] |
| 31 | +struct RecordingTable { |
| 32 | + schema: SchemaRef, |
| 33 | + batch: RecordBatch, |
| 34 | + last_requests: Arc<Mutex<Vec<StatisticsRequest>>>, |
| 35 | +} |
| 36 | + |
| 37 | +#[async_trait] |
| 38 | +impl TableProvider for RecordingTable { |
| 39 | + fn schema(&self) -> SchemaRef { |
| 40 | + self.schema.clone() |
| 41 | + } |
| 42 | + |
| 43 | + fn table_type(&self) -> TableType { |
| 44 | + TableType::Base |
| 45 | + } |
| 46 | + |
| 47 | + async fn scan( |
| 48 | + &self, |
| 49 | + _state: &dyn Session, |
| 50 | + projection: Option<&Vec<usize>>, |
| 51 | + _filters: &[Expr], |
| 52 | + _limit: Option<usize>, |
| 53 | + ) -> Result<Arc<dyn ExecutionPlan>> { |
| 54 | + Ok(MemorySourceConfig::try_new_exec( |
| 55 | + &[vec![self.batch.clone()]], |
| 56 | + self.schema.clone(), |
| 57 | + projection.cloned(), |
| 58 | + )?) |
| 59 | + } |
| 60 | + |
| 61 | + async fn scan_with_args<'a>( |
| 62 | + &self, |
| 63 | + state: &dyn Session, |
| 64 | + args: ScanArgs<'a>, |
| 65 | + ) -> Result<ScanResult> { |
| 66 | + // Record what reached us, then delegate to scan(). |
| 67 | + *self.last_requests.lock().unwrap() = args.statistics_requests().to_vec(); |
| 68 | + let plan = self |
| 69 | + .scan( |
| 70 | + state, |
| 71 | + args.projection().map(|p| p.to_vec()).as_ref(), |
| 72 | + args.filters().unwrap_or(&[]), |
| 73 | + args.limit(), |
| 74 | + ) |
| 75 | + .await?; |
| 76 | + Ok(ScanResult::new(plan)) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn make_table() -> (Arc<RecordingTable>, Arc<Mutex<Vec<StatisticsRequest>>>) { |
| 81 | + let schema = Arc::new(Schema::new(vec![ |
| 82 | + Field::new("a", DataType::Int64, false), |
| 83 | + Field::new("b", DataType::Int64, false), |
| 84 | + ])); |
| 85 | + let batch = RecordBatch::try_new( |
| 86 | + schema.clone(), |
| 87 | + vec![ |
| 88 | + Arc::new(Int64Array::from(vec![1, 2, 3])), |
| 89 | + Arc::new(Int64Array::from(vec![10, 20, 30])), |
| 90 | + ], |
| 91 | + ) |
| 92 | + .unwrap(); |
| 93 | + let last_requests = Arc::new(Mutex::new(Vec::new())); |
| 94 | + let provider = Arc::new(RecordingTable { |
| 95 | + schema, |
| 96 | + batch, |
| 97 | + last_requests: last_requests.clone(), |
| 98 | + }); |
| 99 | + (provider, last_requests) |
| 100 | +} |
| 101 | + |
| 102 | +#[tokio::test] |
| 103 | +async fn requests_reach_provider_scan_with_args() -> Result<()> { |
| 104 | + let (provider, last_requests) = make_table(); |
| 105 | + let ctx = SessionContext::new(); |
| 106 | + ctx.register_table("t", provider)?; |
| 107 | + |
| 108 | + // Filter on `a` + sort on `b` should request Min/Max/NullCount on |
| 109 | + // both, plus DistinctCount on `a` (filter), plus a RowCount. |
| 110 | + let _ = ctx |
| 111 | + .sql("SELECT a, b FROM t WHERE a > 0 ORDER BY b LIMIT 10") |
| 112 | + .await? |
| 113 | + .collect() |
| 114 | + .await?; |
| 115 | + |
| 116 | + let got = last_requests.lock().unwrap().clone(); |
| 117 | + assert!(!got.is_empty(), "expected non-empty requests, got {got:?}"); |
| 118 | + |
| 119 | + let has = |needle: &StatisticsRequest| got.iter().any(|r| r == needle); |
| 120 | + use datafusion_common::Column; |
| 121 | + use datafusion_expr_common::statistics::StatisticsRequest::*; |
| 122 | + assert!(has(&RowCount), "expected RowCount, got {got:?}"); |
| 123 | + assert!( |
| 124 | + has(&Min(Column::new_unqualified("a"))), |
| 125 | + "expected Min(a), got {got:?}" |
| 126 | + ); |
| 127 | + assert!( |
| 128 | + has(&DistinctCount(Column::new_unqualified("a"))), |
| 129 | + "expected DistinctCount(a), got {got:?}" |
| 130 | + ); |
| 131 | + assert!( |
| 132 | + has(&Min(Column::new_unqualified("b"))), |
| 133 | + "expected Min(b) from ORDER BY, got {got:?}" |
| 134 | + ); |
| 135 | + |
| 136 | + Ok(()) |
| 137 | +} |
| 138 | + |
| 139 | +#[tokio::test] |
| 140 | +async fn no_requests_when_plan_has_no_filter_sort_or_join() -> Result<()> { |
| 141 | + let (provider, last_requests) = make_table(); |
| 142 | + let ctx = SessionContext::new(); |
| 143 | + ctx.register_table("t", provider)?; |
| 144 | + |
| 145 | + // Plain `SELECT *` — only `RowCount` should be requested. |
| 146 | + let _ = ctx.sql("SELECT a, b FROM t").await?.collect().await?; |
| 147 | + |
| 148 | + let got = last_requests.lock().unwrap().clone(); |
| 149 | + use datafusion_expr_common::statistics::StatisticsRequest::*; |
| 150 | + assert_eq!(got.len(), 1, "expected only RowCount, got {got:?}"); |
| 151 | + assert!(matches!(got[0], RowCount)); |
| 152 | + |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments