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