@Override public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(GroupbyClause gc, VariableSubstitutionEnvironment env) throws CompilationException { VariableSubstitutionEnvironment newSubs = env; List<GbyVariableExpressionPair> newGbyList = VariableCloneAndSubstitutionUtil.substInVarExprPair(context, gc.getGbyPairList(), newSubs, this); List<GbyVariableExpressionPair> newDecorList = gc.hasDecorList() ? VariableCloneAndSubstitutionUtil.substInVarExprPair(context, gc.getDecorPairList(), newSubs, this) : new ArrayList<>(); VariableExpr newGroupVar = null; if (gc.hasGroupVar()) { newGroupVar = generateNewVariable(context, gc.getGroupVar()); } Map<Expression, VariableExpr> newWithMap = new HashMap<>(); if (gc.hasWithMap()) { for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) { Expression newKeyVar = (Expression) entry.getKey().accept(this, env).first; VariableExpr newValueVar = generateNewVariable(context, entry.getValue()); newWithMap.put(newKeyVar, newValueVar); } } List<Pair<Expression, Identifier>> newGroupFieldList = gc.hasGroupFieldList() ? VariableCloneAndSubstitutionUtil.substInFieldList(gc.getGroupFieldList(), env, this) : null; GroupbyClause newGroup = new GroupbyClause(newGbyList, newDecorList, newWithMap, newGroupVar, newGroupFieldList, gc.hasHashGroupByHint(), gc.isGroupAll()); newGroup.setSourceLocation(gc.getSourceLocation()); return new Pair<>(newGroup, newSubs); }
@Override public Boolean visit(GroupbyClause gc, List<FunctionDecl> arg) throws CompilationException { Pair<Boolean, List<GbyVariableExpressionPair>> p1 = inlineUdfsInGbyPairList(gc.getGbyPairList(), arg); gc.setGbyPairList(p1.second); boolean changed = p1.first; if (gc.hasDecorList()) { Pair<Boolean, List<GbyVariableExpressionPair>> p2 = inlineUdfsInGbyPairList(gc.getDecorPairList(), arg); gc.setDecorPairList(p2.second); changed |= p2.first; } if (gc.hasGroupFieldList()) { Pair<Boolean, List<Pair<Expression, Identifier>>> p3 = inlineUdfsInFieldList(gc.getGroupFieldList(), arg); gc.setGroupFieldList(p3.second); changed |= p3.first; } if (gc.hasWithMap()) { Pair<Boolean, Map<Expression, VariableExpr>> p4 = inlineUdfsInVarMap(gc.getWithVarMap(), arg); gc.setWithVarMap(p4.second); changed |= p4.first; } return changed; }
private Map<Expression, Identifier> getGroupFieldVariables(GroupbyClause groupbyClause) { return groupbyClause.hasGroupFieldList() ? SqlppVariableUtil.createFieldVariableMap(groupbyClause.getGroupFieldList()) : Collections.emptyMap(); }
private List<VariableExpr> collectProducedVariablesFromGroupby(GroupbyClause gbyClause) { List<VariableExpr> producedVars = new ArrayList<VariableExpr>(); for (GbyVariableExpressionPair keyPair : gbyClause.getGbyPairList()) { producedVars.add(keyPair.getVar()); } if (gbyClause.hasDecorList()) { for (GbyVariableExpressionPair keyPair : gbyClause.getDecorPairList()) { producedVars.add(keyPair.getVar()); } } if (gbyClause.hasWithMap()) { producedVars.addAll(gbyClause.getWithVarMap().values()); } return producedVars; }
@Override public Void visit(GroupbyClause gc, Void arg) throws CompilationException { for (GbyVariableExpressionPair p : gc.getGbyPairList()) { p.getExpr().accept(this, arg); } if (gc.hasDecorList()) { for (GbyVariableExpressionPair p : gc.getDecorPairList()) { p.getExpr().accept(this, arg); } } if (gc.hasGroupFieldList()) { for (Pair<Expression, Identifier> p : gc.getGroupFieldList()) { p.first.accept(this, arg); } } if (gc.hasWithMap()) { for (Map.Entry<Expression, VariableExpr> me : gc.getWithVarMap().entrySet()) { me.getKey().accept(this, arg); } } return null; }
@Override public Void visit(GroupbyClause gc, Integer step) throws CompilationException { if (gc.isGroupAll()) { out.println(skip(step) + "Group All"); } else { out.println(skip(step) + "Groupby"); for (GbyVariableExpressionPair pair : gc.getGbyPairList()) { if (pair.getVar() != null) { pair.getVar().accept(this, step + 1); if (gc.hasDecorList()) { out.println(skip(step + 1) + "DECOR"); for (GbyVariableExpressionPair pair : gc.getDecorPairList()) { if (pair.getVar() != null) { pair.getVar().accept(this, step + 1); if (gc.hasGroupVar()) { out.print(skip(step + 1) + "GROUP AS "); gc.getGroupVar().accept(this, 0); if (gc.hasGroupFieldList()) { printFieldList(step + 1, gc.getGroupFieldList()); if (gc.hasWithMap()) { out.println(skip(step + 1) + "WITH"); for (Map.Entry<Expression, VariableExpr> pair : gc.getWithVarMap().entrySet()) { pair.getValue().accept(this, step + 1); out.println(skip(step + 1) + ":=");
private void getGroupBindings(GroupbyClause groupbyClause, List<FieldBinding> outFieldBindings, Set<String> outFieldNames) throws CompilationException { for (GbyVariableExpressionPair pair : groupbyClause.getGbyPairList()) { outFieldBindings.add(getFieldBinding(pair.getVar(), outFieldNames)); } if (groupbyClause.hasGroupVar()) { outFieldBindings.add(getFieldBinding(groupbyClause.getGroupVar(), outFieldNames)); } if (groupbyClause.hasWithMap()) { // no WITH in SQLPP throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, groupbyClause.getSourceLocation(), groupbyClause.getWithVarMap().values().toString()); } }
final public Clause GroupClause() throws ParseException, ParseException { GroupbyClause gbc = new GroupbyClause(); String hint = getHint(token); if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) { gbc.setHashGroupByHint(true); newScope.addNewVarSymbolToScope(withVar.getVar()); gbc.setGbyPairList(vePairList); gbc.setDecorPairList(decorPairList); gbc.setWithVarMap(withVarMap); replaceCurrentScope(newScope); {if (true) return gbc;}
final public GroupbyClause GroupbyClause() throws ParseException, ParseException { Token startToken = null; GroupbyClause gbc = new GroupbyClause(); List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>(); VariableExpr var = null; String hint = getHint(token); if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) { gbc.setHashGroupByHint(true); gbc.setGbyPairList(vePairList); gbc.setDecorPairList(new ArrayList<GbyVariableExpressionPair>()); gbc.setWithVarMap(new HashMap<Expression, VariableExpr>()); gbc.setGroupVar(groupVar); gbc.setGroupFieldList(groupFieldList); replaceCurrentScope(newScope); {if (true) return addSourceLocation(gbc, startToken);}
private void rewriteSelectWithGroupBy(SelectBlock selectBlock, ILangExpression arg) throws CompilationException { GroupbyClause gbyClause = selectBlock.getGroupbyClause(); // Sets the group variable. if (!gbyClause.hasGroupVar()) { VariableExpr groupVar = new VariableExpr(context.newVariable()); groupVar.setSourceLocation(gbyClause.getSourceLocation()); gbyClause.setGroupVar(groupVar); } // Sets the field list for the group variable. List<Pair<Expression, Identifier>> groupFieldList; if (gbyClause.hasGroupFieldList()) { groupFieldList = new ArrayList<>(); for (Pair<Expression, Identifier> groupField : gbyClause.getGroupFieldList()) { Expression newFieldExpr = groupField.first.accept(this, arg); groupFieldList.add(new Pair<>(newFieldExpr, groupField.second)); } } else { groupFieldList = createGroupFieldList(selectBlock); } gbyClause.setGroupFieldList(groupFieldList); }
groupbyClause.accept(this, arg); Collection<VariableExpr> visibleVarsInCurrentScope = SqlppVariableUtil.getBindingVariables(groupbyClause); VariableExpr groupVar = groupbyClause.getGroupVar(); Map<Expression, Identifier> groupFieldVars = getGroupFieldVariables(groupbyClause); if (groupbyClause.hasDecorList()) { decorList.addAll(groupbyClause.getDecorPairList()); (Expression) SqlppRewriteUtil.deepCopy(var))); groupbyClause.setDecorPairList(decorList);
@Override public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException { for (GbyVariableExpressionPair gbyVarExpr : gc.getGbyPairList()) { gbyVarExpr.setExpr(visit(gbyVarExpr.getExpr(), gc)); } if (gc.hasDecorList()) { for (GbyVariableExpressionPair decVarExpr : gc.getDecorPairList()) { decVarExpr.setExpr(visit(decVarExpr.getExpr(), gc)); } } return null; }
@Override public Void visit(GroupbyClause gc, Integer step) throws CompilationException { if (gc.hasHashGroupByHint()) { out.println(skip(step) + "/* +hash */"); } out.print(skip(step) + "group by "); printDelimitedGbyExpressions(gc.getGbyPairList(), step + 2); out.println(); return null; }
private void rewriteSelectWithoutGroupBy(SelectBlock selectBlock) throws CompilationException { if (hasSql92Aggregate(selectBlock)) { // Adds an implicit group-by clause for SQL-92 global aggregate. List<GbyVariableExpressionPair> gbyPairList = new ArrayList<>(); List<GbyVariableExpressionPair> decorPairList = new ArrayList<>(); VariableExpr groupVar = new VariableExpr(context.newVariable()); groupVar.setSourceLocation(selectBlock.getSourceLocation()); List<Pair<Expression, Identifier>> groupFieldList = createGroupFieldList(selectBlock); GroupbyClause gbyClause = new GroupbyClause(gbyPairList, decorPairList, new HashMap<>(), groupVar, groupFieldList, false, true); gbyClause.setSourceLocation(selectBlock.getSourceLocation()); selectBlock.setGroupbyClause(gbyClause); } }
@Override public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException { for (GbyVariableExpressionPair gbyVarExpr : gc.getGbyPairList()) { gbyVarExpr.setExpr(visit(gbyVarExpr.getExpr(), gc)); } return null; }
@Override public Void visit(GroupbyClause gc, Void arg) throws CompilationException { for (GbyVariableExpressionPair p : gc.getGbyPairList()) { p.getExpr().accept(this, arg); } if (gc.hasDecorList()) { for (GbyVariableExpressionPair p : gc.getDecorPairList()) { p.getExpr().accept(this, arg); } } if (gc.hasGroupFieldList()) { for (Pair<Expression, Identifier> p : gc.getGroupFieldList()) { p.first.accept(this, arg); } } if (gc.hasWithMap()) { for (Map.Entry<Expression, VariableExpr> me : gc.getWithVarMap().entrySet()) { me.getKey().accept(this, arg); } } return null; }
for (GbyVariableExpressionPair gbyKeyVarExpr : gc.getGbyPairList()) { gbyKeyVarExpr.setExpr(visit(gbyKeyVarExpr.getExpr(), gc)); VariableExpr gbyKeyVar = gbyKeyVarExpr.getVar(); if (gc.hasGroupFieldList()) { for (Pair<Expression, Identifier> gbyField : gc.getGroupFieldList()) { gbyField.first = visit(gbyField.first, arg); if (gc.hasDecorList()) { for (GbyVariableExpressionPair decorVarExpr : gc.getDecorPairList()) { decorVarExpr.setExpr(visit(decorVarExpr.getExpr(), gc)); VariableExpr decorVar = decorVarExpr.getVar(); if (gc.hasGroupVar()) { VariableExpr groupVar = gc.getGroupVar(); addNewVarSymbolToScope(newScope, groupVar.getVar(), groupVar.getSourceLocation()); if (gc.hasWithMap()) { Map<Expression, VariableExpr> newWithMap = new HashMap<>(); for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) { Expression expr = visit(entry.getKey(), arg); Expression newKey = expr; newWithMap.put(newKey, value); gc.setWithVarMap(newWithMap);
public static List<VariableExpr> getBindingVariables(GroupbyClause gbyClause) { List<VariableExpr> bindingVars = new ArrayList<>(); if (gbyClause == null) { return bindingVars; } for (GbyVariableExpressionPair gbyKey : gbyClause.getGbyPairList()) { VariableExpr var = gbyKey.getVar(); if (var != null) { bindingVars.add(var); } } if (gbyClause.hasDecorList()) { for (GbyVariableExpressionPair gbyKey : gbyClause.getDecorPairList()) { VariableExpr var = gbyKey.getVar(); if (var != null) { bindingVars.add(var); } } } if (gbyClause.hasWithMap()) { bindingVars.addAll(gbyClause.getWithVarMap().values()); } bindingVars.add(gbyClause.getGroupVar()); return bindingVars; }
final public Clause GroupClause() throws ParseException, ParseException { GroupbyClause gbc = new GroupbyClause(); String hint = getHint(token); if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) { gbc.setHashGroupByHint(true); newScope.addNewVarSymbolToScope(withVar.getVar()); gbc.setGbyPairList(vePairList); gbc.setDecorPairList(decorPairList); gbc.setWithVarMap(withVarMap); replaceCurrentScope(newScope); {if (true) return gbc;}
@Override public Void visit(GroupbyClause gc, Integer step) throws CompilationException { if (gc.hasHashGroupByHint()) { out.println(skip(step) + "/* +hash */"); } out.print(skip(step) + "group by "); printDelimitedGbyExpressions(gc.getGbyPairList(), step + 2); out.println(); return null; }