deniskuzZ commented on code in PR #4832: URL: https://github.com/apache/hive/pull/4832#discussion_r1393867643
########## ql/src/java/org/apache/hadoop/hive/ql/parse/rewrite/MergeRewriter.java: ########## @@ -0,0 +1,274 @@ +/* + * 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 org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.Warehouse; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.ql.Context; +import org.apache.hadoop.hive.ql.metadata.Hive; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.metadata.HiveUtils; +import org.apache.hadoop.hive.ql.metadata.Table; +import org.apache.hadoop.hive.ql.parse.ASTNode; +import org.apache.hadoop.hive.ql.parse.ParseUtils; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.parse.StorageFormat; +import org.apache.hadoop.hive.ql.parse.rewrite.sql.MultiInsertSqlGenerator; +import org.apache.hadoop.hive.ql.parse.rewrite.sql.SqlGeneratorFactory; +import org.apache.hadoop.hive.ql.session.SessionState; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.apache.commons.lang3.StringUtils.isNotBlank; + +public class MergeRewriter implements Rewriter<MergeStatement>, MergeStatement.DestClausePrefixSupplier { + + private final Hive db; + protected final HiveConf conf; + private final SqlGeneratorFactory sqlGeneratorFactory; + + public MergeRewriter(Hive db, HiveConf conf, SqlGeneratorFactory sqlGeneratorFactory) { + this.db = db; + this.conf = conf; + this.sqlGeneratorFactory = sqlGeneratorFactory; + } + + @Override + public ParseUtils.ReparseResult rewrite(Context ctx, MergeStatement mergeStatement) throws SemanticException { + + setOperation(ctx); + MultiInsertSqlGenerator sqlGenerator = sqlGeneratorFactory.createSqlGenerator(); + handleSource(mergeStatement.hasWhenNotMatchedInsertClause(), mergeStatement.getSourceAlias(), + mergeStatement.getOnClauseAsText(), sqlGenerator); + + MergeStatement.MergeSqlGenerator mergeSqlGenerator = createMergeSqlGenerator(mergeStatement, sqlGenerator); + for (MergeStatement.WhenClause whenClause : mergeStatement.getWhenClauses()) { + whenClause.toSql(mergeSqlGenerator); + } + + boolean validateCardinalityViolation = mergeStatement.shouldValidateCardinalityViolation(conf); + if (validateCardinalityViolation) { + handleCardinalityViolation(mergeStatement.getTargetAlias(), mergeStatement.getOnClauseAsText(), sqlGenerator); + } + + ParseUtils.ReparseResult rr = ParseUtils.parseRewrittenQuery(ctx, sqlGenerator.toString()); + Context rewrittenCtx = rr.rewrittenCtx; + ASTNode rewrittenTree = rr.rewrittenTree; + setOperation(rewrittenCtx); + + //set dest name mapping on new context; 1st child is TOK_FROM + int insClauseIdx = 1; + for (MergeStatement.WhenClause whenClause : mergeStatement.getWhenClauses()) { + List<Context.DestClausePrefix> prefixes = whenClause.getDestClausePrefix(this); + for (Context.DestClausePrefix prefix : prefixes) { + rewrittenCtx.addDestNamePrefix(insClauseIdx, prefix); + insClauseIdx++; + } + } + + if (validateCardinalityViolation) { + //here means the last branch of the multi-insert is Cardinality Validation + rewrittenCtx.addDestNamePrefix(rewrittenTree.getChildCount() - 1, Context.DestClausePrefix.INSERT); + } + + return rr; + } + + protected MergeWhenClauseSqlGenerator createMergeSqlGenerator( + MergeStatement mergeStatement, MultiInsertSqlGenerator sqlGenerator) { + return new MergeWhenClauseSqlGenerator(conf, sqlGenerator, mergeStatement); + } + + private void handleSource(boolean hasWhenNotMatchedClause, String sourceAlias, String onClauseAsText, + MultiInsertSqlGenerator sqlGenerator) { + sqlGenerator.append("FROM\n"); Review Comment: that won't work for COW: see https://github.com/apache/hive/pull/4852/files#diff-5612014abb89e13c290474798741828a8933edda57f9035f0656df47c4b63e7bR471 -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org