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

fix: 'cannot cast non numeric any-value to numeric dtype' error #10804

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ impl<'a> AnyValue<'a> {
DataType::Duration(tu) => AnyValue::Duration($av as i64, *tu),
#[cfg(feature="dtype-time")]
DataType::Time => AnyValue::Time($av as i64),
DataType::Utf8 => AnyValue::Utf8Owned(format_smartstring!("{}", $av)),
_ => polars_bail!(
ComputeError: "cannot cast any-value {:?} to dtype '{}'", self, dtype,
),
Expand All @@ -474,6 +473,7 @@ impl<'a> AnyValue<'a> {
);

let new_av = match self {
AnyValue::Utf8(v) => AnyValue::Utf8Owned(format_smartstring!("{}", v)),
AnyValue::Boolean(v) => cast_to!(*v as u8),
AnyValue::Float32(_) | AnyValue::Float64(_) => cast_to!(self.extract::<f64>().unwrap()),
av if av.is_signed() => cast_to!(av.extract::<i64>().unwrap()),
Expand Down
17 changes: 17 additions & 0 deletions crates/polars-sql/tests/simple_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,23 @@ fn test_group_by_2() -> PolarsResult<()> {
Ok(())
}

#[test]
fn test_cast_and_str_concat_expr(){
let a = Series::new("a", vec!["hello"]);
let df = DataFrame::new(vec![a]).unwrap();
let mut context = SQLContext::new();
context.register("df", df.clone().lazy());
let sql = r#"
SELECT
a || ' world' as a_v2,
'hello' || ' world' as a_v3
FROM df"#;
let df_sql = context.execute(sql).unwrap().collect().unwrap();

assert_eq!(Series::new("a_v2", vec!["hello world"]),*df_sql.column("a_v2").unwrap());
assert_eq!(Series::new("a_v3", vec!["hello world"]),*df_sql.column("a_v3").unwrap());
}

#[test]
fn test_case_expr() {
let df = create_sample_df().unwrap().head(Some(10));
Expand Down