use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class SchemaKStream method createSelectValueMapperAndSchema.
Pair<Schema, SelectValueMapper> createSelectValueMapperAndSchema(final List<Pair<String, Expression>> expressionPairList) {
try {
final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, functionRegistry);
final SchemaBuilder schemaBuilder = SchemaBuilder.struct();
final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
for (Pair<String, Expression> expressionPair : expressionPairList) {
final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
schemaBuilder.field(expressionPair.getLeft(), expressionEvaluator.getExpressionType());
expressionEvaluators.add(expressionEvaluator);
}
return new Pair<>(schemaBuilder.build(), new SelectValueMapper(genericRowValueTypeEnforcer, expressionPairList, expressionEvaluators));
} catch (Exception e) {
throw new KsqlException("Code generation failed for SelectValueMapper", e);
}
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class SchemaKStream method groupBy.
public SchemaKGroupedStream groupBy(final Serde<String> keySerde, final Serde<GenericRow> valSerde, final List<Expression> groupByExpressions) {
boolean rekey = rekeyRequired(groupByExpressions);
if (!rekey) {
KGroupedStream kgroupedStream = kstream.groupByKey(Serialized.with(keySerde, valSerde));
return new SchemaKGroupedStream(schema, kgroupedStream, keyField, Collections.singletonList(this), functionRegistry, schemaRegistryClient);
}
// Collect the column indexes, and build the new key as <column1>+<column2>+...
StringBuilder aggregateKeyName = new StringBuilder();
List<Integer> newKeyIndexes = new ArrayList<>();
boolean addSeparator = false;
for (Expression groupByExpr : groupByExpressions) {
if (addSeparator) {
aggregateKeyName.append("|+|");
} else {
addSeparator = true;
}
aggregateKeyName.append(groupByExpr.toString());
newKeyIndexes.add(SchemaUtil.getIndexInSchema(groupByExpr.toString(), getSchema()));
}
KGroupedStream kgroupedStream = kstream.filter((key, value) -> value != null).groupBy((key, value) -> {
StringBuilder newKey = new StringBuilder();
boolean addSeparator1 = false;
for (int index : newKeyIndexes) {
if (addSeparator1) {
newKey.append("|+|");
} else {
addSeparator1 = true;
}
newKey.append(String.valueOf(value.getColumns().get(index)));
}
return newKey.toString();
}, Serialized.with(keySerde, valSerde));
// TODO: if the key is a prefix of the grouping columns then we can
// use the repartition reflection hack to tell streams not to
// repartition.
Field newKeyField = new Field(aggregateKeyName.toString(), -1, Schema.STRING_SCHEMA);
return new SchemaKGroupedStream(schema, kgroupedStream, newKeyField, Collections.singletonList(this), functionRegistry, schemaRegistryClient);
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class CommandFactoriesTest method shouldFailCreateTableIfTimestampColumnNameIsIncorrect.
@Test
public void shouldFailCreateTableIfTimestampColumnNameIsIncorrect() {
HashMap<String, Expression> tableProperties = new HashMap<>();
tableProperties.putAll(properties);
tableProperties.put(DdlConfig.TIMESTAMP_NAME_PROPERTY, new StringLiteral("COL3"));
try {
final DdlCommand result = commandFactories.create(sqlExpression, new CreateTable(QualifiedName.of("foo"), Arrays.asList(new TableElement("COL1", "BIGINT"), new TableElement("COL2", "VARCHAR")), true, tableProperties), Collections.emptyMap());
} catch (KsqlException e) {
assertThat(e.getMessage(), equalTo("No column with the provided timestamp column name in the WITH clause, COL3, exists in the defined schema."));
}
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class DefaultTraversalVisitor method visitSampledRelation.
@Override
protected R visitSampledRelation(SampledRelation node, C context) {
process(node.getRelation(), context);
process(node.getSamplePercentage(), context);
if (node.getColumnsToStratifyOn().isPresent()) {
for (Expression expression : node.getColumnsToStratifyOn().get()) {
process(expression, context);
}
}
return null;
}
use of io.confluent.ksql.parser.tree.Expression 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