Skip to content
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

Add arrow convert trait to support custom mutiple convert logic #15820

Closed
ZENOTME opened this issue Mar 20, 2024 · 2 comments
Closed

Add arrow convert trait to support custom mutiple convert logic #15820

ZENOTME opened this issue Mar 20, 2024 · 2 comments

Comments

@ZENOTME
Copy link
Contributor

ZENOTME commented Mar 20, 2024

Background

This is a problem I find in #15817. RisingWave uses arrow to connect with external system and it involves how to convert datatype and array between arrow and risingwave. For now, we used a unified implementation of in arrow_impl. When a specific external system need different conversion, it's difficult to custom. (If we want, we need to rewrite the whole conversion. We hope that able to custom the conversion for some type but keep others same.

Design

To support this, we can introduce a Convert trait like the following: The full implementation in #15817. It may need some refining but the basic idea is the same.

pub trait ToArrowConvert {
    fn to_arrow(&self, array: &ArrayImpl) -> Result<arrow_array::ArrayRef, ArrayError> {
        match array {
            ArrayImpl::Int16(array) => self.int16_to_arrow(array),
            ArrayImpl::Int32(array) => self.int32_to_arrow(array),
            ArrayImpl::Int64(array) => self.int64_to_arrow(array),
            ArrayImpl::Float32(array) => self.float32_to_arrow(array),
            ArrayImpl::Float64(array) => self.float64_to_arrow(array),
            ArrayImpl::Utf8(array) => self.utf8_to_arrow(array),
            ArrayImpl::Bool(array) => self.bool_to_arrow(array),
            ArrayImpl::Decimal(array) => self.decimal_to_arrow(array),
            ArrayImpl::Int256(array) => self.int256_to_arrow(array),
            ArrayImpl::Date(array) => self.date_to_arrow(array),
            ArrayImpl::Timestamp(array) => self.timestamp_to_arrow(array),
            ArrayImpl::Timestamptz(array) => self.timestamptz_to_arrow(array),
            ArrayImpl::Time(array) => self.time_to_arrow(array),
            ArrayImpl::Interval(array) => self.interval_to_arrow(array),
            ArrayImpl::Struct(array) => self.struct_to_arrow(array),
            ArrayImpl::List(array) => self.list_to_arrow(array),
            ArrayImpl::Bytea(array) => self.bytea_to_arrow(array),
            ArrayImpl::Jsonb(array) => self.jsonb_to_arrow(array),
            ArrayImpl::Serial(array) => self.serial_to_arrow(array),
        }
    }

    fn int16_to_arrow(&self, array: &I16Array) -> Result<arrow_array::ArrayRef, ArrayError> {
        Ok(Arc::new(arrow_array::Int16Array::from(array)))
    }

    fn int32_to_arrow(&self, array: &I32Array) -> Result<arrow_array::ArrayRef, ArrayError> {
        Ok(Arc::new(arrow_array::Int32Array::from(array)))
    }

    fn int64_to_arrow(&self, array: &I64Array) -> Result<arrow_array::ArrayRef, ArrayError> {
        Ok(Arc::new(arrow_array::Int64Array::from(array)))
    }

    fn float32_to_arrow(&self, array: &F32Array) -> Result<arrow_array::ArrayRef, ArrayError> {
        Ok(Arc::new(arrow_array::Float32Array::from(array)))
    }

    fn float64_to_arrow(&self, array: &F64Array) -> Result<arrow_array::ArrayRef, ArrayError> {
        Ok(Arc::new(arrow_array::Float64Array::from(array)))
    }

    fn utf8_to_arrow(&self, array: &Utf8Array) -> Result<arrow_array::ArrayRef, ArrayError> {
        Ok(Arc::new(arrow_array::StringArray::from(array)))
    }
    ...
}

The specific system can custom by rewrite the function on demand.

Future Optimizations

No response

Discussions

No response

Q&A

No response

@github-actions github-actions bot added this to the release-1.8 milestone Mar 20, 2024
@ZENOTME
Copy link
Contributor Author

ZENOTME commented Mar 20, 2024

cc @chenzl25 @wangrunji0408

@ZENOTME ZENOTME changed the title Refactor arrow convert to support custom mutiple convert logic Refine arrow convert to support custom mutiple convert logic Mar 20, 2024
@ZENOTME ZENOTME changed the title Refine arrow convert to support custom mutiple convert logic Add arrow convert trait to support custom mutiple convert logic Mar 20, 2024
@wangrunji0408
Copy link
Contributor

This design looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants