|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.mr.hive; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc; |
| 24 | +import org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc; |
| 25 | +import org.apache.hadoop.hive.ql.plan.ExprNodeDesc; |
| 26 | +import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc; |
| 27 | +import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; |
| 28 | +import org.apache.hadoop.hive.ql.udf.generic.GenericUDFTryVariantGet; |
| 29 | +import org.apache.hadoop.hive.ql.udf.generic.GenericUDFVariantGet; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +final class VariantFilterRewriter { |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(VariantFilterRewriter.class); |
| 35 | + |
| 36 | + private VariantFilterRewriter() { |
| 37 | + } |
| 38 | + |
| 39 | + static RewriteResult rewriteForShredding(ExprNodeGenericFuncDesc predicate) { |
| 40 | + if (predicate == null) { |
| 41 | + return RewriteResult.none(); |
| 42 | + } |
| 43 | + |
| 44 | + ExprNodeGenericFuncDesc cloned = (ExprNodeGenericFuncDesc) predicate.clone(); |
| 45 | + ExprNodeDesc rewrittenRoot = rewriteNode(cloned); |
| 46 | + if (rewrittenRoot instanceof ExprNodeGenericFuncDesc) { |
| 47 | + return RewriteResult.of((ExprNodeGenericFuncDesc) rewrittenRoot); |
| 48 | + } |
| 49 | + |
| 50 | + // If rewrites ended up replacing the root, fall back to the original predicate clone to avoid |
| 51 | + // changing the expected root type. |
| 52 | + return RewriteResult.of(cloned); |
| 53 | + } |
| 54 | + |
| 55 | + private static ExprNodeDesc rewriteNode(ExprNodeDesc node) { |
| 56 | + if (node == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + if (node instanceof ExprNodeGenericFuncDesc) { |
| 61 | + ExprNodeGenericFuncDesc funcDesc = (ExprNodeGenericFuncDesc) node; |
| 62 | + List<ExprNodeDesc> children = funcDesc.getChildren(); |
| 63 | + if (children != null) { |
| 64 | + for (int i = 0; i < children.size(); i++) { |
| 65 | + ExprNodeDesc rewrittenChild = rewriteNode(children.get(i)); |
| 66 | + children.set(i, rewrittenChild); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (isVariantGet(funcDesc)) { |
| 71 | + ExprNodeDesc replacement = rewriteVariantFunction(funcDesc); |
| 72 | + if (replacement != null) { |
| 73 | + return replacement; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return node; |
| 79 | + } |
| 80 | + |
| 81 | + private static boolean isVariantGet(ExprNodeGenericFuncDesc funcDesc) { |
| 82 | + GenericUDF udf = funcDesc.getGenericUDF(); |
| 83 | + return udf instanceof GenericUDFVariantGet || udf instanceof GenericUDFTryVariantGet; |
| 84 | + } |
| 85 | + |
| 86 | + private static ExprNodeDesc rewriteVariantFunction(ExprNodeGenericFuncDesc funcDesc) { |
| 87 | + List<ExprNodeDesc> args = funcDesc.getChildren(); |
| 88 | + if (args == null || args.size() < 2) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + ExprNodeDesc variantColumn = args.get(0); |
| 93 | + ExprNodeDesc jsonPathDesc = args.get(1); |
| 94 | + |
| 95 | + if (!(variantColumn instanceof ExprNodeColumnDesc) || |
| 96 | + !(jsonPathDesc instanceof ExprNodeConstantDesc)) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + Object literal = ((ExprNodeConstantDesc) jsonPathDesc).getValue(); |
| 101 | + if (!(literal instanceof String)) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + String fieldName = extractTopLevelField((String) literal); |
| 106 | + if (fieldName == null) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + ExprNodeColumnDesc variantColumnDesc = (ExprNodeColumnDesc) variantColumn; |
| 111 | + String shreddedColumn = variantColumnDesc.getColumn() + ".typed_value." + fieldName; |
| 112 | + LOG.debug("Rewriting variant predicate to use shredded column {}", shreddedColumn); |
| 113 | + return new ExprNodeColumnDesc( |
| 114 | + funcDesc.getTypeInfo(), |
| 115 | + shreddedColumn, |
| 116 | + variantColumnDesc.getTabAlias(), |
| 117 | + variantColumnDesc.getIsPartitionColOrVirtualCol()); |
| 118 | + } |
| 119 | + |
| 120 | + private static String extractTopLevelField(String jsonPath) { |
| 121 | + if (jsonPath == null) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + String trimmed = jsonPath.trim(); |
| 126 | + if (!trimmed.startsWith("$.") || trimmed.length() <= 2) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + String remaining = trimmed.substring(2); |
| 131 | + // Only allow top-level field names (no nested objects or arrays) |
| 132 | + if (remaining.contains(".") || remaining.contains("[") || remaining.contains("]")) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + return remaining; |
| 137 | + } |
| 138 | + |
| 139 | + private static boolean containsShreddedReference(ExprNodeDesc node) { |
| 140 | + if (node == null) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + if (node instanceof ExprNodeColumnDesc) { |
| 145 | + String column = ((ExprNodeColumnDesc) node).getColumn(); |
| 146 | + return column != null && column.contains(".typed_value."); |
| 147 | + } |
| 148 | + |
| 149 | + List<ExprNodeDesc> children = node.getChildren(); |
| 150 | + if (children != null) { |
| 151 | + for (ExprNodeDesc child : children) { |
| 152 | + if (containsShreddedReference(child)) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + static final class RewriteResult { |
| 161 | + private final ExprNodeGenericFuncDesc expression; |
| 162 | + private final boolean rewritten; |
| 163 | + |
| 164 | + private RewriteResult(ExprNodeGenericFuncDesc expression, boolean rewritten) { |
| 165 | + this.expression = expression; |
| 166 | + this.rewritten = rewritten; |
| 167 | + } |
| 168 | + |
| 169 | + private static final RewriteResult NONE = new RewriteResult(null, false); |
| 170 | + |
| 171 | + static RewriteResult none() { |
| 172 | + return NONE; |
| 173 | + } |
| 174 | + |
| 175 | + static RewriteResult of(ExprNodeGenericFuncDesc expression) { |
| 176 | + if (expression == null) { |
| 177 | + return none(); |
| 178 | + } |
| 179 | + return new RewriteResult(expression, containsShreddedReference(expression)); |
| 180 | + } |
| 181 | + |
| 182 | + ExprNodeGenericFuncDesc expression() { |
| 183 | + return expression; |
| 184 | + } |
| 185 | + |
| 186 | + boolean hasVariantRewrite() { |
| 187 | + return rewritten; |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments