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;
};
}
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);
}
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);
}
}
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;
}
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));
}
Aggregations