forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(function): support length function for Array & Array<T>
- Loading branch information
Showing
16 changed files
with
374 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed 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 super::length::LengthFunction; | ||
use crate::scalars::FunctionFactory; | ||
|
||
pub struct CommonFunction; | ||
|
||
impl CommonFunction { | ||
pub fn register(factory: &mut FunctionFactory) { | ||
factory.register("length", LengthFunction::desc()); | ||
} | ||
} |
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,54 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed 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 common_datavalues::prelude::*; | ||
use common_datavalues::DataTypeImpl; | ||
use common_exception::ErrorCode; | ||
use common_exception::Result; | ||
|
||
use crate::scalars::ArrayLengthFunction; | ||
use crate::scalars::Function; | ||
use crate::scalars::FunctionDescription; | ||
use crate::scalars::FunctionFeatures; | ||
use crate::scalars::StringLengthFunction; | ||
use crate::scalars::VariantArrayLengthFunction; | ||
|
||
pub struct LengthFunction { | ||
_display_name: String, | ||
} | ||
|
||
impl LengthFunction { | ||
pub fn try_create(display_name: &str, args: &[&DataTypeImpl]) -> Result<Box<dyn Function>> { | ||
let data_type = args[0]; | ||
|
||
if data_type.data_type_id().is_string() { | ||
StringLengthFunction::try_create(display_name, args) | ||
} else if data_type.data_type_id().is_array() { | ||
ArrayLengthFunction::try_create(display_name, args) | ||
} else if data_type.data_type_id().is_variant_or_array() { | ||
VariantArrayLengthFunction::try_create(display_name, args) | ||
} else { | ||
Err(ErrorCode::IllegalDataType(format!( | ||
"Invalid argument types for function '{}': ({:?})", | ||
display_name.to_uppercase(), | ||
data_type.data_type_id(), | ||
))) | ||
} | ||
} | ||
|
||
pub fn desc() -> FunctionDescription { | ||
FunctionDescription::creator(Box::new(Self::try_create)) | ||
.features(FunctionFeatures::default().deterministic().num_arguments(1)) | ||
} | ||
} |
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,19 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed 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. | ||
|
||
mod common; | ||
mod length; | ||
|
||
pub use common::CommonFunction; | ||
pub use length::LengthFunction; |
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
84 changes: 84 additions & 0 deletions
84
common/functions/src/scalars/semi_structureds/array_length.rs
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,84 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed 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; | ||
use std::sync::Arc; | ||
|
||
use common_datavalues::prelude::*; | ||
use common_datavalues::DataTypeImpl; | ||
use common_exception::ErrorCode; | ||
use common_exception::Result; | ||
|
||
use crate::scalars::Function; | ||
use crate::scalars::FunctionContext; | ||
use crate::scalars::FunctionDescription; | ||
use crate::scalars::FunctionFeatures; | ||
|
||
#[derive(Clone)] | ||
pub struct ArrayLengthFunction { | ||
display_name: String, | ||
} | ||
|
||
impl ArrayLengthFunction { | ||
pub fn try_create(display_name: &str, args: &[&DataTypeImpl]) -> Result<Box<dyn Function>> { | ||
let data_type = args[0]; | ||
|
||
if !data_type.data_type_id().is_array() { | ||
return Err(ErrorCode::IllegalDataType(format!( | ||
"Invalid argument types for function '{}': ({:?})", | ||
display_name.to_uppercase(), | ||
data_type.data_type_id(), | ||
))); | ||
} | ||
|
||
Ok(Box::new(ArrayLengthFunction { | ||
display_name: display_name.to_string(), | ||
})) | ||
} | ||
|
||
pub fn desc() -> FunctionDescription { | ||
FunctionDescription::creator(Box::new(Self::try_create)) | ||
.features(FunctionFeatures::default().deterministic().num_arguments(1)) | ||
} | ||
} | ||
|
||
impl Function for ArrayLengthFunction { | ||
fn name(&self) -> &str { | ||
&*self.display_name | ||
} | ||
|
||
fn return_type(&self) -> DataTypeImpl { | ||
u64::to_data_type() | ||
} | ||
|
||
fn eval( | ||
&self, | ||
_func_ctx: FunctionContext, | ||
columns: &ColumnsWithField, | ||
input_rows: usize, | ||
) -> Result<ColumnRef> { | ||
let array_column: &ArrayColumn = Series::check_get(columns[0].column())?; | ||
let vec = (0..input_rows) | ||
.into_iter() | ||
.map(|index| array_column.size_at_index(index) as u64) | ||
.collect(); | ||
Ok(Arc::new(PrimitiveColumn::new_from_vec(vec))) | ||
} | ||
} | ||
|
||
impl fmt::Display for ArrayLengthFunction { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}()", self.display_name) | ||
} | ||
} |
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,93 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed 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; | ||
use std::sync::Arc; | ||
|
||
use common_datavalues::prelude::*; | ||
use common_datavalues::DataTypeImpl; | ||
use common_exception::ErrorCode; | ||
use common_exception::Result; | ||
|
||
use crate::scalars::Function; | ||
use crate::scalars::FunctionContext; | ||
use crate::scalars::FunctionDescription; | ||
use crate::scalars::FunctionFeatures; | ||
|
||
#[derive(Clone)] | ||
pub struct VariantArrayLengthFunction { | ||
display_name: String, | ||
} | ||
|
||
impl VariantArrayLengthFunction { | ||
pub fn try_create(display_name: &str, args: &[&DataTypeImpl]) -> Result<Box<dyn Function>> { | ||
let data_type = args[0]; | ||
|
||
if !data_type.data_type_id().is_variant_or_array() { | ||
return Err(ErrorCode::IllegalDataType(format!( | ||
"Invalid argument types for function '{}': ({:?})", | ||
display_name.to_uppercase(), | ||
data_type.data_type_id(), | ||
))); | ||
} | ||
|
||
Ok(Box::new(VariantArrayLengthFunction { | ||
display_name: display_name.to_string(), | ||
})) | ||
} | ||
|
||
pub fn desc() -> FunctionDescription { | ||
FunctionDescription::creator(Box::new(Self::try_create)) | ||
.features(FunctionFeatures::default().deterministic().num_arguments(1)) | ||
} | ||
} | ||
|
||
impl Function for VariantArrayLengthFunction { | ||
fn name(&self) -> &str { | ||
&*self.display_name | ||
} | ||
|
||
fn return_type(&self) -> DataTypeImpl { | ||
u64::to_data_type() | ||
} | ||
|
||
fn eval( | ||
&self, | ||
_func_ctx: FunctionContext, | ||
columns: &ColumnsWithField, | ||
input_rows: usize, | ||
) -> Result<ColumnRef> { | ||
let array_column: &VariantColumn = Series::check_get(columns[0].column())?; | ||
let mut res: Vec<u64> = Vec::with_capacity(input_rows); | ||
|
||
for array in array_column.iter() { | ||
if !array.is_array() { | ||
return Err(ErrorCode::IllegalDataType(format!( | ||
"Invalid argument types for function '{}': Variant is not VariantArray", | ||
self.display_name.to_uppercase(), | ||
))); | ||
} | ||
let len = array.to_owned_scalar().as_array().unwrap().len() as u64; | ||
res.push(len); | ||
} | ||
|
||
Ok(Arc::new(PrimitiveColumn::new_from_vec(res))) | ||
} | ||
} | ||
|
||
impl fmt::Display for VariantArrayLengthFunction { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}()", self.display_name) | ||
} | ||
} |
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
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
Oops, something went wrong.