Skip to content

Commit

Permalink
Support to unparse ScalarValue::TimestampMillisecond to String (apach…
Browse files Browse the repository at this point in the history
…e#11046)

* wip

Signed-off-by: Kevin Su <[email protected]>

* add a test

Signed-off-by: Kevin Su <[email protected]>

---------

Signed-off-by: Kevin Su <[email protected]>
  • Loading branch information
pingsutw authored and findepi committed Jul 16, 2024
1 parent b9381b3 commit aef5dcb
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use arrow::util::display::array_value_to_string;
use core::fmt;
use std::{fmt::Display, vec};

use arrow_array::{Date32Array, Date64Array, TimestampNanosecondArray};
use arrow_array::{
Date32Array, Date64Array, TimestampMillisecondArray, TimestampNanosecondArray,
};
use arrow_schema::DataType;
use sqlparser::ast::Value::SingleQuotedString;
use sqlparser::ast::{
Expand Down Expand Up @@ -647,6 +649,15 @@ impl Unparser<'_> {
}
}

fn timestamp_string_to_sql(&self, ts: String) -> Result<ast::Expr> {
Ok(ast::Expr::Cast {
kind: ast::CastKind::Cast,
expr: Box::new(ast::Expr::Value(SingleQuotedString(ts))),
data_type: ast::DataType::Timestamp(None, TimezoneInfo::None),
format: None,
})
}

/// DataFusion ScalarValues sometimes require a ast::Expr to construct.
/// For example ScalarValue::Date32(d) corresponds to the ast::Expr CAST('datestr' as DATE)
fn scalar_to_sql(&self, v: &ScalarValue) -> Result<ast::Expr> {
Expand Down Expand Up @@ -808,8 +819,31 @@ impl Unparser<'_> {
ScalarValue::TimestampSecond(None, _) => {
Ok(ast::Expr::Value(ast::Value::Null))
}
ScalarValue::TimestampMillisecond(Some(_ts), _) => {
not_impl_err!("Unsupported scalar: {v:?}")
ScalarValue::TimestampMillisecond(Some(_ts), tz) => {
let result = if let Some(tz) = tz {
v.to_array()?
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.ok_or(internal_datafusion_err!(
"Unable to downcast to TimestampMillisecond from TimestampMillisecond scalar"
))?
.value_as_datetime_with_tz(0, tz.parse()?)
.ok_or(internal_datafusion_err!(
"Unable to convert TimestampMillisecond to DateTime"
))?.to_string()
} else {
v.to_array()?
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.ok_or(internal_datafusion_err!(
"Unable to downcast to TimestampMillisecond from TimestampMillisecond scalar"
))?
.value_as_datetime(0)
.ok_or(internal_datafusion_err!(
"Unable to convert TimestampMillisecond to NaiveDateTime"
))?.to_string()
};
self.timestamp_string_to_sql(result)
}
ScalarValue::TimestampMillisecond(None, _) => {
Ok(ast::Expr::Value(ast::Value::Null))
Expand Down Expand Up @@ -844,12 +878,7 @@ impl Unparser<'_> {
"Unable to convert TimestampNanosecond to NaiveDateTime"
))?.to_string()
};
Ok(ast::Expr::Cast {
kind: ast::CastKind::Cast,
expr: Box::new(ast::Expr::Value(SingleQuotedString(result))),
data_type: ast::DataType::Timestamp(None, TimezoneInfo::None),
format: None,
})
self.timestamp_string_to_sql(result)
}
ScalarValue::TimestampNanosecond(None, _) => {
Ok(ast::Expr::Value(ast::Value::Null))
Expand Down Expand Up @@ -1180,6 +1209,17 @@ mod tests {
Expr::Literal(ScalarValue::Date32(Some(-1))),
r#"CAST('1969-12-31' AS DATE)"#,
),
(
Expr::Literal(ScalarValue::TimestampMillisecond(Some(10001), None)),
r#"CAST('1970-01-01 00:00:10.001' AS TIMESTAMP)"#,
),
(
Expr::Literal(ScalarValue::TimestampMillisecond(
Some(10001),
Some("+08:00".into()),
)),
r#"CAST('1970-01-01 08:00:10.001 +08:00' AS TIMESTAMP)"#,
),
(
Expr::Literal(ScalarValue::TimestampNanosecond(Some(10001), None)),
r#"CAST('1970-01-01 00:00:00.000010001' AS TIMESTAMP)"#,
Expand Down

0 comments on commit aef5dcb

Please sign in to comment.