Search in sources :

Example 6 with ExpressionMetadata

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

the class SqlPredicate method getWindowedKeyPredicate.

private Predicate getWindowedKeyPredicate() {
    final ExpressionMetadata expressionEvaluator = createExpressionMetadata();
    return (Predicate<Windowed<String>, GenericRow>) (key, row) -> {
        try {
            Kudf[] kudfs = expressionEvaluator.getUdfs();
            Object[] values = new Object[columnIndexes.length];
            for (int i = 0; i < values.length; i++) {
                if (columnIndexes[i] < 0) {
                    values[i] = kudfs[i];
                } else {
                    values[i] = genericRowValueTypeEnforcer.enforceFieldType(columnIndexes[i], row.getColumns().get(columnIndexes[i]));
                }
            }
            return (Boolean) ee.evaluate(values);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        log.error("Invalid format: " + key + " : " + row);
        return false;
    };
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) KsqlException(io.confluent.ksql.util.KsqlException) Predicate(org.apache.kafka.streams.kstream.Predicate) Kudf(io.confluent.ksql.function.udf.Kudf)

Example 7 with ExpressionMetadata

use of io.confluent.ksql.util.ExpressionMetadata 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 8 with ExpressionMetadata

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

the class SchemaKStream method createSelectValueMapperAndSchema.

Pair<Schema, SelectValueMapper> createSelectValueMapperAndSchema(final List<Pair<String, Expression>> expressionPairList) {
    try {
        final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, functionRegistry);
        final SchemaBuilder schemaBuilder = SchemaBuilder.struct();
        final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
        for (Pair<String, Expression> expressionPair : expressionPairList) {
            final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
            schemaBuilder.field(expressionPair.getLeft(), expressionEvaluator.getExpressionType());
            expressionEvaluators.add(expressionEvaluator);
        }
        return new Pair<>(schemaBuilder.build(), new SelectValueMapper(genericRowValueTypeEnforcer, expressionPairList, expressionEvaluators));
    } catch (Exception e) {
        throw new KsqlException("Code generation failed for SelectValueMapper", e);
    }
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair)

Example 9 with ExpressionMetadata

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

the class CodeGenRunnerTest method evalBooleanExpr.

private boolean evalBooleanExpr(String queryFormat, int cola, int colb, Object[] values) throws Exception {
    String simpleQuery = String.format(queryFormat, cola, colb);
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluatorMetadata0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    assertThat(expressionEvaluatorMetadata0.getIndexes().length, equalTo(2));
    int idx0 = expressionEvaluatorMetadata0.getIndexes()[0];
    int idx1 = expressionEvaluatorMetadata0.getIndexes()[1];
    assertThat(idx0, anyOf(equalTo(cola), equalTo(colb)));
    assertThat(idx1, anyOf(equalTo(cola), equalTo(colb)));
    assertThat(idx0, not(equalTo(idx1)));
    if (idx0 == colb) {
        Object tmp = values[0];
        values[0] = values[1];
        values[1] = tmp;
    }
    assertThat(expressionEvaluatorMetadata0.getUdfs().length, equalTo(2));
    Object result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(values);
    assertThat(result0, instanceOf(Boolean.class));
    return (Boolean) result0;
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Analysis(io.confluent.ksql.analyzer.Analysis)

Example 10 with ExpressionMetadata

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

the class CodeGenRunnerTest method testIsNull.

@Test
public void testIsNull() throws Exception {
    String simpleQuery = "SELECT col0 IS NULL FROM CODEGEN_TEST;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluatorMetadata0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    assertThat(expressionEvaluatorMetadata0.getIndexes().length, equalTo(1));
    int idx0 = expressionEvaluatorMetadata0.getIndexes()[0];
    assertThat(idx0, equalTo(0));
    assertThat(expressionEvaluatorMetadata0.getUdfs().length, equalTo(1));
    Object result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { null });
    assertThat(result0, instanceOf(Boolean.class));
    assertThat((Boolean) result0, is(true));
    result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { 12345L });
    assertThat(result0, instanceOf(Boolean.class));
    Assert.assertThat((Boolean) result0, is(false));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) 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