Search in sources :

Example 1 with GenericRowValueTypeEnforcer

use of io.confluent.ksql.util.GenericRowValueTypeEnforcer in project ksql by confluentinc.

the class CodeGenRunnerTest method testStringUDFExpr.

@Test
public void testStringUDFExpr() throws Exception {
    GenericRowValueTypeEnforcer genericRowValueTypeEnforcer = new GenericRowValueTypeEnforcer(schema);
    String simpleQuery = "SELECT LCASE(col1), UCASE(col2), TRIM(col1), CONCAT(col1,'_test'), SUBSTRING(col1, 1, 3) FROM codegen_test;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluator0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    Object argObj0 = genericRowValueTypeEnforcer.enforceFieldType(2, "Hello");
    Object result0 = expressionEvaluator0.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator0.getUdfs()[0], argObj0 });
    assertThat(result0, instanceOf(String.class));
    assertThat(result0, equalTo("hello"));
    ExpressionMetadata expressionEvaluator1 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(1));
    Object argObj1 = genericRowValueTypeEnforcer.enforceFieldType(2, "Hello");
    Object result1 = expressionEvaluator1.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator1.getUdfs()[0], argObj1 });
    assertThat(result1, instanceOf(String.class));
    assertThat(result1, equalTo("HELLO"));
    ExpressionMetadata expressionEvaluator2 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(2));
    Object argObj2 = genericRowValueTypeEnforcer.enforceFieldType(2, " Hello ");
    Object result2 = expressionEvaluator2.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator2.getUdfs()[0], argObj2 });
    assertThat(result2, instanceOf(String.class));
    assertThat(result2, equalTo("Hello"));
    ExpressionMetadata expressionEvaluator3 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(3));
    Object argObj3 = genericRowValueTypeEnforcer.enforceFieldType(2, "Hello");
    Object result3 = expressionEvaluator3.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator3.getUdfs()[0], argObj3 });
    assertThat(result3, instanceOf(String.class));
    assertThat(result3, equalTo("Hello_test"));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Analysis(io.confluent.ksql.analyzer.Analysis) Test(org.junit.Test)

Example 2 with GenericRowValueTypeEnforcer

use of io.confluent.ksql.util.GenericRowValueTypeEnforcer in project ksql by confluentinc.

the class SelectValueMapperTest method createMapper.

private SelectValueMapper createMapper(final String query) throws Exception {
    final PlanNode planNode = planBuilder.buildLogicalPlan(query);
    final ProjectNode projectNode = (ProjectNode) planNode.getSources().get(0);
    final Schema schema = planNode.getTheSourceNode().getSchema();
    final List<Pair<String, Expression>> expressionPairList = projectNode.getProjectNameExpressionPairList();
    final List<ExpressionMetadata> metadata = createExpressionMetadata(expressionPairList, schema);
    return new SelectValueMapper(new GenericRowValueTypeEnforcer(schema), expressionPairList, metadata);
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) PlanNode(io.confluent.ksql.planner.plan.PlanNode) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Schema(org.apache.kafka.connect.data.Schema) ProjectNode(io.confluent.ksql.planner.plan.ProjectNode) Pair(io.confluent.ksql.util.Pair)

Example 3 with GenericRowValueTypeEnforcer

use of io.confluent.ksql.util.GenericRowValueTypeEnforcer 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)3 GenericRowValueTypeEnforcer (io.confluent.ksql.util.GenericRowValueTypeEnforcer)3 Analysis (io.confluent.ksql.analyzer.Analysis)2 Test (org.junit.Test)2 PlanNode (io.confluent.ksql.planner.plan.PlanNode)1 ProjectNode (io.confluent.ksql.planner.plan.ProjectNode)1 Pair (io.confluent.ksql.util.Pair)1 Schema (org.apache.kafka.connect.data.Schema)1