Example usage for java.util Stack push

List of usage examples for java.util Stack push

Introduction

In this page you can find the example usage for java.util Stack push.

Prototype

public E push(E item) 

Source Link

Document

Pushes an item onto the top of this stack.

Usage

From source file:com.datatorrent.stram.plan.physical.PhysicalPlan.java

private void redoPartitions(PMapping currentMapping, String note) {
    Partitioner<Operator> partitioner = getPartitioner(currentMapping);
    if (partitioner == null) {
        LOG.warn("No partitioner for {}", currentMapping.logicalOperator);
        return;/* w  w  w. ja v  a2 s .  c  o m*/
    }

    RepartitionContext mainPC = new RepartitionContext(partitioner, currentMapping, 0);
    if (mainPC.newPartitions.isEmpty()) {
        LOG.warn("Empty partition list after repartition: {}", currentMapping.logicalOperator);
        return;
    }

    int memoryPerPartition = currentMapping.logicalOperator.getValue(OperatorContext.MEMORY_MB);
    for (Map.Entry<OutputPortMeta, StreamMeta> stream : currentMapping.logicalOperator.getOutputStreams()
            .entrySet()) {
        if (stream.getValue().getLocality() != Locality.THREAD_LOCAL
                && stream.getValue().getLocality() != Locality.CONTAINER_LOCAL) {
            memoryPerPartition += stream.getKey().getValue(PortContext.BUFFER_MEMORY_MB);
        }
    }
    for (OperatorMeta pp : currentMapping.parallelPartitions) {
        for (Map.Entry<OutputPortMeta, StreamMeta> stream : pp.getOutputStreams().entrySet()) {
            if (stream.getValue().getLocality() != Locality.THREAD_LOCAL
                    && stream.getValue().getLocality() != Locality.CONTAINER_LOCAL) {
                memoryPerPartition += stream.getKey().getValue(PortContext.BUFFER_MEMORY_MB);
            }
        }
        memoryPerPartition += pp.getValue(OperatorContext.MEMORY_MB);
    }
    int requiredMemoryMB = (mainPC.newPartitions.size() - mainPC.currentPartitions.size()) * memoryPerPartition;
    if (requiredMemoryMB > availableMemoryMB) {
        LOG.warn("Insufficient headroom for repartitioning: available {}m required {}m", availableMemoryMB,
                requiredMemoryMB);
        return;
    }

    List<Partition<Operator>> addedPartitions = new ArrayList<Partition<Operator>>();
    // determine modifications of partition set, identify affected operator instance(s)
    for (Partition<Operator> newPartition : mainPC.newPartitions) {
        PTOperator op = mainPC.currentPartitionMap.remove(newPartition);
        if (op == null) {
            addedPartitions.add(newPartition);
        } else {
            // check whether mapping was changed
            for (DefaultPartition<Operator> pi : mainPC.currentPartitions) {
                if (pi == newPartition && pi.isModified()) {
                    // existing partition changed (operator or partition keys)
                    // remove/add to update subscribers and state
                    mainPC.currentPartitionMap.put(newPartition, op);
                    addedPartitions.add(newPartition);
                }
            }
        }
    }

    // remaining entries represent deprecated partitions
    this.undeployOpers.addAll(mainPC.currentPartitionMap.values());
    // downstream dependencies require redeploy, resolve prior to modifying plan
    Set<PTOperator> deps = this.getDependents(mainPC.currentPartitionMap.values());
    this.undeployOpers.addAll(deps);
    // dependencies need redeploy, except operators excluded in remove
    this.deployOpers.addAll(deps);

    // process parallel partitions before removing operators from the plan
    LinkedHashMap<PMapping, RepartitionContext> partitionContexts = Maps.newLinkedHashMap();
    Stack<OperatorMeta> parallelPartitions = new Stack<LogicalPlan.OperatorMeta>();
    parallelPartitions.addAll(currentMapping.parallelPartitions);
    pendingLoop: while (!parallelPartitions.isEmpty()) {
        OperatorMeta ppMeta = parallelPartitions.pop();
        for (StreamMeta s : ppMeta.getInputStreams().values()) {
            if (currentMapping.parallelPartitions.contains(s.getSource().getOperatorMeta())
                    && parallelPartitions.contains(s.getSource().getOperatorMeta())) {
                parallelPartitions.push(ppMeta);
                parallelPartitions.remove(s.getSource().getOperatorMeta());
                parallelPartitions.push(s.getSource().getOperatorMeta());
                continue pendingLoop;
            }
        }
        LOG.debug("Processing parallel partition {}", ppMeta);

        PMapping ppm = this.logicalToPTOperator.get(ppMeta);
        Partitioner<Operator> ppp = getPartitioner(ppm);
        if (ppp == null) {
            partitionContexts.put(ppm, null);
        } else {
            RepartitionContext pc = new RepartitionContext(ppp, ppm, mainPC.newPartitions.size());
            if (pc.newPartitions == null) {
                throw new IllegalStateException(
                        "Partitioner returns null for parallel partition " + ppm.logicalOperator);
            }
            partitionContexts.put(ppm, pc);
        }
    }

    // plan updates start here, after all changes were identified
    // remove obsolete operators first, any freed resources
    // can subsequently be used for new/modified partitions
    List<PTOperator> copyPartitions = Lists.newArrayList(currentMapping.partitions);
    // remove deprecated partitions from plan
    for (PTOperator p : mainPC.currentPartitionMap.values()) {
        copyPartitions.remove(p);
        removePartition(p, currentMapping);
        mainPC.operatorIdToPartition.remove(p.getId());
    }
    currentMapping.partitions = copyPartitions;

    // add new operators
    for (Partition<Operator> newPartition : addedPartitions) {
        PTOperator p = addPTOperator(currentMapping, newPartition, mainPC.minCheckpoint);
        mainPC.operatorIdToPartition.put(p.getId(), newPartition);
    }

    // process parallel partition changes
    for (Map.Entry<PMapping, RepartitionContext> e : partitionContexts.entrySet()) {
        if (e.getValue() == null) {
            // no partitioner, add required operators
            for (int i = 0; i < addedPartitions.size(); i++) {
                LOG.debug("Automatically adding to parallel partition {}", e.getKey());
                // set activation windowId to confirm to upstream checkpoints
                addPTOperator(e.getKey(), null, mainPC.minCheckpoint);
            }
        } else {
            RepartitionContext pc = e.getValue();
            // track previous parallel partition mapping
            Map<Partition<Operator>, Partition<Operator>> prevMapping = Maps.newHashMap();
            for (int i = 0; i < mainPC.currentPartitions.size(); i++) {
                prevMapping.put(pc.currentPartitions.get(i), mainPC.currentPartitions.get(i));
            }
            // determine which new partitions match upstream, remaining to be treated as new operator
            Map<Partition<Operator>, Partition<Operator>> newMapping = Maps.newHashMap();
            Iterator<Partition<Operator>> itMain = mainPC.newPartitions.iterator();
            Iterator<Partition<Operator>> itParallel = pc.newPartitions.iterator();
            while (itMain.hasNext() && itParallel.hasNext()) {
                newMapping.put(itParallel.next(), itMain.next());
            }

            for (Partition<Operator> newPartition : pc.newPartitions) {
                PTOperator op = pc.currentPartitionMap.remove(newPartition);
                if (op == null) {
                    pc.addedPartitions.add(newPartition);
                } else if (prevMapping.get(newPartition) != newMapping.get(newPartition)) {
                    // upstream partitions don't match, remove/add to replace with new operator
                    pc.currentPartitionMap.put(newPartition, op);
                    pc.addedPartitions.add(newPartition);
                } else {
                    // check whether mapping was changed - based on DefaultPartition implementation
                    for (DefaultPartition<Operator> pi : pc.currentPartitions) {
                        if (pi == newPartition && pi.isModified()) {
                            // existing partition changed (operator or partition keys)
                            // remove/add to update subscribers and state
                            mainPC.currentPartitionMap.put(newPartition, op);
                            pc.addedPartitions.add(newPartition);
                        }
                    }
                }
            }

            if (!pc.currentPartitionMap.isEmpty()) {
                // remove obsolete partitions
                List<PTOperator> cowPartitions = Lists.newArrayList(e.getKey().partitions);
                for (PTOperator p : pc.currentPartitionMap.values()) {
                    cowPartitions.remove(p);
                    removePartition(p, e.getKey());
                    pc.operatorIdToPartition.remove(p.getId());
                }
                e.getKey().partitions = cowPartitions;
            }
            // add new partitions
            for (Partition<Operator> newPartition : pc.addedPartitions) {
                PTOperator oper = addPTOperator(e.getKey(), newPartition, mainPC.minCheckpoint);
                pc.operatorIdToPartition.put(oper.getId(), newPartition);
            }

            getPartitioner(e.getKey()).partitioned(pc.operatorIdToPartition);
        }
    }

    updateStreamMappings(currentMapping);
    for (PMapping pp : partitionContexts.keySet()) {
        updateStreamMappings(pp);
    }

    deployChanges();

    if (mainPC.currentPartitions.size() != mainPC.newPartitions.size()) {
        StramEvent ev = new StramEvent.PartitionEvent(currentMapping.logicalOperator.getName(),
                mainPC.currentPartitions.size(), mainPC.newPartitions.size());
        ev.setReason(note);
        this.ctx.recordEventAsync(ev);
    }

    partitioner.partitioned(mainPC.operatorIdToPartition);
}

From source file:com.udojava.evalex.Expression.java

/**
 * Implementation of the <i>Shunting Yard</i> algorithm to transform an
 * infix expression to a RPN expression.
 *
 * @param expression The input expression in infx.
 * @return A RPN representation of the expression, with each token as a list
 * member./* ww  w .  ja  v a  2  s  . com*/
 */
private List<String> shuntingYard(String expression) {
    List<String> outputQueue = new ArrayList<>();
    Stack<String> stack = new Stack<>();

    Tokenizer tokenizer = new Tokenizer(expression);

    String lastFunction = null;
    String previousToken = null;
    while (tokenizer.hasNext()) {
        String token = tokenizer.next();
        if (isNumber(token)) {
            if (token.startsWith("x")) {

                BigInteger bd = new BigInteger(token.substring(1), 16);
                outputQueue.add(bd.toString(10));
            } else if (token.startsWith("b")) {
                BigInteger bd = new BigInteger(token.substring(1), 2);
                outputQueue.add(bd.toString(10));
            } else if (token.startsWith("o")) {
                BigInteger bd = new BigInteger(token.substring(1), 8);
                outputQueue.add(bd.toString(10));
            } else {
                outputQueue.add(token);
            }
        } else if (mainVars.containsKey(token)) {
            outputQueue.add(token);
        } else if (functions.containsKey(token.toUpperCase(Locale.ROOT))) {
            stack.push(token);
            lastFunction = token;
        } else if ((Character.isLetter(token.charAt(0)) || token.charAt(0) == '_')
                && !operators.containsKey(token)) {
            mainVars.put(token, new MyComplex(0, 0)); // create variable
            outputQueue.add(token);
            //stack.push(token);
        } else if (",".equals(token)) {
            if (operators.containsKey(previousToken)) {
                throw new ExpressionException("Missing parameter(s) for operator " + previousToken
                        + " at character position " + (tokenizer.getPos() - 1 - previousToken.length()));
            }
            while (!stack.isEmpty() && !"(".equals(stack.peek())) {
                outputQueue.add(stack.pop());
            }
            if (stack.isEmpty()) {
                throw new ExpressionException("Parse error for function '" + lastFunction + "'");
            }
        } else if (operators.containsKey(token)) {
            if (",".equals(previousToken) || "(".equals(previousToken)) {
                throw new ExpressionException("Missing parameter(s) for operator " + token
                        + " at character position " + (tokenizer.getPos() - token.length()));
            }
            Operator o1 = operators.get(token);
            String token2 = stack.isEmpty() ? null : stack.peek();
            while (token2 != null && operators.containsKey(token2)
                    && ((o1.isLeftAssoc() && o1.getPrecedence() <= operators.get(token2).getPrecedence())
                            || (o1.getPrecedence() < operators.get(token2).getPrecedence()))) {
                outputQueue.add(stack.pop());
                token2 = stack.isEmpty() ? null : stack.peek();
            }
            stack.push(token);
        } else if ("(".equals(token)) {
            if (previousToken != null) {
                if (isNumber(previousToken)) {
                    throw new ExpressionException(
                            "Missing operator at character position " + tokenizer.getPos());
                }
                // if the ( is preceded by a valid function, then it
                // denotes the start of a parameter list
                if (functions.containsKey(previousToken.toUpperCase(Locale.ROOT))) {
                    outputQueue.add(token);
                }
            }
            stack.push(token);
        } else if (")".equals(token)) {
            if (operators.containsKey(previousToken)) {
                throw new ExpressionException("Missing parameter(s) for operator " + previousToken
                        + " at character position " + (tokenizer.getPos() - 1 - previousToken.length()));
            }
            while (!stack.isEmpty() && !"(".equals(stack.peek())) {
                outputQueue.add(stack.pop());
            }
            if (stack.isEmpty()) {
                throw new ExpressionException("Mismatched parentheses");
            }
            stack.pop();
            if (!stack.isEmpty() && functions.containsKey(stack.peek().toUpperCase(Locale.ROOT))) {
                outputQueue.add(stack.pop());
            }
        }
        previousToken = token;
    }
    while (!stack.isEmpty()) {
        String element = stack.pop();
        if ("(".equals(element) || ")".equals(element)) {
            throw new ExpressionException("Mismatched parentheses");
        }

        if (!operators.containsKey(element)) {
            throw new ExpressionException("Unknown operator or function: " + element);
        }
        outputQueue.add(element);
    }
    return outputQueue;
}

