Skip to main content

vortex_array/compute/
is_constant.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::any::Any;
5
6use vortex_error::VortexResult;
7
8use crate::ArrayRef;
9use crate::LEGACY_SESSION;
10use crate::VortexSessionExecute;
11use crate::compute::Options;
12
13/// Computes whether an array has constant values.
14///
15/// **Deprecated**: Use [`crate::aggregate_fn::fns::is_constant::is_constant`] instead.
16#[deprecated(note = "Use crate::aggregate_fn::fns::is_constant::is_constant instead")]
17pub fn is_constant(array: &ArrayRef) -> VortexResult<Option<bool>> {
18    let mut ctx = LEGACY_SESSION.create_execution_ctx();
19    Ok(Some(crate::aggregate_fn::fns::is_constant::is_constant(
20        array, &mut ctx,
21    )?))
22}
23
24/// Computes whether an array has constant values.
25///
26/// **Deprecated**: Use [`crate::aggregate_fn::fns::is_constant::is_constant`] instead.
27#[deprecated(note = "Use crate::aggregate_fn::fns::is_constant::is_constant instead")]
28pub fn is_constant_opts(array: &ArrayRef, _opts: &IsConstantOpts) -> VortexResult<Option<bool>> {
29    let mut ctx = LEGACY_SESSION.create_execution_ctx();
30    Ok(Some(crate::aggregate_fn::fns::is_constant::is_constant(
31        array, &mut ctx,
32    )?))
33}
34
35/// When calling `is_constant` the children are all checked for constantness.
36/// This enum decide at each precision/cost level the constant check should run as.
37/// The cost increase as we move down the list.
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39pub enum Cost {
40    /// Only apply constant time computation to estimate constantness.
41    Negligible,
42    /// Allow the encoding to do a linear amount of work to determine is constant.
43    Specialized,
44    /// Same as linear, but when necessary canonicalize the array and check is constant.
45    Canonicalize,
46}
47
48/// Configuration for [`is_constant_opts`] operations.
49#[derive(Clone, Debug)]
50pub struct IsConstantOpts {
51    /// What precision cost trade off should be used
52    pub cost: Cost,
53}
54
55impl Default for IsConstantOpts {
56    fn default() -> Self {
57        Self {
58            cost: Cost::Canonicalize,
59        }
60    }
61}
62
63impl Options for IsConstantOpts {
64    fn as_any(&self) -> &dyn Any {
65        self
66    }
67}
68
69impl IsConstantOpts {
70    pub fn is_negligible_cost(&self) -> bool {
71        self.cost == Cost::Negligible
72    }
73}