Advertisement
Guest User

Untitled

a guest
May 24th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.55 KB | None | 0 0
  1. package query;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.Queue;
  6.  
  7. import global.Minibase;
  8. import global.SortKey;
  9. import parser.AST_Select;
  10. import relop.FileScan;
  11. import relop.Predicate;
  12. import relop.Projection;
  13. import relop.Schema;
  14. import relop.Selection;
  15. import relop.SimpleJoin;
  16.  
  17. /**
  18.  * Execution plan for selecting tuples.
  19.  */
  20. class Select implements Plan {
  21.     /**
  22.      * Optimizes the plan, given the parsed query.
  23.      *
  24.      * @throws QueryException
  25.      *             if validation fails
  26.      */
  27.  
  28.     private Predicate[][] predicates;
  29. //  private SortKey[] order_keys;
  30.     private String[] tables;
  31.     private String[] projection_columns; // if size = 0 then it is *
  32.     private Projection root;
  33.  
  34.     final int LEAF = 1;
  35.     final int SELECT = 2;
  36.     final int JOIN = 3;
  37.     final int ROOT = 4;
  38.  
  39.     public Select(AST_Select tree) throws QueryException {
  40.         /*
  41.          * projection | selections(predicates)A | B / \ simple_join simple_join
  42.          * /\ /\
  43.          */
  44.  
  45.         predicates = tree.getPredicates();
  46. //      order_keys = tree.getOrders();
  47.         tables = tree.getTables();
  48.         projection_columns = tree.getColumns();
  49.  
  50.         // all we have to do is "pushing selects"
  51.         // so as the example in page 704 step b
  52.  
  53.         // so i think to construct a new tree for the execution
  54.  
  55.         build_execution_tree();
  56.  
  57.     } // public Select(AST_Select tree) throws QueryException
  58.  
  59.     private void build_execution_tree() {
  60.  
  61.         for (int i = 0; i < predicates.length; i++) {
  62.             for (int j = 0; j < predicates[i].length; j++) {
  63.                 for (int j2 = 0; j2 < tables.length; j2++) {
  64.                     Schema schema = Minibase.SystemCatalog
  65.                             .getSchema(tables[j2]);
  66.                     if (predicates[i][j].validate(schema)) {
  67.                         predicates[i][j].table_left = tables[j2];
  68.                     }
  69.  
  70.                     for (int k = 0; k < tables.length; k++) {
  71.                         Schema sc1 = Minibase.SystemCatalog
  72.                                 .getSchema(tables[j2]);
  73.                         Schema sc2 = Minibase.SystemCatalog
  74.                                 .getSchema(tables[k]);
  75.                         if (predicates[i][j].validate(sc1, sc2)) {
  76.                             predicates[i][j].table_left = tables[j2];
  77.                             predicates[i][j].table_right = tables[k];
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         ArrayList<ArrayList<Predicate>> pred = new ArrayList<ArrayList<Predicate>>();
  85.         for (int i = 0; i < predicates.length; i++) {
  86.             pred.add(new ArrayList<Predicate>());
  87.             for (int j = 0; j < predicates[i].length; j++) {
  88.                 pred.get(i).add(predicates[i][j]);
  89.             }
  90.         }
  91.  
  92.         Node _root = new Node(ROOT, projection_columns);
  93.         Node A = new Node(SELECT, pred);
  94.         _root.right = A;
  95.  
  96.         int levels = (int) (Math.ceil(Math.log(tables.length) / Math.log(2)));
  97.  
  98.         ArrayList<ArrayList<Node>> tree_levels = new ArrayList<ArrayList<Node>>();
  99.  
  100.         tree_levels.add(new ArrayList<Node>());
  101.         for (int i = 0; i < tables.length; i++) {
  102.             Node temp_node = new Node(LEAF, tables[i]);
  103.             temp_node.tables.add(tables[i]);
  104.             tree_levels.get(0).add(temp_node);
  105.  
  106.         }
  107.  
  108.         int t = 1;
  109.         while (t <= levels) {
  110.             tree_levels.add(new ArrayList<Node>());
  111.             ArrayList<Node> prev = tree_levels.get(t - 1);
  112.             for (int i = 0; i < prev.size();) {
  113.                 if (i + 1 < prev.size()) {
  114.                     Node temp1 = new Node(JOIN, null);
  115.                     temp1.left = prev.get(i);
  116.                     temp1.right = prev.get(i + 1);
  117.                     for (int j = 0; j < temp1.left.tables.size(); j++) {
  118.                         temp1.tables.add(temp1.left.tables.get(j));
  119.                     }
  120.                     for (int j = 0; j < temp1.right.tables.size(); j++) {
  121.                         temp1.tables.add(temp1.right.tables.get(j));
  122.                     }
  123.                     tree_levels.get(t).add(temp1);
  124.                     i += 2;
  125.                 } else {
  126.                     tree_levels.get(t).add(prev.get(i));
  127.                     i++;
  128.                 }
  129.             }
  130.             t++;
  131.         }
  132.  
  133.         Node B = tree_levels.get(levels - 1).get(0);
  134.         A.left = B;
  135.  
  136.         // Schema schema = Minibase.SystemCatalog.getSchema(tableName);
  137.         // pushing selection
  138.  
  139.         Queue<Node> bfs = new LinkedList<Node>();
  140.         bfs.add(A);
  141.  
  142.         while (bfs.isEmpty()) {
  143.             Node x = bfs.poll();
  144.  
  145.             if (x.type == SELECT && x.left.type == JOIN) {
  146.                 ArrayList<ArrayList<Predicate>> p = (ArrayList<ArrayList<Predicate>>) x.data;
  147.                 Node xjoin = x.left;
  148.  
  149.                 for (int i = 0; i < p.size();) {
  150.                     boolean[][] dir = new boolean[p.get(i).size()][2];
  151.                     for (int j = 0; j < p.get(i).size(); j++) {
  152.  
  153.                         ArrayList<String> names = xjoin.left.tables;
  154.                         boolean ch1 = p.get(i).get(j).table_left == null ? true
  155.                                 : false;
  156.                         boolean ch2 = p.get(i).get(j).table_right == null ? true
  157.                                 : false;
  158.  
  159.                         for (int k = 0; !ch1 && k < names.size(); k++) {
  160.                             if (names.get(k) == p.get(i).get(j).table_left)
  161.                                 ch1 = true;
  162.                         }
  163.                         for (int k = 0; !ch2 && k < names.size(); k++) {
  164.                             if (names.get(k) == p.get(i).get(j).table_right)
  165.                                 ch2 = true;
  166.                         }
  167.  
  168.                         dir[j][0] = ch1 & ch2;
  169.  
  170.                         names = xjoin.right.tables;
  171.                         ch1 = p.get(i).get(j).table_left == null ? true : false;
  172.                         ch2 = p.get(i).get(j).table_right == null ? true
  173.                                 : false;
  174.  
  175.                         for (int k = 0; !ch1 && k < names.size(); k++) {
  176.                             if (names.get(k) == p.get(i).get(j).table_left)
  177.                                 ch1 = true;
  178.                         }
  179.                         for (int k = 0; !ch2 && k < names.size(); k++) {
  180.                             if (names.get(k) == p.get(i).get(j).table_right)
  181.                                 ch2 = true;
  182.                         }
  183.  
  184.                         dir[j][1] = ch1 & ch2;
  185.  
  186.                     }
  187.                     boolean pushright = true, pushleft = true;
  188.                     for (int j = 0; j < dir.length; j++) {
  189.                         if (!dir[j][0])
  190.                             pushleft = false;
  191.                     }
  192.                     for (int j = 0; j < dir.length; j++) {
  193.                         if (!dir[j][1])
  194.                             pushright = false;
  195.                     }
  196.  
  197.                     if (pushright && !pushleft) {
  198.                         if (xjoin.right.type == JOIN
  199.                                 || xjoin.right.type == LEAF) {
  200.                             ArrayList<ArrayList<Predicate>> pushed_row = new ArrayList<ArrayList<Predicate>>();
  201.                             ArrayList<Predicate> row = new ArrayList<Predicate>();
  202.                             for (int j = 0; j < p.get(i).size(); j++) {
  203.                                 row.add(p.get(i).get(j));
  204.                             }
  205.                             p.remove(i); // remove the row after pushing
  206.  
  207.                             i--;
  208.  
  209.                             Node pushed_select = new Node(SELECT, pushed_row);
  210.                             pushed_select.tables = xjoin.right.tables;
  211.                             /* el mafrod neshel el row men el selection el aslia */
  212.                             pushed_select.left = xjoin.right;
  213.                             xjoin.right = pushed_select;
  214.  
  215.                         } else if (xjoin.right.type == SELECT) {
  216.  
  217.                             ArrayList<Predicate> row = new ArrayList<Predicate>();
  218.                             for (int j = 0; j < p.get(i).size(); j++) {
  219.                                 row.add(p.get(i).get(j));
  220.                             }
  221.                             ArrayList<ArrayList<Predicate>> sel_pred = (ArrayList<ArrayList<Predicate>>) xjoin.right.data;
  222.                             sel_pred.add(row);
  223.                             p.remove(i);
  224.  
  225.                             i--;
  226.  
  227.                         }
  228.                     } else if (!pushright && pushleft) {
  229.                         if (xjoin.left.type == JOIN || xjoin.left.type == LEAF) {
  230.                             ArrayList<ArrayList<Predicate>> pushed_row = new ArrayList<ArrayList<Predicate>>();
  231.                             ArrayList<Predicate> row = new ArrayList<Predicate>();
  232.                             for (int j = 0; j < p.get(i).size(); j++) {
  233.                                 row.add(p.get(i).get(j));
  234.                             }
  235.                             p.remove(i); // remove the row after pushing
  236.  
  237.                             i--;
  238.  
  239.                             Node pushed_select = new Node(SELECT, pushed_row);
  240.                             pushed_select.tables = xjoin.right.tables;
  241.  
  242.                             pushed_select.left = xjoin.right;
  243.                             xjoin.left = pushed_select;
  244.  
  245.                         } else if (xjoin.left.type == SELECT) {
  246.  
  247.                             ArrayList<Predicate> row = new ArrayList<Predicate>();
  248.                             for (int j = 0; j < p.get(i).size(); j++) {
  249.                                 row.add(p.get(i).get(j));
  250.                             }
  251.                             ArrayList<ArrayList<Predicate>> sel_pred = (ArrayList<ArrayList<Predicate>>) xjoin.left.data;
  252.                             sel_pred.add(row);
  253.                             p.remove(i);
  254.  
  255.                             i--;
  256.  
  257.                         }
  258.                     } else {
  259.                         i++;
  260.                     }
  261.  
  262.                 }
  263.             }
  264.             if (x.left != null)
  265.                 bfs.add(x.left);
  266.             if (x.right != null)
  267.                 bfs.add(x.right);
  268.  
  269.         }
  270.        
  271.         // nebda3 ba2a w ne3mel el iterators :"D
  272.        
  273.         DFS(A);
  274.         root = new Projection(A.itr, getColumnsIndex());
  275.        
  276.        
  277.  
  278.     }
  279.    
  280.     private Integer[] getColumnsIndex() {
  281.         return null;
  282.     }
  283.    
  284.    
  285.     private void DFS(Node n) {
  286.        
  287.         if(n.type==SELECT) {
  288.             Predicate[][] pred = n.getPredicates();
  289.             Selection select;
  290.             if(n.left.type==LEAF) {
  291.                 FileScan scan = new FileScan(Minibase.SystemCatalog.getSchema((String)n.left.data), Minibase.SystemCatalog.f_rel);
  292.                 select = new Selection(scan, null/*pred*/);
  293.                 n.itr = select;
  294.             } else {
  295.                 // it will be join
  296.                 DFS(n.left);
  297.                 select = new Selection(n.left.itr, null/*pred*/);
  298.                 n.itr = select;
  299.             }
  300.         } else if (n.type==JOIN) {
  301.             SimpleJoin simpleJoin ;
  302.             if(n.left.type==LEAF&&n.right.type==LEAF) {
  303.                 FileScan scan1 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.left.data), Minibase.SystemCatalog.f_rel);
  304.                 FileScan scan2 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.right.data), Minibase.SystemCatalog.f_rel);
  305.                 simpleJoin = new SimpleJoin(scan1, scan2, null);
  306.                 n.itr = simpleJoin;
  307.             } else if (n.left.type!=LEAF&&n.right.type==LEAF) {
  308.                 FileScan scan2 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.right.data), Minibase.SystemCatalog.f_rel);
  309.                 DFS(n.left);
  310.                 simpleJoin = new SimpleJoin(n.left.itr, scan2, null);
  311.                 n.itr = simpleJoin;
  312.             } else if(n.left.type==LEAF&&n.right.type!=LEAF) {
  313.                 FileScan scan1 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.left.data), Minibase.SystemCatalog.f_rel);
  314.                 DFS(n.right);
  315.                 simpleJoin = new SimpleJoin(scan1, n.right.itr, null);
  316.                 n.itr = simpleJoin;
  317.             } else if(n.left.type!=LEAF&&n.right.type!=LEAF) {
  318.                 DFS(n.left);
  319.                 DFS(n.right);
  320.                 simpleJoin = new SimpleJoin(n.left.itr, n.right.itr, null);
  321.                 n.itr = simpleJoin;
  322.             }
  323.         }
  324.        
  325.     }
  326.    
  327.    
  328.  
  329.     /**
  330.      * Executes the plan and prints applicable output.
  331.      */
  332.     public void execute() {
  333.  
  334.         root.execute();
  335.  
  336.     } // public void execute()
  337.  
  338. } // class Select implements Plan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement