@Override protected Void visitSingleColumn(SingleColumn node, Integer indent) { builder.append(formatExpression(node.getExpression(), parameters)); if (node.getAlias().isPresent()) { builder.append(' ') .append(formatExpression(node.getAlias().get(), parameters)); } return null; }
public static Select selectList(List<Expression> expressions) { ImmutableList.Builder<SelectItem> items = ImmutableList.builder(); for (Expression expression : expressions) { items.add(new SingleColumn(expression)); } return new Select(false, items.build()); }
@Override protected R visitSingleColumn(SingleColumn node, C context) { process(node.getExpression(), context); return null; }
ExpressionAnalysis expressionAnalysis = analyzeExpression(column.getExpression(), scope); analysis.recordSubqueries(node, expressionAnalysis); outputExpressionBuilder.add(column.getExpression()); Type type = expressionAnalysis.getType(column.getExpression()); if (node.getSelect().isDistinct() && !type.isComparable()) { throw new SemanticException(TYPE_MISMATCH, node.getSelect(), "DISTINCT can only be applied to comparable types (actual: %s): %s", type, column.getExpression());
private Multimap<QualifiedName, Expression> extractNamedOutputExpressions(Select node) { // Compute aliased output terms so we can resolve order by expressions against them first ImmutableMultimap.Builder<QualifiedName, Expression> assignments = ImmutableMultimap.builder(); for (SelectItem item : node.getSelectItems()) { if (item instanceof SingleColumn) { SingleColumn column = (SingleColumn) item; Optional<Identifier> alias = column.getAlias(); if (alias.isPresent()) { assignments.put(QualifiedName.of(alias.get().getValue()), column.getExpression()); // TODO: need to know if alias was quoted } else if (column.getExpression() instanceof Identifier) { assignments.put(QualifiedName.of(((Identifier) column.getExpression()).getValue()), column.getExpression()); } } } return assignments.build(); }
public static SelectItem unaliasedName(String name) { return new SingleColumn(identifier(name)); }
private Optional<Integer> findSelectIndex(List<Set<Expression>> items, List<SelectItem> selectItems) { if (items.size() == 1) { Set<Expression> item = items.get(0); if (item.size() == 1) { Expression next = item.iterator().next(); if (next instanceof LongLiteral) { return Optional.of(((int) ((LongLiteral) next).getValue())); } else if (next instanceof Identifier) { for (int i = 0; i < selectItems.size(); i++) { if (selectItems.get(i) instanceof SingleColumn) { if (((SingleColumn) selectItems.get(i)).getExpression().equals(next)) { return Optional.of(i + 1); } } } } return Optional.empty(); } } return Optional.empty(); }
SingleColumn column = (SingleColumn) item; Expression expression = column.getExpression(); Optional<Identifier> field = column.getAlias(); outputFields.add(Field.newUnqualified(field.map(Identifier::getValue), analysis.getType(expression), originTable, originColumn, column.getAlias().isPresent())); // TODO don't use analysis as a side-channel. Use outputExpressions to look up the type
public static SelectItem aliasedName(String name, String alias) { return new SingleColumn(identifier(name), identifier(alias)); }
@Override protected R visitSingleColumn(SingleColumn node, C context) { process(node.getExpression(), context); return null; }
@Override protected Void visitSingleColumn(SingleColumn node, Integer indent) { builder.append(formatExpression(node.getExpression(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)); if (node.getAlias().isPresent()) { builder.append(' ') .append(formatExpression(node.getAlias().get(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)); } return null; }
private String checksumSql(List<Column> columns, QualifiedName table) throws QueryRewriteException { if (columns.isEmpty()) { throw new QueryRewriteException("Table " + table + " has no columns"); } ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder(); for (Column column : columns) { Expression expression = new Identifier(column.getName()); if (column.isApproximateType()) { expression = new FunctionCall(QualifiedName.of("round"), ImmutableList.of(expression, new LongLiteral(Integer.toString(doublePrecision)))); } selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(expression)))); } Select select = new Select(false, selectItems.build()); return formatSql(new QuerySpecification(select, Optional.of(new Table(table)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty()); }
@Override protected R visitSingleColumn(SingleColumn node, C context) { process(node.getExpression(), context); return null; }
if (singleColumn.getAlias().isPresent()) { throw new RakamException("Alias in SELECT is not supported", BAD_REQUEST); if (singleColumn.getExpression() instanceof FunctionCall) { throw new RakamException("Function call in SELECT is not supported", BAD_REQUEST); String select = RakamSqlFormatter.formatExpression(singleColumn.getExpression(), qualifiedNameStringFunction, columnMappingFunction, ' '); if (!reportRequest.dimensions.stream().anyMatch(e -> e.name.equals(select))) { reportRequest.metrics.add(new GARequest.ReportRequest.Metric(select));
@Override public Node visitSelectSingle(SqlBaseParser.SelectSingleContext context) { return new SingleColumn( getLocation(context), (Expression) visit(context.expression()), visitIfPresent(context.identifier(), Identifier.class)); }
ExpressionAnalysis expressionAnalysis = analyzeExpression(column.getExpression(), tupleDescriptor, context); analysis.recordSubqueries(node, expressionAnalysis); outputExpressionBuilder.add(new FieldOrExpression(column.getExpression())); Type type = expressionAnalysis.getType(column.getExpression()); if (node.getSelect().isDistinct() && !type.isComparable()) { throw new SemanticException(TYPE_MISMATCH, node.getSelect(), "DISTINCT can only be applied to comparable types (actual: %s): %s", type, column.getExpression());
@Override protected Void visitSingleColumn(SingleColumn node, Integer indent) { builder.append(formatExpression(node.getExpression())); if (node.getAlias().isPresent()) { builder.append(' ') .append('"') .append(node.getAlias().get()) .append('"'); // TODO: handle quoting properly } return null; }