From 338aaa8f427f8f5b20c4251f24d02695bb6c5adb Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Sat, 10 Aug 2024 15:21:55 -0400 Subject: [PATCH] cast the other way --- vegafusion-sql/src/dataframe/mod.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/vegafusion-sql/src/dataframe/mod.rs b/vegafusion-sql/src/dataframe/mod.rs index b5025cc1..e3a87dae 100644 --- a/vegafusion-sql/src/dataframe/mod.rs +++ b/vegafusion-sql/src/dataframe/mod.rs @@ -1135,6 +1135,12 @@ impl SqlDataFrame { groupby: &[String], order_field: Option<&str>, ) -> Result> { + let (_, field_field) = self + .schema() + .column_with_name(field) + .with_context(|| format!("No field named {field}"))?; + let field_type = field_field.data_type(); + if groupby.is_empty() { // Value replacement for field with no groupby fields specified is equivalent to replacing // null values of that column with the fill value @@ -1147,8 +1153,8 @@ impl SqlDataFrame { Ok(if col_name == field { coalesce(vec![ - flat_col(field).cast_to(&value.data_type(), &self.schema_df()?)?, - lit(value.clone()), + flat_col(field), + lit(value.clone()).cast_to(&field_type, &self.schema_df()?)?, ]) .alias(col_name) } else { @@ -1192,8 +1198,8 @@ impl SqlDataFrame { .map(|col_name| { Ok(if col_name == field { coalesce(vec![ - flat_col(field).cast_to(&value.data_type(), &self.schema_df()?)?, - lit(value.clone()), + flat_col(field), + lit(value.clone()).cast_to(&field_type, &self.schema_df()?)?, ]) .alias(col_name) } else { @@ -1210,8 +1216,8 @@ impl SqlDataFrame { .map(|col_name| { let expr = if col_name == field { coalesce(vec![ - flat_col(field).cast_to(&value.data_type(), &self.schema_df()?)?, - lit(value.clone()), + flat_col(field), + lit(value.clone()).cast_to(&field_type, &self.schema_df()?)?, ]) .alias(col_name) } else if col_name == key {