use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.
the class FinalProjectNode method build.
private Pair<LogicalSchema, List<SelectExpression>> build(final MetaStore metaStore, final KsqlConfig ksqlConfig) {
final LogicalSchema parentSchema = getSource().getSchema();
final Optional<LogicalSchema> targetSchema = getTargetSchema(metaStore);
final List<SelectExpression> selectExpressions = SelectionUtil.buildSelectExpressions(getSource(), projection.selectItems(), targetSchema);
final LogicalSchema schema = SelectionUtil.buildProjectionSchema(parentSchema, selectExpressions, metaStore);
if (into.isPresent()) {
// Persistent queries have key columns as value columns - final projection can exclude them:
final Map<ColumnName, Set<ColumnName>> seenKeyColumns = new HashMap<>();
selectExpressions.removeIf(se -> {
if (se.getExpression() instanceof UnqualifiedColumnReferenceExp) {
final ColumnName columnName = ((UnqualifiedColumnReferenceExp) se.getExpression()).getColumnName();
// Window bounds columns are currently removed if not aliased:
if (SystemColumns.isWindowBound(columnName) && se.getAlias().equals(columnName)) {
return true;
}
if (parentSchema.isKeyColumn(columnName)) {
seenKeyColumns.computeIfAbsent(columnName, k -> new HashSet<>()).add(se.getAlias());
return true;
}
}
return false;
});
for (final Entry<ColumnName, Set<ColumnName>> seenKey : seenKeyColumns.entrySet()) {
if (seenKey.getValue().size() > 1) {
final String keys = GrammaticalJoiner.and().join(seenKey.getValue().stream().map(Name::text).sorted());
throw new KsqlException("The projection contains a key column (" + seenKey.getKey() + ") more than once, aliased as: " + keys + "." + System.lineSeparator() + "Each key column must only be in the projection once. " + "If you intended to copy the key into the value, then consider using the " + AsValue.NAME + " function to indicate which key reference should be copied.");
}
}
}
final LogicalSchema nodeSchema;
if (into.isPresent()) {
nodeSchema = schema.withoutPseudoAndKeyColsInValue(ksqlConfig);
} else {
// Transient queries return key columns in the value, so the projection includes them, and
// the schema needs to include them too:
final Builder builder = LogicalSchema.builder();
builder.keyColumns(parentSchema.key());
schema.columns().forEach(builder::valueColumn);
nodeSchema = builder.build();
}
return Pair.of(nodeSchema, selectExpressions);
}
use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.
the class SelectionUtil method resolveSelectItem.
private static Stream<SelectExpression> resolveSelectItem(final int idx, final List<? extends SelectItem> selectItems, final PlanNode parentNode, final Optional<LogicalSchema> targetSchema) {
final SelectItem selectItem = selectItems.get(idx);
if (selectItem instanceof SingleColumn) {
final SingleColumn column = (SingleColumn) selectItem;
// if the column we are trying to coerce into a target schema is beyond
// the target schema's max columns ignore it. this will generate a failure
// down the line when we check that the result schema is identical to
// the schema of the source we are attempting to fit
final Optional<Column> targetColumn = targetSchema.filter(schema -> schema.columns().size() > idx).map(schema -> schema.columns().get(idx));
return resolveSingleColumn(idx, parentNode, column, targetColumn);
}
if (selectItem instanceof AllColumns) {
return resolveAllColumns(parentNode, (AllColumns) selectItem);
}
throw new IllegalArgumentException("Unsupported SelectItem type: " + selectItem.getClass().getName());
}
use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.
the class SelectionUtil method resolveSingleColumn.
private static Stream<SelectExpression> resolveSingleColumn(final int idx, final PlanNode parentNode, final SingleColumn column, final Optional<Column> targetColumn) {
final Expression expression = parentNode.resolveSelect(idx, column.getExpression());
final ColumnName alias = column.getAlias().orElseThrow(() -> new IllegalStateException("Alias should be present by this point"));
return Stream.of(SelectExpression.of(alias, targetColumn.map(col -> ImplicitlyCastResolver.resolve(expression, col.type())).orElse(expression)));
}
use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.
the class ProjectOperator method createRowForSelectStar.
// Optimization for select star, to avoid having to do code generation
private List<?> createRowForSelectStar(final GenericRow intermediate) {
final List<Object> rowList = new ArrayList<>();
for (SelectExpression selectExpression : logicalNode.getSelectExpressions()) {
final Optional<Column> column = logicalNode.getIntermediateSchema().findValueColumn(selectExpression.getAlias());
if (!column.isPresent()) {
throw new IllegalStateException("Couldn't find alias in intermediate schema " + selectExpression.getAlias());
}
final int i = column.get().index();
rowList.add(intermediate.get(i));
}
return rowList;
}
use of io.confluent.ksql.execution.plan.SelectExpression in project ksql by confluentinc.
the class ExpressionParserTest method shouldParseSelectExpression.
@Test
public void shouldParseSelectExpression() {
// When:
final SelectExpression parsed = ExpressionParser.parseSelectExpression("1 + 2 AS `three`");
// Then:
assertThat(parsed, equalTo(SelectExpression.of(ColumnName.of("three"), new ArithmeticBinaryExpression(parsed.getExpression().getLocation(), Operator.ADD, ONE, TWO))));
}
Aggregations