use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class QueryAnalyzer method processSelectExpressions.
private void processSelectExpressions(final Analysis analysis, final AggregateAnalysis aggregateAnalysis, final AggregateAnalyzer aggregateAnalyzer, final AggregateExpressionRewriter aggregateExpressionRewriter) {
for (Expression expression : analysis.getSelectExpressions()) {
aggregateAnalyzer.process(expression, new AnalysisContext());
if (!aggregateAnalyzer.isHasAggregateFunction()) {
aggregateAnalysis.addNonAggResultColumns(expression);
}
aggregateAnalysis.addFinalSelectExpression(ExpressionTreeRewriter.rewriteWith(aggregateExpressionRewriter, expression));
aggregateAnalyzer.setHasAggregateFunction(false);
}
}
use of io.confluent.ksql.parser.tree.Expression 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.parser.tree.Expression in project ksql by confluentinc.
the class AggregateNode method createAggregateValueToValueColumnMap.
private Map<Integer, Integer> createAggregateValueToValueColumnMap(final SchemaKStream aggregateArgExpanded, final SchemaBuilder aggregateSchema) {
Map<Integer, Integer> aggValToValColumnMap = new HashMap<>();
int nonAggColumnIndex = 0;
for (Expression expression : getRequiredColumnList()) {
String exprStr = expression.toString();
int index = SchemaUtil.getIndexInSchema(exprStr, aggregateArgExpanded.getSchema());
aggValToValColumnMap.put(nonAggColumnIndex, index);
nonAggColumnIndex++;
Field field = aggregateArgExpanded.getSchema().fields().get(index);
aggregateSchema.field(field.name(), field.schema());
}
return aggValToValColumnMap;
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class AggregateExpressionRewriter method rewriteFunctionCall.
@Override
public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
String functionName = node.getName().getSuffix();
if (functionRegistry.isAnAggregateFunction(functionName)) {
String aggVarName = AGGREGATE_FUNCTION_VARIABLE_PREFIX + aggVariableIndex;
aggVariableIndex++;
return new QualifiedNameReference(QualifiedName.of(aggVarName));
} else {
List<Expression> arguments = new ArrayList<>();
for (Expression argExpression : node.getArguments()) {
arguments.add(treeRewriter.rewrite(argExpression, context));
}
return new FunctionCall(node.getName(), arguments);
}
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class SchemaKStreamTest method testGroupByKey.
@Test
public void testGroupByKey() {
String selectQuery = "SELECT col0, col1 FROM test1 WHERE col0 > 100;";
PlanNode logicalPlan = planBuilder.buildLogicalPlan(selectQuery);
initialSchemaKStream = new SchemaKStream(logicalPlan.getTheSourceNode().getSchema(), kStream, ksqlStream.getKeyField(), new ArrayList<>(), SchemaKStream.Type.SOURCE, functionRegistry, new MockSchemaRegistryClient());
Expression keyExpression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL0");
KsqlTopicSerDe ksqlTopicSerDe = new KsqlJsonTopicSerDe();
Serde<GenericRow> rowSerde = ksqlTopicSerDe.getGenericRowSerde(initialSchemaKStream.getSchema(), null, false, null);
List<Expression> groupByExpressions = Arrays.asList(keyExpression);
SchemaKGroupedStream groupedSchemaKStream = initialSchemaKStream.groupBy(Serdes.String(), rowSerde, groupByExpressions);
Assert.assertEquals(groupedSchemaKStream.getKeyField().name(), "COL0");
}
Aggregations