adriangb commented on code in PR #25173:
URL: https://github.com/apache/datafusion/pull/25173#discussion_r4020073774
##########
datafusion/functions-table/src/generate_series.rs:
##########
@@ -874,16 +964,347 @@ impl TableFunctionImpl for RangeFunc {
#[cfg(test)]
mod generate_series_tests {
+ use std::any::Any;
use std::sync::Arc;
- use arrow::datatypes::{DataType, Field, Schema};
- use datafusion_common::Result;
+ use arrow::datatypes::{
+ DataType, Field, IntervalMonthDayNano, Schema, SchemaRef, TimeUnit,
+ };
+ use datafusion_catalog::TableProvider;
+ use datafusion_common::{Result, ScalarValue};
+ use datafusion_expr::Expr;
use datafusion_physical_plan::memory::LazyBatchGenerator;
use crate::generate_series::{
- GenSeriesArgs, GenerateSeriesTable, GenericSeriesState,
+ GenSeriesArgs, GenerateSeriesFuncImpl, GenerateSeriesTable,
GenericSeriesState,
+ timestamp_arg_to_nanos,
};
+ /// Nanoseconds in a day, for readable expectations below.
+ const DAY_NANOS: i64 = 24 * 60 * 60 * 1_000_000_000;
+
+ fn lit(scalar: ScalarValue) -> Expr {
+ Expr::Literal(scalar, None)
+ }
+
+ /// `2024-01-01T00:00:00Z` in seconds since the epoch.
+ const JAN_1_2024_SECS: i64 = 1_704_067_200;
+
+ fn tz(s: &str) -> Option<Arc<str>> {
+ Some(Arc::from(s))
+ }
+
+ fn generate_series_impl() -> GenerateSeriesFuncImpl {
+ GenerateSeriesFuncImpl {
+ name: "generate_series",
+ include_end: true,
+ }
+ }
+
+ fn range_impl() -> GenerateSeriesFuncImpl {
+ GenerateSeriesFuncImpl {
+ name: "range",
+ include_end: false,
+ }
+ }
+
+ /// Run `call_timestamp` and return the resulting table's schema and args.
+ fn call_timestamp(
+ func: &GenerateSeriesFuncImpl,
+ start: ScalarValue,
+ end: ScalarValue,
+ step_days: i32,
+ ) -> Result<(SchemaRef, GenSeriesArgs)> {
+ let exprs = vec![
+ lit(start),
+ lit(end),
+ lit(ScalarValue::IntervalMonthDayNano(Some(
+ IntervalMonthDayNano::new(0, step_days, 0),
+ ))),
+ ];
+ let provider = func.call_timestamp(&exprs)?;
+ let table = (provider.as_ref() as &dyn Any)
+ .downcast_ref::<GenerateSeriesTable>()
+ .expect("call_timestamp returns a GenerateSeriesTable");
+ Ok((table.schema(), table.args.clone()))
+ }
+
+ fn timestamp_args(args: &GenSeriesArgs) -> (i64, i64, Option<Arc<str>>) {
+ match args {
+ GenSeriesArgs::TimestampArgs { start, end, tz, .. } => {
+ (*start, *end, tz.clone())
+ }
+ other => panic!("expected TimestampArgs, got {other:?}"),
+ }
+ }
+
+ /// Every `TimeUnit` is accepted and widened to nanoseconds, keeping its
Review Comment:
Done, moved into `table_functions.slt`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]