Skip to content

add cast() to ScalarValue #6696

@waynexia

Description

@waynexia

Is your feature request related to a problem or challenge?

The place parses literal to ScalarValue doesn't care about the target data type. This may require somebody to cast the parsed ScalarValue to expected data type

https://github.com/apache/arrow-datafusion/blob/c2b260620718f1f6e67fdb32af87c3b3551bc0f0/datafusion/sql/src/expr/value.rs#L30

Describe the solution you'd like

Add a method to ScalarValue like:

fn cast(&self, data_type: &DataType, force: bool) -> Option<ScalarValue>;

The third parameter force is a switch to whether perform force cast like cast integer to float or string to number. Without force this method only perform lossless cast (i.e., only cast to bigger type)

Describe alternatives you've considered

For logical plan we can wrap a Cast over Literal to get the same result.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions