viirya commented on code in PR #307:
URL: https://github.com/apache/datafusion-comet/pull/307#discussion_r1581872458


##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -103,10 +125,72 @@ impl Cast {
             (DataType::LargeUtf8, DataType::Boolean) => {
                 Self::spark_cast_utf8_to_boolean::<i64>(&array, 
self.eval_mode)?
             }
-            _ => cast_with_options(&array, to_type, &CAST_OPTIONS)?,
+            (
+                DataType::Utf8,
+                DataType::Int8 | DataType::Int16 | DataType::Int32 | 
DataType::Int64,
+            ) => Self::cast_string_to_int(to_type, &array, self.eval_mode)?,
+            (
+                DataType::Dictionary(key_type, value_type),
+                DataType::Int8 | DataType::Int16 | DataType::Int32 | 
DataType::Int64,
+            ) if key_type.as_ref() == &DataType::Int32
+                && value_type.as_ref() == &DataType::Utf8 =>
+            {
+                // TODO: we are unpacking a dictionary-encoded array and then 
performing
+                // the cast. We could potentially improve performance here by 
casting the
+                // dictionary values directly without unpacking the array 
first, although this
+                // would add more complexity to the code
+                let unpacked_array = 
Self::unpack_dict_string_array::<Int32Type>(&array)?;
+                Self::cast_string_to_int(to_type, &unpacked_array, 
self.eval_mode)?
+            }
+            _ => {
+                // when we have no Spark-specific casting we delegate to 
DataFusion
+                cast_with_options(&array, to_type, &CAST_OPTIONS)?
+            }
         };
-        let result = spark_cast(cast_result, from_type, to_type);
-        Ok(result)
+        Ok(spark_cast(cast_result, from_type, to_type))
+    }
+
+    fn cast_string_to_int(
+        to_type: &DataType,
+        array: &ArrayRef,
+        eval_mode: EvalMode,
+    ) -> CometResult<ArrayRef> {
+        let string_array = array
+            .as_any()
+            .downcast_ref::<GenericStringArray<i32>>()
+            .expect("cast_string_to_int expected a string array");
+
+        let cast_array: ArrayRef = match to_type {
+            DataType::Int8 => {
+                cast_utf8_to_int!(string_array, eval_mode, Int8Type, 
cast_string_to_i8)?
+            }
+            DataType::Int16 => {
+                cast_utf8_to_int!(string_array, eval_mode, Int16Type, 
cast_string_to_i16)?
+            }
+            DataType::Int32 => {
+                cast_utf8_to_int!(string_array, eval_mode, Int32Type, 
cast_string_to_i32)?
+            }
+            DataType::Int64 => {
+                cast_utf8_to_int!(string_array, eval_mode, Int64Type, 
cast_string_to_i64)?
+            }
+            dt => unreachable!(
+                "{}",
+                format!("invalid integer type {dt} in cast from string")
+            ),
+        };
+        Ok(cast_array)
+    }
+
+    fn unpack_dict_string_array<T: ArrowDictionaryKeyType>(
+        array: &ArrayRef,
+    ) -> DataFusionResult<ArrayRef> {
+        let dict_array = array
+            .as_any()
+            .downcast_ref::<DictionaryArray<T>>()
+            .expect("DictionaryArray<T>");
+
+        let unpacked_array = take(dict_array.values().as_ref(), 
dict_array.keys(), None)?;
+        Ok(unpacked_array)
     }

Review Comment:
   Just call `cast_with_options` from arrow-rs? For dictionary type, it will 
call `unpack_dictionary`
   
   
https://github.com/apache/arrow-rs/blob/a61f1dc8ea132add731e4426e11d341a1de5ca92/arrow-cast/src/cast/dictionary.rs#L93
   
   which looks like the same as this implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to