Skip to content

Commit

Permalink
fix: UTF8 cast error
Browse files Browse the repository at this point in the history
  • Loading branch information
holicc committed Aug 30, 2023
1 parent b1f60cd commit d7fa4d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 @@ -457,7 +457,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 @@ -467,6 +466,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

0 comments on commit d7fa4d4

Please sign in to comment.