@Override public void visit(AsExpression asExpression) { aliases.add(asExpression.getAlias()); asExpression.getExpression().accept(this); }
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"); } }); }
private Expression visitSqlSpecialOperator(SqlSpecialOperator specialOperator, List<SqlNode> operands) { if (specialOperator.getName().equalsIgnoreCase("ITEM")) { Expression left = operands.get(0).accept(this); SqlNode right = operands.get(1); if (right instanceof SqlNumericLiteral) { SqlNumericLiteral index = (SqlNumericLiteral) right; if (!index.isInteger()) { throw new IllegalArgumentException("Invalid array index " + index); } return new ArrayFieldExpression(left, Integer.parseInt(index.toValue())); } else if (right instanceof SqlCharStringLiteral) { String key = ((SqlCharStringLiteral) right).toValue(); return new MapFieldExpression(left, key); } else { throw new IllegalArgumentException("Item right operand '" + right + "' must be numeric or character type"); } } else if (specialOperator.getName().equalsIgnoreCase("AS")) { Expression left = operands.get(0).accept(this); String alias = operands.get(1).toString(); return new AsExpression(left, alias); } else { throw new UnsupportedOperationException("Operator " + specialOperator + " not implemented"); } }