From source file:hydrograph.engine.core.xmlparser.parametersubstitution.ParameterSubstitutor.java

private void substituteMutable(StringBuilder mutable, Stack<String> unresolvedParameters) {

    int startIndex = mutable.indexOf(VARIABLE_PREFIX);
    int endIndex = mutable.indexOf(VARIABLE_SUFFIX, startIndex);

    // return if nothing to substitute
    if (startIndex == -1 || endIndex == -1) {
        return;//from   w w w . j ava 2 s.  c  o m
    }

    // get parameter name
    String parameterName = mutable.substring(startIndex + VARIABLE_PREFIX.length(), endIndex);

    // raise exception if parameter name is blank
    if (parameterName == null || parameterName.trim().length() == 0) {
        throw new ParameterSubstitutorException("Parameter name can not be blank. Please correct.");
    }

    parameterName = parameterName.trim();

    String parameterValue = null;

    if (resolvedParameterCache.containsKey(parameterName)) {
        // obtain value from cache if already present
        parameterValue = resolvedParameterCache.get(parameterName);
        LOG.info("cache used for " + parameterName);
    } else {

        // check if the parameter is already on the stack then raise
        // exception
        // that it is circular substitution
        if (unresolvedParameters.search(parameterName) != -1) {
            throw new ParameterSubstitutorException("Found a circular depencency between parameter "
                    + parameterName + " and " + unresolvedParameters.peek()
                    + ". Both are referencing each other and cannot be resolved. Please correct.");
        }

        // get parameter value
        parameterValue = parameterBank.getParameter(parameterName);

        // if value is null then raise exception
        if (parameterValue == null) {
            throw new ParameterSubstitutorException(
                    "No value is found for the parameter " + parameterName + " to substitute");
        }

        // if parameter key to be substituted is in quotes("") then escape
        // special characters from its value
        if (isParameterPresentInQuotes(mutable, startIndex, endIndex)) {
            parameterValue = StringEscapeUtils.escapeXml(parameterValue);
        }

        // add current parameter to stack to check for circular loop later
        unresolvedParameters.push(parameterName);

        // check of substitution if there is a parameter reference in
        // parameter
        // value(internal substitution)
        parameterValue = substitute(parameterValue, unresolvedParameters);

        // remove parameter from stack as soon as it is resolved
        unresolvedParameters.pop();

        // add resolved value to cache
        resolvedParameterCache.put(parameterName, parameterValue);
    }
    // delete parameter syntax
    mutable.delete(startIndex, endIndex + VARIABLE_SUFFIX.length());

    // insert parameter value
    mutable.insert(startIndex, parameterValue);

    // check for next substitution and do it if available
    substituteMutable(mutable, unresolvedParameters);

}

