deniskuzZ commented on code in PR #4852: URL: https://github.com/apache/hive/pull/4852#discussion_r1399067322
########## ql/src/java/org/apache/hadoop/hive/ql/parse/rewrite/CopyOnWriteMergeRewriter.java: ########## @@ -0,0 +1,238 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hive.ql.parse.rewrite; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.ql.Context; +import org.apache.hadoop.hive.ql.metadata.Hive; +import org.apache.hadoop.hive.ql.metadata.HiveUtils; +import org.apache.hadoop.hive.ql.metadata.Table; +import org.apache.hadoop.hive.ql.metadata.VirtualColumn; +import org.apache.hadoop.hive.ql.parse.ParseUtils; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.parse.rewrite.sql.COWWithClauseBuilder; +import org.apache.hadoop.hive.ql.parse.rewrite.sql.MultiInsertSqlGenerator; +import org.apache.hadoop.hive.ql.parse.rewrite.sql.SqlGeneratorFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.UnaryOperator; + +import static org.apache.commons.lang3.StringUtils.isNotBlank; +import static org.apache.hadoop.hive.ql.parse.rewrite.sql.SqlGeneratorFactory.TARGET_PREFIX; + +public class CopyOnWriteMergeRewriter extends MergeRewriter { + + public CopyOnWriteMergeRewriter(Hive db, HiveConf conf, SqlGeneratorFactory sqlGeneratorFactory) { + super(db, conf, sqlGeneratorFactory); + } + + @Override + public ParseUtils.ReparseResult rewrite(Context ctx, MergeStatement mergeStatement) throws SemanticException { + + setOperation(ctx); + MultiInsertSqlGenerator sqlGenerator = sqlGeneratorFactory.createSqlGenerator(); + handleSource(mergeStatement, sqlGenerator); + + sqlGenerator.append('\n'); + sqlGenerator.append("INSERT INTO ").appendTargetTableName(); + sqlGenerator.append('\n'); + + List<MergeStatement.WhenClause> whenClauses = Lists.newArrayList(mergeStatement.getWhenClauses()); + + Optional<String> extraPredicate = whenClauses.stream() + .filter(whenClause -> !(whenClause instanceof MergeStatement.InsertClause)) + .map(MergeStatement.WhenClause::getExtraPredicate) + .map(Strings::nullToEmpty) + .reduce((p1, p2) -> isNotBlank(p2) ? p1 + " OR " + p2 : p2); + + whenClauses.removeIf(whenClause -> whenClause instanceof MergeStatement.DeleteClause); + extraPredicate.ifPresent(p -> whenClauses.add(new MergeStatement.DeleteClause(p, null))); + + MergeStatement.MergeSqlGenerator mergeSqlGenerator = createMergeSqlGenerator(mergeStatement, sqlGenerator); + + for (MergeStatement.WhenClause whenClause : whenClauses) { + whenClause.toSql(mergeSqlGenerator); + } + + // TODO: handleCardinalityViolation; + + ParseUtils.ReparseResult rr = ParseUtils.parseRewrittenQuery(ctx, sqlGenerator.toString()); + Context rewrittenCtx = rr.rewrittenCtx; + setOperation(rewrittenCtx); + + //set dest name mapping on new context; 1st child is TOK_FROM + rewrittenCtx.addDestNamePrefix(1, Context.DestClausePrefix.MERGE); + return rr; + } + + @Override + protected CopyOnWriteMergeWhenClauseSqlGenerator createMergeSqlGenerator( + MergeStatement mergeStatement, MultiInsertSqlGenerator sqlGenerator) { + return new CopyOnWriteMergeWhenClauseSqlGenerator(conf, sqlGenerator, mergeStatement); + } + + private void handleSource(MergeStatement mergeStatement, MultiInsertSqlGenerator sqlGenerator) { + boolean hasWhenNotMatchedInsertClause = mergeStatement.hasWhenNotMatchedInsertClause(); + + String sourceName = mergeStatement.getSourceName(); + String sourceAlias = mergeStatement.getSourceAlias(); + + String targetAlias = mergeStatement.getTargetAlias(); + String onClauseAsString = replaceColumnRefsWithTargetPrefix(targetAlias, mergeStatement.getOnClauseAsText()); + + sqlGenerator.newCteExpr(); + + sqlGenerator.append(sourceName + " AS ( SELECT * FROM\n"); + sqlGenerator.append("(SELECT "); + sqlGenerator.appendAcidSelectColumns(Context.Operation.MERGE); + sqlGenerator.appendAllColsOfTargetTable(TARGET_PREFIX); + sqlGenerator.append(" FROM ").appendTargetTableName().append(") "); + sqlGenerator.append(targetAlias); + sqlGenerator.append('\n'); + sqlGenerator.indent().append(hasWhenNotMatchedInsertClause ? "FULL OUTER JOIN" : "LEFT OUTER JOIN").append("\n"); + sqlGenerator.indent().append(sourceAlias); + sqlGenerator.append('\n'); + sqlGenerator.indent().append("ON ").append(onClauseAsString); + sqlGenerator.append('\n'); + sqlGenerator.append(")"); + + sqlGenerator.addCteExpr(); + } + + private static String replaceColumnRefsWithTargetPrefix(String columnRef, String strValue) { + return strValue.replaceAll(columnRef + "\\.(`?)", "$1" + TARGET_PREFIX); + } Review Comment: replaces alias ref in column with prefix: ```` - WHERE `t`.`a` = `src`.`a` AND NOT(`t`.`a` > 100) + WHERE `t__a` = `src`.`a` AND NOT(`t__a` > 100) ```` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
