private void validateProjection(List<Expression> projections, List<Expression> groupByExpressions) { Set<Expression> groupBy = new HashSet<>(groupByExpressions); projections.stream() .map(expr -> { if (expr instanceof FieldExpression) { return (FieldExpression) expr; } else if (expr instanceof AsExpression) { Expression inner = ((AsExpression) expr).getExpression(); if (inner instanceof FieldExpression) { return (FieldExpression) inner; } } return null; }) .filter(Objects::nonNull) .forEach(e -> { if (!groupBy.contains(e)) { throw new IllegalArgumentException("field: " + e.getValue().getName() + " is not being grouped"); } }); }
@Override public Expression visit(SqlIdentifier id) { if (id.isStar()) { return STAR; } else if (id.isSimple()) { return new FieldExpression(getField(getSchema(), id.getSimple())); } else if (id.names.size() == 2) { return new FieldExpression(getField(getSchema(id.names.get(0)), id.names.get(1))); } else { throw new UnsupportedOperationException("Compound identifier with more than two levels"); } }
@Override public void visit(FieldExpression fieldExpression) { Schema.Field field = fieldExpression.getValue(); if (!getTable().isEmpty()) { builder.append(getTable()).append("."); } builder.append(getQuote()).append(field.getName()).append(getQuote()); fields.add(field); }