Search in sources :

Example 11 with ExpressionMetadata

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));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Analysis(io.confluent.ksql.analyzer.Analysis) Test(org.junit.Test)

Aggregations

ExpressionMetadata (io.confluent.ksql.util.ExpressionMetadata)11 Analysis (io.confluent.ksql.analyzer.Analysis)6 Test (org.junit.Test)5 GenericRowValueTypeEnforcer (io.confluent.ksql.util.GenericRowValueTypeEnforcer)3 CodeGenRunner (io.confluent.ksql.codegen.CodeGenRunner)2 Kudf (io.confluent.ksql.function.udf.Kudf)2 Expression (io.confluent.ksql.parser.tree.Expression)2 KsqlException (io.confluent.ksql.util.KsqlException)2 Pair (io.confluent.ksql.util.Pair)2 ArrayList (java.util.ArrayList)2 Schema (org.apache.kafka.connect.data.Schema)2 FunctionRegistry (io.confluent.ksql.function.FunctionRegistry)1 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)1 PlanNode (io.confluent.ksql.planner.plan.PlanNode)1 ProjectNode (io.confluent.ksql.planner.plan.ProjectNode)1 ExpressionTypeManager (io.confluent.ksql.util.ExpressionTypeManager)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)1 Predicate (org.apache.kafka.streams.kstream.Predicate)1