-
Notifications
You must be signed in to change notification settings - Fork 752
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
Showing
14 changed files
with
333 additions
and
27 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
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
141 changes: 141 additions & 0 deletions
141
common/functions/src/scalars/semi_structureds/array_get.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,141 @@ | ||
// 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 common_datavalues::prelude::*; | ||
use common_datavalues::with_match_scalar_types_error; | ||
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 ArrayGetFunction { | ||
array_type: ArrayType, | ||
display_name: String, | ||
} | ||
|
||
impl ArrayGetFunction { | ||
pub fn try_create(display_name: &str, args: &[&DataTypeImpl]) -> Result<Box<dyn Function>> { | ||
let data_type = args[0]; | ||
let path_type = args[1]; | ||
|
||
if !data_type.data_type_id().is_array() || !path_type.data_type_id().is_integer() { | ||
return Err(ErrorCode::IllegalDataType(format!( | ||
"Invalid argument types for function '{}': ({:?}, {:?})", | ||
display_name.to_uppercase(), | ||
data_type.data_type_id(), | ||
path_type.data_type_id() | ||
))); | ||
} | ||
|
||
let array_type: ArrayType = data_type.clone().try_into()?; | ||
Ok(Box::new(ArrayGetFunction { | ||
array_type, | ||
display_name: display_name.to_string(), | ||
})) | ||
} | ||
|
||
pub fn desc() -> FunctionDescription { | ||
FunctionDescription::creator(Box::new(Self::try_create)) | ||
.features(FunctionFeatures::default().deterministic().num_arguments(2)) | ||
} | ||
} | ||
|
||
impl Function for ArrayGetFunction { | ||
fn name(&self) -> &str { | ||
&*self.display_name | ||
} | ||
|
||
fn return_type(&self) -> DataTypeImpl { | ||
NullableType::new_impl(self.array_type.inner_type().clone()) | ||
} | ||
|
||
fn eval( | ||
&self, | ||
_func_ctx: FunctionContext, | ||
columns: &ColumnsWithField, | ||
input_rows: usize, | ||
) -> Result<ColumnRef> { | ||
let array_column: &ArrayColumn = if columns[0].column().is_const() { | ||
let const_column: &ConstColumn = Series::check_get(columns[0].column())?; | ||
Series::check_get(const_column.inner())? | ||
} else { | ||
Series::check_get(columns[0].column())? | ||
}; | ||
|
||
let inner_type = self.array_type.inner_type().data_type_id(); | ||
with_match_scalar_types_error!(inner_type.to_physical_type(), |$T| { | ||
let inner_column: &<$T as Scalar>::ColumnType = Series::check_get(array_column.values())?; | ||
let mut builder = NullableColumnBuilder::<$T>::with_capacity(input_rows); | ||
if columns[1].column().is_const() { | ||
let const_column: &ConstColumn = Series::check_get(columns[1].column())?; | ||
let index = const_column.get(0).as_u64()? as usize; | ||
|
||
let mut offset = 0; | ||
for i in 0..input_rows { | ||
let len = array_column.size_at_index(i); | ||
if index < len { | ||
builder.append(inner_column.get_data(offset + index), true); | ||
} else { | ||
return Err(ErrorCode::BadArguments(format!( | ||
"Index out of array column bounds: the len is {} but the index is {}", | ||
len, index | ||
))); | ||
} | ||
offset += len; | ||
} | ||
} else if columns[0].column().is_const() { | ||
let len = array_column.size_at_index(0); | ||
for i in 0..input_rows { | ||
let index = columns[1].column().get(i).as_u64()? as usize; | ||
if index < len { | ||
builder.append(inner_column.get_data(index), true); | ||
} else { | ||
return Err(ErrorCode::BadArguments(format!( | ||
"Index out of array column bounds: the len is {} but the index is {}", | ||
len, index | ||
))); | ||
} | ||
} | ||
} else { | ||
let mut offset = 0; | ||
for i in 0..input_rows { | ||
let index = columns[1].column().get(i).as_u64()? as usize; | ||
let len = array_column.size_at_index(i); | ||
if index < len { | ||
builder.append(inner_column.get_data(offset + index), true); | ||
} else { | ||
return Err(ErrorCode::BadArguments(format!( | ||
"Index out of array column bounds: the len is {} but the index is {}", | ||
len, index | ||
))); | ||
} | ||
offset += len; | ||
} | ||
} | ||
Ok(builder.build(input_rows)) | ||
}) | ||
} | ||
} | ||
|
||
impl fmt::Display for ArrayGetFunction { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.display_name.to_uppercase()) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,8 @@ NULL | |
2 2 | ||
1 NULL | ||
2 2 | ||
==get from array table== | ||
1 10 | ||
2 50 | ||
1 20 | ||
2 60 |
Oops, something went wrong.