From source file:com.vmware.identity.idm.server.provider.ldap.LdapWithAdMappingsProvider.java

Set<Group> populateNestedGroups(ILdapConnectionEx connection, String dn, boolean groupNameOnly,
        Set<Group> groups, IIdmAuthStatRecorder authStatRecorder)
        throws NoSuchGroupException, InvalidPrincipalException {
    Validate.notNull(groups, "groups");

    final String ATTR_NAME_GROUP_CN = _ldapSchemaMapping
            .getGroupAttribute(IdentityStoreAttributeMapping.AttributeIds.GroupAttributeAccountName);
    final String ATTR_DESCRIPTION = _ldapSchemaMapping
            .getGroupAttribute(IdentityStoreAttributeMapping.AttributeIds.GroupAttributeDescription);
    final String ATTR_GROUP_SID = _ldapSchemaMapping
            .getGroupAttribute(IdentityStoreAttributeMapping.AttributeIds.GroupAttributeObjectId);

    ArrayList<String> attributeNames = new ArrayList<String>();
    attributeNames.add(ATTR_NAME_GROUP_CN);
    attributeNames.add(ATTR_GROUP_SID);//from ww  w .  j  a  v  a  2s  .c  o  m
    if (!groupNameOnly) {
        attributeNames.add(ATTR_DESCRIPTION);
    }

    final boolean bRecurse = (!this.useDirectGroupsOnly()) && (!this.useMatchingRuleInChain());
    final String groupSearchBaseDn = (this.useGroupBaseDnForNestedGroups()
            ? this.getStoreDataEx().getGroupBaseDn()
            : ServerUtils.getDomainDN(this.getDomain()));
    final String searchQueryTemplate = this.useMatchingRuleInChain()
            ? _ldapSchemaMapping.getNestedParentGroupsQuery()
            : _ldapSchemaMapping.getDirectParentGroupsQuery();

    if (logger.isDebugEnabled()) {
        logger.debug(String.format(
                "LdapWithAdMappingsProvider.populateNestedGroups -- GroupSearchBaseDn: %s; query template: %s",
                groupSearchBaseDn, searchQueryTemplate));
    }
    int numberOfLdapSearches = 0;

    HashSet<String> groupsProcessed = new HashSet<String>();
    Stack<String> groupsToProcess = new Stack<String>();

    if (ServerUtils.isNullOrEmpty(dn) == false) {
        groupsToProcess.push(dn);
    }

    long startTimeForAllGrups = System.nanoTime();

    while (groupsToProcess.isEmpty() == false) {
        String currentDn = groupsToProcess.pop();
        groupsProcessed.add(currentDn);

        String filter = String.format(searchQueryTemplate,
                (this.useMatchingRuleInChain() ? LdapFilterString.encodeMatchingRuleInChainDnFilter(currentDn)
                        : LdapFilterString.encode(currentDn)));

        String groupName = null;
        String groupDescription = null;
        String groupEntryObjectSid = null;

        ILdapPagedSearchResult prev_pagedResult = null;
        ILdapPagedSearchResult pagedResult = null;
        boolean isSearchFinished = false;

        try {
            int numOfQueriesPerGroup = 0;
            long startTimePerGroup = System.nanoTime();

            while (!isSearchFinished) {
                if (logger.isTraceEnabled()) {
                    logger.trace(String.format(
                            "LdapWithAdMappingsProvider.populateNestedGroups -- running connection.search_one_page( %s )",
                            filter));
                }
                pagedResult = connection.search_one_page(groupSearchBaseDn, LdapScope.SCOPE_SUBTREE, filter,
                        attributeNames, DEFAULT_PAGE_SIZE, prev_pagedResult);

                numOfQueriesPerGroup += 1;
                numberOfLdapSearches += 1;

                if (pagedResult != null) {
                    ILdapEntry[] entries = pagedResult.getEntries();
                    if ((entries != null) && (entries.length > 0)) {
                        for (ILdapEntry entry : entries) {
                            groupName = ServerUtils
                                    .getStringValue(entry.getAttributeValues(ATTR_NAME_GROUP_CN));

                            if (groupNameOnly == false) {
                                groupDescription = ServerUtils
                                        .getStringValue(entry.getAttributeValues(ATTR_DESCRIPTION));
                            }

                            byte[] resultObjectSID = ServerUtils
                                    .getBinaryValue(entry.getAttributeValues(ATTR_GROUP_SID));

                            SecurityIdentifier sid = SecurityIdentifier.build(resultObjectSID);
                            groupEntryObjectSid = sid.toString();

                            String groupDomainName = ServerUtils.getDomainFromDN(entry.getDN());

                            PrincipalId groupId = new PrincipalId(groupName, groupDomainName);
                            PrincipalId groupAlias = null;
                            GroupDetail groupDetail = null;

                            if (groupNameOnly == false) {
                                // If group lives in the registered Ad over Ldap IDP, we know the alias
                                // Otherwise, we do not know the alias for the domain where group lives.
                                if (groupDomainName.equalsIgnoreCase(this.getStoreData().getName())) {
                                    groupAlias = ServerUtils.getPrincipalAliasId(groupName, this.getAlias());
                                }

                                groupDetail = new GroupDetail(
                                        (groupDescription == null) ? "" : groupDescription);
                            }

                            Group g = new Group(groupId, groupAlias, groupEntryObjectSid, groupDetail);
                            groups.add(g);

                            if ((bRecurse == true) && (groupsProcessed.contains(entry.getDN()) == false)) {
                                groupsToProcess.add(entry.getDN());
                            }
                        }
                    }
                }
                isSearchFinished = (pagedResult == null) || (pagedResult.isSearchFinished());
                if (prev_pagedResult != null) {
                    prev_pagedResult.close();
                    prev_pagedResult = null;
                }
                prev_pagedResult = pagedResult;
                pagedResult = null;
            } // while !isSearchFinished

            // If summarizeLdapQueries is set false, log each ldap query
            if (authStatRecorder != null && !authStatRecorder.summarizeLdapQueries()) {
                authStatRecorder
                        .add(new LdapQueryStat(filter, groupSearchBaseDn, getConnectionString(connection),
                                TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTimePerGroup),
                                numOfQueriesPerGroup));
            }
        } finally {
            if (prev_pagedResult != null) {
                prev_pagedResult.close();
                prev_pagedResult = null;
            }
            if (pagedResult != null) {
                pagedResult.close();
                pagedResult = null;
            }
        }

    } // groupstoprocess not empty

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("LdapWithAdMappingsProvider.populateNestedGroups -- ran [%d] ldap searches.",
                numberOfLdapSearches));
    }

    // If summarizeLdapQueries is set true, log once only with summary
    if (authStatRecorder != null && authStatRecorder.summarizeLdapQueries()) {
        authStatRecorder.add(new LdapQueryStat(searchQueryTemplate, groupSearchBaseDn,
                getConnectionString(connection),
                TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTimeForAllGrups), numberOfLdapSearches));
    }

    return groups;
}

