Skip to content

Add support for Dictionary to AST datatype in unparser - #14783

Merged
alamb merged 1 commit into
apache:mainfrom
pydantic:support_ast_dictionary_dtype
Feb 24, 2025
Merged

Add support for Dictionary to AST datatype in unparser#14783
alamb merged 1 commit into
apache:mainfrom
pydantic:support_ast_dictionary_dtype

Conversation

@cetra3

@cetra3 cetra3 commented Feb 20, 2025

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

No issue raised, just something we've seen in production

Rationale for this change

Ensures we can convert dictionary & interval types to ast datatypes

What changes are included in this PR?

Adds a small change to support dictionaries

Are these changes tested?

I am actually not too sure how best to test this, might need a bit of guidance here if we wanna add a test

Are there any user-facing changes?

No

@github-actions github-actions Bot added the sql SQL Planner label Feb 20, 2025
@alamb alamb changed the title Add support for Dictionary to AST datatype Add support for Dictionary to AST datatype in unparser Feb 20, 2025
@alamb

alamb commented Feb 20, 2025

Copy link
Copy Markdown
Contributor

Thanks @cetra3

@phillipleblanc do you have any suggestions about where we could test this one?

@cetra3
cetra3 force-pushed the support_ast_dictionary_dtype branch from d2c76df to 38c6e91 Compare February 21, 2025 03:35

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @cetra3

Ideally we would write a test for this., but since I couldn't find any existing tests for data types, I don't think it is strictly necessary here

Maybe @phillipleblanc has some idea of how to write such a test

@phillipleblanc

Copy link
Copy Markdown
Contributor

Thank you @cetra3

Ideally we would write a test for this., but since I couldn't find any existing tests for data types, I don't think it is strictly necessary here

Maybe @phillipleblanc has some idea of how to write such a test

There are tests at the bottom of this file that can be added - see for example this PR I made for supporting Utf8View: #13462

DataType::Dictionary(_, _) => {
not_impl_err!("Unsupported DataType: conversion: {data_type:?}")
}
DataType::Dictionary(_, val) => self.arrow_dtype_to_ast_dtype(val),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test for this new type, see this example for supporting the Utf8View type:

    #[test]
    fn test_utf8_view_to_sql() -> Result<()> {
        let dialect = CustomDialectBuilder::new()
            .with_utf8_cast_dtype(ast::DataType::Char(None))
            .build();
        let unparser = Unparser::new(&dialect);

        let ast_dtype = unparser.arrow_dtype_to_ast_dtype(&DataType::Utf8View)?;

        assert_eq!(ast_dtype, ast::DataType::Char(None));

        let expr = cast(col("a"), DataType::Utf8View);
        let ast = unparser.expr_to_sql(&expr)?;

        let actual = format!("{}", ast);
        let expected = r#"CAST(a AS CHAR)"#.to_string();

        assert_eq!(actual, expected);

        let expr = col("a").eq(lit(ScalarValue::Utf8View(Some("hello".to_string()))));
        let ast = unparser.expr_to_sql(&expr)?;

        let actual = format!("{}", ast);
        let expected = r#"(a = 'hello')"#.to_string();

        assert_eq!(actual, expected);

        let expr = col("a").is_not_null();

        let ast = unparser.expr_to_sql(&expr)?;
        let actual = format!("{}", ast);
        let expected = r#"a IS NOT NULL"#.to_string();

        assert_eq!(actual, expected);

        let expr = col("a").is_null();

        let ast = unparser.expr_to_sql(&expr)?;
        let actual = format!("{}", ast);
        let expected = r#"a IS NULL"#.to_string();

        assert_eq!(actual, expected);

        Ok(())
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test for this. Let me know if I need to flesh it out more

@cetra3
cetra3 force-pushed the support_ast_dictionary_dtype branch 3 times, most recently from 4cf08c9 to e496e52 Compare February 24, 2025 07:13
Comment thread datafusion/sql/src/unparser/expr.rs Outdated
}
DataType::Interval(_) => {
not_impl_err!("Unsupported DataType: conversion: {data_type:?}")
Ok(ast::DataType::Interval)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this as well, it seems to be a bit of a trivial conversion so not sure a test is warranted

@cetra3
cetra3 force-pushed the support_ast_dictionary_dtype branch from e496e52 to b790c86 Compare February 24, 2025 07:37

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @cetra3 and @phillipleblanc

Box::new(DataType::Utf8),
))?;

assert_eq!(ast_dtype, ast::DataType::Varchar(None));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@alamb
alamb merged commit cec457a into apache:main Feb 24, 2025
ozankabak pushed a commit to synnada-ai/datafusion-upstream that referenced this pull request Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants