use of org.apache.spark.sql.execution.command.mutation.merge.MergeAction in project carbondata by apache.
the class CarbonAntlrSqlVisitor method visitMergeIntoCarbonTable.
public CarbonMergeIntoModel visitMergeIntoCarbonTable(CarbonSqlBaseParser.MergeIntoContext ctx) throws MalformedCarbonCommandException {
// handle the exception msg from base parser
if (ctx.exception != null) {
throw new MalformedCarbonCommandException("Parse failed!");
}
TableModel targetTable = visitMultipartIdentifier(ctx.target);
TableModel sourceTable = visitMultipartIdentifier(ctx.source);
// Once get these two table,
// We can try to get CarbonTable
// Build a matched clause list to store the when matched and when not matched clause
int size = ctx.getChildCount();
int currIdx = 0;
Expression joinExpression = null;
List<Expression> mergeExpressions = new ArrayList<>();
List<MergeAction> mergeActions = new ArrayList<>();
// when matched / when not matched context
while (currIdx < size) {
if (ctx.getChild(currIdx) instanceof CarbonSqlBaseParser.PredicatedContext) {
// This branch will visit the Join Expression
ctx.getChild(currIdx).getChildCount();
joinExpression = this.visitCarbonPredicated((CarbonSqlBaseParser.PredicatedContext) ctx.getChild(currIdx));
} else if (ctx.getChild(currIdx) instanceof CarbonSqlBaseParser.MatchedClauseContext) {
// This branch will deal with the Matched Clause
Expression whenMatchedExpression = null;
// Get the whenMatched expression
try {
if (this.containsWhenMatchedPredicateExpression(ctx.getChild(currIdx).getChildCount())) {
whenMatchedExpression = sparkParser.parseExpression(((CarbonSqlBaseParser.MatchedClauseContext) ctx.getChild(currIdx)).booleanExpression().getText());
}
} catch (ParseException e) {
throw new MalformedCarbonCommandException("Parse failed: " + e.getMessage());
}
mergeExpressions.add(whenMatchedExpression);
mergeActions.add(visitCarbonMatchedAction((CarbonSqlBaseParser.MatchedActionContext) ctx.getChild(currIdx).getChild(ctx.getChild(currIdx).getChildCount() - 1)));
} else if (ctx.getChild(currIdx) instanceof CarbonSqlBaseParser.NotMatchedClauseContext) {
// This branch will deal with the Matched Clause
Expression whenNotMatchedExpression = null;
// Get the whenMatched expression
try {
if (this.containsWhenNotMatchedPredicateExpression(ctx.getChild(currIdx).getChildCount())) {
whenNotMatchedExpression = sparkParser.parseExpression(((CarbonSqlBaseParser.NotMatchedClauseContext) ctx.getChild(currIdx)).booleanExpression().getText());
}
} catch (ParseException e) {
throw new MalformedCarbonCommandException("Parse failed: " + e.getMessage());
}
mergeExpressions.add(whenNotMatchedExpression);
CarbonSqlBaseParser.NotMatchedActionContext notMatchedActionContext = (CarbonSqlBaseParser.NotMatchedActionContext) ctx.getChild(currIdx).getChild(ctx.getChild(currIdx).getChildCount() - 1);
if (notMatchedActionContext.getChildCount() <= 2) {
mergeActions.add(InsertAction.apply(null, true));
} else if (notMatchedActionContext.ASTERISK() == null) {
if (notMatchedActionContext.columns.multipartIdentifier().size() != notMatchedActionContext.expression().size()) {
throw new MalformedCarbonCommandException("Parse failed: size of columns " + "is not equal to size of expression in not matched action.");
}
Map<Column, Column> insertMap = new HashMap<>();
for (int i = 0; i < notMatchedActionContext.columns.multipartIdentifier().size(); i++) {
String left = visitMultipartIdentifier(notMatchedActionContext.columns.multipartIdentifier().get(i), "").getColName();
String right = notMatchedActionContext.expression().get(i).getText();
// some times the right side is literal or expression, not table column
// so we need to check the left side is a column or expression
Column rightColumn = null;
try {
Expression expression = sparkParser.parseExpression(right);
rightColumn = new Column(expression);
} catch (Exception ex) {
throw new MalformedCarbonCommandException("Parse failed: " + ex.getMessage());
}
insertMap.put(new Column(left), rightColumn);
}
mergeActions.add(InsertAction.apply(SparkUtil.convertMap(insertMap), false));
} else {
mergeActions.add(InsertAction.apply(null, false));
}
}
currIdx++;
}
return new CarbonMergeIntoModel(targetTable, sourceTable, joinExpression, mergeExpressions, mergeActions);
}
Aggregations