From source file:org.apache.tajo.engine.planner.LogicalPlanner.java

private LogicalNode buildSetPlan(PlanContext context, Stack<Expr> stack, SetOperation setOperation)
        throws PlanningException {

    // 1. Init Phase
    LogicalPlan plan = context.plan;/*from   w  w w  .  j a  va  2  s . co m*/
    QueryBlock block = context.queryBlock;

    ////////////////////////////////////////////////////////
    // Visit and Build Left Child Plan
    ////////////////////////////////////////////////////////
    QueryBlock leftBlock = context.plan.getBlockByExpr(setOperation.getLeft());
    PlanContext leftContext = new PlanContext(context, leftBlock);
    stack.push(setOperation);
    LogicalNode leftChild = visit(leftContext, new Stack<Expr>(), setOperation.getLeft());
    stack.pop();
    // Connect left child and current blocks
    context.plan.connectBlocks(leftContext.queryBlock, context.queryBlock, BlockType.TableSubQuery);

    ////////////////////////////////////////////////////////
    // Visit and Build Right Child Plan
    ////////////////////////////////////////////////////////
    QueryBlock rightBlock = context.plan.getBlockByExpr(setOperation.getRight());
    PlanContext rightContext = new PlanContext(context, rightBlock);
    stack.push(setOperation);
    LogicalNode rightChild = visit(rightContext, new Stack<Expr>(), setOperation.getRight());
    stack.pop();
    // Connect right child and current blocks
    context.plan.connectBlocks(rightContext.queryBlock, context.queryBlock, BlockType.TableSubQuery);

    BinaryNode setOp;
    if (setOperation.getType() == OpType.Union) {
        setOp = block.getNodeFromExpr(setOperation);
    } else if (setOperation.getType() == OpType.Except) {
        setOp = block.getNodeFromExpr(setOperation);
    } else if (setOperation.getType() == OpType.Intersect) {
        setOp = block.getNodeFromExpr(setOperation);
    } else {
        throw new VerifyException("Invalid Type: " + setOperation.getType());
    }
    setOp.setLeftChild(leftChild);
    setOp.setRightChild(rightChild);

    // An union statement can be derived from two query blocks.
    // For one union statement between both relations, we can ensure that each corresponding data domain of both
    // relations are the same. However, if necessary, the schema of left query block will be used as a base schema.
    Target[] leftStrippedTargets = PlannerUtil
            .stripTarget(PlannerUtil.schemaToTargets(leftBlock.getRoot().getOutSchema()));

    setOp.setInSchema(leftChild.getOutSchema());
    Schema outSchema = PlannerUtil.targetToSchema(leftStrippedTargets);
    setOp.setOutSchema(outSchema);

    return setOp;
}

