-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathbinary_pad.rs
More file actions
114 lines (103 loc) · 4.28 KB
/
binary_pad.rs
File metadata and controls
114 lines (103 loc) · 4.28 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
// 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::builder::BinaryBuilder;
use arrow::array::{Array, ArrayRef, AsArray};
use arrow::datatypes::DataType;
use datafusion::common::{DataFusionError, ScalarValue};
use datafusion::physical_plan::ColumnarValue;
use std::sync::Arc;
/// Spark's ByteArray.lpad: left-pad binary array with cyclic pattern.
pub fn spark_binary_lpad(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
binary_pad_impl(args, true)
}
/// Spark's ByteArray.rpad: right-pad binary array with cyclic pattern.
pub fn spark_binary_rpad(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
binary_pad_impl(args, false)
}
fn binary_pad_impl(
args: &[ColumnarValue],
is_left_pad: bool,
) -> Result<ColumnarValue, DataFusionError> {
match args {
[ColumnarValue::Array(array), ColumnarValue::Scalar(ScalarValue::Int32(Some(len))), ColumnarValue::Scalar(ScalarValue::Binary(Some(pad)))] =>
{
let len = *len;
match array.data_type() {
DataType::Binary => {
let binary_array = array.as_binary::<i32>();
let mut builder = BinaryBuilder::with_capacity(binary_array.len(), 0);
for i in 0..binary_array.len() {
if binary_array.is_null(i) {
builder.append_null();
} else {
let bytes = binary_array.value(i);
let result = pad_bytes(bytes, len as usize, pad, is_left_pad);
builder.append_value(&result);
}
}
Ok(ColumnarValue::Array(Arc::new(builder.finish()) as ArrayRef))
}
other => Err(DataFusionError::Internal(format!(
"Unsupported data type {other:?} for binary_pad",
))),
}
}
other => Err(DataFusionError::Internal(format!(
"Unsupported arguments {other:?} for binary_pad",
))),
}
}
/// Pad bytes to target length using cyclic pad pattern.
/// Matches Spark's ByteArray.lpad/rpad behavior.
fn pad_bytes(bytes: &[u8], len: usize, pad: &[u8], is_left_pad: bool) -> Vec<u8> {
if len == 0 {
return Vec::new();
}
if pad.is_empty() {
// Empty pattern: return first `len` bytes or copy of input
let take = bytes.len().min(len);
return bytes[..take].to_vec();
}
let mut result = vec![0u8; len];
let min_len = bytes.len().min(len);
if is_left_pad {
// Copy input bytes to the right side of result
result[len - min_len..].copy_from_slice(&bytes[..min_len]);
// Fill remaining left side with pad pattern
if bytes.len() < len {
fill_with_pattern(&mut result, 0, len - bytes.len(), pad);
}
} else {
// Copy input bytes to the left side of result
result[..min_len].copy_from_slice(&bytes[..min_len]);
// Fill remaining right side with pad pattern
if bytes.len() < len {
fill_with_pattern(&mut result, bytes.len(), len, pad);
}
}
result
}
/// Fill result[first_pos..beyond_pos] with cyclic pad pattern.
fn fill_with_pattern(result: &mut [u8], first_pos: usize, beyond_pos: usize, pad: &[u8]) {
let mut pos = first_pos;
while pos < beyond_pos {
let remaining = beyond_pos - pos;
let take = pad.len().min(remaining);
result[pos..pos + take].copy_from_slice(&pad[..take]);
pos += take;
}
}