mirror of
https://github.com/apache/druid.git
synced 2025-02-17 07:25:02 +00:00
add round test (#11088)
* add round test * code style * handle null val for round function * handle null val for round function * support null for round * fix compatiblity * fix test * fix test * code style * optimize format
This commit is contained in:
parent
c158207ab6
commit
b8423a38df
@ -1311,6 +1311,9 @@ public interface Function
|
|||||||
public ExprEval apply(List<Expr> args, Expr.ObjectBinding bindings)
|
public ExprEval apply(List<Expr> args, Expr.ObjectBinding bindings)
|
||||||
{
|
{
|
||||||
ExprEval value1 = args.get(0).eval(bindings);
|
ExprEval value1 = args.get(0).eval(bindings);
|
||||||
|
if (NullHandling.sqlCompatible() && value1.isNumericNull()) {
|
||||||
|
return ExprEval.of(null);
|
||||||
|
}
|
||||||
if (value1.type() != ExprType.LONG && value1.type() != ExprType.DOUBLE) {
|
if (value1.type() != ExprType.LONG && value1.type() != ExprType.DOUBLE) {
|
||||||
throw new IAE(
|
throw new IAE(
|
||||||
"The first argument to the function[%s] should be integer or double type but got the type: %s",
|
"The first argument to the function[%s] should be integer or double type but got the type: %s",
|
||||||
|
@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import org.apache.druid.common.config.NullHandling;
|
import org.apache.druid.common.config.NullHandling;
|
||||||
import org.apache.druid.java.util.common.Pair;
|
import org.apache.druid.java.util.common.Pair;
|
||||||
|
import org.apache.druid.java.util.common.StringUtils;
|
||||||
import org.apache.druid.testing.InitializedNullHandlingTest;
|
import org.apache.druid.testing.InitializedNullHandlingTest;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -407,12 +408,40 @@ public class FunctionTest extends InitializedNullHandlingTest
|
|||||||
assertExpr("round(CAST(minLong, 'DOUBLE') - 1, -2)", BigDecimal.valueOf(((double) Long.MIN_VALUE) - 1).setScale(-2, RoundingMode.HALF_UP).doubleValue());
|
assertExpr("round(CAST(minLong, 'DOUBLE') - 1, -2)", BigDecimal.valueOf(((double) Long.MIN_VALUE) - 1).setScale(-2, RoundingMode.HALF_UP).doubleValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRoundWithNullValue()
|
||||||
|
{
|
||||||
|
Set<Pair<String, String>> invalidArguments = ImmutableSet.of(
|
||||||
|
Pair.of("null", "STRING"),
|
||||||
|
Pair.of("x", "STRING")
|
||||||
|
);
|
||||||
|
for (Pair<String, String> argAndType : invalidArguments) {
|
||||||
|
if (NullHandling.sqlCompatible()) {
|
||||||
|
assertExpr(StringUtils.format("round(%s)", argAndType.lhs), null);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
assertExpr(StringUtils.format("round(%s)", argAndType.lhs), null);
|
||||||
|
Assert.fail("Did not throw IllegalArgumentException");
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException e) {
|
||||||
|
Assert.assertEquals(
|
||||||
|
String.format(
|
||||||
|
Locale.ENGLISH,
|
||||||
|
"The first argument to the function[round] should be integer or double type but got the type: %s",
|
||||||
|
argAndType.rhs
|
||||||
|
),
|
||||||
|
e.getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRoundWithInvalidFirstArgument()
|
public void testRoundWithInvalidFirstArgument()
|
||||||
{
|
{
|
||||||
Set<Pair<String, String>> invalidArguments = ImmutableSet.of(
|
Set<Pair<String, String>> invalidArguments = ImmutableSet.of(
|
||||||
Pair.of("b", "LONG_ARRAY"),
|
Pair.of("b", "LONG_ARRAY"),
|
||||||
Pair.of("x", "STRING"),
|
|
||||||
Pair.of("c", "DOUBLE_ARRAY"),
|
Pair.of("c", "DOUBLE_ARRAY"),
|
||||||
Pair.of("a", "STRING_ARRAY")
|
Pair.of("a", "STRING_ARRAY")
|
||||||
|
|
||||||
|
@ -17552,4 +17552,43 @@ public class CalciteQueryTest extends BaseCalciteQueryTest
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRoundFuc() throws Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
testQuery(
|
||||||
|
"SELECT f1, round(f1) FROM druid.numfoo",
|
||||||
|
ImmutableList.of(
|
||||||
|
new Druids.ScanQueryBuilder()
|
||||||
|
.dataSource(CalciteTests.DATASOURCE3)
|
||||||
|
.intervals(querySegmentSpec(Filtration.eternity()))
|
||||||
|
.virtualColumns(
|
||||||
|
expressionVirtualColumn("v0", "round(\"f1\")", ValueType.FLOAT)
|
||||||
|
)
|
||||||
|
.columns("f1", "v0")
|
||||||
|
.legacy(false)
|
||||||
|
.resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
|
||||||
|
.context(QUERY_CONTEXT_DEFAULT)
|
||||||
|
.build()
|
||||||
|
),
|
||||||
|
NullHandling.sqlCompatible()
|
||||||
|
? ImmutableList.of(
|
||||||
|
new Object[]{1.0f, 1.0f},
|
||||||
|
new Object[]{0.1f, 0.0f},
|
||||||
|
new Object[]{0.0f, 0.0f},
|
||||||
|
new Object[]{null, null},
|
||||||
|
new Object[]{null, null},
|
||||||
|
new Object[]{null, null}
|
||||||
|
)
|
||||||
|
: ImmutableList.of(
|
||||||
|
new Object[]{1.0f, 1.0f},
|
||||||
|
new Object[]{0.1f, 0.0f},
|
||||||
|
new Object[]{0.0f, 0.0f},
|
||||||
|
new Object[]{0.0f, 0.0f},
|
||||||
|
new Object[]{0.0f, 0.0f},
|
||||||
|
new Object[]{0.0f, 0.0f}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -904,15 +904,17 @@ public class ExpressionsTest extends ExpressionTestBase
|
|||||||
{
|
{
|
||||||
final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
|
final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
|
||||||
|
|
||||||
expectException(
|
if (!NullHandling.sqlCompatible()) {
|
||||||
IAE.class,
|
expectException(
|
||||||
"The first argument to the function[round] should be integer or double type but got the type: STRING"
|
IAE.class,
|
||||||
);
|
"The first argument to the function[round] should be integer or double type but got the type: STRING"
|
||||||
|
);
|
||||||
|
}
|
||||||
testHelper.testExpression(
|
testHelper.testExpression(
|
||||||
roundFunction,
|
roundFunction,
|
||||||
testHelper.makeInputRef("s"),
|
testHelper.makeInputRef("s"),
|
||||||
DruidExpression.fromExpression("round(\"s\")"),
|
DruidExpression.fromExpression("round(\"s\")"),
|
||||||
"IAE Exception"
|
NullHandling.sqlCompatible() ? null : "IAE Exception"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user