From source file:org.apache.tajo.engine.planner.LogicalPlanner.java

@Override
public LimitNode visitLimit(PlanContext context, Stack<Expr> stack, Limit limit) throws PlanningException {
    QueryBlock block = context.queryBlock;

    EvalNode firstFetNum;//from  ww w.j  a  v a2  s.  c o m
    LogicalNode child;
    if (limit.getFetchFirstNum().getType() == OpType.Literal) {
        firstFetNum = exprAnnotator.createEvalNode(context, limit.getFetchFirstNum(),
                NameResolvingMode.RELS_ONLY);

        ////////////////////////////////////////////////////////
        // Visit and Build Child Plan
        ////////////////////////////////////////////////////////
        stack.push(limit);
        child = visit(context, stack, limit.getChild());
        stack.pop();
        ////////////////////////////////////////////////////////
    } else {
        ExprNormalizedResult normalizedResult = normalizer.normalize(context, limit.getFetchFirstNum());
        String referName = block.namedExprsMgr.addExpr(normalizedResult.baseExpr);
        block.namedExprsMgr.addNamedExprArray(normalizedResult.aggExprs);
        block.namedExprsMgr.addNamedExprArray(normalizedResult.scalarExprs);

        ////////////////////////////////////////////////////////
        // Visit and Build Child Plan
        ////////////////////////////////////////////////////////
        stack.push(limit);
        child = visit(context, stack, limit.getChild());
        stack.pop();
        ////////////////////////////////////////////////////////

        if (block.namedExprsMgr.isEvaluated(referName)) {
            firstFetNum = block.namedExprsMgr.getTarget(referName).getEvalTree();
        } else {
            NamedExpr namedExpr = block.namedExprsMgr.getNamedExpr(referName);
            firstFetNum = exprAnnotator.createEvalNode(context, namedExpr.getExpr(),
                    NameResolvingMode.SUBEXPRS_AND_RELS);
            block.namedExprsMgr.markAsEvaluated(referName, firstFetNum);
        }
    }
    LimitNode limitNode = block.getNodeFromExpr(limit);
    limitNode.setChild(child);
    limitNode.setInSchema(child.getOutSchema());
    limitNode.setOutSchema(child.getOutSchema());

    limitNode.setFetchFirst(firstFetNum.eval(null, null).asInt8());

    return limitNode;
}

From source file:org.apache.cocoon.util.log.ExtensiblePatternFormatter.java

/**
 * Extract and build a pattern from input string.
 *
 * @param stack the stack on which to place patterns
 * @param pattern the input string/* w w  w . jav a  2 s. c om*/
 * @param index the start of pattern run
 * @return the number of characters in pattern run
 */
protected int addPatternRun(final Stack stack, final char pattern[], int index) {
    final PatternRun run = new PatternRun();
    final int start = index++;

    // first check for a +|- sign
    if ('+' == pattern[index]) {
        index++;
    } else if ('-' == pattern[index]) {
        run.m_rightJustify = true;
        index++;
    }

    if (Character.isDigit(pattern[index])) {
        int total = 0;
        while (Character.isDigit(pattern[index])) {
            total = total * 10 + (pattern[index] - '0');
            index++;
        }
        run.m_minSize = total;
    }

    //check for . sign indicating a maximum is to follow
    if (index < pattern.length && '.' == pattern[index]) {
        index++;
        if (Character.isDigit(pattern[index])) {
            int total = 0;
            while (Character.isDigit(pattern[index])) {
                total = total * 10 + (pattern[index] - '0');
                index++;
            }
            run.m_maxSize = total;
        }
    }

    if (index >= pattern.length || '{' != pattern[index]) {
        throw new IllegalArgumentException("Badly formed pattern at character " + index);
    }

    int typeStart = index;

    while (index < pattern.length && pattern[index] != ':' && pattern[index] != '}') {
        index++;
    }

    int typeEnd = index - 1;

    final String type = new String(pattern, typeStart + 1, typeEnd - typeStart);

    run.m_type = getTypeIdFor(type);

    if (index < pattern.length && pattern[index] == ':') {
        index++;
        while (index < pattern.length && pattern[index] != '}') {
            index++;
        }
        final int length = index - typeEnd - 2;

        if (0 != length) {
            run.m_format = new String(pattern, typeEnd + 2, length);
        }
    }

    if (index >= pattern.length || '}' != pattern[index]) {
        throw new IllegalArgumentException("Unterminated type in pattern at character " + index);
    }
    index++;
    stack.push(run);
    return index - start;
}

