Search in sources :

Example 1 with IExpressionEvaluator

use of org.codehaus.commons.compiler.IExpressionEvaluator 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);
}
Also used : ExpressionTypeManager(io.confluent.ksql.util.ExpressionTypeManager) Schema(org.apache.kafka.connect.data.Schema) IExpressionEvaluator(org.codehaus.commons.compiler.IExpressionEvaluator) Kudf(io.confluent.ksql.function.udf.Kudf) ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Kudf (io.confluent.ksql.function.udf.Kudf)1 ExpressionMetadata (io.confluent.ksql.util.ExpressionMetadata)1 ExpressionTypeManager (io.confluent.ksql.util.ExpressionTypeManager)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Schema (org.apache.kafka.connect.data.Schema)1 IExpressionEvaluator (org.codehaus.commons.compiler.IExpressionEvaluator)1