Skip to main content

arrow_string/
binary_like.rs

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//! Provide SQL's CONTAINS, STARTS_WITH, ENDS_WITH operators for Arrow's binary arrays
6
7use crate::binary_predicate::BinaryPredicate;
8
9use arrow_array::cast::AsArray;
10use arrow_array::*;
11use arrow_schema::*;
12use arrow_select::take::take;
13
14#[derive(Debug)]
15pub(crate) enum Op {
16    Contains,
17    StartsWith,
18    EndsWith,
19}
20
21impl TryFrom<crate::like::Op> for Op {
22    type Error = ArrowError;
23
24    fn try_from(value: crate::like::Op) -> Result<Self, Self::Error> {
25        match value {
26            crate::like::Op::Contains => Ok(Op::Contains),
27            crate::like::Op::StartsWith => Ok(Op::StartsWith),
28            crate::like::Op::EndsWith => Ok(Op::EndsWith),
29            _ => Err(ArrowError::InvalidArgumentError(format!(
30                "Invalid binary operation: {value}"
31            ))),
32        }
33    }
34}
35
36impl std::fmt::Display for Op {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Op::Contains => write!(f, "CONTAINS"),
40            Op::StartsWith => write!(f, "STARTS_WITH"),
41            Op::EndsWith => write!(f, "ENDS_WITH"),
42        }
43    }
44}
45
46pub(crate) fn binary_apply<'a, T: BinaryArrayType<'a> + 'a>(
47    op: Op,
48    l: T,
49    l_s: bool,
50    l_v: Option<&'a dyn AnyDictionaryArray>,
51    r: T,
52    r_s: bool,
53    r_v: Option<&'a dyn AnyDictionaryArray>,
54) -> Result<BooleanArray, ArrowError> {
55    let l_len = l_v.map_or_else(|| l.len(), |l| l.len());
56    // A valid dictionary without values can only contain null keys.
57    if l_v.is_some_and(|dict| dict.values().is_empty())
58        || r_v.is_some_and(|dict| dict.values().is_empty())
59    {
60        let len = if l_s {
61            r_v.map_or_else(|| r.len(), |r| r.len())
62        } else {
63            l_len
64        };
65        return Ok(BooleanArray::new_null(len));
66    }
67    if r_s {
68        let idx = match r_v {
69            Some(dict) if dict.null_count() != 0 => return Ok(BooleanArray::new_null(l_len)),
70            Some(dict) => dict.normalized_keys()[0],
71            None => 0,
72        };
73        if r.is_null(idx) {
74            return Ok(BooleanArray::new_null(l_len));
75        }
76        op_scalar::<T>(op, l, l_v, r.value(idx))
77    } else {
78        match (l_s, l_v, r_v) {
79            (true, None, None) => {
80                let v = l.is_valid(0).then(|| l.value(0));
81                op_binary(op, std::iter::repeat(v), r.iter())
82            }
83            (true, Some(l_v), None) => {
84                let idx = l_v.is_valid(0).then(|| l_v.normalized_keys()[0]);
85                let v = idx.and_then(|idx| l.is_valid(idx).then(|| l.value(idx)));
86                op_binary(op, std::iter::repeat(v), r.iter())
87            }
88            (true, None, Some(r_v)) => {
89                let v = l.is_valid(0).then(|| l.value(0));
90                op_binary(op, std::iter::repeat(v), vectored_iter(r, r_v))
91            }
92            (true, Some(l_v), Some(r_v)) => {
93                let idx = l_v.is_valid(0).then(|| l_v.normalized_keys()[0]);
94                let v = idx.and_then(|idx| l.is_valid(idx).then(|| l.value(idx)));
95                op_binary(op, std::iter::repeat(v), vectored_iter(r, r_v))
96            }
97            (false, None, None) => op_binary(op, l.iter(), r.iter()),
98            (false, Some(l_v), None) => op_binary(op, vectored_iter(l, l_v), r.iter()),
99            (false, None, Some(r_v)) => op_binary(op, l.iter(), vectored_iter(r, r_v)),
100            (false, Some(l_v), Some(r_v)) => {
101                op_binary(op, vectored_iter(l, l_v), vectored_iter(r, r_v))
102            }
103        }
104    }
105}
106
107#[inline(never)]
108fn op_scalar<'a, T: BinaryArrayType<'a>>(
109    op: Op,
110    l: T,
111    l_v: Option<&dyn AnyDictionaryArray>,
112    r: &[u8],
113) -> Result<BooleanArray, ArrowError> {
114    let r = match op {
115        Op::Contains => BinaryPredicate::contains(r).evaluate_array(l, false),
116        Op::StartsWith => BinaryPredicate::StartsWith(r).evaluate_array(l, false),
117        Op::EndsWith => BinaryPredicate::EndsWith(r).evaluate_array(l, false),
118    };
119
120    Ok(match l_v {
121        Some(v) => take(&r, v.keys(), None)?.as_boolean().clone(),
122        None => r,
123    })
124}
125
126fn vectored_iter<'a, T: BinaryArrayType<'a> + 'a>(
127    a: T,
128    a_v: &'a dyn AnyDictionaryArray,
129) -> impl Iterator<Item = Option<&'a [u8]>> + 'a {
130    let nulls = a_v.nulls();
131    let keys = a_v.normalized_keys();
132    keys.into_iter().enumerate().map(move |(idx, key)| {
133        if nulls.is_some_and(|n| n.is_null(idx)) || a.is_null(key) {
134            return None;
135        }
136        Some(a.value(key))
137    })
138}
139
140#[inline(never)]
141fn op_binary<'a>(
142    op: Op,
143    l: impl Iterator<Item = Option<&'a [u8]>>,
144    r: impl Iterator<Item = Option<&'a [u8]>>,
145) -> Result<BooleanArray, ArrowError> {
146    match op {
147        Op::Contains => Ok(l
148            .zip(r)
149            .map(|(l, r)| Some(bytes_contains(l?, r?)))
150            .collect()),
151        Op::StartsWith => Ok(l
152            .zip(r)
153            .map(|(l, r)| Some(BinaryPredicate::StartsWith(r?).evaluate(l?)))
154            .collect()),
155        Op::EndsWith => Ok(l
156            .zip(r)
157            .map(|(l, r)| Some(BinaryPredicate::EndsWith(r?).evaluate(l?)))
158            .collect()),
159    }
160}
161
162fn bytes_contains(haystack: &[u8], needle: &[u8]) -> bool {
163    memchr::memmem::find(haystack, needle).is_some()
164}