Skip to content

Commit a35f963

Browse files
Added missing integration tests for the pow and power functions (#247) (#1457)
* Changed pow and power to return null when an invalid response would be returned and added missing integration tests for the pow and power function. Signed-off-by: Matthew Wells <matthew.wells@improving.com>
1 parent aa92f99 commit a35f963

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,15 @@ FunctionBuilder>>> powerFunctionImpl() {
343343
DOUBLE, LONG, LONG),
344344
FunctionDSL.impl(
345345
FunctionDSL.nullMissingHandling(
346-
(v1, v2) -> new ExprDoubleValue(Math.pow(v1.floatValue(), v2.floatValue()))),
346+
(v1, v2) -> v1.floatValue() <= 0 && v2.floatValue()
347+
!= Math.floor(v2.floatValue()) ? ExprNullValue.of() :
348+
new ExprDoubleValue(Math.pow(v1.floatValue(), v2.floatValue()))),
347349
DOUBLE, FLOAT, FLOAT),
348350
FunctionDSL.impl(
349351
FunctionDSL.nullMissingHandling(
350-
(v1, v2) -> new ExprDoubleValue(Math.pow(v1.doubleValue(), v2.doubleValue()))),
352+
(v1, v2) -> v1.doubleValue() <= 0 && v2.doubleValue()
353+
!= Math.floor(v2.doubleValue()) ? ExprNullValue.of() :
354+
new ExprDoubleValue(Math.pow(v1.doubleValue(), v2.doubleValue()))),
351355
DOUBLE, DOUBLE, DOUBLE));
352356
}
353357

core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,56 @@ public void pow_null_missing() {
14091409
assertTrue(power.valueOf(valueEnv()).isMissing());
14101410
}
14111411

1412+
/**
1413+
* Test pow/power with null output.
1414+
*/
1415+
@Test
1416+
public void pow_null_output() {
1417+
FunctionExpression pow = DSL.pow(DSL.literal((double) -2), DSL.literal(1.5));
1418+
assertEquals(pow.type(), DOUBLE);
1419+
assertEquals(String.format("pow(%s, %s)", (double) -2, 1.5), pow.toString());
1420+
assertTrue(pow.valueOf(valueEnv()).isNull());
1421+
1422+
pow = DSL.pow(DSL.literal((float) -2), DSL.literal((float) 1.5));
1423+
assertEquals(pow.type(), DOUBLE);
1424+
assertEquals(String.format("pow(%s, %s)", (float) -2, (float) 1.5), pow.toString());
1425+
assertTrue(pow.valueOf(valueEnv()).isNull());
1426+
}
1427+
1428+
/**
1429+
* Test pow/power with edge cases.
1430+
*/
1431+
@Test
1432+
public void pow_edge_cases() {
1433+
FunctionExpression pow = DSL.pow(DSL.literal((double) -2), DSL.literal((double) 2));
1434+
assertEquals(pow.type(), DOUBLE);
1435+
assertEquals(String.format("pow(%s, %s)",(double) -2, (double) 2), pow.toString());
1436+
assertThat(
1437+
pow.valueOf(valueEnv()),
1438+
allOf(hasType(DOUBLE), hasValue(Math.pow(-2, 2))));
1439+
1440+
pow = DSL.pow(DSL.literal((double) 2), DSL.literal((double) 1.5));
1441+
assertEquals(pow.type(), DOUBLE);
1442+
assertEquals(String.format("pow(%s, %s)", (double) 2, (double) 1.5), pow.toString());
1443+
assertThat(
1444+
pow.valueOf(valueEnv()),
1445+
allOf(hasType(DOUBLE), hasValue(Math.pow(2, 1.5))));
1446+
1447+
pow = DSL.pow(DSL.literal((float) -2), DSL.literal((float) 2));
1448+
assertEquals(pow.type(), DOUBLE);
1449+
assertEquals(String.format("pow(%s, %s)", (float) -2, (float) 2), pow.toString());
1450+
assertThat(
1451+
pow.valueOf(valueEnv()),
1452+
allOf(hasType(DOUBLE), hasValue(Math.pow((float) -2, (float) 2))));
1453+
1454+
pow = DSL.pow(DSL.literal((float) 2), DSL.literal((float) 1.5));
1455+
assertEquals(pow.type(), DOUBLE);
1456+
assertEquals(String.format("pow(%s, %s)", (float) 2, (float) 1.5), pow.toString());
1457+
assertThat(
1458+
pow.valueOf(valueEnv()),
1459+
allOf(hasType(DOUBLE), hasValue(Math.pow((float) 2, (float) 1.5))));
1460+
}
1461+
14121462
/**
14131463
* Test rint with byte value.
14141464
*/

integ-test/src/test/java/org/opensearch/sql/sql/MathematicalFunctionIT.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,68 @@ public void testMod() throws IOException {
113113
verifyDataRows(result, rows(1.1));
114114
}
115115

116+
@Test
117+
public void testPow() throws IOException {
118+
JSONObject result = executeQuery("select pow(3, 2)");
119+
verifySchema(result, schema("pow(3, 2)", null, "double"));
120+
verifyDataRows(result, rows(9.0));
121+
122+
result = executeQuery("select pow(0, 2)");
123+
verifySchema(result, schema("pow(0, 2)", null, "double"));
124+
verifyDataRows(result, rows(0.0));
125+
126+
result = executeQuery("select pow(3, 0)");
127+
verifySchema(result, schema("pow(3, 0)", null, "double"));
128+
verifyDataRows(result, rows(1.0));
129+
130+
result = executeQuery("select pow(-2, 3)");
131+
verifySchema(result, schema("pow(-2, 3)", null, "double"));
132+
verifyDataRows(result, rows(-8.0));
133+
134+
result = executeQuery("select pow(2, -2)");
135+
verifySchema(result, schema("pow(2, -2)", null, "double"));
136+
verifyDataRows(result, rows(0.25));
137+
138+
result = executeQuery("select pow(-2, -3)");
139+
verifySchema(result, schema("pow(-2, -3)", null, "double"));
140+
verifyDataRows(result, rows(-0.125));
141+
142+
result = executeQuery("select pow(-1, 0.5)");
143+
verifySchema(result, schema("pow(-1, 0.5)", null, "double"));
144+
verifyDataRows(result, rows((Object) null));
145+
}
146+
147+
@Test
148+
public void testPower() throws IOException {
149+
JSONObject result = executeQuery("select power(3, 2)");
150+
verifySchema(result, schema("power(3, 2)", null, "double"));
151+
verifyDataRows(result, rows(9.0));
152+
153+
result = executeQuery("select power(0, 2)");
154+
verifySchema(result, schema("power(0, 2)", null, "double"));
155+
verifyDataRows(result, rows(0.0));
156+
157+
result = executeQuery("select power(3, 0)");
158+
verifySchema(result, schema("power(3, 0)", null, "double"));
159+
verifyDataRows(result, rows(1.0));
160+
161+
result = executeQuery("select power(-2, 3)");
162+
verifySchema(result, schema("power(-2, 3)", null, "double"));
163+
verifyDataRows(result, rows(-8.0));
164+
165+
result = executeQuery("select power(2, -2)");
166+
verifySchema(result, schema("power(2, -2)", null, "double"));
167+
verifyDataRows(result, rows(0.25));
168+
169+
result = executeQuery("select power(2, -2)");
170+
verifySchema(result, schema("power(2, -2)", null, "double"));
171+
verifyDataRows(result, rows(0.25));
172+
173+
result = executeQuery("select power(-2, -3)");
174+
verifySchema(result, schema("power(-2, -3)", null, "double"));
175+
verifyDataRows(result, rows(-0.125));
176+
}
177+
116178
@Test
117179
public void testRint() throws IOException {
118180
JSONObject result = executeQuery("select rint(56.78)");

0 commit comments

Comments
 (0)