Skip to content

Commit 192ccda

Browse files
WinkerDugandronchik
authored andcommitted
move sql tests from context.rs to corresponding test files in tests/sql (apache#2329)
* move context.rs ut out * fix clippy * fix fmt
1 parent fc0e00d commit 192ccda

File tree

9 files changed

+1420
-1267
lines changed

9 files changed

+1420
-1267
lines changed

datafusion/core/src/execution/context.rs

Lines changed: 34 additions & 1267 deletions
Large diffs are not rendered by default.

datafusion/core/tests/sql/aggregates.rs

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,3 +804,306 @@ async fn aggregate_timestamps_avg() -> Result<()> {
804804
assert_eq!(results.to_string(), "Error during planning: The function Avg does not support inputs of type Timestamp(Nanosecond, None).");
805805
Ok(())
806806
}
807+
808+
#[tokio::test]
809+
async fn aggregate_decimal_min() -> Result<()> {
810+
let ctx = SessionContext::new();
811+
// the data type of c1 is decimal(10,3)
812+
ctx.register_table("d_table", table_with_decimal()).unwrap();
813+
let result = plan_and_collect(&ctx, "select min(c1) from d_table")
814+
.await
815+
.unwrap();
816+
let expected = vec![
817+
"+-----------------+",
818+
"| MIN(d_table.c1) |",
819+
"+-----------------+",
820+
"| -100.009 |",
821+
"+-----------------+",
822+
];
823+
assert_eq!(
824+
&DataType::Decimal(10, 3),
825+
result[0].schema().field(0).data_type()
826+
);
827+
assert_batches_sorted_eq!(expected, &result);
828+
Ok(())
829+
}
830+
831+
#[tokio::test]
832+
async fn aggregate_decimal_max() -> Result<()> {
833+
let ctx = SessionContext::new();
834+
// the data type of c1 is decimal(10,3)
835+
ctx.register_table("d_table", table_with_decimal()).unwrap();
836+
837+
let result = plan_and_collect(&ctx, "select max(c1) from d_table")
838+
.await
839+
.unwrap();
840+
let expected = vec![
841+
"+-----------------+",
842+
"| MAX(d_table.c1) |",
843+
"+-----------------+",
844+
"| 110.009 |",
845+
"+-----------------+",
846+
];
847+
assert_eq!(
848+
&DataType::Decimal(10, 3),
849+
result[0].schema().field(0).data_type()
850+
);
851+
assert_batches_sorted_eq!(expected, &result);
852+
Ok(())
853+
}
854+
855+
#[tokio::test]
856+
async fn aggregate_decimal_sum() -> Result<()> {
857+
let ctx = SessionContext::new();
858+
// the data type of c1 is decimal(10,3)
859+
ctx.register_table("d_table", table_with_decimal()).unwrap();
860+
let result = plan_and_collect(&ctx, "select sum(c1) from d_table")
861+
.await
862+
.unwrap();
863+
let expected = vec![
864+
"+-----------------+",
865+
"| SUM(d_table.c1) |",
866+
"+-----------------+",
867+
"| 100.000 |",
868+
"+-----------------+",
869+
];
870+
assert_eq!(
871+
&DataType::Decimal(20, 3),
872+
result[0].schema().field(0).data_type()
873+
);
874+
assert_batches_sorted_eq!(expected, &result);
875+
Ok(())
876+
}
877+
878+
#[tokio::test]
879+
async fn aggregate_decimal_avg() -> Result<()> {
880+
let ctx = SessionContext::new();
881+
// the data type of c1 is decimal(10,3)
882+
ctx.register_table("d_table", table_with_decimal()).unwrap();
883+
let result = plan_and_collect(&ctx, "select avg(c1) from d_table")
884+
.await
885+
.unwrap();
886+
let expected = vec![
887+
"+-----------------+",
888+
"| AVG(d_table.c1) |",
889+
"+-----------------+",
890+
"| 5.0000000 |",
891+
"+-----------------+",
892+
];
893+
assert_eq!(
894+
&DataType::Decimal(14, 7),
895+
result[0].schema().field(0).data_type()
896+
);
897+
assert_batches_sorted_eq!(expected, &result);
898+
Ok(())
899+
}
900+
901+
#[tokio::test]
902+
async fn aggregate() -> Result<()> {
903+
let results = execute_with_partition("SELECT SUM(c1), SUM(c2) FROM test", 4).await?;
904+
assert_eq!(results.len(), 1);
905+
906+
let expected = vec![
907+
"+--------------+--------------+",
908+
"| SUM(test.c1) | SUM(test.c2) |",
909+
"+--------------+--------------+",
910+
"| 60 | 220 |",
911+
"+--------------+--------------+",
912+
];
913+
assert_batches_sorted_eq!(expected, &results);
914+
915+
Ok(())
916+
}
917+
918+
#[tokio::test]
919+
async fn aggregate_empty() -> Result<()> {
920+
// The predicate on this query purposely generates no results
921+
let results =
922+
execute_with_partition("SELECT SUM(c1), SUM(c2) FROM test where c1 > 100000", 4)
923+
.await
924+
.unwrap();
925+
926+
assert_eq!(results.len(), 1);
927+
928+
let expected = vec![
929+
"+--------------+--------------+",
930+
"| SUM(test.c1) | SUM(test.c2) |",
931+
"+--------------+--------------+",
932+
"| | |",
933+
"+--------------+--------------+",
934+
];
935+
assert_batches_sorted_eq!(expected, &results);
936+
937+
Ok(())
938+
}
939+
940+
#[tokio::test]
941+
async fn aggregate_avg() -> Result<()> {
942+
let results = execute_with_partition("SELECT AVG(c1), AVG(c2) FROM test", 4).await?;
943+
assert_eq!(results.len(), 1);
944+
945+
let expected = vec![
946+
"+--------------+--------------+",
947+
"| AVG(test.c1) | AVG(test.c2) |",
948+
"+--------------+--------------+",
949+
"| 1.5 | 5.5 |",
950+
"+--------------+--------------+",
951+
];
952+
assert_batches_sorted_eq!(expected, &results);
953+
954+
Ok(())
955+
}
956+
957+
#[tokio::test]
958+
async fn aggregate_max() -> Result<()> {
959+
let results = execute_with_partition("SELECT MAX(c1), MAX(c2) FROM test", 4).await?;
960+
assert_eq!(results.len(), 1);
961+
962+
let expected = vec![
963+
"+--------------+--------------+",
964+
"| MAX(test.c1) | MAX(test.c2) |",
965+
"+--------------+--------------+",
966+
"| 3 | 10 |",
967+
"+--------------+--------------+",
968+
];
969+
assert_batches_sorted_eq!(expected, &results);
970+
971+
Ok(())
972+
}
973+
974+
#[tokio::test]
975+
async fn aggregate_min() -> Result<()> {
976+
let results = execute_with_partition("SELECT MIN(c1), MIN(c2) FROM test", 4).await?;
977+
assert_eq!(results.len(), 1);
978+
979+
let expected = vec![
980+
"+--------------+--------------+",
981+
"| MIN(test.c1) | MIN(test.c2) |",
982+
"+--------------+--------------+",
983+
"| 0 | 1 |",
984+
"+--------------+--------------+",
985+
];
986+
assert_batches_sorted_eq!(expected, &results);
987+
988+
Ok(())
989+
}
990+
991+
#[tokio::test]
992+
async fn aggregate_grouped() -> Result<()> {
993+
let results =
994+
execute_with_partition("SELECT c1, SUM(c2) FROM test GROUP BY c1", 4).await?;
995+
996+
let expected = vec![
997+
"+----+--------------+",
998+
"| c1 | SUM(test.c2) |",
999+
"+----+--------------+",
1000+
"| 0 | 55 |",
1001+
"| 1 | 55 |",
1002+
"| 2 | 55 |",
1003+
"| 3 | 55 |",
1004+
"+----+--------------+",
1005+
];
1006+
assert_batches_sorted_eq!(expected, &results);
1007+
1008+
Ok(())
1009+
}
1010+
1011+
#[tokio::test]
1012+
async fn aggregate_grouped_avg() -> Result<()> {
1013+
let results =
1014+
execute_with_partition("SELECT c1, AVG(c2) FROM test GROUP BY c1", 4).await?;
1015+
1016+
let expected = vec![
1017+
"+----+--------------+",
1018+
"| c1 | AVG(test.c2) |",
1019+
"+----+--------------+",
1020+
"| 0 | 5.5 |",
1021+
"| 1 | 5.5 |",
1022+
"| 2 | 5.5 |",
1023+
"| 3 | 5.5 |",
1024+
"+----+--------------+",
1025+
];
1026+
assert_batches_sorted_eq!(expected, &results);
1027+
1028+
Ok(())
1029+
}
1030+
1031+
#[tokio::test]
1032+
async fn aggregate_grouped_empty() -> Result<()> {
1033+
let results = execute_with_partition(
1034+
"SELECT c1, AVG(c2) FROM test WHERE c1 = 123 GROUP BY c1",
1035+
4,
1036+
)
1037+
.await?;
1038+
1039+
let expected = vec![
1040+
"+----+--------------+",
1041+
"| c1 | AVG(test.c2) |",
1042+
"+----+--------------+",
1043+
"+----+--------------+",
1044+
];
1045+
assert_batches_sorted_eq!(expected, &results);
1046+
1047+
Ok(())
1048+
}
1049+
1050+
#[tokio::test]
1051+
async fn aggregate_grouped_max() -> Result<()> {
1052+
let results =
1053+
execute_with_partition("SELECT c1, MAX(c2) FROM test GROUP BY c1", 4).await?;
1054+
1055+
let expected = vec![
1056+
"+----+--------------+",
1057+
"| c1 | MAX(test.c2) |",
1058+
"+----+--------------+",
1059+
"| 0 | 10 |",
1060+
"| 1 | 10 |",
1061+
"| 2 | 10 |",
1062+
"| 3 | 10 |",
1063+
"+----+--------------+",
1064+
];
1065+
assert_batches_sorted_eq!(expected, &results);
1066+
1067+
Ok(())
1068+
}
1069+
1070+
#[tokio::test]
1071+
async fn aggregate_grouped_min() -> Result<()> {
1072+
let results =
1073+
execute_with_partition("SELECT c1, MIN(c2) FROM test GROUP BY c1", 4).await?;
1074+
1075+
let expected = vec![
1076+
"+----+--------------+",
1077+
"| c1 | MIN(test.c2) |",
1078+
"+----+--------------+",
1079+
"| 0 | 1 |",
1080+
"| 1 | 1 |",
1081+
"| 2 | 1 |",
1082+
"| 3 | 1 |",
1083+
"+----+--------------+",
1084+
];
1085+
assert_batches_sorted_eq!(expected, &results);
1086+
1087+
Ok(())
1088+
}
1089+
1090+
#[tokio::test]
1091+
async fn aggregate_avg_add() -> Result<()> {
1092+
let results = execute_with_partition(
1093+
"SELECT AVG(c1), AVG(c1) + 1, AVG(c1) + 2, 1 + AVG(c1) FROM test",
1094+
4,
1095+
)
1096+
.await?;
1097+
assert_eq!(results.len(), 1);
1098+
1099+
let expected = vec![
1100+
"+--------------+----------------------------+----------------------------+----------------------------+",
1101+
"| AVG(test.c1) | AVG(test.c1) Plus Int64(1) | AVG(test.c1) Plus Int64(2) | Int64(1) Plus AVG(test.c1) |",
1102+
"+--------------+----------------------------+----------------------------+----------------------------+",
1103+
"| 1.5 | 2.5 | 3.5 | 2.5 |",
1104+
"+--------------+----------------------------+----------------------------+----------------------------+",
1105+
];
1106+
assert_batches_sorted_eq!(expected, &results);
1107+
1108+
Ok(())
1109+
}

0 commit comments

Comments
 (0)