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

Use original value when comparing with dictionary column in unparser #12610

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,24 @@ impl Unparser<'_> {
}
Expr::Cast(Cast { expr, data_type }) => {
let inner_expr = self.expr_to_sql_inner(expr)?;
Ok(ast::Expr::Cast {
kind: ast::CastKind::Cast,
expr: Box::new(inner_expr),
data_type: self.arrow_dtype_to_ast_dtype(data_type)?,
format: None,
})
match data_type {
DataType::Dictionary(_, _) => match inner_expr {
// Dictionary values don't need to be cast to other types when rewritten back to sql
Copy link
Contributor

Choose a reason for hiding this comment

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

this is another example of where logical types as discussed in #11513 might make sense

ast::Expr::Value(_) => Ok(inner_expr),
_ => Ok(ast::Expr::Cast {
kind: ast::CastKind::Cast,
expr: Box::new(inner_expr),
data_type: self.arrow_dtype_to_ast_dtype(data_type)?,
format: None,
}),
},
_ => Ok(ast::Expr::Cast {
kind: ast::CastKind::Cast,
expr: Box::new(inner_expr),
data_type: self.arrow_dtype_to_ast_dtype(data_type)?,
format: None,
}),
}
}
Expr::Literal(value) => Ok(self.scalar_to_sql(value)?),
Expr::Alias(Alias { expr, name: _, .. }) => self.expr_to_sql_inner(expr),
Expand Down Expand Up @@ -2361,4 +2373,29 @@ mod tests {
}
Ok(())
}

#[test]
fn test_cast_value_to_dict_expr() {
let tests = [(
Expr::Cast(Cast {
expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some(
"variation".to_string(),
)))),
data_type: DataType::Dictionary(
Box::new(DataType::Int8),
Box::new(DataType::Utf8),
),
}),
"'variation'",
)];
for (value, expected) in tests {
let dialect = CustomDialectBuilder::new().build();
let unparser = Unparser::new(&dialect);

let ast = unparser.expr_to_sql(&value).expect("to be unparsed");
let actual = format!("{ast}");

assert_eq!(actual, expected);
}
}
}