use of io.confluent.ksql.util.ExpressionMetadata in project ksql by confluentinc.
the class CodeGenRunnerTest method testIsNotNull.
@Test
public void testIsNotNull() throws Exception {
String simpleQuery = "SELECT col0 IS NOT 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));
Assert.assertThat((Boolean) result0, is(false));
result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { 12345L });
assertThat(result0, instanceOf(Boolean.class));
assertThat((Boolean) result0, is(true));
}
use of io.confluent.ksql.util.ExpressionMetadata 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"));
}
use of io.confluent.ksql.util.ExpressionMetadata in project ksql by confluentinc.
the class CodeGenRunnerTest method testArithmaticExpr.
@Test
public void testArithmaticExpr() throws Exception {
String simpleQuery = "SELECT col0+col3, col2, col3+10, col0*25, 12*4+2 FROM codegen_test WHERE col0 > 100;";
Analysis analysis = analyzeQuery(simpleQuery);
ExpressionMetadata expressionEvaluatorMetadata0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
assertThat(expressionEvaluatorMetadata0.getIndexes().length, equalTo(2));
assertThat(expressionEvaluatorMetadata0.getIndexes()[0], equalTo(3));
assertThat(expressionEvaluatorMetadata0.getIndexes()[1], equalTo(0));
assertThat(expressionEvaluatorMetadata0.getUdfs().length, equalTo(2));
Object result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { 10.0, 5l });
assertThat(result0, instanceOf(Double.class));
assertThat(((Double) result0), equalTo(15.0));
ExpressionMetadata expressionEvaluatorMetadata1 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(3));
assertThat(expressionEvaluatorMetadata1.getIndexes().length, equalTo(1));
assertThat(expressionEvaluatorMetadata1.getIndexes()[0], equalTo(0));
assertThat(expressionEvaluatorMetadata1.getUdfs().length, equalTo(1));
Object result1 = expressionEvaluatorMetadata1.getExpressionEvaluator().evaluate(new Object[] { 5l });
assertThat(result1, instanceOf(Long.class));
assertThat(((Long) result1), equalTo(125l));
ExpressionMetadata expressionEvaluatorMetadata2 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(4));
assertThat(expressionEvaluatorMetadata2.getIndexes().length, equalTo(0));
assertThat(expressionEvaluatorMetadata2.getUdfs().length, equalTo(0));
Object result2 = expressionEvaluatorMetadata2.getExpressionEvaluator().evaluate(new Object[] {});
assertThat(result2, instanceOf(Long.class));
assertThat(((Long) result2), equalTo(50L));
}
use of io.confluent.ksql.util.ExpressionMetadata in project ksql by confluentinc.
the class SelectValueMapperTest method createExpressionMetadata.
private List<ExpressionMetadata> createExpressionMetadata(final List<Pair<String, Expression>> expressionPairList, final Schema schema) throws Exception {
final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, new FunctionRegistry());
final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
for (Pair<String, Expression> expressionPair : expressionPairList) {
final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
expressionEvaluators.add(expressionEvaluator);
}
return expressionEvaluators;
}
use of io.confluent.ksql.util.ExpressionMetadata in project ksql by confluentinc.
the class CodeGenRunner method buildCodeGenFromParseTree.
public ExpressionMetadata buildCodeGenFromParseTree(final Expression expression) throws Exception {
Map<String, Class> parameterMap = getParameterInfo(expression);
String[] parameterNames = new String[parameterMap.size()];
Class[] parameterTypes = new Class[parameterMap.size()];
int[] columnIndexes = new int[parameterMap.size()];
Kudf[] kudfObjects = new Kudf[parameterMap.size()];
int index = 0;
for (Map.Entry<String, Class> entry : parameterMap.entrySet()) {
parameterNames[index] = entry.getKey();
parameterTypes[index] = entry.getValue();
columnIndexes[index] = SchemaUtil.getFieldIndexByName(schema, entry.getKey());
if (columnIndexes[index] < 0) {
kudfObjects[index] = (Kudf) entry.getValue().newInstance();
} else {
kudfObjects[index] = null;
}
index++;
}
String javaCode = new SqlToJavaVisitor(schema, functionRegistry).process(expression);
IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
// The expression will have two "int" parameters: "a" and "b".
ee.setParameters(parameterNames, parameterTypes);
// And the expression (i.e. "result") type is also "int".
ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
Schema expressionType = expressionTypeManager.getExpressionType(expression);
ee.setExpressionType(SchemaUtil.getJavaType(expressionType));
// And now we "cook" (scan, parse, compile and load) the fabulous expression.
ee.cook(javaCode);
return new ExpressionMetadata(ee, columnIndexes, kudfObjects, expressionType);
}
Aggregations