coderfender commented on issue #3459:
URL:
https://github.com/apache/datafusion-comet/issues/3459#issuecomment-3874427981
@andygrove , the goal here is to eshtablish a trait / contract for cast
operations through a trait . Example as follows
Boolean module (boolean.rs)
```
use super::traits::SparkCast;
use super::utils::is_boolean;
pub struct BooleanCast;
impl SparkCast for BooleanCast {
fn can_cast(&self, from: &DataType, to: &DataType) -> bool {
is_boolean(from) || is_boolean(to)
}
fn cast(
&self,
array: &ArrayRef,
from: &DataType,
to: &DataType,
opts: &SparkCastOptions,
) -> SparkResult<ArrayRef> {
match (from, to) {
// Boolean -> other types
(Boolean, Int8) => self.boolean_to_int8(array),
(Boolean, Int16) => self.boolean_to_int16(array),
(Boolean, Int32) => self.boolean_to_int32(array),
(Boolean, Int64) => self.boolean_to_int64(array),
(Boolean, Float32) => self.boolean_to_float32(array),
(Boolean, Float64) => self.boolean_to_float64(array),
(Boolean, Utf8) => self.boolean_to_string(array),
// Other types -> Boolean
(Int8 | Int16 | Int32 | Int64, Boolean) =>
self.int_to_boolean(array),
(Utf8, Boolean) => self.string_to_boolean(array, opts),
(Decimal128(_, _), Boolean) => self.decimal_to_boolean(array),
_ => Err(SparkError::Internal(format!(
"BooleanCast: unsupported {} -> {}", from, to
))),
}
}
}
impl BooleanCast {
fn boolean_to_int8(&self, array: &ArrayRef) -> SparkResult<ArrayRef> {
<implentation details>
}
fn string_to_boolean(&self, array: &ArrayRef, opts: &SparkCastOptions)
-> SparkResult<ArrayRef> {
// <implentation details>
}
// other methods
}
on the caller side `cast.rs` / `mod.rs`
```
pub fn spark_cast(
array: &ArrayRef,
from: &DataType,
to: &DataType,
opts: &SparkCastOptions,
) -> SparkResult<ArrayRef> {
match from {
_ if is_boolean(from) => BooleanCast.cast.cast(array, from, to,
opts),
_ if is_string(from) => StringCast.cast(array, from, to, opts),
_ if is_numeric(from) => NumericCast.cast(array, from, to, opts),
// ... other data types
}
}
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]