@@ -115,3 +115,138 @@ async fn test_mode_time64() {
115115 - +-----------------------------+
116116 "### ) ;
117117}
118+
119+ #[ tokio:: test]
120+ async fn test_max_by_and_min_by ( ) {
121+ let mut execution = TestExecution :: new ( ) . await . unwrap ( ) ;
122+
123+ // Test max_by with numbers
124+ let actual = execution
125+ . run_and_format ( "SELECT max_by(x, y) FROM VALUES (1, 10), (2, 5), (3, 15), (4, 8) as tab(x, y);" )
126+ . await ;
127+
128+ insta:: assert_yaml_snapshot!( actual, @r###"
129+ - +---------------------+
130+ - "| max_by(tab.x,tab.y) |"
131+ - +---------------------+
132+ - "| 3 |"
133+ - +---------------------+
134+ "### ) ;
135+
136+ // Test min_by with numbers
137+ let actual = execution
138+ . run_and_format ( "SELECT min_by(x, y) FROM VALUES (1, 10), (2, 5), (3, 15), (4, 8) as tab(x, y);" )
139+ . await ;
140+
141+ insta:: assert_yaml_snapshot!( actual, @r###"
142+ - +---------------------+
143+ - "| min_by(tab.x,tab.y) |"
144+ - +---------------------+
145+ - "| 2 |"
146+ - +---------------------+
147+ "### ) ;
148+
149+ // Test max_by with strings
150+ let actual = execution
151+ . run_and_format ( "SELECT max_by(name, length(name)) FROM VALUES ('Alice'), ('Bob'), ('Charlie') as tab(name);" )
152+ . await ;
153+
154+ insta:: assert_yaml_snapshot!( actual, @r###"
155+ - +---------------------------------------------+
156+ - "| max_by(tab.name,character_length(tab.name)) |"
157+ - +---------------------------------------------+
158+ - "| Charlie |"
159+ - +---------------------------------------------+
160+ "### ) ;
161+
162+ // Test min_by with strings
163+ let actual = execution
164+ . run_and_format ( "SELECT min_by(name, length(name)) FROM VALUES ('Alice'), ('Bob'), ('Charlie') as tab(name);" )
165+ . await ;
166+
167+ insta:: assert_yaml_snapshot!( actual, @r###"
168+ - +---------------------------------------------+
169+ - "| min_by(tab.name,character_length(tab.name)) |"
170+ - +---------------------------------------------+
171+ - "| Bob |"
172+ - +---------------------------------------------+
173+ "### ) ;
174+
175+ // Test max_by with null values
176+ let actual = execution
177+ . run_and_format ( "SELECT max_by(x, y) FROM VALUES (1, 10), (2, null), (3, 15), (null, 8) as tab(x, y);" )
178+ . await ;
179+
180+ insta:: assert_yaml_snapshot!( actual, @r###"
181+ - +---------------------+
182+ - "| max_by(tab.x,tab.y) |"
183+ - +---------------------+
184+ - "| 2 |"
185+ - +---------------------+
186+ "### ) ;
187+
188+ // Test min_by with null values
189+ let actual = execution
190+ . run_and_format ( "SELECT min_by(x, y) FROM VALUES (1, 10), (2, null), (3, 15), (null, 8) as tab(x, y);" )
191+ . await ;
192+
193+ insta:: assert_yaml_snapshot!( actual, @r###"
194+ - +---------------------+
195+ - "| min_by(tab.x,tab.y) |"
196+ - +---------------------+
197+ - "| 2 |"
198+ - +---------------------+
199+ "### ) ;
200+
201+ // Test max_by with a single value
202+ let actual = execution
203+ . run_and_format ( "SELECT max_by(x, y) FROM VALUES (1, 10) as tab(x, y);" )
204+ . await ;
205+
206+ insta:: assert_yaml_snapshot!( actual, @r###"
207+ - +---------------------+
208+ - "| max_by(tab.x,tab.y) |"
209+ - +---------------------+
210+ - "| 1 |"
211+ - +---------------------+
212+ "### ) ;
213+
214+ // Test min_by with a single value
215+ let actual = execution
216+ . run_and_format ( "SELECT min_by(x, y) FROM VALUES (1, 10) as tab(x, y);" )
217+ . await ;
218+
219+ insta:: assert_yaml_snapshot!( actual, @r###"
220+ - +---------------------+
221+ - "| min_by(tab.x,tab.y) |"
222+ - +---------------------+
223+ - "| 1 |"
224+ - +---------------------+
225+ "### ) ;
226+
227+ // Test max_by with an empty set
228+ let actual = execution
229+ . run_and_format ( "SELECT max_by(x, y) FROM (SELECT * FROM (VALUES (1, 10)) WHERE 1=0) as tab(x, y);" )
230+ . await ;
231+
232+ insta:: assert_yaml_snapshot!( actual, @r###"
233+ - +---------------------+
234+ - "| max_by(tab.x,tab.y) |"
235+ - +---------------------+
236+ - "| |"
237+ - +---------------------+
238+ "### ) ;
239+
240+ // Test min_by with an empty set
241+ let actual = execution
242+ . run_and_format ( "SELECT min_by(x, y) FROM (SELECT * FROM (VALUES (1, 10)) WHERE 1=0) as tab(x, y);" )
243+ . await ;
244+
245+ insta:: assert_yaml_snapshot!( actual, @r###"
246+ - +---------------------+
247+ - "| min_by(tab.x,tab.y) |"
248+ - +---------------------+
249+ - "| |"
250+ - +---------------------+
251+ "### ) ;
252+ }
0 commit comments