use of io.confluent.ksql.execution.expression.tree.DoubleLiteral in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateArithmetic.
@Test
public void shouldEvaluateArithmetic() {
// Given:
final Expression expression1 = new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new IntegerLiteral(2));
final Expression expression2 = new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LongLiteral(4));
final Expression expression3 = new ArithmeticBinaryExpression(Operator.ADD, new DoubleLiteral(5.5), new LongLiteral(4));
final Expression expression4 = new ArithmeticBinaryExpression(Operator.MULTIPLY, new IntegerLiteral(5), new LongLiteral(4));
final Expression expression5 = new ArithmeticBinaryExpression(Operator.DIVIDE, new LongLiteral(18), new LongLiteral(3));
final Expression expression6 = new ArithmeticBinaryExpression(Operator.MODULUS, new LongLiteral(20), new LongLiteral(3));
final Expression expression7 = new ArithmeticBinaryExpression(Operator.ADD, new DecimalLiteral(new BigDecimal("12.5").setScale(2)), new DecimalLiteral(new BigDecimal("1.25").setScale(2)));
final Expression expression8 = new ArithmeticBinaryExpression(Operator.ADD, new DecimalLiteral(new BigDecimal("12.5").setScale(2)), new DoubleLiteral(2.0d));
// When:
InterpretedExpression interpreter1 = interpreter(expression1);
InterpretedExpression interpreter2 = interpreter(expression2);
InterpretedExpression interpreter3 = interpreter(expression3);
InterpretedExpression interpreter4 = interpreter(expression4);
InterpretedExpression interpreter5 = interpreter(expression5);
InterpretedExpression interpreter6 = interpreter(expression6);
InterpretedExpression interpreter7 = interpreter(expression7);
InterpretedExpression interpreter8 = interpreter(expression8);
// Then:
assertThat(interpreter1.evaluate(ROW), is(3));
assertThat(interpreter2.evaluate(ROW), is(5L));
assertThat(interpreter3.evaluate(ROW), is(9.5d));
assertThat(interpreter4.evaluate(ROW), is(20L));
assertThat(interpreter5.evaluate(ROW), is(6L));
assertThat(interpreter6.evaluate(ROW), is(2L));
assertThat(interpreter7.evaluate(ROW), is(BigDecimal.valueOf(13.75).setScale(2)));
assertThat(interpreter8.evaluate(ROW), is(14.5d));
}
use of io.confluent.ksql.execution.expression.tree.DoubleLiteral in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateCastToDecimal.
@Test
public void shouldEvaluateCastToDecimal() {
// Given:
final Expression cast1 = new Cast(new LongLiteral(10L), new Type(SqlTypes.decimal(10, 1)));
final Expression cast2 = new Cast(new StringLiteral("1234.5"), new Type(SqlTypes.decimal(10, 1)));
final Expression cast3 = new Cast(new IntegerLiteral(12), new Type(SqlTypes.decimal(10, 1)));
final Expression cast4 = new Cast(new DoubleLiteral(4567.5), new Type(SqlTypes.decimal(10, 1)));
// When:
InterpretedExpression interpreter1 = interpreter(cast1);
InterpretedExpression interpreter2 = interpreter(cast2);
InterpretedExpression interpreter3 = interpreter(cast3);
InterpretedExpression interpreter4 = interpreter(cast4);
// Then:
assertThat(interpreter1.evaluate(ROW), is(BigDecimal.valueOf(10L).setScale(1)));
assertThat(interpreter2.evaluate(ROW), is(BigDecimal.valueOf(1234.5d).setScale(1)));
assertThat(interpreter3.evaluate(ROW), is(BigDecimal.valueOf(12).setScale(1)));
assertThat(interpreter4.evaluate(ROW), is(BigDecimal.valueOf(4567.5d).setScale(1)));
}
use of io.confluent.ksql.execution.expression.tree.DoubleLiteral in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateComparisons_double.
@Test
public void shouldEvaluateComparisons_double() {
// Given:
final Expression expression1 = new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL3, new DoubleLiteral(-10.0));
final Expression expression2 = new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL3, new DoubleLiteral(10));
final Expression expression3 = new ComparisonExpression(ComparisonExpression.Type.EQUAL, COL3, new DoubleLiteral(6.5));
// When:
InterpretedExpression interpreter1 = interpreter(expression1);
InterpretedExpression interpreter2 = interpreter(expression2);
InterpretedExpression interpreter3 = interpreter(expression3);
// Then:
assertThat(interpreter1.evaluate(make(3, 5d)), is(true));
assertThat(interpreter1.evaluate(make(3, -20d)), is(false));
assertThat(interpreter2.evaluate(make(3, 5d)), is(true));
assertThat(interpreter2.evaluate(make(3, 20d)), is(false));
assertThat(interpreter3.evaluate(make(3, 6.5d)), is(true));
assertThat(interpreter3.evaluate(make(3, 8.5d)), is(false));
}
use of io.confluent.ksql.execution.expression.tree.DoubleLiteral in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateCastToMap.
@Test
public void shouldEvaluateCastToMap() {
// Given:
final Expression cast1 = new Cast(new CreateMapExpression(ImmutableMap.of(new StringLiteral("1"), new StringLiteral("2"), new StringLiteral("3"), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.INTEGER, SqlTypes.INTEGER)));
final Expression cast2 = new Cast(new CreateMapExpression(ImmutableMap.of(new DoubleLiteral(2.5), new StringLiteral("2"), new DoubleLiteral(3.5), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.STRING, SqlTypes.BIGINT)));
// When:
InterpretedExpression interpreter1 = interpreter(cast1);
InterpretedExpression interpreter2 = interpreter(cast2);
// Then:
assertThat(interpreter1.evaluate(ROW), is(ImmutableMap.of(1, 2, 3, 4)));
assertThat(interpreter2.evaluate(ROW), is(ImmutableMap.of("2.5", 2L, "3.5", 4L)));
}
use of io.confluent.ksql.execution.expression.tree.DoubleLiteral in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldProcessCreateMapExpressionCorrectly.
@Test
public void shouldProcessCreateMapExpressionCorrectly() {
// Given:
Expression expression = new CreateMapExpression(ImmutableMap.of(new StringLiteral("foo"), new SubscriptExpression(MAPCOL, new StringLiteral("key1")), new StringLiteral("bar"), new DoubleLiteral(1.0d)));
// When:
String java = sqlToJavaVisitor.process(expression);
// Then:
assertThat(java, equalTo("((Map)new MapBuilder(2)" + ".put( (new Supplier<Object>() {@Override public Object get() { try { return \"foo\"; } catch (Exception e) { " + onException("map key") + " }}}).get(), (new Supplier<Object>() {@Override public Object get() { try { return ((Double) ((java.util.Map)COL5).get(\"key1\")); } catch (Exception e) { " + onException("map value") + " }}}).get())" + ".put( (new Supplier<Object>() {@Override public Object get() { try { return \"bar\"; } catch (Exception e) { " + onException("map key") + " }}}).get(), (new Supplier<Object>() {@Override public Object get() { try { return 1E0; } catch (Exception e) { " + onException("map value") + " }}}).get()).build())"));
}
Aggregations