Skip to content

Support Utf8View <=> boolean casting #6713

Closed

Description

@Omega359

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

While working on upgrading to DF 43 I discovered that casting support for utf8view <-> bool has not been implemented

Describe the solution you'd like

Support casting to/from bool with utf8view's

Additional context

arrow-cast/src/cast/mod.rs

 #[test]
    fn test_cast_utf8view_to_bool() {
        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
        let casted = cast(&strings, &DataType::Boolean).unwrap();
        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
        assert_eq!(*as_boolean_array(&casted), expected);
    }

    #[test]
    fn test_cast_with_options_utf8view_to_bool() {
        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
        let casted = cast_with_options(
            &strings,
            &DataType::Boolean,
            &CastOptions {
                safe: false,
                format_options: FormatOptions::default(),
            },
        );
        match casted {
            Ok(_) => panic!("expected error"),
            Err(e) => {
                assert!(e
                    .to_string()
                    .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
            }
        }
    }

    #[test]
    fn test_cast_bool_to_utf8view() {
        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
        let b = cast(&array, &DataType::Utf8View).unwrap();
        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
        assert_eq!("true", c.value(0));
        assert_eq!("false", c.value(1));
        assert!(!c.is_valid(2));
    }

Activity

  1. tlm365 commented on Nov 11, 2024

    @tlm365
    Contributor

    take

  2. alamb commented on Dec 17, 2024

    @alamb
    Contributor

    label_issue.py automatically added labels {'arrow'} from #6822

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

Metadata

Metadata

Assignees

Labels

arrowChanges to the arrow crateenhancementNew feature/addition

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions