Search in sources :

Example 96 with ArrayDeque

use of java.util.ArrayDeque in project suite by stupidsing.

the class InstructionExtractor method extractInstructions.

private void extractInstructions(Node snippet, List<List<Node>> rsList) {
    Deque<Node> deque = new ArrayDeque<>();
    deque.add(snippet);
    Tree tree;
    Node value;
    while (!deque.isEmpty()) if ((tree = Tree.decompose(deque.pop(), TermOp.AND___)) != null) {
        IdentityKey<Node> key = IdentityKey.of(tree);
        Integer ip = ipByLabelId.get(key);
        if (ip == null) {
            ipByLabelId.put(key, ip = rsList.size());
            List<Node> rs = tupleToList(tree.getLeft());
            if (rs.get(0) == FRAME)
                if ((value = label(rs.get(1))) != null) {
                    rsList.add(List.of(Atom.of("FRAME-BEGIN")));
                    extractInstructions(value, rsList);
                    rsList.add(List.of(Atom.of("FRAME-END")));
                } else
                    Fail.t("bad frame definition");
            else {
                rsList.add(rs);
                for (Node op : List_.right(rs, 1)) if ((value = label(op)) != null)
                    deque.push(value);
                deque.push(tree.getRight());
            }
        } else
            rsList.add(List.of(Atom.of("JUMP"), Int.of(ip)));
    }
}
Also used : IdentityKey(suite.adt.IdentityKey) Node(suite.node.Node) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) List(java.util.List) ArrayDeque(java.util.ArrayDeque)

Example 97 with ArrayDeque

use of java.util.ArrayDeque in project suite by stupidsing.

the class Grapher method bind.

public static boolean bind(Node n0, Node n1, Trail trail) {
    Map<IdentityKey<Node>, Integer> mapn0 = new HashMap<>();
    Map<IdentityKey<Node>, Integer> mapn1 = new HashMap<>();
    Grapher g0 = new Grapher();
    Grapher g1 = new Grapher();
    g0.id = g0.graph_(mapn0, n0);
    g1.id = g1.graph_(mapn1, n1);
    IntObjMap<IdentityKey<Node>> mapi0 = new IntObjMap<>();
    IntObjMap<IdentityKey<Node>> mapi1 = new IntObjMap<>();
    for (Entry<IdentityKey<Node>, Integer> e : mapn0.entrySet()) mapi0.put(e.getValue(), e.getKey());
    for (Entry<IdentityKey<Node>, Integer> e : mapn1.entrySet()) mapi1.put(e.getValue(), e.getKey());
    Set<IntIntPair> set = new HashSet<>();
    Deque<IntIntPair> deque = new ArrayDeque<>();
    deque.add(IntIntPair.of(g0.id, g1.id));
    IntIntPair pair;
    while ((pair = deque.pollLast()) != null) if (set.add(pair)) {
        GN gn0 = g0.gns.get(pair.t0);
        GN gn1 = g1.gns.get(pair.t1);
        if (// 
        gn0.type == ReadType.TERM && // 
        gn0.terminal instanceof Reference && Binder.bind(gn0.terminal, mapi1.get(pair.t1).key, trail))
            ;
        else if (// 
        gn1.type == ReadType.TERM && // 
        gn1.terminal instanceof Reference && Binder.bind(gn1.terminal, mapi0.get(pair.t0).key, trail))
            ;
        else if (gn0.type == gn1.type && Objects.equals(gn0.terminal, gn1.terminal) && gn0.op == gn1.op) {
            List<IntIntPair> children0 = gn0.children;
            List<IntIntPair> children1 = gn1.children;
            int size0 = children0.size();
            int size1 = children1.size();
            if (size0 == size1)
                for (int i = 0; i < size0; i++) {
                    IntIntPair p0 = children0.get(i);
                    IntIntPair p1 = children1.get(i);
                    deque.addLast(IntIntPair.of(p0.t0, p1.t0));
                    deque.addLast(IntIntPair.of(p0.t1, p1.t1));
                }
            else
                return false;
        } else
            return false;
    }
    return true;
}
Also used : IdentityKey(suite.adt.IdentityKey) HashMap(java.util.HashMap) Reference(suite.node.Reference) IntObjMap(suite.primitive.adt.map.IntObjMap) ArrayDeque(java.util.ArrayDeque) ArrayList(java.util.ArrayList) List(java.util.List) IntIntPair(suite.primitive.adt.pair.IntIntPair) HashSet(java.util.HashSet)

Example 98 with ArrayDeque

use of java.util.ArrayDeque in project hive by apache.

the class SemanticAnalyzer method processPositionAlias.

// Process the position alias in GROUPBY and ORDERBY
public void processPositionAlias(ASTNode ast) throws SemanticException {
    boolean isBothByPos = HiveConf.getBoolVar(conf, ConfVars.HIVE_GROUPBY_ORDERBY_POSITION_ALIAS);
    boolean isGbyByPos = isBothByPos || HiveConf.getBoolVar(conf, ConfVars.HIVE_GROUPBY_POSITION_ALIAS);
    boolean isObyByPos = isBothByPos || HiveConf.getBoolVar(conf, ConfVars.HIVE_ORDERBY_POSITION_ALIAS);
    Deque<ASTNode> stack = new ArrayDeque<ASTNode>();
    stack.push(ast);
    while (!stack.isEmpty()) {
        ASTNode next = stack.pop();
        if (next.getChildCount() == 0) {
            continue;
        }
        boolean isAllCol;
        ASTNode selectNode = null;
        ASTNode groupbyNode = null;
        ASTNode orderbyNode = null;
        // get node type
        int child_count = next.getChildCount();
        for (int child_pos = 0; child_pos < child_count; ++child_pos) {
            ASTNode node = (ASTNode) next.getChild(child_pos);
            int type = node.getToken().getType();
            if (type == HiveParser.TOK_SELECT || type == HiveParser.TOK_SELECTDI) {
                selectNode = node;
            } else if (type == HiveParser.TOK_GROUPBY) {
                groupbyNode = node;
            } else if (type == HiveParser.TOK_ORDERBY) {
                orderbyNode = node;
            }
        }
        if (selectNode != null) {
            int selectExpCnt = selectNode.getChildCount();
            // replace each of the position alias in GROUPBY with the actual column name
            if (groupbyNode != null) {
                for (int child_pos = 0; child_pos < groupbyNode.getChildCount(); ++child_pos) {
                    ASTNode node = (ASTNode) groupbyNode.getChild(child_pos);
                    if (node.getToken().getType() == HiveParser.Number) {
                        if (isGbyByPos) {
                            int pos = Integer.parseInt(node.getText());
                            if (pos > 0 && pos <= selectExpCnt) {
                                groupbyNode.setChild(child_pos, selectNode.getChild(pos - 1).getChild(0));
                            } else {
                                throw new SemanticException(ErrorMsg.INVALID_POSITION_ALIAS_IN_GROUPBY.getMsg("Position alias: " + pos + " does not exist\n" + "The Select List is indexed from 1 to " + selectExpCnt));
                            }
                        } else {
                            warn("Using constant number  " + node.getText() + " in group by. If you try to use position alias when hive.groupby.position.alias is false, the position alias will be ignored.");
                        }
                    }
                }
            }
            // if cbo is enabled, orderby position will be processed in genPlan
            if (!HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_CBO_ENABLED) && orderbyNode != null) {
                isAllCol = false;
                for (int child_pos = 0; child_pos < selectNode.getChildCount(); ++child_pos) {
                    ASTNode node = (ASTNode) selectNode.getChild(child_pos).getChild(0);
                    if (node != null && node.getToken().getType() == HiveParser.TOK_ALLCOLREF) {
                        isAllCol = true;
                    }
                }
                for (int child_pos = 0; child_pos < orderbyNode.getChildCount(); ++child_pos) {
                    ASTNode colNode = null;
                    ASTNode node = null;
                    if (orderbyNode.getChildCount() > 0) {
                        colNode = (ASTNode) orderbyNode.getChild(child_pos).getChild(0);
                        if (colNode.getChildCount() > 0) {
                            node = (ASTNode) colNode.getChild(0);
                        }
                    }
                    if (node != null && node.getToken().getType() == HiveParser.Number) {
                        if (isObyByPos) {
                            if (!isAllCol) {
                                int pos = Integer.parseInt(node.getText());
                                if (pos > 0 && pos <= selectExpCnt && selectNode.getChild(pos - 1).getChildCount() > 0) {
                                    colNode.setChild(0, selectNode.getChild(pos - 1).getChild(0));
                                } else {
                                    throw new SemanticException(ErrorMsg.INVALID_POSITION_ALIAS_IN_ORDERBY.getMsg("Position alias: " + pos + " does not exist\n" + "The Select List is indexed from 1 to " + selectExpCnt));
                                }
                            } else {
                                throw new SemanticException(ErrorMsg.NO_SUPPORTED_ORDERBY_ALLCOLREF_POS.getMsg());
                            }
                        } else {
                            // if not using position alias and it is a number.
                            warn("Using constant number " + node.getText() + " in order by. If you try to use position alias when hive.orderby.position.alias is false, the position alias will be ignored.");
                        }
                    }
                }
            }
        }
        for (int i = next.getChildren().size() - 1; i >= 0; i--) {
            stack.push((ASTNode) next.getChildren().get(i));
        }
    }
}
Also used : ArrayDeque(java.util.ArrayDeque) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) CheckConstraint(org.apache.hadoop.hive.ql.metadata.CheckConstraint) NotNullConstraint(org.apache.hadoop.hive.ql.metadata.NotNullConstraint) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) DefaultConstraint(org.apache.hadoop.hive.ql.metadata.DefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)

Example 99 with ArrayDeque

use of java.util.ArrayDeque in project carbon-apimgt by wso2.

the class MySQLScriptValidationTestCase method testCharacterSetUTF8.

@Test(description = "Test if UTF8 character set has been applied to all tables in script")
public void testCharacterSetUTF8() throws Exception {
    try (InputStream is = new FileInputStream(dbscriptPath);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8"))) {
        final String createTableSyntax = "CREATE TABLE";
        final char openingBrace = '(';
        final String utf8CharSet = "CHARACTER SET utf8 COLLATE utf8_general_ci";
        final String endOfStatement = ";";
        Deque<String> tables = new ArrayDeque<>();
        Map<String, Boolean> tableCharsetUTF8 = new HashMap<>();
        boolean isCharsetDetected = false;
        boolean isTableDetected = false;
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(createTableSyntax)) {
                int startOfCreateTable = line.indexOf(createTableSyntax);
                int startOfOpeningBrace = line.indexOf(openingBrace);
                tables.add(line.substring(startOfCreateTable + createTableSyntax.length(), startOfOpeningBrace).trim());
                isTableDetected = true;
            }
            if (line.contains(utf8CharSet) && isTableDetected) {
                isCharsetDetected = true;
            }
            if (line.contains(endOfStatement)) {
                if (isTableDetected) {
                    tableCharsetUTF8.put(tables.getLast(), isCharsetDetected);
                }
                isCharsetDetected = false;
                isTableDetected = false;
            }
        }
        for (Map.Entry<String, Boolean> entry : tableCharsetUTF8.entrySet()) {
            Assert.assertTrue(entry.getValue(), "Table " + entry.getKey() + " has been defined without " + utf8CharSet + " in mysql db script");
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) ArrayDeque(java.util.ArrayDeque) BufferedReader(java.io.BufferedReader) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 100 with ArrayDeque

use of java.util.ArrayDeque in project ignite by apache.

the class GridCircularQueueTest method testQueue.

/**
 */
public void testQueue() {
    GridCacheQueryManager.CircularQueue<Integer> q = new GridCacheQueryManager.CircularQueue<>(4);
    ArrayDeque<Integer> d = new ArrayDeque<>();
    for (int i = 0; i < 10; i++) {
        q.add(i);
        d.add(i);
    }
    check(q, d);
    q.remove(4);
    remove(d, 4);
    check(q, d);
    for (int i = 100; i < 110; i++) {
        q.add(i);
        d.add(i);
    }
    check(q, d);
    int size = q.size();
    q.remove(size);
    remove(d, size);
    check(q, d);
    assertEquals(0, q.size());
    GridRandom rnd = new GridRandom();
    for (int i = 0; i < 15000; i++) {
        switch(rnd.nextInt(2)) {
            case 1:
                if (q.size() > 0) {
                    int cnt = 1;
                    if (q.size() > 1)
                        cnt += rnd.nextInt(q.size() - 1);
                    q.remove(cnt);
                    remove(d, cnt);
                    break;
                }
            case 0:
                int cnt = rnd.nextInt(50);
                for (int j = 0; j < cnt; j++) {
                    int x = rnd.nextInt();
                    q.add(x);
                    d.add(x);
                }
                break;
        }
        check(q, d);
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) ArrayDeque(java.util.ArrayDeque)

Aggregations

ArrayDeque (java.util.ArrayDeque)217 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)36 IOException (java.io.IOException)27 HashMap (java.util.HashMap)23 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)17 Deque (java.util.Deque)11 Iterator (java.util.Iterator)10 NoSuchElementException (java.util.NoSuchElementException)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 File (java.io.File)7 Path (java.nio.file.Path)7 Random (java.util.Random)7 ByteBuffer (java.nio.ByteBuffer)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpFields (org.eclipse.jetty.http.HttpFields)5 Name (com.github.anba.es6draft.ast.scope.Name)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4