|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.facebook.coresql.rewriter; |
| 16 | + |
| 17 | +import com.facebook.coresql.parser.AstNode; |
| 18 | +import com.facebook.coresql.parser.FunctionCall; |
| 19 | +import com.facebook.coresql.parser.SqlParserDefaultVisitor; |
| 20 | +import com.facebook.coresql.parser.Unparser; |
| 21 | +import com.google.common.collect.ArrayListMultimap; |
| 22 | +import com.google.common.collect.ImmutableListMultimap; |
| 23 | +import com.google.common.collect.Multimap; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Formatter; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static com.facebook.coresql.parser.ParserHelper.parseStatement; |
| 32 | +import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTARGUMENTLIST; |
| 33 | +import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTIDENTIFIER; |
| 34 | +import static com.facebook.coresql.parser.SqlParserTreeConstants.JJTUNSIGNEDNUMERICLITERAL; |
| 35 | +import static java.util.Collections.binarySearch; |
| 36 | +import static java.util.Collections.sort; |
| 37 | +import static java.util.Objects.requireNonNull; |
| 38 | + |
| 39 | +public class ApproxPercentileRewriter |
| 40 | + extends Rewriter |
| 41 | +{ |
| 42 | + private final PatternMatcher<Multimap<String, AstNode>> matcher; |
| 43 | + private static final String REPLACEMENT = " APPROX_PERCENTILE(%s, ARRAY%s)[%d]"; |
| 44 | + private Multimap<String, AstNode> firstArgMap; // A map of String to the APPROX_PERCENTILE nodes with that String as its first argument |
| 45 | + private Map<String, ArrayList<Double>> percentiles; |
| 46 | + private static final String REWRITE_NAME = "Multiple APPROX PERCENTILE with same first arg and literal second arg"; |
| 47 | + |
| 48 | + public ApproxPercentileRewriter() |
| 49 | + { |
| 50 | + this.matcher = new ApproxPercentilePatternMatcher(); |
| 51 | + this.firstArgMap = ArrayListMultimap.create(); |
| 52 | + this.percentiles = new HashMap<>(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean rewritePatternIsPresent(String sql) |
| 57 | + { |
| 58 | + AstNode root = requireNonNull(parseStatement(sql)); |
| 59 | + firstArgMap = matcher.matchPattern(root); |
| 60 | + return firstArgMap.keySet().stream().anyMatch(key -> firstArgMap.get(key).size() >= 2); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public RewriteResult rewrite(String sql) |
| 65 | + { |
| 66 | + AstNode root = requireNonNull(parseStatement(sql)); |
| 67 | + this.firstArgMap = matcher.matchPattern(root); |
| 68 | + getPercentilesFromFirstArgMap(); |
| 69 | + String rewrittenSql = Unparser.unparse(root, this); |
| 70 | + return new RewriteResult(REWRITE_NAME, sql, rewrittenSql); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void visit(FunctionCall node, Void data) |
| 75 | + { |
| 76 | + if (canRewrite(node)) { |
| 77 | + applyRewrite(node); |
| 78 | + } |
| 79 | + else { |
| 80 | + defaultVisit(node, data); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Generates a rewritten version of the current subtree. |
| 86 | + * |
| 87 | + * @param node The function call node we're rewriting |
| 88 | + */ |
| 89 | + private void applyRewrite(AstNode node) |
| 90 | + { |
| 91 | + // First, unparse up to the node. This ensures we don't miss any special tokens |
| 92 | + unparseUpto((AstNode) node.jjtGetChild(0)); |
| 93 | + // Then, add the rewritten version to the Unparser |
| 94 | + String firstArg = getFirstArgAsString(node); |
| 95 | + Double secondArg = getSecondArgAsDouble(node); |
| 96 | + |
| 97 | + Formatter formatter = new Formatter(stringBuilder); |
| 98 | + formatter.format(REPLACEMENT, firstArg, percentiles.get(firstArg), binarySearch(percentiles.get(firstArg), secondArg) + 1); |
| 99 | + // Move to end of this node -- we've already put in a rewritten version of it, so we don't need to unparse it |
| 100 | + moveToEndOfNode(node); |
| 101 | + } |
| 102 | + |
| 103 | + private String getFirstArgAsString(AstNode approxPercentile) |
| 104 | + { |
| 105 | + AstNode args = approxPercentile.GetFirstChildOfKind(JJTARGUMENTLIST); |
| 106 | + AstNode firstArg = (AstNode) args.jjtGetChild(0); |
| 107 | + return Unparser.unparse(firstArg); |
| 108 | + } |
| 109 | + |
| 110 | + private Double getSecondArgAsDouble(AstNode approxPercentile) |
| 111 | + { |
| 112 | + AstNode args = approxPercentile.GetFirstChildOfKind(JJTARGUMENTLIST); |
| 113 | + AstNode secondArg = (AstNode) args.jjtGetChild(1); |
| 114 | + return Double.parseDouble(Unparser.unparse(secondArg)); |
| 115 | + } |
| 116 | + |
| 117 | + private boolean canRewrite(AstNode node) |
| 118 | + { |
| 119 | + String firstArg = getFirstArgAsString(node); |
| 120 | + return firstArgMap.containsValue(node) && firstArgMap.get(firstArg).size() >= 2; |
| 121 | + } |
| 122 | + |
| 123 | + private void getPercentilesFromFirstArgMap() |
| 124 | + { |
| 125 | + // Map each first argument to a list of the percentiles of the APPROX_PERCENTILE nodes that have that first argument |
| 126 | + for (Map.Entry<String, AstNode> entry : firstArgMap.entries()) { |
| 127 | + String firstArg = entry.getKey(); |
| 128 | + AstNode approxPercentileNode = entry.getValue(); |
| 129 | + percentiles.putIfAbsent(firstArg, new ArrayList<>()); |
| 130 | + List<Double> percentilesWithThisFirstArg = percentiles.get(firstArg); |
| 131 | + percentilesWithThisFirstArg.add(getSecondArgAsDouble(approxPercentileNode)); |
| 132 | + } |
| 133 | + // Sort each percentile list. This will allow binary sort downstream |
| 134 | + for (String key : percentiles.keySet()) { |
| 135 | + sort(percentiles.get(key)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static class ApproxPercentilePatternMatcher |
| 140 | + extends SqlParserDefaultVisitor |
| 141 | + implements PatternMatcher<Multimap<String, AstNode>> |
| 142 | + { |
| 143 | + private Multimap<String, AstNode> firstArgMap; // A map of String to the APPROX_PERCENTILE nodes with that String as its first argument |
| 144 | + |
| 145 | + public ApproxPercentilePatternMatcher() |
| 146 | + { } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Multimap<String, AstNode> matchPattern(AstNode root) |
| 150 | + { |
| 151 | + this.firstArgMap = ArrayListMultimap.create(); |
| 152 | + requireNonNull(root, "AST passed to pattern matcher was null"); |
| 153 | + root.jjtAccept(this, null); |
| 154 | + return ImmutableListMultimap.copyOf(firstArgMap); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void visit(FunctionCall node, Void data) |
| 159 | + { |
| 160 | + if (isApproxPercentile(node)) { |
| 161 | + AstNode argList = node.GetFirstChildOfKind(JJTARGUMENTLIST); |
| 162 | + AstNode secondArg = (AstNode) argList.jjtGetChild(1); |
| 163 | + if (!isUnsignedLiteral(secondArg)) { |
| 164 | + return; |
| 165 | + } |
| 166 | + AstNode firstArg = (AstNode) argList.jjtGetChild(0); |
| 167 | + String firstArgAsString = Unparser.unparse(firstArg); |
| 168 | + firstArgMap.put(firstArgAsString, node); |
| 169 | + } |
| 170 | + defaultVisit(node, data); |
| 171 | + } |
| 172 | + |
| 173 | + public static boolean isUnsignedLiteral(AstNode node) |
| 174 | + { |
| 175 | + return node.getId() == JJTUNSIGNEDNUMERICLITERAL; |
| 176 | + } |
| 177 | + |
| 178 | + private static boolean isApproxPercentile(AstNode node) |
| 179 | + { |
| 180 | + AstNode identifier = node.GetFirstChildOfKind(JJTIDENTIFIER); |
| 181 | + if (identifier == null) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + String image = identifier.GetImage(); |
| 185 | + return image != null && image.equalsIgnoreCase("APPROX_PERCENTILE"); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments