|
27 | 27 | } |
28 | 28 | } |
29 | 29 |
|
| 30 | +#[derive(Default, Clone)] |
| 31 | +pub struct IsConstantOpts { |
| 32 | + pub fallback_to_canonicalize: bool, |
| 33 | +} |
| 34 | + |
| 35 | +pub fn is_constant(array: &dyn Array) -> VortexResult<bool> { |
| 36 | + let opts = IsConstantOpts::default(); |
| 37 | + is_constant_opts(array, &opts) |
| 38 | +} |
| 39 | + |
30 | 40 | /// Computes whether an array has constant values. |
31 | 41 | /// An array is constant IFF at least one of the following conditions apply: |
32 | 42 | /// 1. It has one elements. |
|
37 | 47 | /// |
38 | 48 | /// If the array has some null values but is not all null, it'll never be constant. |
39 | 49 | /// **Please note:** Might return false negatives if a specific encoding couldn't make a determination. |
40 | | -pub fn is_constant(array: &dyn Array) -> VortexResult<bool> { |
| 50 | +pub fn is_constant_opts(array: &dyn Array, opts: &IsConstantOpts) -> VortexResult<bool> { |
41 | 51 | match array.len() { |
42 | 52 | // Our current semantics are that we can always get a value out of a constant array. We might want to change that in the future. |
43 | 53 | 0 => return Ok(false), |
@@ -96,15 +106,19 @@ pub fn is_constant(array: &dyn Array) -> VortexResult<bool> { |
96 | 106 | array.encoding() |
97 | 107 | ); |
98 | 108 |
|
99 | | - let array = array.to_canonical()?; |
100 | | - |
101 | | - if let Some(is_constant_fn) = array.as_ref().vtable().is_constant_fn() { |
102 | | - is_constant_fn.is_constant(array.as_ref())? |
| 109 | + if opts.fallback_to_canonicalize { |
| 110 | + let array = array.to_canonical()?; |
| 111 | + |
| 112 | + if let Some(is_constant_fn) = array.as_ref().vtable().is_constant_fn() { |
| 113 | + is_constant_fn.is_constant(array.as_ref())? |
| 114 | + } else { |
| 115 | + vortex_bail!( |
| 116 | + "No is_constant function for canonical array: {}", |
| 117 | + array.as_ref().encoding(), |
| 118 | + ) |
| 119 | + } |
103 | 120 | } else { |
104 | | - vortex_bail!( |
105 | | - "No is_constant function for canonical array: {}", |
106 | | - array.as_ref().encoding(), |
107 | | - ) |
| 121 | + None |
108 | 122 | } |
109 | 123 | }; |
110 | 124 |
|
|
0 commit comments