|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.expression; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList; |
| 14 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 15 | +import net.sf.jsqlparser.schema.Column; |
| 16 | +import net.sf.jsqlparser.statement.select.Select; |
| 17 | +import net.sf.jsqlparser.statement.select.SelectItem; |
| 18 | + |
| 19 | +/** |
| 20 | + * A ClickHouse transformer following a {@code COLUMNS(...)} matcher, for example the |
| 21 | + * {@code APPLY(x -> round(x, 2))} in {@code SELECT COLUMNS('^m') APPLY(x -> round(x, 2))}. |
| 22 | + * |
| 23 | + * ClickHouse parses its transformers in a loop, so they may repeat and combine in any order. |
| 24 | + */ |
| 25 | +public class ColumnsTransformer extends ASTNodeAccessImpl { |
| 26 | + |
| 27 | + public enum ColumnsTransformerType { |
| 28 | + APPLY, EXCEPT, REPLACE |
| 29 | + } |
| 30 | + |
| 31 | + private ColumnsTransformerType type; |
| 32 | + private Expression applyExpression; |
| 33 | + private ParenthesedExpressionList<Column> exceptColumns; |
| 34 | + private List<SelectItem<?>> replaceItems; |
| 35 | + |
| 36 | + public ColumnsTransformer(ColumnsTransformerType type) { |
| 37 | + this.type = type; |
| 38 | + } |
| 39 | + |
| 40 | + public ColumnsTransformerType getType() { |
| 41 | + return type; |
| 42 | + } |
| 43 | + |
| 44 | + public ColumnsTransformer setType(ColumnsTransformerType type) { |
| 45 | + this.type = type; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public Expression getApplyExpression() { |
| 50 | + return applyExpression; |
| 51 | + } |
| 52 | + |
| 53 | + public ColumnsTransformer setApplyExpression(Expression applyExpression) { |
| 54 | + this.applyExpression = applyExpression; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public ParenthesedExpressionList<Column> getExceptColumns() { |
| 59 | + return exceptColumns; |
| 60 | + } |
| 61 | + |
| 62 | + public ColumnsTransformer setExceptColumns(ParenthesedExpressionList<Column> exceptColumns) { |
| 63 | + this.exceptColumns = exceptColumns; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public List<SelectItem<?>> getReplaceItems() { |
| 68 | + return replaceItems; |
| 69 | + } |
| 70 | + |
| 71 | + public ColumnsTransformer setReplaceItems(List<SelectItem<?>> replaceItems) { |
| 72 | + this.replaceItems = replaceItems; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + void collectExpressions(List<Expression> expressions) { |
| 77 | + switch (type) { |
| 78 | + case APPLY: |
| 79 | + if (applyExpression != null) { |
| 80 | + expressions.add(applyExpression); |
| 81 | + } |
| 82 | + break; |
| 83 | + case EXCEPT: |
| 84 | + if (exceptColumns != null) { |
| 85 | + expressions.addAll(exceptColumns); |
| 86 | + } |
| 87 | + break; |
| 88 | + case REPLACE: |
| 89 | + if (replaceItems != null) { |
| 90 | + for (SelectItem<?> item : replaceItems) { |
| 91 | + expressions.add(item.getExpression()); |
| 92 | + } |
| 93 | + } |
| 94 | + break; |
| 95 | + default: |
| 96 | + throw new IllegalStateException("Unhandled ColumnsTransformerType: " + type); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public StringBuilder appendTo(StringBuilder builder) { |
| 101 | + switch (type) { |
| 102 | + case APPLY: |
| 103 | + builder.append("APPLY(").append(applyExpression).append(")"); |
| 104 | + break; |
| 105 | + case EXCEPT: |
| 106 | + builder.append("EXCEPT ").append(exceptColumns); |
| 107 | + break; |
| 108 | + case REPLACE: |
| 109 | + builder.append("REPLACE(").append(Select.getStringList(replaceItems)).append(")"); |
| 110 | + break; |
| 111 | + default: |
| 112 | + throw new IllegalStateException("Unhandled ColumnsTransformerType: " + type); |
| 113 | + } |
| 114 | + return builder; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String toString() { |
| 119 | + return appendTo(new StringBuilder()).toString(); |
| 120 | + } |
| 121 | +} |
0 commit comments