Search in sources :

Example 1 with TableModel

use of org.apache.spark.sql.merge.model.TableModel 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);
}
Also used : CarbonSqlBaseParser(org.apache.spark.sql.parser.CarbonSqlBaseParser) ArrayList(java.util.ArrayList) ParseException(org.apache.spark.sql.catalyst.parser.ParseException) MalformedCarbonCommandException(org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException) MergeAction(org.apache.spark.sql.execution.command.mutation.merge.MergeAction) Expression(org.apache.spark.sql.catalyst.expressions.Expression) CarbonJoinExpression(org.apache.spark.sql.merge.model.CarbonJoinExpression) MalformedCarbonCommandException(org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException) ParseException(org.apache.spark.sql.catalyst.parser.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) TableModel(org.apache.spark.sql.merge.model.TableModel) CarbonMergeIntoModel(org.apache.spark.sql.merge.model.CarbonMergeIntoModel)

Example 2 with TableModel

use of org.apache.spark.sql.merge.model.TableModel in project carbondata by apache.

the class CarbonAntlrSqlVisitor method visitMultipartIdentifier.

public TableModel visitMultipartIdentifier(CarbonSqlBaseParser.MultipartIdentifierContext ctx) {
    TableModel table = new TableModel();
    List<CarbonSqlBaseParser.ErrorCapturingIdentifierContext> parts = ctx.parts;
    if (parts.size() == 2) {
        table.setDatabase(parts.get(0).getText());
        table.setTable(parts.get(1).getText());
    }
    if (parts.size() == 1) {
        table.setTable(parts.get(0).getText());
    }
    return table;
}
Also used : TableModel(org.apache.spark.sql.merge.model.TableModel)

Aggregations

TableModel (org.apache.spark.sql.merge.model.TableModel)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MalformedCarbonCommandException (org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException)1 Expression (org.apache.spark.sql.catalyst.expressions.Expression)1 ParseException (org.apache.spark.sql.catalyst.parser.ParseException)1 MergeAction (org.apache.spark.sql.execution.command.mutation.merge.MergeAction)1 CarbonJoinExpression (org.apache.spark.sql.merge.model.CarbonJoinExpression)1 CarbonMergeIntoModel (org.apache.spark.sql.merge.model.CarbonMergeIntoModel)1 CarbonSqlBaseParser (org.apache.spark.sql.parser.CarbonSqlBaseParser)1