MathematicalFunction { private static FunctionResolver truncate() { return FunctionDSL.define(BuiltinFunctionName.TRUNCATE.getName(), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( new BigDecimal(x.integerValue()).setScale(y.integerValue(), RoundingMode.DOWN).longValue())), LONG, INTEGER, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( new BigDecimal(x.integerValue()).setScale(y.integerValue(), RoundingMode.DOWN).longValue())), LONG, LONG, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( new BigDecimal(x.floatValue()).setScale(y.integerValue(), RoundingMode.DOWN).doubleValue())), DOUBLE, FLOAT, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( new BigDecimal(x.doubleValue()).setScale(y.integerValue(), RoundingMode.DOWN).doubleValue())), DOUBLE, DOUBLE, INTEGER)); }  static void register(BuiltinFunctionRepository repository);  }
@Test public void truncate_null_value() { FunctionExpression truncate = dsl.truncate(DSL.ref(INT_TYPE_NULL_VALUE_FIELD, INTEGER), DSL.literal(1)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isNull()); truncate = dsl.truncate(DSL.literal(1), DSL.ref(INT_TYPE_NULL_VALUE_FIELD, INTEGER)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isNull()); truncate = dsl.truncate( DSL.ref(INT_TYPE_NULL_VALUE_FIELD, INTEGER), DSL.ref(INT_TYPE_NULL_VALUE_FIELD, INTEGER)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isNull()); }
@Test public void truncate_missing_value() { FunctionExpression truncate = dsl.truncate(DSL.ref(INT_TYPE_MISSING_VALUE_FIELD, INTEGER), DSL.literal(1)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isMissing()); truncate = dsl.truncate(DSL.literal(1), DSL.ref(INT_TYPE_MISSING_VALUE_FIELD, INTEGER)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isMissing()); truncate = dsl.truncate( DSL.ref(INT_TYPE_MISSING_VALUE_FIELD, INTEGER), DSL.ref(INT_TYPE_MISSING_VALUE_FIELD, INTEGER)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isMissing()); }
@Test public void truncate_null_missing() { FunctionExpression truncate = dsl.truncate(DSL.ref(INT_TYPE_NULL_VALUE_FIELD, INTEGER), DSL.ref(INT_TYPE_MISSING_VALUE_FIELD, INTEGER)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isMissing()); truncate = dsl.truncate(DSL.ref(INT_TYPE_MISSING_VALUE_FIELD, INTEGER), DSL.ref(INT_TYPE_NULL_VALUE_FIELD, INTEGER)); assertEquals(LONG, truncate.type()); assertTrue(truncate.valueOf(valueEnv()).isMissing()); }