Skip to content

Apache DataFusion SQL Query Engine

Minor: Port more aggregate tests to sqllogictests (#5574)

Andrew Lambe6d71068474f
diff --git a/datafusion/core/tests/sql/aggregates.rs b/datafusion/core/tests/sql/aggregates.rs
index 3eaa2fd5d1..48dd53830a 100644
--- a/datafusion/core/tests/sql/aggregates.rs
+++ b/datafusion/core/tests/sql/aggregates.rs
@@ -70,194 +70,6 @@ async fn csv_query_array_agg_distinct() -> Result<()> {
Ok(())
}
-#[tokio::test]
-async fn aggregate_timestamps_sum() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_timestamps()).unwrap();
-
- let results = plan_and_collect(
- &ctx,
- "SELECT sum(nanos), sum(micros), sum(millis), sum(secs) FROM t",
- )
- .await
- .unwrap_err();
-
- assert_eq!(results.to_string(), "Error during planning: The function Sum does not support inputs of type Timestamp(Nanosecond, None).");
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_timestamps_count() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_timestamps()).unwrap();
-
- let results = execute_to_batches(
- &ctx,
- "SELECT count(nanos), count(micros), count(millis), count(secs) FROM t",
- )
- .await;
-
- let expected = vec![
- "+----------------+-----------------+-----------------+---------------+",
- "| COUNT(t.nanos) | COUNT(t.micros) | COUNT(t.millis) | COUNT(t.secs) |",
- "+----------------+-----------------+-----------------+---------------+",
- "| 3 | 3 | 3 | 3 |",
- "+----------------+-----------------+-----------------+---------------+",
- ];
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_timestamps_min() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_timestamps()).unwrap();
-
- let results = execute_to_batches(
- &ctx,
- "SELECT min(nanos), min(micros), min(millis), min(secs) FROM t",
- )
- .await;
-
- let expected = vec![
- "+----------------------------+----------------------------+-------------------------+---------------------+",
- "| MIN(t.nanos) | MIN(t.micros) | MIN(t.millis) | MIN(t.secs) |",
- "+----------------------------+----------------------------+-------------------------+---------------------+",
- "| 2011-12-13T11:13:10.123450 | 2011-12-13T11:13:10.123450 | 2011-12-13T11:13:10.123 | 2011-12-13T11:13:10 |",
- "+----------------------------+----------------------------+-------------------------+---------------------+",
- ];
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_timestamps_max() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_timestamps()).unwrap();
-
- let results = execute_to_batches(
- &ctx,
- "SELECT max(nanos), max(micros), max(millis), max(secs) FROM t",
- )
- .await;
-
- let expected = vec![
- "+-------------------------+-------------------------+-------------------------+---------------------+",
- "| MAX(t.nanos) | MAX(t.micros) | MAX(t.millis) | MAX(t.secs) |",
- "+-------------------------+-------------------------+-------------------------+---------------------+",
- "| 2021-01-01T05:11:10.432 | 2021-01-01T05:11:10.432 | 2021-01-01T05:11:10.432 | 2021-01-01T05:11:10 |",
- "+-------------------------+-------------------------+-------------------------+---------------------+",
- ];
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_times_sum() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_times()).unwrap();
-
- let results = plan_and_collect(
- &ctx,
- "SELECT sum(nanos), sum(micros), sum(millis), sum(secs) FROM t",
- )
- .await
- .unwrap_err();
-
- assert_eq!(results.to_string(), "Error during planning: The function Sum does not support inputs of type Time64(Nanosecond).");
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_times_count() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_times()).unwrap();
-
- let results = execute_to_batches(
- &ctx,
- "SELECT count(nanos), count(micros), count(millis), count(secs) FROM t",
- )
- .await;
-
- let expected = vec![
- "+----------------+-----------------+-----------------+---------------+",
- "| COUNT(t.nanos) | COUNT(t.micros) | COUNT(t.millis) | COUNT(t.secs) |",
- "+----------------+-----------------+-----------------+---------------+",
- "| 4 | 4 | 4 | 4 |",
- "+----------------+-----------------+-----------------+---------------+",
- ];
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_times_min() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_times()).unwrap();
-
- let results = execute_to_batches(
- &ctx,
- "SELECT min(nanos), min(micros), min(millis), min(secs) FROM t",
- )
- .await;
-
- let expected = vec![
- "+--------------------+-----------------+---------------+-------------+",
- "| MIN(t.nanos) | MIN(t.micros) | MIN(t.millis) | MIN(t.secs) |",
- "+--------------------+-----------------+---------------+-------------+",
- "| 18:06:30.243620451 | 18:06:30.243620 | 18:06:30.243 | 18:06:30 |",
- "+--------------------+-----------------+---------------+-------------+",
- ];
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_times_max() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_times()).unwrap();
-
- let results = execute_to_batches(
- &ctx,
- "SELECT max(nanos), max(micros), max(millis), max(secs) FROM t",
- )
- .await;
-
- let expected = vec![
- "+--------------------+-----------------+---------------+-------------+",
- "| MAX(t.nanos) | MAX(t.micros) | MAX(t.millis) | MAX(t.secs) |",
- "+--------------------+-----------------+---------------+-------------+",
- "| 21:06:28.247821084 | 21:06:28.247821 | 21:06:28.247 | 21:06:28 |",
- "+--------------------+-----------------+---------------+-------------+",
- ];
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn aggregate_timestamps_avg() -> Result<()> {
- let ctx = SessionContext::new();
- ctx.register_table("t", table_with_timestamps()).unwrap();
-
- let results = plan_and_collect(
- &ctx,
- "SELECT avg(nanos), avg(micros), avg(millis), avg(secs) FROM t",
- )
- .await
- .unwrap_err();
-
- assert_eq!(results.to_string(), "Error during planning: The function Avg does not support inputs of type Timestamp(Nanosecond, None).");
- Ok(())
-}
-
#[tokio::test]
async fn aggregate_decimal_sum() -> Result<()> {
let ctx = SessionContext::new();
diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rs
index 456dc12a10..8b810737cf 100644
--- a/datafusion/core/tests/sql/mod.rs
+++ b/datafusion/core/tests/sql/mod.rs
@@ -1438,99 +1438,6 @@ pub fn make_timestamps() -> RecordBatch {
.unwrap()
}
-/// Return a new table provider containing all of the supported timestamp types
-pub fn table_with_times() -> Arc<dyn TableProvider> {
- let batch = make_times();
- let schema = batch.schema();
- let partitions = vec![vec![batch]];
- Arc::new(MemTable::try_new(schema, partitions).unwrap())
-}
-
-/// Return record batch with all of the supported time types
-/// values
-///
-/// Columns are named:
-/// "nanos" --> Time64NanosecondArray
-/// "micros" --> Time64MicrosecondArray
-/// "millis" --> Time32MillisecondArray
-/// "secs" --> Time32SecondArray
-/// "names" --> StringArray
-pub fn make_times() -> RecordBatch {
- let ts_strings = vec![
- Some("18:06:30.243620451"),
- Some("20:08:28.161121654"),
- Some("19:11:04.156423842"),
- Some("21:06:28.247821084"),
- ];
-
- let ts_nanos = ts_strings
- .into_iter()
- .map(|t| {
- t.map(|t| {
- let integer_sec = t
- .parse::<chrono::NaiveTime>()
- .unwrap()
- .num_seconds_from_midnight() as i64;
- let extra_nano =
- t.parse::<chrono::NaiveTime>().unwrap().nanosecond() as i64;
- // Total time in nanoseconds given by integer number of seconds multiplied by 10^9
- // plus number of nanoseconds corresponding to the extra fraction of second
- integer_sec * 1_000_000_000 + extra_nano
- })
- })
- .collect::<Vec<_>>();
-
- let ts_micros = ts_nanos
- .iter()
- .map(|t| t.as_ref().map(|ts_nanos| ts_nanos / 1000))
- .collect::<Vec<_>>();
-
- let ts_millis = ts_nanos
- .iter()
- .map(|t| t.as_ref().map(|ts_nanos| { ts_nanos / 1000000 } as i32))
- .collect::<Vec<_>>();
-
- let ts_secs = ts_nanos
- .iter()
- .map(|t| t.as_ref().map(|ts_nanos| { ts_nanos / 1000000000 } as i32))
- .collect::<Vec<_>>();
-
- let names = ts_nanos
- .iter()
- .enumerate()
- .map(|(i, _)| format!("Row {i}"))
- .collect::<Vec<_>>();
-
- let arr_nanos = Time64NanosecondArray::from(ts_nanos);
- let arr_micros = Time64MicrosecondArray::from(ts_micros);
- let arr_millis = Time32MillisecondArray::from(ts_millis);
- let arr_secs = Time32SecondArray::from(ts_secs);
-
- let names = names.iter().map(|s| s.as_str()).collect::<Vec<_>>();
- let arr_names = StringArray::from(names);
-
- let schema = Schema::new(vec![
- Field::new("nanos", arr_nanos.data_type().clone(), true),
- Field::new("micros", arr_micros.data_type().clone(), true),
- Field::new("millis", arr_millis.data_type().clone(), true),
- Field::new("secs", arr_secs.data_type().clone(), true),
- Field::new("name", arr_names.data_type().clone(), true),
- ]);
- let schema = Arc::new(schema);
-
- RecordBatch::try_new(
- schema,
- vec![
- Arc::new(arr_nanos),
- Arc::new(arr_micros),
- Arc::new(arr_millis),
- Arc::new(arr_secs),
- Arc::new(arr_names),
- ],
- )
- .unwrap()
-}
-
#[tokio::test]
async fn nyc() -> Result<()> {
// schema for nyxtaxi csv files
diff --git a/datafusion/core/tests/sqllogictests/test_files/aggregate.slt b/datafusion/core/tests/sqllogictests/test_files/aggregate.slt
index 37ba169fed..76ff53af3d 100644
--- a/datafusion/core/tests/sqllogictests/test_files/aggregate.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/aggregate.slt
@@ -1253,6 +1253,150 @@ NULL 2
statement ok
drop table the_nulls;
+# All supported timestamp types
+
+# "nanos" --> TimestampNanosecondArray
+# "micros" --> TimestampMicrosecondArray
+# "millis" --> TimestampMillisecondArray
+# "secs" --> TimestampSecondArray
+# "names" --> StringArray
+
+statement ok
+create table t_source
+as values
+ ('2018-11-13T17:11:10.011375885995', 'Row 0'),
+ ('2011-12-13T11:13:10.12345', 'Row 1'),
+ (null, 'Row 2'),
+ ('2021-1-1T05:11:10.432', 'Row 3');
+
+
+statement ok
+create table t as
+select
+ arrow_cast(column1, 'Timestamp(Nanosecond, None)') as nanos,
+ arrow_cast(column1, 'Timestamp(Microsecond, None)') as micros,
+ arrow_cast(column1, 'Timestamp(Millisecond, None)') as millis,
+ arrow_cast(column1, 'Timestamp(Second, None)') as secs,
+ column2 as names
+from t_source;
+
+# Demonstate the contents
+query PPPPT
+select * from t;
+----
+2018-11-13T17:11:10.011375885 2018-11-13T17:11:10.011375 2018-11-13T17:11:10.011 2018-11-13T17:11:10 Row 0
+2011-12-13T11:13:10.123450 2011-12-13T11:13:10.123450 2011-12-13T11:13:10.123 2011-12-13T11:13:10 Row 1
+NULL NULL NULL NULL Row 2
+2021-01-01T05:11:10.432 2021-01-01T05:11:10.432 2021-01-01T05:11:10.432 2021-01-01T05:11:10 Row 3
+
+
+# aggregate_timestamps_sum
+statement error Error during planning: The function Sum does not support inputs of type Timestamp\(Nanosecond, None\)
+SELECT sum(nanos), sum(micros), sum(millis), sum(secs) FROM t;
+
+# aggregate_timestamps_count
+query IIII
+SELECT count(nanos), count(micros), count(millis), count(secs) FROM t;
+----
+3 3 3 3
+
+
+# aggregate_timestamps_min
+query PPPP
+SELECT min(nanos), min(micros), min(millis), min(secs) FROM t;
+----
+2011-12-13T11:13:10.123450 2011-12-13T11:13:10.123450 2011-12-13T11:13:10.123 2011-12-13T11:13:10
+
+# aggregate_timestamps_max
+query PPPP
+SELECT max(nanos), max(micros), max(millis), max(secs) FROM t;
+----
+2021-01-01T05:11:10.432 2021-01-01T05:11:10.432 2021-01-01T05:11:10.432 2021-01-01T05:11:10
+
+
+
+# aggregate_timestamps_avg
+statement error Error during planning: The function Avg does not support inputs of type Timestamp\(Nanosecond, None\).
+SELECT avg(nanos), avg(micros), avg(millis), avg(secs) FROM t
+
+
+statement ok
+drop table t_source;
+
+statement ok
+drop table t;
+
+# All supported time types
+
+# Columns are named:
+# "nanos" --> Time64NanosecondArray
+# "micros" --> Time64MicrosecondArray
+# "millis" --> Time32MillisecondArray
+# "secs" --> Time32SecondArray
+# "names" --> StringArray
+
+statement ok
+create table t_source
+as values
+ ('18:06:30.243620451', 'Row 0'),
+ ('20:08:28.161121654', 'Row 1'),
+ ('19:11:04.156423842', 'Row 2'),
+ ('21:06:28.247821084', 'Row 3');
+
+
+statement ok
+create table t as
+select
+ arrow_cast(column1, 'Time64(Nanosecond)') as nanos,
+ arrow_cast(column1, 'Time64(Microsecond)') as micros,
+ arrow_cast(column1, 'Time32(Millisecond)') as millis,
+ arrow_cast(column1, 'Time32(Second)') as secs,
+ column2 as names
+from t_source;
+
+# Demonstate the contents
+query DDDDT
+select * from t;
+----
+18:06:30.243620451 18:06:30.243620 18:06:30.243 18:06:30 Row 0
+20:08:28.161121654 20:08:28.161121 20:08:28.161 20:08:28 Row 1
+19:11:04.156423842 19:11:04.156423 19:11:04.156 19:11:04 Row 2
+21:06:28.247821084 21:06:28.247821 21:06:28.247 21:06:28 Row 3
+
+# aggregate_times_sum
+statement error DataFusion error: Error during planning: The function Sum does not support inputs of type Time64\(Nanosecond\).
+SELECT sum(nanos), sum(micros), sum(millis), sum(secs) FROM t
+
+# aggregate_times_count
+query IIII
+SELECT count(nanos), count(micros), count(millis), count(secs) FROM t
+----
+4 4 4 4
+
+
+# aggregate_times_min
+query DDDD
+SELECT min(nanos), min(micros), min(millis), min(secs) FROM t
+----
+18:06:30.243620451 18:06:30.243620 18:06:30.243 18:06:30
+
+# aggregate_times_max
+query DDDD
+SELECT max(nanos), max(micros), max(millis), max(secs) FROM t
+----
+21:06:28.247821084 21:06:28.247821 21:06:28.247 21:06:28
+
+
+# aggregate_times_avg
+statement error DataFusion error: Error during planning: The function Avg does not support inputs of type Time64\(Nanosecond\).
+SELECT avg(nanos), avg(micros), avg(millis), avg(secs) FROM t
+
+statement ok
+drop table t_source;
+
+statement ok
+drop table t;
+
query error DataFusion error: Execution error: median requires at least one non-null value
select median(a) from (select 1 as a where 1=0);