@Override public final String toString() { return ExpressionFormatter.formatExpression(this, Optional.empty()); // This will not replace parameters, but we don't have access to them here } }
append(indent, "WHERE " + formatExpression(node.getWhere().get(), parameters)) .append('\n'); append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n'); append(indent, "HAVING " + formatExpression(node.getHaving().get(), parameters)) .append('\n');
static String formatGroupBy(List<GroupingElement> groupingElements, Optional<List<Expression>> parameters) { ImmutableList.Builder<String> resultStrings = ImmutableList.builder(); for (GroupingElement groupingElement : groupingElements) { String result = ""; if (groupingElement instanceof SimpleGroupBy) { List<Expression> columns = ((SimpleGroupBy) groupingElement).getExpressions(); if (columns.size() == 1) { result = formatExpression(getOnlyElement(columns), parameters); } else { result = formatGroupingSet(columns, parameters); } } else if (groupingElement instanceof GroupingSets) { result = format("GROUPING SETS (%s)", Joiner.on(", ").join( ((GroupingSets) groupingElement).getSets().stream() .map(e -> formatGroupingSet(e, parameters)) .iterator())); } else if (groupingElement instanceof Cube) { result = format("CUBE %s", formatGroupingSet(((Cube) groupingElement).getExpressions(), parameters)); } else if (groupingElement instanceof Rollup) { result = format("ROLLUP %s", formatGroupingSet(((Rollup) groupingElement).getExpressions(), parameters)); } resultStrings.add(result); } return Joiner.on(", ").join(resultStrings.build()); }
private String formatColumnDefinition(ColumnDefinition column) { return formatExpression(column.getName(), parameters) + " " + column.getType() + column.getComment() .map(comment -> " COMMENT " + formatStringLiteral(comment)) .orElse("") + formatPropertiesSingleLine(column.getProperties()); }
append(indent, "WHERE " + formatExpression(node.getWhere().get())) .append('\n'); append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n'); append(indent, "HAVING " + formatExpression(node.getHaving().get())) .append('\n'); append(indent, "ORDER BY " + formatSortItems(node.getOrderBy())) .append('\n');
@Override protected Void visitShowPartitions(ShowPartitions node, Integer context) { builder.append("SHOW PARTITIONS FROM ") .append(node.getTable()); if (node.getWhere().isPresent()) { builder.append(" WHERE ") .append(formatExpression(node.getWhere().get())); } if (!node.getOrderBy().isEmpty()) { builder.append(" ORDER BY ") .append(formatSortItems(node.getOrderBy())); } if (node.getLimit().isPresent()) { builder.append(" LIMIT ") .append(node.getLimit().get()); } return null; }
@Override protected String visitStringLiteral(StringLiteral node, Void context) { return formatStringLiteral(node.getValue()); }
static String formatGroupBy(List<GroupingElement> groupingElements) { return formatGroupBy(groupingElements, Optional.empty()); }
static String formatOrderBy(OrderBy orderBy, Optional<List<Expression>> parameters) { return "ORDER BY " + formatSortItems(orderBy.getSortItems(), parameters); }
static String formatSortItems(List<SortItem> sortItems, Optional<List<Expression>> parameters) { return Joiner.on(", ").join(sortItems.stream() .map(sortItemFormatterFunction(parameters)) .iterator()); }
@Override protected Void visitCreateTableAsSelect(CreateTableAsSelect node, Integer indent) { builder.append("CREATE TABLE "); if (node.isNotExists()) { builder.append("IF NOT EXISTS "); } builder.append(formatName(node.getName())); if (node.getColumnAliases().isPresent()) { String columnList = node.getColumnAliases().get().stream().map(element -> formatExpression(element, parameters)).collect(joining(", ")); builder.append(format("( %s )", columnList)); } if (node.getComment().isPresent()) { builder.append("\nCOMMENT " + formatStringLiteral(node.getComment().get())); } builder.append(formatPropertiesMultiLine(node.getProperties())); builder.append(" AS "); process(node.getQuery(), indent); if (!node.isWithData()) { builder.append(" WITH NO DATA"); } return null; }
@Override protected String visitCharLiteral(CharLiteral node, Void context) { return "CHAR " + formatStringLiteral(node.getValue()); }
@Override protected Void visitQuerySpecification(QuerySpecification node, Integer indent) { process(node.getSelect(), indent); if (node.getFrom().isPresent()) { append(indent, "FROM"); builder.append('\n'); append(indent, " "); process(node.getFrom().get(), indent); } builder.append('\n'); if (node.getWhere().isPresent()) { append(indent, "WHERE " + formatExpression(node.getWhere().get(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)) .append('\n'); } if (node.getGroupBy().isPresent()) { append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n'); } if (node.getHaving().isPresent()) { append(indent, "HAVING " + formatExpression(node.getHaving().get(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)) .append('\n'); } if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), indent); } if (node.getLimit().isPresent()) { append(indent, "LIMIT " + node.getLimit().get()) .append('\n'); } return null; }
static String formatSortItems(List<SortItem> sortItems) { return formatSortItems(sortItems, true); }
static String formatSortItems(List<SortItem> sortItems, Optional<List<Expression>> parameters) { return Joiner.on(", ").join(sortItems.stream() .map(sortItemFormatterFunction(parameters)) .iterator()); }
@Override protected Void visitExpression(Expression node, Integer indent) { checkArgument(indent == 0, "visitExpression should only be called at root"); builder.append(formatExpression(node, parameters)); return null; }
private String formatColumnDefinition(ColumnDefinition column) { return formatExpression(column.getName(), parameters) + " " + column.getType() + column.getComment() .map(comment -> " COMMENT " + formatStringLiteral(comment)) .orElse("") + formatPropertiesSingleLine(column.getProperties()); }
static String formatGroupBy(List<GroupingElement> groupingElements, Optional<List<Expression>> parameters) { ImmutableList.Builder<String> resultStrings = ImmutableList.builder(); for (GroupingElement groupingElement : groupingElements) { String result = ""; if (groupingElement instanceof SimpleGroupBy) { List<Expression> columns = ((SimpleGroupBy) groupingElement).getExpressions(); if (columns.size() == 1) { result = formatExpression(getOnlyElement(columns), parameters); } else { result = formatGroupingSet(columns, parameters); } } else if (groupingElement instanceof GroupingSets) { result = format("GROUPING SETS (%s)", Joiner.on(", ").join( ((GroupingSets) groupingElement).getSets().stream() .map(e -> formatGroupingSet(e, parameters)) .iterator())); } else if (groupingElement instanceof Cube) { result = format("CUBE %s", formatGroupingSet(((Cube) groupingElement).getExpressions(), parameters)); } else if (groupingElement instanceof Rollup) { result = format("ROLLUP %s", formatGroupingSet(((Rollup) groupingElement).getExpressions(), parameters)); } resultStrings.add(result); } return Joiner.on(", ").join(resultStrings.build()); }
append(indent, "WHERE " + formatExpression(node.getWhere().get(), parameters)) .append('\n'); append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n'); append(indent, "HAVING " + formatExpression(node.getHaving().get(), parameters)) .append('\n');
@Override protected Void visitShowCatalogs(ShowCatalogs node, Integer context) { builder.append("SHOW CATALOGS"); node.getLikePattern().ifPresent((value) -> builder.append(" LIKE ") .append(formatStringLiteral(value))); return null; }