From 3ea9453aa9d56530da0e2ff7579fb83dcce37808 Mon Sep 17 00:00:00 2001 From: baishen Date: Thu, 7 Apr 2022 14:05:19 +0800 Subject: [PATCH] fix(function): Const(Nullable(Object)) column downcast to Nullable(Object) failed --- common/datavalues/src/columns/array/mod.rs | 4 ++ common/datavalues/src/columns/boolean/mod.rs | 4 ++ common/datavalues/src/columns/column.rs | 2 + common/datavalues/src/columns/const_/mod.rs | 4 ++ common/datavalues/src/columns/null/mod.rs | 4 ++ common/datavalues/src/columns/nullable/mod.rs | 4 ++ common/datavalues/src/columns/object/mod.rs | 4 ++ .../datavalues/src/columns/primitive/mod.rs | 4 ++ common/datavalues/src/columns/series.rs | 4 +- common/datavalues/src/columns/string/mod.rs | 4 ++ common/datavalues/src/columns/struct_/mod.rs | 4 ++ .../functions/src/scalars/function_adapter.rs | 52 +++++++++---------- 12 files changed, 66 insertions(+), 28 deletions(-) diff --git a/common/datavalues/src/columns/array/mod.rs b/common/datavalues/src/columns/array/mod.rs index 38ebe787e0a7..e1ac8d90a0f6 100644 --- a/common/datavalues/src/columns/array/mod.rs +++ b/common/datavalues/src/columns/array/mod.rs @@ -91,6 +91,10 @@ impl Column for ArrayColumn { self.data_type.clone() } + fn column_type_name(&self) -> String { + "Array".to_string() + } + fn len(&self) -> usize { self.offsets.len() - 1 } diff --git a/common/datavalues/src/columns/boolean/mod.rs b/common/datavalues/src/columns/boolean/mod.rs index 53d85d8df0a9..f3a14a71f50c 100644 --- a/common/datavalues/src/columns/boolean/mod.rs +++ b/common/datavalues/src/columns/boolean/mod.rs @@ -75,6 +75,10 @@ impl Column for BooleanColumn { BooleanType::arc() } + fn column_type_name(&self) -> String { + "Boolean".to_string() + } + fn len(&self) -> usize { self.values.len() } diff --git a/common/datavalues/src/columns/column.rs b/common/datavalues/src/columns/column.rs index ad0f9fdd3cd6..16b902a64f3e 100644 --- a/common/datavalues/src/columns/column.rs +++ b/common/datavalues/src/columns/column.rs @@ -39,6 +39,8 @@ pub trait Column: Send + Sync { } fn data_type(&self) -> DataTypePtr; + fn column_type_name(&self) -> String; + fn is_nullable(&self) -> bool { false } diff --git a/common/datavalues/src/columns/const_/mod.rs b/common/datavalues/src/columns/const_/mod.rs index 2e3d387a0164..accd1b08762a 100644 --- a/common/datavalues/src/columns/const_/mod.rs +++ b/common/datavalues/src/columns/const_/mod.rs @@ -50,6 +50,10 @@ impl Column for ConstColumn { self.column.data_type() } + fn column_type_name(&self) -> String { + format!("Const({})", self.column.column_type_name()) + } + fn is_nullable(&self) -> bool { self.column.is_nullable() } diff --git a/common/datavalues/src/columns/null/mod.rs b/common/datavalues/src/columns/null/mod.rs index cf4105ff8b43..29050f79b587 100644 --- a/common/datavalues/src/columns/null/mod.rs +++ b/common/datavalues/src/columns/null/mod.rs @@ -55,6 +55,10 @@ impl Column for NullColumn { Arc::new(NullType {}) } + fn column_type_name(&self) -> String { + "Null".to_string() + } + fn is_null(&self) -> bool { true } diff --git a/common/datavalues/src/columns/nullable/mod.rs b/common/datavalues/src/columns/nullable/mod.rs index ec25021107a9..c0a19dcba1a0 100644 --- a/common/datavalues/src/columns/nullable/mod.rs +++ b/common/datavalues/src/columns/nullable/mod.rs @@ -71,6 +71,10 @@ impl Column for NullableColumn { Arc::new(NullableType::create(nest)) } + fn column_type_name(&self) -> String { + format!("Nullable({})", self.column.column_type_name()) + } + fn is_nullable(&self) -> bool { true } diff --git a/common/datavalues/src/columns/object/mod.rs b/common/datavalues/src/columns/object/mod.rs index 17cd167dea04..3f4d5e52a1a4 100644 --- a/common/datavalues/src/columns/object/mod.rs +++ b/common/datavalues/src/columns/object/mod.rs @@ -101,6 +101,10 @@ impl Column for ObjectColumn { T::data_type() } + fn column_type_name(&self) -> String { + "Object".to_string() + } + fn len(&self) -> usize { self.values.len() } diff --git a/common/datavalues/src/columns/primitive/mod.rs b/common/datavalues/src/columns/primitive/mod.rs index 136398c3c654..c1375fb1971d 100644 --- a/common/datavalues/src/columns/primitive/mod.rs +++ b/common/datavalues/src/columns/primitive/mod.rs @@ -156,6 +156,10 @@ impl Column for PrimitiveColumn { create_primitive_datatype::() } + fn column_type_name(&self) -> String { + "Primitive".to_string() + } + fn len(&self) -> usize { self.values.len() } diff --git a/common/datavalues/src/columns/series.rs b/common/datavalues/src/columns/series.rs index 8427f345eb1b..960df3212f41 100644 --- a/common/datavalues/src/columns/series.rs +++ b/common/datavalues/src/columns/series.rs @@ -48,7 +48,7 @@ impl Series { .ok_or_else(|| { ErrorCode::UnknownColumn(format!( "downcast column error, column type: {:?}, expected column: {:?}", - column.data_type(), + column.column_type_name(), std::any::type_name::(), )) }); @@ -59,7 +59,7 @@ impl Series { let arr = column.as_any().downcast_ref::().ok_or_else(|| { ErrorCode::UnknownColumn(format!( "downcast column error, column type: {:?}, expected column: {:?}", - column.data_type(), + column.column_type_name(), std::any::type_name::(), )) }); diff --git a/common/datavalues/src/columns/string/mod.rs b/common/datavalues/src/columns/string/mod.rs index cf21b5d22af6..acf8a3c6f53d 100644 --- a/common/datavalues/src/columns/string/mod.rs +++ b/common/datavalues/src/columns/string/mod.rs @@ -143,6 +143,10 @@ impl Column for StringColumn { StringType::arc() } + fn column_type_name(&self) -> String { + "String".to_string() + } + fn len(&self) -> usize { self.offsets.len() - 1 } diff --git a/common/datavalues/src/columns/struct_/mod.rs b/common/datavalues/src/columns/struct_/mod.rs index 6a123ca38570..937e24e79044 100644 --- a/common/datavalues/src/columns/struct_/mod.rs +++ b/common/datavalues/src/columns/struct_/mod.rs @@ -71,6 +71,10 @@ impl Column for StructColumn { self.data_type.clone() } + fn column_type_name(&self) -> String { + "Struct".to_string() + } + fn len(&self) -> usize { self.values[0].len() } diff --git a/common/functions/src/scalars/function_adapter.rs b/common/functions/src/scalars/function_adapter.rs index f695010e7e0d..48b930aa05bd 100644 --- a/common/functions/src/scalars/function_adapter.rs +++ b/common/functions/src/scalars/function_adapter.rs @@ -128,6 +128,31 @@ impl Function for FunctionAdapter { return inner.eval(columns, input_rows); } + // is there nullable constant? Did not consider this case + // unwrap constant + if self.passthrough_constant() && columns.iter().all(|v| v.column().is_const()) { + let columns = columns + .iter() + .map(|v| { + let c = v.column(); + let c: &ConstColumn = unsafe { Series::static_cast(c) }; + + ColumnWithField::new(c.inner().clone(), v.field().clone()) + }) + .collect::>(); + + let col = self.eval(&columns, 1)?; + let col = if col.is_const() && col.len() == 1 { + col.replicate(&[input_rows]) + } else if col.is_null() { + NullColumn::new(input_rows).arc() + } else { + ConstColumn::new(col, input_rows).arc() + }; + + return Ok(col); + } + // nullable or null if self.passthrough_null { if columns @@ -180,7 +205,7 @@ impl Function for FunctionAdapter { }); let col = if col.is_nullable() { - let nullable_column: &NullableColumn = unsafe { Series::static_cast(&col) }; + let nullable_column: &NullableColumn = Series::check_get(&col)?; NullableColumn::new(nullable_column.inner().clone(), validity) } else { NullableColumn::new(col, validity) @@ -189,31 +214,6 @@ impl Function for FunctionAdapter { } } - // is there nullable constant? Did not consider this case - // unwrap constant - if self.passthrough_constant() && columns.iter().all(|v| v.column().is_const()) { - let columns = columns - .iter() - .map(|v| { - let c = v.column(); - let c: &ConstColumn = unsafe { Series::static_cast(c) }; - - ColumnWithField::new(c.inner().clone(), v.field().clone()) - }) - .collect::>(); - - let col = self.eval(&columns, 1)?; - let col = if col.is_const() && col.len() == 1 { - col.replicate(&[input_rows]) - } else if col.is_null() { - NullColumn::new(input_rows).arc() - } else { - ConstColumn::new(col, input_rows).arc() - }; - - return Ok(col); - } - inner.eval(columns, input_rows) }