MCPcopy Create free account
hub / github.com/apache/datafusion / sqlite_date_trunc_to_sql

Function sqlite_date_trunc_to_sql

datafusion/sql/src/unparser/utils.rs:573–605  ·  view source on GitHub ↗

SQLite does not support timestamp/date scalars like `to_timestamp`, `from_unixtime`, `date_trunc`, etc. This uses the `strftime` function to format the timestamp as a string depending on the truncation unit. # Errors - If the number of arguments is not 2 - truncation unit and the column or expression to convert. - If the scalar function cannot be converted to SQL.

(
    unparser: &Unparser,
    date_trunc_args: &[Expr],
)

Source from the content-addressed store, hash-verified

571/// - If the number of arguments is not 2 - truncation unit and the column or expression to convert.
572/// - If the scalar function cannot be converted to SQL.
573pub(crate) fn sqlite_date_trunc_to_sql(
574 unparser: &Unparser,
575 date_trunc_args: &[Expr],
576) -> Result<Option<ast::Expr>> {
577 assert_eq_or_internal_err!(
578 date_trunc_args.len(),
579 2,
580 "date_trunc for SQLite expects 2 arguments, found {}",
581 date_trunc_args.len()
582 );
583
584 if let Expr::Literal(ScalarValue::Utf8(Some(unit)), _) = &date_trunc_args[0] {
585 let format = match unit.to_lowercase().as_str() {
586 "year" => "%Y",
587 "month" => "%Y-%m",
588 "day" => "%Y-%m-%d",
589 "hour" => "%Y-%m-%d %H",
590 "minute" => "%Y-%m-%d %H:%M",
591 "second" => "%Y-%m-%d %H:%M:%S",
592 _ => return Ok(None),
593 };
594
595 return Ok(Some(unparser.scalar_function_to_sql(
596 "strftime",
597 &[
598 Expr::Literal(ScalarValue::Utf8(Some(format.to_string())), None),
599 date_trunc_args[1].clone(),
600 ],
601 )?));
602 }
603
604 Ok(None)
605}

Callers 1

Calls 5

LiteralInterface · 0.50
as_strMethod · 0.45
to_stringMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…