-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add expression array_size #1122
base: main
Are you sure you want to change the base?
Changes from 3 commits
22617c1
c33d711
ae100e1
f2512ca
afc926d
b078747
6404a83
938de8d
1a1efb1
15ba494
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -708,6 +708,91 @@ impl PartialEq<dyn Any> for ArrayInsert { | |
} | ||
} | ||
|
||
#[derive(Debug, Hash)] | ||
pub struct ArraySize { | ||
src_array_expr: Arc<dyn PhysicalExpr>, | ||
} | ||
|
||
impl ArraySize { | ||
pub fn new(src_array_expr: Arc<dyn PhysicalExpr>) -> Self { | ||
Self { src_array_expr } | ||
} | ||
} | ||
|
||
impl Display for ArraySize { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "ArraySize [array: {:?}]", self.src_array_expr) | ||
} | ||
} | ||
|
||
impl PartialEq<dyn Any> for ArraySize { | ||
fn eq(&self, other: &dyn Any) -> bool { | ||
down_cast_any_ref(other) | ||
.downcast_ref::<Self>() | ||
.map(|x| self.src_array_expr.eq(&x.src_array_expr)) | ||
.unwrap_or(false) | ||
} | ||
} | ||
|
||
impl PhysicalExpr for ArraySize { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn data_type(&self, _input_schema: &Schema) -> DataFusionResult<DataType> { | ||
Ok(DataType::Int32) | ||
} | ||
|
||
fn nullable(&self, input_schema: &Schema) -> DataFusionResult<bool> { | ||
self.src_array_expr.nullable(input_schema) | ||
} | ||
|
||
fn evaluate(&self, batch: &RecordBatch) -> DataFusionResult<ColumnarValue> { | ||
let array_value = self | ||
.src_array_expr | ||
.evaluate(batch)? | ||
.into_array(batch.num_rows())?; | ||
match array_value.data_type() { | ||
DataType::List(_) => { | ||
let list_array = as_list_array(&array_value)?; | ||
let mut builder = Int32Array::builder(list_array.len()); | ||
for i in 0..list_array.len() { | ||
if list_array.is_null(i) { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(list_array.value_length(i)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will |
||
} | ||
} | ||
let sizes_array = Int32Array::from(builder.finish()); | ||
Ok(ColumnarValue::Array(Arc::new(sizes_array))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a more efficient way to do this? |
||
} | ||
_ => Err(DataFusionError::Internal(format!( | ||
"Unexpected data type in ArraySize: {:?}", | ||
array_value.data_type() | ||
))), | ||
} | ||
} | ||
|
||
fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { | ||
vec![&self.src_array_expr] | ||
} | ||
fn with_new_children( | ||
Groennbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self: Arc<Self>, | ||
children: Vec<Arc<dyn PhysicalExpr>>, | ||
) -> datafusion_common::Result<Arc<dyn PhysicalExpr>> { | ||
match children.len() { | ||
1 => Ok(Arc::new(ArraySize::new(Arc::clone(&children[0])))), | ||
_ => internal_err!("ListExtract should have exactly two children"), | ||
Groennbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
fn dyn_hash(&self, state: &mut dyn Hasher) { | ||
let mut s = state; | ||
self.src_array_expr.hash(&mut s); | ||
self.hash(&mut s); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::list::{array_insert, list_extract, zero_based_index}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2220,6 +2220,16 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim | |
expr.children(2)) | ||
None | ||
} | ||
case Size(child, _) if child.dataType.isInstanceOf[ArrayType] => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This either needs to handle when |
||
val srcExprProto = exprToProto(child, inputs, binding) | ||
val arraySizeBuilder = ExprOuterClass.ArraySize | ||
.newBuilder() | ||
.setSrcArrayExpr(srcExprProto.get) | ||
Some( | ||
ExprOuterClass.Expr | ||
.newBuilder() | ||
.setArraySize(arraySizeBuilder) | ||
.build()) | ||
|
||
case ElementAt(child, ordinal, defaultValue, failOnError) | ||
if child.dataType.isInstanceOf[ArrayType] => | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to handle
legacySizeOfNull
here