|
| 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 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow::array::{Array, AsArray, BooleanArray}; |
| 19 | +use arrow::compute::take; |
| 20 | +use arrow::datatypes::DataType; |
| 21 | +use datafusion_common::Result; |
| 22 | + |
| 23 | +use super::static_filter::{StaticFilter, StaticFilterRef}; |
| 24 | + |
| 25 | +/// Adds dictionary-encoded needle support to a filter that expects plain arrays. |
| 26 | +/// This wrapper is only used when the input expression returns dictionaries. |
| 27 | +pub(super) struct DictionaryFilter { |
| 28 | + inner: StaticFilterRef, |
| 29 | +} |
| 30 | + |
| 31 | +impl DictionaryFilter { |
| 32 | + pub(super) fn new(inner: StaticFilterRef) -> Self { |
| 33 | + Self { inner } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl StaticFilter for DictionaryFilter { |
| 38 | + fn null_count(&self) -> usize { |
| 39 | + self.inner.null_count() |
| 40 | + } |
| 41 | + |
| 42 | + fn contains(&self, needles: &dyn Array, negated: bool) -> Result<BooleanArray> { |
| 43 | + let Some(dictionary) = needles.as_any_dictionary_opt() else { |
| 44 | + return self.inner.contains(needles, negated); |
| 45 | + }; |
| 46 | + let values = dictionary.values(); |
| 47 | + let values_contains = if matches!(values.data_type(), DataType::Dictionary(_, _)) |
| 48 | + { |
| 49 | + self.contains(values.as_ref(), negated)? |
| 50 | + } else { |
| 51 | + self.inner.contains(values.as_ref(), negated)? |
| 52 | + }; |
| 53 | + let result = take(&values_contains, dictionary.keys(), None)?; |
| 54 | + Ok(result.as_boolean().clone()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use std::sync::Arc; |
| 61 | + |
| 62 | + use arrow::array::{ |
| 63 | + ArrayRef, Decimal128Array, DictionaryArray, Int8Array, Int16Array, Int32Array, |
| 64 | + Int64Array, TimestampNanosecondArray, UInt8Array, UInt16Array, UInt32Array, |
| 65 | + UInt64Array, |
| 66 | + }; |
| 67 | + |
| 68 | + use super::super::strategy::instantiate_static_filter; |
| 69 | + use super::*; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn dictionary_needles_support_all_key_types() -> Result<()> { |
| 73 | + let values: ArrayRef = Arc::new(Int32Array::from(vec![1, 2])); |
| 74 | + let expected = BooleanArray::from(vec![Some(true), Some(false), None]); |
| 75 | + |
| 76 | + macro_rules! check_keys { |
| 77 | + ($keys:expr) => {{ |
| 78 | + let needles = DictionaryArray::try_new($keys, Arc::clone(&values))?; |
| 79 | + let filter = instantiate_static_filter( |
| 80 | + Arc::new(Int32Array::from(vec![1, 3])), |
| 81 | + needles.data_type(), |
| 82 | + )?; |
| 83 | + assert_eq!(filter.contains(&needles, false)?, expected); |
| 84 | + }}; |
| 85 | + } |
| 86 | + |
| 87 | + check_keys!(Int8Array::from(vec![Some(0), Some(1), None])); |
| 88 | + check_keys!(Int16Array::from(vec![Some(0), Some(1), None])); |
| 89 | + check_keys!(Int32Array::from(vec![Some(0), Some(1), None])); |
| 90 | + check_keys!(Int64Array::from(vec![Some(0), Some(1), None])); |
| 91 | + check_keys!(UInt8Array::from(vec![Some(0), Some(1), None])); |
| 92 | + check_keys!(UInt16Array::from(vec![Some(0), Some(1), None])); |
| 93 | + check_keys!(UInt32Array::from(vec![Some(0), Some(1), None])); |
| 94 | + check_keys!(UInt64Array::from(vec![Some(0), Some(1), None])); |
| 95 | + |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn nested_dictionary_needles_preserve_nulls() -> Result<()> { |
| 101 | + let values: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), Some(2), None])); |
| 102 | + let inner: ArrayRef = Arc::new(DictionaryArray::try_new( |
| 103 | + Int8Array::from(vec![0, 1, 2]), |
| 104 | + values, |
| 105 | + )?); |
| 106 | + let needles = DictionaryArray::try_new( |
| 107 | + Int16Array::from(vec![Some(0), Some(1), Some(2), None]), |
| 108 | + inner, |
| 109 | + )?; |
| 110 | + let filter = instantiate_static_filter( |
| 111 | + Arc::new(Int32Array::from(vec![1, 3])), |
| 112 | + needles.data_type(), |
| 113 | + )?; |
| 114 | + |
| 115 | + assert_eq!( |
| 116 | + filter.contains(&needles, false)?, |
| 117 | + BooleanArray::from(vec![Some(true), Some(false), None, None]) |
| 118 | + ); |
| 119 | + assert_eq!( |
| 120 | + filter.contains(&needles, true)?, |
| 121 | + BooleanArray::from(vec![Some(false), Some(true), None, None]) |
| 122 | + ); |
| 123 | + |
| 124 | + Ok(()) |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn dictionary_timestamp_needles_keep_timezone_compatibility() -> Result<()> { |
| 129 | + let timestamps: ArrayRef = |
| 130 | + Arc::new(TimestampNanosecondArray::from(vec![1, 3]).with_timezone("UTC")); |
| 131 | + let values: ArrayRef = Arc::new( |
| 132 | + TimestampNanosecondArray::from(vec![1, 2]).with_timezone("Europe/Paris"), |
| 133 | + ); |
| 134 | + let needles = DictionaryArray::try_new(Int8Array::from(vec![0, 1]), values)?; |
| 135 | + let filter = instantiate_static_filter(timestamps, needles.data_type())?; |
| 136 | + assert_eq!( |
| 137 | + filter.contains(&needles, false)?, |
| 138 | + BooleanArray::from(vec![true, false]) |
| 139 | + ); |
| 140 | + |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn dictionary_decimal_needles_keep_precision_scale_compatibility() -> Result<()> { |
| 146 | + // Five list values use the hash-set filter. |
| 147 | + let decimals: ArrayRef = Arc::new( |
| 148 | + Decimal128Array::from(vec![1, 3, 5, 7, 9]).with_precision_and_scale(10, 2)?, |
| 149 | + ); |
| 150 | + let values: ArrayRef = |
| 151 | + Arc::new(Decimal128Array::from(vec![1, 2]).with_precision_and_scale(11, 3)?); |
| 152 | + let needles = DictionaryArray::try_new(Int8Array::from(vec![0, 1]), values)?; |
| 153 | + let filter = instantiate_static_filter(decimals, needles.data_type())?; |
| 154 | + assert_eq!( |
| 155 | + filter.contains(&needles, false)?, |
| 156 | + BooleanArray::from(vec![true, false]) |
| 157 | + ); |
| 158 | + |
| 159 | + Ok(()) |
| 160 | + } |
| 161 | +} |
0 commit comments