Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package query;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Queue;
- import global.Minibase;
- import global.SortKey;
- import parser.AST_Select;
- import relop.FileScan;
- import relop.Predicate;
- import relop.Projection;
- import relop.Schema;
- import relop.Selection;
- import relop.SimpleJoin;
- /**
- * Execution plan for selecting tuples.
- */
- class Select implements Plan {
- /**
- * Optimizes the plan, given the parsed query.
- *
- * @throws QueryException
- * if validation fails
- */
- private Predicate[][] predicates;
- // private SortKey[] order_keys;
- private String[] tables;
- private String[] projection_columns; // if size = 0 then it is *
- private Projection root;
- final int LEAF = 1;
- final int SELECT = 2;
- final int JOIN = 3;
- final int ROOT = 4;
- public Select(AST_Select tree) throws QueryException {
- /*
- * projection | selections(predicates)A | B / \ simple_join simple_join
- * /\ /\
- */
- predicates = tree.getPredicates();
- // order_keys = tree.getOrders();
- tables = tree.getTables();
- projection_columns = tree.getColumns();
- // all we have to do is "pushing selects"
- // so as the example in page 704 step b
- // so i think to construct a new tree for the execution
- build_execution_tree();
- } // public Select(AST_Select tree) throws QueryException
- private void build_execution_tree() {
- for (int i = 0; i < predicates.length; i++) {
- for (int j = 0; j < predicates[i].length; j++) {
- for (int j2 = 0; j2 < tables.length; j2++) {
- Schema schema = Minibase.SystemCatalog
- .getSchema(tables[j2]);
- if (predicates[i][j].validate(schema)) {
- predicates[i][j].table_left = tables[j2];
- }
- for (int k = 0; k < tables.length; k++) {
- Schema sc1 = Minibase.SystemCatalog
- .getSchema(tables[j2]);
- Schema sc2 = Minibase.SystemCatalog
- .getSchema(tables[k]);
- if (predicates[i][j].validate(sc1, sc2)) {
- predicates[i][j].table_left = tables[j2];
- predicates[i][j].table_right = tables[k];
- }
- }
- }
- }
- }
- ArrayList<ArrayList<Predicate>> pred = new ArrayList<ArrayList<Predicate>>();
- for (int i = 0; i < predicates.length; i++) {
- pred.add(new ArrayList<Predicate>());
- for (int j = 0; j < predicates[i].length; j++) {
- pred.get(i).add(predicates[i][j]);
- }
- }
- Node _root = new Node(ROOT, projection_columns);
- Node A = new Node(SELECT, pred);
- _root.right = A;
- int levels = (int) (Math.ceil(Math.log(tables.length) / Math.log(2)));
- ArrayList<ArrayList<Node>> tree_levels = new ArrayList<ArrayList<Node>>();
- tree_levels.add(new ArrayList<Node>());
- for (int i = 0; i < tables.length; i++) {
- Node temp_node = new Node(LEAF, tables[i]);
- temp_node.tables.add(tables[i]);
- tree_levels.get(0).add(temp_node);
- }
- int t = 1;
- while (t <= levels) {
- tree_levels.add(new ArrayList<Node>());
- ArrayList<Node> prev = tree_levels.get(t - 1);
- for (int i = 0; i < prev.size();) {
- if (i + 1 < prev.size()) {
- Node temp1 = new Node(JOIN, null);
- temp1.left = prev.get(i);
- temp1.right = prev.get(i + 1);
- for (int j = 0; j < temp1.left.tables.size(); j++) {
- temp1.tables.add(temp1.left.tables.get(j));
- }
- for (int j = 0; j < temp1.right.tables.size(); j++) {
- temp1.tables.add(temp1.right.tables.get(j));
- }
- tree_levels.get(t).add(temp1);
- i += 2;
- } else {
- tree_levels.get(t).add(prev.get(i));
- i++;
- }
- }
- t++;
- }
- Node B = tree_levels.get(levels - 1).get(0);
- A.left = B;
- // Schema schema = Minibase.SystemCatalog.getSchema(tableName);
- // pushing selection
- Queue<Node> bfs = new LinkedList<Node>();
- bfs.add(A);
- while (bfs.isEmpty()) {
- Node x = bfs.poll();
- if (x.type == SELECT && x.left.type == JOIN) {
- ArrayList<ArrayList<Predicate>> p = (ArrayList<ArrayList<Predicate>>) x.data;
- Node xjoin = x.left;
- for (int i = 0; i < p.size();) {
- boolean[][] dir = new boolean[p.get(i).size()][2];
- for (int j = 0; j < p.get(i).size(); j++) {
- ArrayList<String> names = xjoin.left.tables;
- boolean ch1 = p.get(i).get(j).table_left == null ? true
- : false;
- boolean ch2 = p.get(i).get(j).table_right == null ? true
- : false;
- for (int k = 0; !ch1 && k < names.size(); k++) {
- if (names.get(k) == p.get(i).get(j).table_left)
- ch1 = true;
- }
- for (int k = 0; !ch2 && k < names.size(); k++) {
- if (names.get(k) == p.get(i).get(j).table_right)
- ch2 = true;
- }
- dir[j][0] = ch1 & ch2;
- names = xjoin.right.tables;
- ch1 = p.get(i).get(j).table_left == null ? true : false;
- ch2 = p.get(i).get(j).table_right == null ? true
- : false;
- for (int k = 0; !ch1 && k < names.size(); k++) {
- if (names.get(k) == p.get(i).get(j).table_left)
- ch1 = true;
- }
- for (int k = 0; !ch2 && k < names.size(); k++) {
- if (names.get(k) == p.get(i).get(j).table_right)
- ch2 = true;
- }
- dir[j][1] = ch1 & ch2;
- }
- boolean pushright = true, pushleft = true;
- for (int j = 0; j < dir.length; j++) {
- if (!dir[j][0])
- pushleft = false;
- }
- for (int j = 0; j < dir.length; j++) {
- if (!dir[j][1])
- pushright = false;
- }
- if (pushright && !pushleft) {
- if (xjoin.right.type == JOIN
- || xjoin.right.type == LEAF) {
- ArrayList<ArrayList<Predicate>> pushed_row = new ArrayList<ArrayList<Predicate>>();
- ArrayList<Predicate> row = new ArrayList<Predicate>();
- for (int j = 0; j < p.get(i).size(); j++) {
- row.add(p.get(i).get(j));
- }
- p.remove(i); // remove the row after pushing
- i--;
- Node pushed_select = new Node(SELECT, pushed_row);
- pushed_select.tables = xjoin.right.tables;
- /* el mafrod neshel el row men el selection el aslia */
- pushed_select.left = xjoin.right;
- xjoin.right = pushed_select;
- } else if (xjoin.right.type == SELECT) {
- ArrayList<Predicate> row = new ArrayList<Predicate>();
- for (int j = 0; j < p.get(i).size(); j++) {
- row.add(p.get(i).get(j));
- }
- ArrayList<ArrayList<Predicate>> sel_pred = (ArrayList<ArrayList<Predicate>>) xjoin.right.data;
- sel_pred.add(row);
- p.remove(i);
- i--;
- }
- } else if (!pushright && pushleft) {
- if (xjoin.left.type == JOIN || xjoin.left.type == LEAF) {
- ArrayList<ArrayList<Predicate>> pushed_row = new ArrayList<ArrayList<Predicate>>();
- ArrayList<Predicate> row = new ArrayList<Predicate>();
- for (int j = 0; j < p.get(i).size(); j++) {
- row.add(p.get(i).get(j));
- }
- p.remove(i); // remove the row after pushing
- i--;
- Node pushed_select = new Node(SELECT, pushed_row);
- pushed_select.tables = xjoin.right.tables;
- pushed_select.left = xjoin.right;
- xjoin.left = pushed_select;
- } else if (xjoin.left.type == SELECT) {
- ArrayList<Predicate> row = new ArrayList<Predicate>();
- for (int j = 0; j < p.get(i).size(); j++) {
- row.add(p.get(i).get(j));
- }
- ArrayList<ArrayList<Predicate>> sel_pred = (ArrayList<ArrayList<Predicate>>) xjoin.left.data;
- sel_pred.add(row);
- p.remove(i);
- i--;
- }
- } else {
- i++;
- }
- }
- }
- if (x.left != null)
- bfs.add(x.left);
- if (x.right != null)
- bfs.add(x.right);
- }
- // nebda3 ba2a w ne3mel el iterators :"D
- DFS(A);
- root = new Projection(A.itr, getColumnsIndex());
- }
- private Integer[] getColumnsIndex() {
- return null;
- }
- private void DFS(Node n) {
- if(n.type==SELECT) {
- Predicate[][] pred = n.getPredicates();
- Selection select;
- if(n.left.type==LEAF) {
- FileScan scan = new FileScan(Minibase.SystemCatalog.getSchema((String)n.left.data), Minibase.SystemCatalog.f_rel);
- select = new Selection(scan, null/*pred*/);
- n.itr = select;
- } else {
- // it will be join
- DFS(n.left);
- select = new Selection(n.left.itr, null/*pred*/);
- n.itr = select;
- }
- } else if (n.type==JOIN) {
- SimpleJoin simpleJoin ;
- if(n.left.type==LEAF&&n.right.type==LEAF) {
- FileScan scan1 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.left.data), Minibase.SystemCatalog.f_rel);
- FileScan scan2 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.right.data), Minibase.SystemCatalog.f_rel);
- simpleJoin = new SimpleJoin(scan1, scan2, null);
- n.itr = simpleJoin;
- } else if (n.left.type!=LEAF&&n.right.type==LEAF) {
- FileScan scan2 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.right.data), Minibase.SystemCatalog.f_rel);
- DFS(n.left);
- simpleJoin = new SimpleJoin(n.left.itr, scan2, null);
- n.itr = simpleJoin;
- } else if(n.left.type==LEAF&&n.right.type!=LEAF) {
- FileScan scan1 = new FileScan(Minibase.SystemCatalog.getSchema((String)n.left.data), Minibase.SystemCatalog.f_rel);
- DFS(n.right);
- simpleJoin = new SimpleJoin(scan1, n.right.itr, null);
- n.itr = simpleJoin;
- } else if(n.left.type!=LEAF&&n.right.type!=LEAF) {
- DFS(n.left);
- DFS(n.right);
- simpleJoin = new SimpleJoin(n.left.itr, n.right.itr, null);
- n.itr = simpleJoin;
- }
- }
- }
- /**
- * Executes the plan and prints applicable output.
- */
- public void execute() {
- root.execute();
- } // public void execute()
- } // class Select implements Plan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement