Skip to content
Merged
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
100 changes: 54 additions & 46 deletions crates/polars-expr/src/dispatch/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,23 +466,39 @@ pub(super) fn split_regex(s: &[Column], inclusive: bool, strict: bool) -> Polars
Ok(out.into_column())
}

/// Yield nulls when a non-strict parse could not infer a format, and report the
/// values that failed when strict.
#[cfg(feature = "temporal")]
fn finish_strptime(
out: PolarsResult<Column>,
input: &Column,
dtype: &DataType,
options: &StrptimeOptions,
) -> PolarsResult<Column> {
let out = match out {
Err(_) if !options.strict && options.format.is_none() => {
Column::full_null(input.name().clone(), input.len(), dtype)
},
out => out?,
};

if options.strict && input.null_count() != out.null_count() {
handle_casting_failures(input.as_materialized_series(), out.as_materialized_series())?;
}
Ok(out)
}

#[cfg(feature = "dtype-date")]
fn to_date(s: &Column, options: &StrptimeOptions) -> PolarsResult<Column> {
let ca = s.str()?;
let out = {
if options.exact {
ca.as_date(options.format.as_deref(), options.cache)?
.into_column()
} else {
ca.as_date_not_exact(options.format.as_deref())?
.into_column()
}
};

if options.strict && ca.null_count() != out.null_count() {
handle_casting_failures(s.as_materialized_series(), out.as_materialized_series())?;
let out = if options.exact {
ca.as_date(options.format.as_deref(), options.cache)
} else {
ca.as_date_not_exact(options.format.as_deref())
}
Ok(out.into_column())
.map(|ca| ca.into_column());

finish_strptime(out, s, &DataType::Date, options)
}

#[cfg(feature = "dtype-datetime")]
Expand Down Expand Up @@ -511,33 +527,28 @@ fn to_datetime(
};

let out = if options.exact {
datetime_strings
.as_datetime(
options.format.as_deref(),
*time_unit,
options.cache,
tz_aware,
time_zone,
ambiguous,
)?
.into_column()
datetime_strings.as_datetime(
options.format.as_deref(),
*time_unit,
options.cache,
tz_aware,
time_zone,
ambiguous,
)
} else {
datetime_strings
.as_datetime_not_exact(
options.format.as_deref(),
*time_unit,
tz_aware,
time_zone,
ambiguous,
true,
)?
.into_column()
};

if options.strict && datetime_strings.null_count() != out.null_count() {
handle_casting_failures(s[0].as_materialized_series(), out.as_materialized_series())?;
datetime_strings.as_datetime_not_exact(
options.format.as_deref(),
*time_unit,
tz_aware,
time_zone,
ambiguous,
true,
)
}
Ok(out.into_column())
.map(|ca| ca.into_column());

let dtype = DataType::Datetime(*time_unit, time_zone.cloned());
finish_strptime(out, &s[0], &dtype, options)
}

#[cfg(feature = "dtype-time")]
Expand All @@ -546,15 +557,12 @@ fn to_time(s: &Column, options: &StrptimeOptions) -> PolarsResult<Column> {
options.exact, ComputeError: "non-exact not implemented for Time data type"
);

let ca = s.str()?;
let out = ca
.as_time(options.format.as_deref(), options.cache)?
.into_column();
let out = s
.str()?
.as_time(options.format.as_deref(), options.cache)
.map(|ca| ca.into_column());

if options.strict && ca.null_count() != out.null_count() {
handle_casting_failures(s.as_materialized_series(), out.as_materialized_series())?;
}
Ok(out.into_column())
finish_strptime(out, s, &DataType::Time, options)
}

#[cfg(feature = "concat_str")]
Expand Down
88 changes: 56 additions & 32 deletions crates/polars-sql/src/sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ fn cast_literal_series(s: &Series, dtype: &DataType) -> PolarsResult<Series> {
s.strict_cast(dtype)
}

/// Parse a string expression into `Date`/`Time`/`Datetime`; `None` for other dtypes.
fn parse_string_as_temporal(expr: Expr, dtype: &DataType, strict: bool) -> Option<Expr> {
let options = StrptimeOptions {
strict,
..Default::default()
};
Some(match dtype {
DataType::Date => expr.str().to_date(options),
DataType::Time => expr.str().to_time(options),
DataType::Datetime(tu, tz) => {
expr.str()
.to_datetime(Some(*tu), tz.clone(), options, lit("latest"))
},
_ => return None,
})
}

/// Extract the literal value; returns `(sql_value, optional_op)`.
fn extract_literal_with_op<'a>(
expr: &'a SQLExpr,
Expand Down Expand Up @@ -550,39 +567,27 @@ impl SQLExprVisitor<'_> {
},
|dt| dt.as_literal(),
);
match left_dtype {
Some(DataType::Time) if is_iso_time(s) => {
right.clone().str().to_time(StrptimeOptions {
strict: true,
..Default::default()
})
let parsed = match left_dtype {
Some(dtype @ DataType::Time) if is_iso_time(s) => {
parse_string_as_temporal(right.clone(), dtype, true)
},
Some(DataType::Date) if is_iso_date(s) => {
right.clone().str().to_date(StrptimeOptions {
strict: true,
..Default::default()
})
Some(dtype @ DataType::Date) if is_iso_date(s) => {
parse_string_as_temporal(right.clone(), dtype, true)
},
Some(DataType::Datetime(tu, tz)) if is_iso_datetime(s) || is_iso_date(s) => {
if s.len() == 10 {
Some(dtype @ DataType::Datetime(_, _))
if is_iso_datetime(s) || is_iso_date(s) =>
{
let s = if s.len() == 10 {
// handle upcast from ISO date string (10 chars) to datetime
lit(format!("{s}T00:00:00"))
format!("{s}T00:00:00")
} else {
lit(s.replacen(' ', "T", 1))
}
.str()
.to_datetime(
Some(*tu),
tz.clone(),
StrptimeOptions {
strict: true,
..Default::default()
},
lit("latest"),
)
s.replacen(' ', "T", 1)
};
parse_string_as_temporal(lit(s), dtype, true)
},
_ => right.clone(),
}
_ => None,
};
parsed.unwrap_or_else(|| right.clone())
}
} else {
right.clone()
Expand Down Expand Up @@ -954,7 +959,7 @@ impl SQLExprVisitor<'_> {
dtype,
DataType::Date | DataType::Time | DataType::Datetime(_, _)
) {
return elems.strict_cast(dtype);
return cast_literal_series(&elems, dtype);
}
}
}
Expand Down Expand Up @@ -1005,12 +1010,31 @@ impl SQLExprVisitor<'_> {
return Ok(expr.str().json_decode(DataType::Struct(Vec::new())));
}
let polars_type = map_sql_dtype_to_polars(dtype)?;
Ok(match cast_kind {
CastKind::Cast | CastKind::DoubleColon => expr.strict_cast(polars_type),
CastKind::TryCast | CastKind::SafeCast => expr.cast(polars_type),
let strict = matches!(cast_kind, CastKind::Cast | CastKind::DoubleColon);

// `CAST(<string> AS DATE/TIME/TIMESTAMP)` parses rather than casts
if matches!(
polars_type,
DataType::Date | DataType::Time | DataType::Datetime(_, _)
) && self.is_string_expr(&expr)
&& let Some(parsed) = parse_string_as_temporal(expr.clone(), &polars_type, strict)
{
return Ok(parsed);
}
Ok(if strict {
expr.strict_cast(polars_type)
} else {
expr.cast(polars_type)
})
}

/// Whether `expr` is known to be `String`; false if the dtype cannot be resolved.
fn is_string_expr(&self, expr: &Expr) -> bool {
let empty = Schema::default();
let schema = self.active_schema.unwrap_or(&empty);
matches!(expr.to_field(schema), Ok(fld) if fld.dtype == DataType::String)
}

/// Visit a SQL literal.
///
/// e.g. 1, 'foo', 1.0, NULL
Expand Down
Loading
Loading