Search in sources :

Example 1 with RoundOperatorConversion

use of org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion in project druid by druid-io.

the class ExpressionsTest method testRoundWithNanShouldRoundTo0.

@Test
public void testRoundWithNanShouldRoundTo0() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("nan"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "nan"))), 0D);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("fnan"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "fnan"))), 0D);
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 2 with RoundOperatorConversion

use of org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion in project druid by druid-io.

the class ExpressionsTest method testRoundWithInfinityShouldRoundTo0.

@Test
public void testRoundWithInfinityShouldRoundTo0() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    // CHECKSTYLE.OFF: Regexp
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("inf"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "inf"))), Double.MAX_VALUE);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("-inf"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "-inf"))), -1 * Double.MAX_VALUE);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("finf"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "finf"))), Double.MAX_VALUE);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("-finf"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "-finf"))), -1 * Double.MAX_VALUE);
// CHECKSTYLE.ON: Regexp
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 3 with RoundOperatorConversion

use of org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion in project druid by druid-io.

the class ExpressionsTest method testRoundWithInvalidSecondArgument.

@Test
public void testRoundWithInvalidSecondArgument() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    expectException(IAE.class, "The second argument to the function[round] should be integer type but got the type: STRING");
    testHelper.testExpressionString(roundFunction, ImmutableList.of(testHelper.makeInputRef("x"), testHelper.makeLiteral("foo")), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"), DruidExpression.ofStringLiteral("foo"))), "IAE Exception");
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 4 with RoundOperatorConversion

use of org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion in project druid by druid-io.

the class ExpressionsTest method testRoundWithInvalidArgument.

@Test
public void testRoundWithInvalidArgument() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    if (!NullHandling.sqlCompatible()) {
        expectException(IAE.class, "The first argument to the function[round] should be integer or double type but got the type: STRING");
    }
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("s"), DruidExpression.ofExpression(ColumnType.STRING, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.STRING, "s"))), NullHandling.sqlCompatible() ? null : "IAE Exception");
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Example 5 with RoundOperatorConversion

use of org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion in project druid by druid-io.

the class ExpressionsTest method testRound.

@Test
public void testRound() {
    final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("a"), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "a"))), 10L);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("b"), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "b"))), 25L);
    testHelper.testExpressionString(roundFunction, ImmutableList.of(testHelper.makeInputRef("b"), testHelper.makeLiteral(-1)), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "b"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(-1)))), 30L);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("x"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"))), 2.0);
    testHelper.testExpressionString(roundFunction, ImmutableList.of(testHelper.makeInputRef("x"), testHelper.makeLiteral(1)), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(1)))), 2.3);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("y"), DruidExpression.ofExpression(ColumnType.LONG, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "y"))), 3.0);
    testHelper.testExpression(roundFunction, testHelper.makeInputRef("z"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "z"))), -2.0);
}
Also used : RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Aggregations

SqlFunction (org.apache.calcite.sql.SqlFunction)5 RoundOperatorConversion (org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion)5 Test (org.junit.Test)5