forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_contains.rs
More file actions
168 lines (150 loc) · 5.78 KB
/
array_contains.rs
File metadata and controls
168 lines (150 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use arrow::array::{
Array, AsArray, BooleanArray, BooleanBufferBuilder, GenericListArray, OffsetSizeTrait,
};
use arrow::buffer::{BooleanBuffer, NullBuffer};
use arrow::datatypes::DataType;
use datafusion_common::{Result, exec_err};
use datafusion_expr::{
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
};
use datafusion_functions_nested::array_has::array_has_udf;
use std::any::Any;
use std::sync::Arc;
/// Spark-compatible `array_contains` function.
///
/// Calls DataFusion's `array_has` and then applies Spark's null semantics:
/// - If the result from `array_has` is `true`, return `true`.
/// - If the result is `false` and the input array row contains any null elements,
/// return `null` (because the element might have been the null).
/// - If the result is `false` and the input array row has no null elements,
/// return `false`.
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SparkArrayContains {
signature: Signature,
}
impl Default for SparkArrayContains {
fn default() -> Self {
Self::new()
}
}
impl SparkArrayContains {
pub fn new() -> Self {
Self {
signature: Signature::array_and_element(Volatility::Immutable),
}
}
}
impl ScalarUDFImpl for SparkArrayContains {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"array_contains"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _: &[DataType]) -> Result<DataType> {
Ok(DataType::Boolean)
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
let haystack = args.args[0].clone();
let array_has_result = array_has_udf().invoke_with_args(args)?;
let result_array = array_has_result.to_array(1)?;
let patched = apply_spark_null_semantics(result_array.as_boolean(), &haystack)?;
Ok(ColumnarValue::Array(Arc::new(patched)))
}
}
/// For each row where `array_has` returned `false`, set the output to null
/// if that row's input array contains any null elements.
fn apply_spark_null_semantics(
result: &BooleanArray,
haystack_arg: &ColumnarValue,
) -> Result<BooleanArray> {
// happy path
if result.false_count() == 0 || haystack_arg.data_type() == DataType::Null {
return Ok(result.clone());
}
let haystack = haystack_arg.to_array_of_size(result.len())?;
let row_has_nulls = compute_row_has_nulls(&haystack)?;
// A row keeps its validity when result is true OR the row has no nulls.
let keep_mask = result.values() | &!&row_has_nulls;
let new_validity = match result.nulls() {
Some(n) => n.inner() & &keep_mask,
None => keep_mask,
};
Ok(BooleanArray::new(
result.values().clone(),
Some(NullBuffer::new(new_validity)),
))
}
/// Returns a per-row bitmap where bit i is set if row i's list contains any null element.
fn compute_row_has_nulls(haystack: &dyn Array) -> Result<BooleanBuffer> {
match haystack.data_type() {
DataType::List(_) => generic_list_row_has_nulls(haystack.as_list::<i32>()),
DataType::LargeList(_) => generic_list_row_has_nulls(haystack.as_list::<i64>()),
DataType::FixedSizeList(_, _) => {
let list = haystack.as_fixed_size_list();
let buf = match list.values().nulls() {
Some(nulls) => {
let validity = nulls.inner();
let vl = list.value_length() as usize;
let mut builder = BooleanBufferBuilder::new(list.len());
for i in 0..list.len() {
builder.append(validity.slice(i * vl, vl).count_set_bits() < vl);
}
builder.finish()
}
None => BooleanBuffer::new_unset(list.len()),
};
Ok(mask_with_list_nulls(buf, list.nulls()))
}
dt => exec_err!("compute_row_has_nulls: unsupported data type {dt}"),
}
}
/// Computes per-row null presence for `List` and `LargeList` arrays.
fn generic_list_row_has_nulls<O: OffsetSizeTrait>(
list: &GenericListArray<O>,
) -> Result<BooleanBuffer> {
let buf = match list.values().nulls() {
Some(nulls) => {
let validity = nulls.inner();
let offsets = list.offsets();
let mut builder = BooleanBufferBuilder::new(list.len());
for i in 0..list.len() {
let s = offsets[i].as_usize();
let len = offsets[i + 1].as_usize() - s;
builder.append(validity.slice(s, len).count_set_bits() < len);
}
builder.finish()
}
None => BooleanBuffer::new_unset(list.len()),
};
Ok(mask_with_list_nulls(buf, list.nulls()))
}
/// Rows where the list itself is null should not be marked as "has nulls".
fn mask_with_list_nulls(
buf: BooleanBuffer,
list_nulls: Option<&NullBuffer>,
) -> BooleanBuffer {
match list_nulls {
Some(n) => &buf & n.inner(),
None => buf,
}
}