-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cdf2e6
commit abd4682
Showing
5 changed files
with
210 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// 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 std::{fmt::Debug, sync::Arc}; | ||
|
||
use anyhow::Context; | ||
use arrow::{ | ||
array::{BinaryArray, RecordBatch}, | ||
buffer::OffsetBuffer, | ||
}; | ||
use arrow_schema::DataType; | ||
use macros::ensure; | ||
|
||
use crate::Result; | ||
|
||
pub trait MergeOperator: Send + Sync + Debug { | ||
fn merge(&self, _batch: &RecordBatch) -> Result<RecordBatch>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct LastValueOperator; | ||
|
||
impl MergeOperator for LastValueOperator { | ||
fn merge(&self, batch: &RecordBatch) -> Result<RecordBatch> { | ||
let last_row = batch.slice(batch.num_rows() - 1, 1); | ||
Ok(last_row) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct BytesMergeOperator { | ||
/// Column index of the column need to append together | ||
/// The column type must be `Binary`. | ||
value_idxes: Vec<usize>, | ||
} | ||
|
||
impl BytesMergeOperator { | ||
pub fn new(value_idxes: Vec<usize>) -> Self { | ||
Self { value_idxes } | ||
} | ||
} | ||
|
||
impl MergeOperator for BytesMergeOperator { | ||
fn merge(&self, batch: &RecordBatch) -> Result<RecordBatch> { | ||
assert!(batch.num_rows() > 0); | ||
|
||
for idx in &self.value_idxes { | ||
let data_type = batch.column(*idx).data_type(); | ||
ensure!( | ||
data_type == &DataType::Binary, | ||
"MergeOperator is only used for binary column, current:{data_type}" | ||
); | ||
} | ||
|
||
let columns = batch | ||
.columns() | ||
.iter() | ||
.enumerate() | ||
.map(|(idx, column)| { | ||
if self.value_idxes.contains(&idx) { | ||
// For value column, we append all elements | ||
let binary_array = column.as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
// bytes buffer is cheap for clone. | ||
let byte_buffer = binary_array.values().clone(); | ||
let offsets = OffsetBuffer::from_lengths([byte_buffer.len()]); | ||
let concated_column = BinaryArray::new(offsets, byte_buffer, None); | ||
Arc::new(concated_column) | ||
} else { | ||
// For other columns, we just take the first element since the primary key | ||
// columns are the same. | ||
column.slice(0, 1) | ||
} | ||
}) | ||
.collect(); | ||
|
||
let merged_batch = RecordBatch::try_new(batch.schema(), columns) | ||
.context("failed to construct RecordBatch in BytesMergeOperator.")?; | ||
|
||
Ok(merged_batch) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use arrow::array::{self as arrow_array}; | ||
use datafusion::common::{create_array, record_batch}; | ||
|
||
use super::*; | ||
use crate::arrow_schema; | ||
|
||
#[test] | ||
fn test_last_value_operator() { | ||
let operator = LastValueOperator; | ||
let batch = record_batch!( | ||
("pk1", UInt8, vec![11, 11, 11, 11]), | ||
("pk2", UInt8, vec![100, 100, 100, 100]), | ||
("value", Int64, vec![2, 7, 4, 1]) | ||
) | ||
.unwrap(); | ||
|
||
let actual = operator.merge(&batch).unwrap(); | ||
let expected = record_batch!( | ||
("pk1", UInt8, vec![11]), | ||
("pk2", UInt8, vec![100]), | ||
("value", Int64, vec![1]) | ||
) | ||
.unwrap(); | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
#[test] | ||
fn test_bytes_merge_operator() { | ||
let operator = BytesMergeOperator::new(vec![2]); | ||
let schema = arrow_schema!(("pk1", UInt8), ("pk2", UInt8), ("value", Binary)); | ||
|
||
let batch = RecordBatch::try_new( | ||
schema.clone(), | ||
vec![ | ||
create_array!(UInt8, vec![11, 11, 11, 11]), | ||
create_array!(UInt8, vec![100, 100, 100, 100]), | ||
Arc::new(BinaryArray::from_vec(vec![ | ||
b"one", b"two", b"three", b"four", | ||
])), | ||
], | ||
) | ||
.unwrap(); | ||
|
||
let actual = operator.merge(&batch).unwrap(); | ||
let expected = RecordBatch::try_new( | ||
schema.clone(), | ||
vec![ | ||
create_array!(UInt8, vec![11]), | ||
create_array!(UInt8, vec![100]), | ||
Arc::new(BinaryArray::from_vec(vec![b"onetwothreefour"])), | ||
], | ||
) | ||
.unwrap(); | ||
assert_eq!(actual, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.