Search in sources :

Example 1 with ExpressionTypeManager

use of io.confluent.ksql.util.ExpressionTypeManager 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)

Example 2 with ExpressionTypeManager

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

the class LogicalPlanner method buildProjectNode.

private ProjectNode buildProjectNode(final Schema inputSchema, final PlanNode sourcePlanNode) {
    SchemaBuilder projectionSchema = SchemaBuilder.struct();
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(inputSchema, functionRegistry);
    for (int i = 0; i < analysis.getSelectExpressions().size(); i++) {
        Expression expression = analysis.getSelectExpressions().get(i);
        String alias = analysis.getSelectExpressionAlias().get(i);
        Schema expressionType = expressionTypeManager.getExpressionType(expression);
        projectionSchema = projectionSchema.field(alias, expressionType);
    }
    return new ProjectNode(new PlanNodeId("Project"), sourcePlanNode, projectionSchema, analysis.getSelectExpressions());
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) ExpressionTypeManager(io.confluent.ksql.util.ExpressionTypeManager) Expression(io.confluent.ksql.parser.tree.Expression) Schema(org.apache.kafka.connect.data.Schema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) ProjectNode(io.confluent.ksql.planner.plan.ProjectNode)

Example 3 with ExpressionTypeManager

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

the class FunctionRegistry method getAggregateFunction.

public KsqlAggregateFunction getAggregateFunction(String functionName, List<Expression> functionArgs, Schema schema) {
    AggregateFunctionFactory aggregateFunctionFactory = aggregateFunctionMap.get(functionName);
    if (aggregateFunctionFactory == null) {
        throw new KsqlException("No aggregate function with name " + functionName + " exists!");
    }
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, this);
    Schema expressionType = expressionTypeManager.getExpressionType(functionArgs.get(0));
    return aggregateFunctionFactory.getProperAggregateFunction(Arrays.asList(expressionType));
}
Also used : ExpressionTypeManager(io.confluent.ksql.util.ExpressionTypeManager) TopKAggregateFunctionFactory(io.confluent.ksql.function.udaf.topk.TopKAggregateFunctionFactory) Schema(org.apache.kafka.connect.data.Schema) KsqlException(io.confluent.ksql.util.KsqlException)

Example 4 with ExpressionTypeManager

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

the class LogicalPlanner method buildAggregateNode.

private AggregateNode buildAggregateNode(final Schema inputSchema, final PlanNode sourcePlanNode) {
    SchemaBuilder aggregateSchema = SchemaBuilder.struct();
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(inputSchema, functionRegistry);
    for (int i = 0; i < analysis.getSelectExpressions().size(); i++) {
        Expression expression = analysis.getSelectExpressions().get(i);
        String alias = analysis.getSelectExpressionAlias().get(i);
        Schema expressionType = expressionTypeManager.getExpressionType(expression);
        aggregateSchema = aggregateSchema.field(alias, expressionType);
    }
    return new AggregateNode(new PlanNodeId("Aggregate"), sourcePlanNode, aggregateSchema, analysis.getGroupByExpressions(), analysis.getWindowExpression(), aggregateAnalysis.getAggregateFunctionArguments(), aggregateAnalysis.getFunctionList(), aggregateAnalysis.getRequiredColumnsList(), aggregateAnalysis.getFinalSelectExpressions(), aggregateAnalysis.getHavingExpression());
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) ExpressionTypeManager(io.confluent.ksql.util.ExpressionTypeManager) AggregateNode(io.confluent.ksql.planner.plan.AggregateNode) Expression(io.confluent.ksql.parser.tree.Expression) Schema(org.apache.kafka.connect.data.Schema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder)

Aggregations

ExpressionTypeManager (io.confluent.ksql.util.ExpressionTypeManager)4 Schema (org.apache.kafka.connect.data.Schema)4 Expression (io.confluent.ksql.parser.tree.Expression)2 PlanNodeId (io.confluent.ksql.planner.plan.PlanNodeId)2 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)2 TopKAggregateFunctionFactory (io.confluent.ksql.function.udaf.topk.TopKAggregateFunctionFactory)1 Kudf (io.confluent.ksql.function.udf.Kudf)1 AggregateNode (io.confluent.ksql.planner.plan.AggregateNode)1 ProjectNode (io.confluent.ksql.planner.plan.ProjectNode)1 ExpressionMetadata (io.confluent.ksql.util.ExpressionMetadata)1 KsqlException (io.confluent.ksql.util.KsqlException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IExpressionEvaluator (org.codehaus.commons.compiler.IExpressionEvaluator)1