Skip to content

Commit 8212787

Browse files
committed
Test type checking for aggregation functions
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 7f26f9d commit 8212787

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLFunctionTypeTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,73 @@ public void testLog2WithWrongArgShouldThrow() {
221221
verifyErrorMessageContains(
222222
wrongArgException, "LOG2 function expects {[INTEGER],[DOUBLE]}, but got [STRING,STRING]");
223223
}
224+
225+
@Test
226+
public void testAvgWithWrongArgType() {
227+
Exception e =
228+
Assert.assertThrows(
229+
ExpressionEvaluationException.class,
230+
() -> getRelNode("source=EMP | stats avg(ENAME) as avg_name"));
231+
verifyErrorMessageContains(
232+
e, "Aggregation function AVG expects field type {[INTEGER],[DOUBLE]}, but got [STRING]");
233+
}
234+
235+
@Test
236+
public void testVarsampWithWrongArgType() {
237+
Exception e =
238+
Assert.assertThrows(
239+
ExpressionEvaluationException.class,
240+
() -> getRelNode("source=EMP | stats var_samp(ENAME) as varsamp_name"));
241+
verifyErrorMessageContains(
242+
e,
243+
"Aggregation function VARSAMP expects field type {[INTEGER],[DOUBLE]}, but got [STRING]");
244+
}
245+
246+
@Test
247+
public void testVarpopWithWrongArgType() {
248+
Exception e =
249+
Assert.assertThrows(
250+
ExpressionEvaluationException.class,
251+
() -> getRelNode("source=EMP | stats var_pop(ENAME) as varpop_name"));
252+
verifyErrorMessageContains(
253+
e, "Aggregation function VARPOP expects field type {[INTEGER],[DOUBLE]}, but got [STRING]");
254+
}
255+
256+
@Test
257+
public void testStddevSampWithWrongArgType() {
258+
Exception e =
259+
Assert.assertThrows(
260+
ExpressionEvaluationException.class,
261+
() -> getRelNode("source=EMP | stats stddev_samp(ENAME) as stddev_name"));
262+
verifyErrorMessageContains(
263+
e,
264+
"Aggregation function STDDEV_SAMP expects field type {[INTEGER],[DOUBLE]}, but got"
265+
+ " [STRING]");
266+
}
267+
268+
@Test
269+
public void testStddevPopWithWrongArgType() {
270+
Exception e =
271+
Assert.assertThrows(
272+
ExpressionEvaluationException.class,
273+
() -> getRelNode("source=EMP | stats stddev_pop(ENAME) as stddev_name"));
274+
verifyErrorMessageContains(
275+
e,
276+
"Aggregation function STDDEV_POP expects field type {[INTEGER],[DOUBLE]}, but got"
277+
+ " [STRING]");
278+
}
279+
280+
@Test
281+
public void testPercentileApproxWithWrongArgType() {
282+
// First argument should be numeric
283+
Exception e1 =
284+
Assert.assertThrows(
285+
ExpressionEvaluationException.class,
286+
() -> getRelNode("source=EMP | stats percentile_approx(ENAME, 50) as percentile"));
287+
verifyErrorMessageContains(
288+
e1,
289+
"Aggregation function PERCENTILE_APPROX expects field type and additional arguments"
290+
+ " {[INTEGER,INTEGER],[INTEGER,DOUBLE],[DOUBLE,INTEGER],[DOUBLE,DOUBLE],[INTEGER,INTEGER,INTEGER],[INTEGER,INTEGER,DOUBLE],[INTEGER,DOUBLE,INTEGER],[INTEGER,DOUBLE,DOUBLE],[DOUBLE,INTEGER,INTEGER],[DOUBLE,INTEGER,DOUBLE],[DOUBLE,DOUBLE,INTEGER],[DOUBLE,DOUBLE,DOUBLE]},"
291+
+ " but got [STRING,INTEGER]");
292+
}
224293
}

0 commit comments

Comments
 (0)