use of io.confluent.ksql.util.ExpressionMetadata in project ksql by confluentinc.
the class CodeGenRunnerTest method testU1DFExpr.
@Test
public void testU1DFExpr() throws Exception {
String simpleQuery = "SELECT FLOOR(col3), CEIL(col3*3), ABS(col0+1.34), RANDOM()+10, ROUND(col3*2)+12 FROM codegen_test;";
Analysis analysis = analyzeQuery(simpleQuery);
GenericRowValueTypeEnforcer genericRowValueTypeEnforcer = new GenericRowValueTypeEnforcer(schema);
ExpressionMetadata expressionEvaluator0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
Object argObj0 = genericRowValueTypeEnforcer.enforceFieldType(3, 1.5);
Object result0 = expressionEvaluator0.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator0.getUdfs()[0], argObj0 });
assertThat(argObj0, instanceOf(Double.class));
assertThat(result0, instanceOf(Double.class));
assertThat(((Double) result0), equalTo(1.0));
ExpressionMetadata expressionEvaluator1 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(1));
Object argObj1 = genericRowValueTypeEnforcer.enforceFieldType(3, 1.5);
Object result1 = evaluateU1DF(expressionEvaluator1, argObj1);
assertThat(argObj1, instanceOf(Double.class));
assertThat(result1, instanceOf(Double.class));
assertThat(((Double) result1), equalTo(5.0));
ExpressionMetadata expressionEvaluator2 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(2));
Object argObj2 = genericRowValueTypeEnforcer.enforceFieldType(0, 15);
Object result2 = evaluateU1DF(expressionEvaluator2, argObj2);
assertThat(argObj2, instanceOf(Long.class));
assertThat(result2, instanceOf(Double.class));
assertThat(((Double) result2), equalTo(16.34));
ExpressionMetadata expressionEvaluator3 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(3));
Object result3 = expressionEvaluator3.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator3.getUdfs()[0] });
assertThat(result3, instanceOf(Double.class));
assertThat(((Double) result3).intValue(), equalTo(10));
ExpressionMetadata expressionEvaluator4 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(4));
Object argObj4 = genericRowValueTypeEnforcer.enforceFieldType(3, 1.5);
Object result4 = evaluateU1DF(expressionEvaluator4, argObj4);
assertThat(argObj4, instanceOf(Double.class));
assertThat(result4, instanceOf(Long.class));
assertThat(((Long) result4), equalTo(15L));
}
Aggregations