use of org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException in project carbondata by apache.
the class CarbonDataMergerUtil method identitySegmentsToBeMergedBasedOnSpecifiedSegments.
/**
* This method will return the list of loads which are specified by user in SQL.
*
* @param listOfSegments
* @param segmentIds
* @return
*/
private static List<LoadMetadataDetails> identitySegmentsToBeMergedBasedOnSpecifiedSegments(List<LoadMetadataDetails> listOfSegments, Set<String> segmentIds) throws MalformedCarbonCommandException {
Map<String, LoadMetadataDetails> specifiedSegments = new LinkedHashMap<>(CarbonCommonConstants.DEFAULT_COLLECTION_SIZE);
for (LoadMetadataDetails detail : listOfSegments) {
if (segmentIds.contains(detail.getLoadName())) {
specifiedSegments.put(detail.getLoadName(), detail);
}
}
// all requested segments should exist and be valid
for (String segmentId : segmentIds) {
if (!specifiedSegments.containsKey(segmentId) || !isSegmentValid(specifiedSegments.get(segmentId))) {
String errMsg = String.format("Segment %s does not exist or is not valid", segmentId);
LOGGER.error(errMsg);
throw new MalformedCarbonCommandException(errMsg);
}
}
return new ArrayList<>(specifiedSegments.values());
}
use of org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException 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);
}
use of org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException in project carbondata by apache.
the class CarbonAntlrSqlVisitor method visitCarbonAssignmentList.
public MergeAction visitCarbonAssignmentList(CarbonSqlBaseParser.AssignmentListContext ctx) throws MalformedCarbonCommandException {
// UPDATE SET assignmentList
Map<Column, Column> map = new HashMap<>();
for (int currIdx = 0; currIdx < ctx.getChildCount(); currIdx++) {
if (ctx.getChild(currIdx) instanceof CarbonSqlBaseParser.AssignmentContext) {
// Assume the actions are all use to pass value
String left = ctx.getChild(currIdx).getChild(0).getText();
if (left.split("\\.").length > 1) {
left = left.split("\\.")[1];
}
String right = ctx.getChild(currIdx).getChild(2).getText();
Column rightColumn = null;
try {
Expression expression = sparkParser.parseExpression(right);
rightColumn = new Column(expression);
} catch (Exception e) {
throw new MalformedCarbonCommandException("Parse failed: " + e.getMessage());
}
map.put(new Column(left), rightColumn);
}
}
return new UpdateAction(SparkUtil.convertMap(map), false);
}
Aggregations