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