From source file:org.apache.pig.backend.hadoop.executionengine.tez.plan.TezCompiler.java

private POSplit findPOSplit(TezOperator tezOp, OperatorKey splitKey) throws PlanException {
    POSplit split = (POSplit) tezOp.plan.getOperator(splitKey);
    if (split != null) {
        // The split is the leaf operator.
        return split;
    } else {//  www  .ja va 2  s  .c  o m
        // The split is a nested split
        Stack<POSplit> stack = new Stack<POSplit>();
        split = (POSplit) tezOp.plan.getLeaves().get(0);
        stack.push(split);
        while (!stack.isEmpty()) {
            split = stack.pop();
            for (PhysicalPlan plan : split.getPlans()) {
                PhysicalOperator op = plan.getLeaves().get(0);
                if (op instanceof POSplit) {
                    split = (POSplit) op;
                    if (split.getOperatorKey().equals(splitKey)) {
                        return split;
                    } else {
                        stack.push(split);
                    }
                }
            }
        }
    }
    // TODO: what should be the new error code??
    throw new PlanException("Could not find the split operator " + splitKey, 2059, PigException.BUG);
}

From source file:org.apache.tajo.engine.planner.LogicalPlanner.java

@Override
public LogicalNode visitHaving(PlanContext context, Stack<Expr> stack, Having expr) throws PlanningException {
    QueryBlock block = context.queryBlock;

    ExprNormalizedResult normalizedResult = normalizer.normalize(context, expr.getQual());
    String referName = block.namedExprsMgr.addExpr(normalizedResult.baseExpr);
    block.namedExprsMgr.addNamedExprArray(normalizedResult.aggExprs);
    block.namedExprsMgr.addNamedExprArray(normalizedResult.scalarExprs);

    ////////////////////////////////////////////////////////
    // Visit and Build Child Plan
    ////////////////////////////////////////////////////////
    stack.push(expr);
    LogicalNode child = visit(context, stack, expr.getChild());
    stack.pop();//w ww .  ja  v a 2s  . c om
    ////////////////////////////////////////////////////////

    HavingNode having = context.queryBlock.getNodeFromExpr(expr);
    having.setChild(child);
    having.setInSchema(child.getOutSchema());
    having.setOutSchema(child.getOutSchema());

    EvalNode havingCondition;
    if (block.namedExprsMgr.isEvaluated(referName)) {
        havingCondition = block.namedExprsMgr.getTarget(referName).getEvalTree();
    } else {
        NamedExpr namedExpr = block.namedExprsMgr.getNamedExpr(referName);
        havingCondition = exprAnnotator.createEvalNode(context, namedExpr.getExpr(),
                NameResolvingMode.SUBEXPRS_AND_RELS);
        block.namedExprsMgr.markAsEvaluated(referName, havingCondition);
    }

    // set having condition
    having.setQual(havingCondition);

    return having;
}