|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership |
| 3 | +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements |
| 4 | + |
| 5 | +//! Functions that count how many rows a call is evaluated for, so a test can |
| 6 | +//! check that the optimizer does not evaluate a call more (or fewer) times |
| 7 | +//! than the query says: |
| 8 | +//! |
| 9 | +//! - `counted_volatile(x)` returns `x` and is volatile, like `random()`. |
| 10 | +//! - `counted_immutable(x)` returns `x` and is immutable. |
| 11 | +//! - `evaluations()` returns the number of rows that the two functions above |
| 12 | +//! were evaluated for since the last call to `evaluations()`, and resets it. |
| 13 | +//! |
| 14 | +//! The count is per row, not per call: a function is invoked once per batch, |
| 15 | +//! and `CASE` evaluates its branches on a subset of a batch. |
| 16 | +//! |
| 17 | +//! ```text |
| 18 | +//! statement ok |
| 19 | +//! SELECT k FROM (SELECT counted_volatile(a) AS k FROM t) WHERE k > 1; |
| 20 | +//! |
| 21 | +//! query I |
| 22 | +//! SELECT evaluations(); |
| 23 | +//! ---- |
| 24 | +//! 3 |
| 25 | +//! ``` |
| 26 | +
|
| 27 | +use std::hash::{Hash, Hasher}; |
| 28 | +use std::sync::Arc; |
| 29 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 30 | + |
| 31 | +use arrow::datatypes::DataType; |
| 32 | +use datafusion::common::{Result, ScalarValue}; |
| 33 | +use datafusion::logical_expr::{ |
| 34 | + ColumnarValue, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, Signature, Volatility, |
| 35 | +}; |
| 36 | +use datafusion::prelude::SessionContext; |
| 37 | + |
| 38 | +/// Registers `counted_volatile`, `counted_immutable` and `evaluations` with `ctx`. Each |
| 39 | +/// context gets its own counter. |
| 40 | +pub fn register_evaluation_counter(ctx: &SessionContext) { |
| 41 | + let counter = Arc::new(AtomicUsize::new(0)); |
| 42 | + for (name, volatility) in [ |
| 43 | + ("counted_volatile", Volatility::Volatile), |
| 44 | + ("counted_immutable", Volatility::Immutable), |
| 45 | + ] { |
| 46 | + ctx.register_udf(ScalarUDF::new_from_impl(Counted { |
| 47 | + name, |
| 48 | + signature: Signature::any(1, volatility), |
| 49 | + counter: Arc::clone(&counter), |
| 50 | + })); |
| 51 | + } |
| 52 | + ctx.register_udf(ScalarUDF::new_from_impl(Evaluations { |
| 53 | + signature: Signature::nullary(Volatility::Volatile), |
| 54 | + counter, |
| 55 | + })); |
| 56 | +} |
| 57 | + |
| 58 | +/// Returns its argument and adds the number of rows to `counter`. |
| 59 | +#[derive(Debug)] |
| 60 | +struct Counted { |
| 61 | + name: &'static str, |
| 62 | + signature: Signature, |
| 63 | + counter: Arc<AtomicUsize>, |
| 64 | +} |
| 65 | + |
| 66 | +impl ScalarUDFImpl for Counted { |
| 67 | + fn name(&self) -> &str { |
| 68 | + self.name |
| 69 | + } |
| 70 | + |
| 71 | + fn signature(&self) -> &Signature { |
| 72 | + &self.signature |
| 73 | + } |
| 74 | + |
| 75 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 76 | + Ok(arg_types[0].clone()) |
| 77 | + } |
| 78 | + |
| 79 | + fn invoke_with_args(&self, mut args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 80 | + self.counter.fetch_add(args.number_rows, Ordering::SeqCst); |
| 81 | + Ok(args.args.swap_remove(0)) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Returns the count and resets it. |
| 86 | +#[derive(Debug)] |
| 87 | +struct Evaluations { |
| 88 | + signature: Signature, |
| 89 | + counter: Arc<AtomicUsize>, |
| 90 | +} |
| 91 | + |
| 92 | +impl ScalarUDFImpl for Evaluations { |
| 93 | + fn name(&self) -> &str { |
| 94 | + "evaluations" |
| 95 | + } |
| 96 | + |
| 97 | + fn signature(&self) -> &Signature { |
| 98 | + &self.signature |
| 99 | + } |
| 100 | + |
| 101 | + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { |
| 102 | + Ok(DataType::Int64) |
| 103 | + } |
| 104 | + |
| 105 | + fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { |
| 106 | + let count = self.counter.swap(0, Ordering::SeqCst); |
| 107 | + Ok(ColumnarValue::Scalar(ScalarValue::Int64(Some( |
| 108 | + count as i64, |
| 109 | + )))) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Two instances are equal only if they share a counter. |
| 114 | + |
| 115 | +impl PartialEq for Counted { |
| 116 | + fn eq(&self, other: &Self) -> bool { |
| 117 | + self.name == other.name && Arc::ptr_eq(&self.counter, &other.counter) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Eq for Counted {} |
| 122 | + |
| 123 | +impl Hash for Counted { |
| 124 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 125 | + self.name.hash(state); |
| 126 | + Arc::as_ptr(&self.counter).hash(state); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl PartialEq for Evaluations { |
| 131 | + fn eq(&self, other: &Self) -> bool { |
| 132 | + Arc::ptr_eq(&self.counter, &other.counter) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Eq for Evaluations {} |
| 137 | + |
| 138 | +impl Hash for Evaluations { |
| 139 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 140 | + Arc::as_ptr(&self.counter).hash(state); |
| 141 | + } |
| 142 | +} |
0 commit comments