├── README.md └── CSE562 ├── src └── edu │ └── buffalo │ └── cse562 │ ├── jsqlparser.jar │ ├── Miscellaneous │ ├── JoinDetails.java │ ├── OrderDetails.java │ ├── IndexScanOperation.java │ ├── CustomSecondaryKeyExtractor.java │ ├── SpecialStringComparator.java │ └── IntegerSet.java │ ├── Evaluators │ ├── TablePriorityQueueComparator.java │ ├── OrderbyComparator.java │ ├── ConditionalEvaluators.java │ ├── ArithmeticEvaluator.java │ └── RelationalEvaluator.java │ ├── Main.java │ ├── QueryHandler │ ├── Relation.java │ ├── fieldDetails.java │ ├── TupleSerializer.java │ ├── Tuple.java │ ├── Indexer.java │ └── SQLTable.java │ ├── Operators │ ├── SelectNode.java │ ├── CartesianProductNode.java │ ├── OrderbyNode.java │ ├── ProjectNode.java │ ├── ASTNode.java │ ├── EvaluatorNode.java │ ├── ScannerNode.java │ ├── IndexScanner.java │ ├── JoinNode.java │ └── GroupingNode.java │ ├── IOHandler │ ├── FileHandler.java │ ├── IndexBuilder.java │ └── Parser.java │ └── AbstractExpressionVisitor.java └── Extra code ├── IntegerComparator.java ├── temp.java ├── ExpressionExample.java ├── IndexScanner.java ├── Evaluator.java ├── Extra1 ├── testerMotaBhai.java └── testerBhai.java ├── TestIndex.java └── newclass.java /README.md: -------------------------------------------------------------------------------- 1 | Query-Evaluator 2 | =============== 3 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/jsqlparser.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/router/Query-Evaluator/master/CSE562/src/edu/buffalo/cse562/jsqlparser.jar -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Miscellaneous/JoinDetails.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Miscellaneous; 2 | 3 | public class JoinDetails { 4 | public String table; 5 | public String key; 6 | public JoinDetails(String t,String k) 7 | { 8 | table=t; 9 | key=k; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Miscellaneous/OrderDetails.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Miscellaneous; 2 | 3 | public class OrderDetails { 4 | public String field; 5 | public String orderType; 6 | public OrderDetails(String t,String k) 7 | { 8 | field = t; 9 | orderType = k; 10 | } 11 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Miscellaneous/IndexScanOperation.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Miscellaneous; 2 | 3 | public class IndexScanOperation { 4 | public String key; 5 | public ValueType lowerLimit; 6 | public ValueType upperLimit; 7 | public boolean lowerInclusive; 8 | public boolean upperInclusive; 9 | public String type; 10 | 11 | 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Evaluators/TablePriorityQueueComparator.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Evaluators; 2 | 3 | import java.util.Comparator; 4 | 5 | import edu.buffalo.cse562.QueryHandler.SQLTable; 6 | 7 | public class TablePriorityQueueComparator implements Comparator { 8 | 9 | public int compare(SQLTable t1, SQLTable t2) { 10 | return (int) (t1.getTupleCount() - t2.getTupleCount()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CSE562/Extra code/IntegerComparator.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.Comparator; 6 | 7 | public class IntegerSet implements Serializable,Comparator{ 8 | 9 | 10 | public ArrayList list; 11 | 12 | @Override 13 | public int compare(Integer o1, Integer o2) { 14 | // TODO Auto-generated method stub 15 | return ((Integer)o1).compareTo(o2); 16 | } 17 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Miscellaneous/CustomSecondaryKeyExtractor.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Miscellaneous; 2 | 3 | import jdbm.SecondaryKeyExtractor; 4 | import edu.buffalo.cse562.QueryHandler.Tuple; 5 | 6 | public class CustomSecondaryKeyExtractor implements SecondaryKeyExtractor 7 | { 8 | private String fKey; 9 | public CustomSecondaryKeyExtractor(String key) 10 | { 11 | fKey=key; 12 | } 13 | public ForeignKeyType extractSecondaryKey(Object key,Object value) 14 | { 15 | // TODO Auto-generated method stub 16 | return (ForeignKeyType)((Tuple)value).valueForField(fKey); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Miscellaneous/SpecialStringComparator.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Miscellaneous; 2 | 3 | import java.io.Serializable; 4 | import java.util.Comparator; 5 | 6 | public class SpecialStringComparator implements Comparator,Serializable{ 7 | 8 | @Override 9 | public int compare(String o1, String o2) { 10 | // TODO Auto-generated method stub 11 | String[] str1 = o1.split("#"); 12 | String[] str2 = o2.split("#"); 13 | int val1 = Integer.parseInt(str1[0]); 14 | int val2 = Integer.parseInt(str1[1]); 15 | int val3 = Integer.parseInt(str2[0]); 16 | int val4 = Integer.parseInt(str2[1]); 17 | 18 | if(val1 != val3) 19 | return val1 - val3; 20 | 21 | return val2 - val4; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Main.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import edu.buffalo.cse562.IOHandler.Parser; 4 | import edu.buffalo.cse562.Operators.ASTNode; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | //CHK0 11 | //System.out.println("We, the members of our team, agree that we will not submit any code that we have not written ourselves, share our code with anyone outside of our group, or use code that we have not written ourselves as a reference."); 12 | ASTNode.startTime = System.currentTimeMillis(); 13 | Parser parser=new Parser(); 14 | parser.parseCmdLineArguments(args); 15 | // long endTime = System.currentTimeMillis(); 16 | // long totalTime = endTime - ASTNode.startTime; 17 | // System.out.println(totalTime/100); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/QueryHandler/Relation.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.QueryHandler; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Relation { 6 | 7 | private ArrayList tupleSet; 8 | public Relation() 9 | { 10 | tupleSet= new ArrayList(); 11 | } 12 | 13 | public int tupleCount() 14 | { 15 | return tupleSet.size(); 16 | } 17 | public ArrayList getTuples() 18 | { 19 | return tupleSet; 20 | } 21 | public void addTuple(Tuple t) 22 | { 23 | tupleSet.add(t); 24 | } 25 | public void addTuples(ArrayList t) 26 | { 27 | tupleSet.addAll(t); 28 | // Check if tupleset size if exceeding the memory limit. If yes append to the member file fileToDump and flush remove all contents from arraylist 29 | } 30 | 31 | public void clearRelation() 32 | { 33 | tupleSet.clear(); 34 | System.gc(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CSE562/Extra code/temp.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.StringReader; 4 | import java.util.Iterator; 5 | 6 | import net.sf.jsqlparser.JSQLParserException; 7 | import net.sf.jsqlparser.expression.Expression; 8 | import net.sf.jsqlparser.parser.*; 9 | import net.sf.jsqlparser.statement.Statement; 10 | import net.sf.jsqlparser.statement.StatementVisitor; 11 | import net.sf.jsqlparser.statement.create.table.CreateTable; 12 | import net.sf.jsqlparser.statement.select.Select; 13 | 14 | public class temp 15 | { 16 | public static void main(String[] args) throws ParseException, JSQLParserException 17 | { 18 | //String sql = "CREATE TABLE example (id INT, data VARCHAR(100));"; 19 | String sql = "SELECT COLUMN_1, COLUMN_2 FROM MY_TABLE_1 JOIN MY_TABLE_2 JOIN MY_TABLE_3 WHERE COLUMN_1 = 'CONDITION_1'"; 20 | //String sql = "SELECT fname, lname, addr FROM prospect UNION SELECT first_name, last_name, address FROM customer UNION SELECT company, '', street FROM vendor;"; 21 | Statement statement = parseSQLExpression(sql); 22 | Evaluator temp = new Evaluator(statement); 23 | temp.Statement(); 24 | } 25 | 26 | public static Statement parseSQLExpression(String exprStr) throws ParseException, JSQLParserException 27 | { 28 | CCJSqlParserManager pm = new CCJSqlParserManager(); 29 | return pm.parse(new StringReader(exprStr)); 30 | } 31 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/SelectNode.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import net.sf.jsqlparser.expression.Expression; 4 | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; 5 | import net.sf.jsqlparser.expression.operators.conditional.OrExpression; 6 | import edu.buffalo.cse562.Evaluators.ConditionalEvaluators; 7 | import edu.buffalo.cse562.Evaluators.RelationalEvaluator; 8 | import edu.buffalo.cse562.QueryHandler.*; 9 | 10 | public class SelectNode extends ASTNode { 11 | 12 | void setInput(Relation input) {} 13 | public Relation evaluateOnPredicate(Relation input, Expression filter) 14 | { 15 | if(filter == null) 16 | return input; 17 | else 18 | { 19 | Relation output=new Relation(); 20 | for(Tuple t: input.getTuples()) 21 | { 22 | if(filterTuple(t, filter)) 23 | output.addTuple(t); 24 | } 25 | return output; 26 | } 27 | } 28 | 29 | public boolean filterTuple(Tuple t, Expression exp) // true if passes and false otherwise 30 | { 31 | if(exp instanceof AndExpression || exp instanceof OrExpression) 32 | { 33 | ConditionalEvaluators c = new ConditionalEvaluators(t); 34 | exp.accept(c); 35 | return c.getResult(); 36 | } 37 | else 38 | { 39 | RelationalEvaluator r=new RelationalEvaluator(t); 40 | exp.accept(r); 41 | return r.getResult(); 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/QueryHandler/fieldDetails.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.QueryHandler; 2 | 3 | public class fieldDetails implements Comparable 4 | { 5 | private String name; 6 | private String type; 7 | 8 | public fieldDetails(String name, String type) { 9 | this.setName(name); 10 | 11 | if(type.trim().toUpperCase().startsWith("INT")) 12 | this.setType("int"); 13 | 14 | else if(type.trim().toUpperCase().startsWith("DECIMAL")) 15 | this.setType("dec"); 16 | 17 | else if(type.trim().toUpperCase().startsWith("DEC")) 18 | this.setType("dec"); 19 | 20 | else if(type.trim().toUpperCase().startsWith("CHAR")) 21 | this.setType("char"); 22 | 23 | else if(type.trim().toUpperCase().startsWith("VARCHAR")) 24 | this.setType("str"); 25 | 26 | else if(type.trim().toUpperCase().startsWith("STR")) 27 | this.setType("str"); 28 | 29 | else if(type.trim().toUpperCase().startsWith("DATE")) 30 | this.setType("date"); 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getType() { 42 | return type; 43 | } 44 | 45 | public void setType(String type) { 46 | this.type = type; 47 | } 48 | 49 | public int compareTo(fieldDetails f) 50 | { 51 | return (this.getName().equals(f.getName())&& this.getType().equals(f.getType())) ? 0 : 1 ; 52 | } 53 | } -------------------------------------------------------------------------------- /CSE562/Extra code/ExpressionExample.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.StringReader; 4 | import net.sf.jsqlparser.expression.*; 5 | import net.sf.jsqlparser.parser.*; 6 | 7 | public class ExpressionExample 8 | { 9 | public static void main(String[] args) throws ParseException 10 | { 11 | String str = "21+((23-67)-(2+3))"; 12 | Expression arithmeticExpr = parseArithmeticExpression(str); 13 | // SimpleCalculator calculator = new SimpleCalculator(); 14 | // arithmeticExpr.accept(calculator); 15 | // System.out.println(str+" = "+calculator.getResult()); 16 | } 17 | 18 | /** 19 | * 20 | * Parse something like "(3+2)-A" 21 | * 22 | * @param exprStr 23 | * @return 24 | * @throws ParseException 25 | */ 26 | public static Expression parseArithmeticExpression(String exprStr) throws ParseException 27 | { 28 | CCJSqlParser parser = new CCJSqlParser(new StringReader(exprStr)); 29 | // System.out.println(parser.SimpleExpression()); 30 | return parser.SimpleExpression(); 31 | } 32 | 33 | 34 | /** 35 | * 36 | * Parse something like "A fields) 21 | { 22 | OrderbyComparator oc; 23 | if(fields != null && !fields.isEmpty()) 24 | oc=new OrderbyComparator(fields); 25 | else 26 | oc=new OrderbyComparator(orderFields); 27 | 28 | Collections.sort(rel.getTuples(),oc ); 29 | return rel; 30 | } 31 | 32 | 33 | public Relation externalOrderByLimit(String filePath, SQLTable t, ArrayList fields, long limitVal, ArrayList projection) 34 | { 35 | OrderbyComparator oc = new OrderbyComparator(fields); 36 | TreeSet tupleset = new TreeSet(oc); 37 | Relation output = new Relation(); 38 | 39 | ScannerNode scan = new ScannerNode(null, t, filePath, true); 40 | 41 | while(scan.hasNextTuple()) 42 | { 43 | Tuple tuple = scan.readNextTuple(); 44 | if(tupleset.size() < limitVal) 45 | tupleset.add(tuple); 46 | 47 | else 48 | { 49 | int compare = oc.compare(tuple, tupleset.last()); 50 | if(compare < 0) 51 | { 52 | tupleset.add(tuple); 53 | tupleset.pollLast(); 54 | } 55 | } 56 | } 57 | 58 | Iterator itrTuple = tupleset.iterator(); 59 | 60 | while(itrTuple.hasNext()) 61 | { 62 | Tuple tup = ProjectNode.projectTuple(itrTuple.next(), projection); 63 | output.addTuple(tup); 64 | 65 | } 66 | 67 | return output; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/ProjectNode.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | import edu.buffalo.cse562.IOHandler.FileHandler; 7 | import edu.buffalo.cse562.QueryHandler.Relation; 8 | import edu.buffalo.cse562.QueryHandler.SQLTable; 9 | import edu.buffalo.cse562.QueryHandler.Tuple; 10 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 11 | 12 | public class ProjectNode extends ASTNode{ 13 | 14 | public Relation projectRelationOnFields(Relation input,ArrayList fields) 15 | { 16 | Relation output=new Relation(); 17 | 18 | Iterator it=input.getTuples().iterator(); 19 | while(it.hasNext()) 20 | { 21 | Tuple t=(Tuple)(it.next()); 22 | Tuple otp = projectTuple(t, fields); 23 | output.addTuple(otp); 24 | } 25 | return output; 26 | } 27 | 28 | public static Tuple projectTuple(Tuple t, ArrayList fields) 29 | { 30 | if(t==null) 31 | return null; 32 | 33 | ArrayList fieldDetails = new ArrayList(); 34 | ArrayList values = new ArrayList(); 35 | Iterator fieldsItr = fields.iterator(); 36 | 37 | while(fieldsItr.hasNext()) 38 | { 39 | String fieldName = fieldsItr.next(); 40 | fieldDetails.add(new fieldDetails(fieldName, "int")); 41 | values.add(t.valueForField(fieldName)); 42 | } 43 | 44 | SQLTable projectTable = new SQLTable("projectTable", fieldDetails); 45 | Tuple tuple = projectTable.createNewTupleWithValues(values); 46 | return tuple; 47 | } 48 | 49 | public void externalProjectOnFields(String inPath, String outPath, SQLTable t, ArrayList fields) 50 | { 51 | FileHandler fh = new FileHandler(); 52 | String output = null; 53 | 54 | ScannerNode scan = new ScannerNode(null, t, inPath, true); 55 | while (scan.hasNextTuple()) 56 | { 57 | Tuple tuple = scan.readNextTuple(); 58 | Tuple outTuple = ProjectNode.projectTuple(tuple, fields); 59 | if(outPath == null) 60 | { 61 | String temp = outTuple.finalPrint(); 62 | 63 | //temp = temp.substring(0, temp.length()-1); 64 | 65 | System.out.println(temp); 66 | outTuple = null; 67 | //counter++; 68 | } 69 | else 70 | { 71 | output += outTuple.convertToString() + "\n"; 72 | 73 | if(output.length()%STRING_LIMIT == 0) 74 | { 75 | fh.writeFile(outPath,output); 76 | output = null; 77 | } 78 | } 79 | } 80 | //System.out.println(counter+" values dumped"); 81 | fh = null; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/ASTNode.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.TreeMap; 6 | 7 | import edu.buffalo.cse562.Miscellaneous.OrderDetails; 8 | import edu.buffalo.cse562.QueryHandler.SQLTable; 9 | import edu.buffalo.cse562.QueryHandler.Tuple; 10 | import net.sf.jsqlparser.expression.Expression; 11 | 12 | public abstract class ASTNode { 13 | 14 | public static long TUPLE_LIMIT = 5000; 15 | public static long STRING_LIMIT = 500; 16 | 17 | public enum OperationType 18 | { 19 | SELECT, 20 | PROJECT, 21 | JOIN, 22 | GROUPBY, 23 | UNION, 24 | SUM, 25 | COUNT, 26 | MAX, 27 | MIN, 28 | ADDITION, 29 | SUBTRACTION, 30 | MULTIPLICATION, 31 | DIVISION, 32 | LESSTHAN, 33 | LESSTHANEQUAL, 34 | GREATERTHAN, 35 | GREATHEREQUAL, 36 | EQUAL, 37 | COMPLEMENT 38 | }; 39 | 40 | public static HashMap aliasMap = new HashMap(); 41 | public void setAliasMap(HashMap aMap) 42 | { 43 | ASTNode.aliasMap=aMap; 44 | } 45 | 46 | public static String baseDir; 47 | public static String swapDir; 48 | public static String indexDir; 49 | 50 | public static ArrayList joinTables = new ArrayList(); 51 | public static SQLTable joinOutputTable = null; 52 | public static SQLTable groupOutputTable = null; 53 | public static SQLTable orderOutputTable = null; 54 | 55 | public static ArrayList extraPredicates=new ArrayList(); 56 | public static ArrayList joinPredicates = new ArrayList(); 57 | public static ArrayList groupFields = new ArrayList(); 58 | public static ArrayList projectFields = new ArrayList(); 59 | public static ArrayList innerProjectFields = new ArrayList(); 60 | public static ArrayList orderFields = new ArrayList(); 61 | public static ArrayList outerprojectFields = new ArrayList(); 62 | 63 | public static String distinctField = null; 64 | public static long limit = -1; 65 | public static boolean isNestedQuery = false; 66 | public static boolean isOuter = false; 67 | public static long startTime=0; 68 | public static TreeMap groupBucketsAggOuter = new TreeMap(); 69 | public static HashMap groupBucketsAgg = new HashMap(); 70 | public static HashMap> groupBucketsDis = new HashMap>(); 71 | public static TreeMap> groupBucketsDisOuter = new TreeMap>(); 72 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/IOHandler/FileHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Kaushal Hakani 3 | */ 4 | package edu.buffalo.cse562.IOHandler; 5 | 6 | 7 | import java.io.BufferedReader; 8 | import java.io.File; 9 | import java.io.FileInputStream; 10 | import java.io.FileNotFoundException; 11 | import java.io.FileReader; 12 | import java.io.FileWriter; 13 | import java.io.BufferedInputStream; 14 | import java.io.BufferedWriter; 15 | import java.io.IOException; 16 | 17 | public class FileHandler { 18 | 19 | File file = null; 20 | FileReader fr = null; 21 | BufferedReader br = null; 22 | FileWriter fw = null; 23 | BufferedWriter bw = null; 24 | 25 | 26 | 27 | int offset; 28 | 29 | 30 | public boolean createDirectory(String directoryName) { 31 | return (new File(directoryName)).mkdir(); 32 | } 33 | 34 | public void createFile(String name) { 35 | try { 36 | file=new File(name); 37 | file.createNewFile(); 38 | } catch (IOException e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | 43 | public void openReadFile(String fileName) 44 | { 45 | try { 46 | fr = new FileReader(new File(fileName)); 47 | br = new BufferedReader(fr); 48 | } catch (FileNotFoundException e) { 49 | e.printStackTrace(); 50 | } 51 | } 52 | 53 | public String readFile() 54 | { 55 | String line = ""; 56 | try { 57 | line = br.readLine(); 58 | } catch (IOException e) { 59 | e.printStackTrace(); 60 | } 61 | return line; 62 | } 63 | 64 | public void resetFile() 65 | { 66 | try { 67 | fr.reset(); 68 | br = new BufferedReader(fr); 69 | } catch (IOException e) { 70 | // TODO Auto-generated catch block 71 | e.printStackTrace(); 72 | } 73 | } 74 | 75 | public void writeFile(String fileName, String write) 76 | { 77 | try { 78 | file = new File(fileName); 79 | if(!file.exists()) 80 | createFile(fileName); 81 | 82 | fw = new FileWriter(file,true); 83 | bw = new BufferedWriter(fw); 84 | bw.write(write); 85 | bw.close(); 86 | } catch (IOException e) { 87 | e.printStackTrace(); 88 | } 89 | } 90 | 91 | public boolean endOfFile() 92 | { 93 | try { 94 | if(fr.ready() || br.ready()) 95 | return false; 96 | else 97 | { 98 | //closeFile(); 99 | return true; 100 | } 101 | } catch (IOException e) { 102 | e.printStackTrace(); 103 | } 104 | return true; 105 | } 106 | 107 | public void closeFile() 108 | { 109 | try { 110 | fr.close(); 111 | br.close(); 112 | } catch (IOException e) { 113 | // TODO Auto-generated catch block 114 | e.printStackTrace(); 115 | } 116 | } 117 | 118 | public boolean deleteFile(String fileName) 119 | { 120 | File f = new File(fileName); 121 | return f.delete(); 122 | } 123 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Evaluators/OrderbyComparator.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Evaluators; 2 | 3 | import java.math.BigDecimal; 4 | import java.text.SimpleDateFormat; 5 | import java.util.ArrayList; 6 | import java.util.Comparator; 7 | import java.util.Date; 8 | import java.util.Iterator; 9 | import edu.buffalo.cse562.Miscellaneous.OrderDetails; 10 | 11 | //import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; 12 | 13 | import edu.buffalo.cse562.QueryHandler.Tuple; 14 | 15 | public class OrderbyComparator implements Comparator { 16 | 17 | private ArrayList fieldsForComparison; 18 | //private boolean isAsc; 19 | public OrderbyComparator(ArrayList fields) 20 | { 21 | fieldsForComparison=fields; 22 | } 23 | 24 | public int compare(Tuple a,Tuple b) 25 | { 26 | if(a == null || b == null) 27 | System.out.println("OC tuples empty!!"); 28 | 29 | Iterator it = fieldsForComparison.listIterator(); 30 | 31 | while(it.hasNext()) 32 | { 33 | OrderDetails fields=it.next(); 34 | String orderType=fields.orderType; 35 | String field = fields.field; 36 | int result=0; 37 | SimpleDateFormat sdfObj = new SimpleDateFormat("yyyy-MM-dd"); 38 | if(isNumeric(a.valueForField(field))) 39 | { 40 | double val1 = Double.parseDouble(a.valueForField(field)); 41 | double val2 = Double.parseDouble(b.valueForField(field)); 42 | 43 | 44 | if(val1 < val2) 45 | result= - 1; 46 | else if(val1 > val2) 47 | result= 1; 48 | else if(val1 == val2) 49 | result = 0; 50 | } 51 | else if(isValidDate(a.valueForField(field),"yyyy-MM-dd")) 52 | { 53 | try { 54 | Date d1=sdfObj.parse(a.valueForField(field)); 55 | Date d2=sdfObj.parse(b.valueForField(field)); 56 | result=d1.compareTo(d2); 57 | } 58 | catch(Exception e) 59 | {// this should not come here. 60 | } 61 | } 62 | else 63 | result=a.valueForField(field).compareTo(b.valueForField(field)); 64 | 65 | if(result!=0 || !it.hasNext()) 66 | { 67 | return orderType.equals("ASC")? result: -result; 68 | } 69 | 70 | 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | public boolean isNumeric(String s) 77 | { 78 | try 79 | { 80 | double d = Double.parseDouble(s); 81 | } 82 | catch(NumberFormatException nfe) 83 | { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | public boolean isValidDate(String dateToValidate, String dateFromat) 90 | { 91 | if(dateToValidate == null) 92 | return false; 93 | 94 | SimpleDateFormat sdf = new SimpleDateFormat(dateFromat); 95 | sdf.setLenient(false); 96 | try { 97 | Date date = sdf.parse(dateToValidate); 98 | } 99 | catch (Exception e){ 100 | return false; 101 | } 102 | 103 | return true; 104 | } 105 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Miscellaneous/IntegerSet.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Miscellaneous; 2 | 3 | import java.io.IOException; 4 | import java.io.ObjectInputStream; 5 | import java.io.ObjectOutputStream; 6 | import java.io.Serializable; 7 | import java.util.ArrayList; 8 | import java.util.Comparator; 9 | 10 | import jdbm.Serializer; 11 | import jdbm.SerializerInput; 12 | import jdbm.SerializerOutput; 13 | 14 | public class IntegerSet implements Comparable,Serializable{ 15 | 16 | 17 | //public ArrayList list; 18 | public int value1; 19 | public int value2; 20 | public IntegerSet() 21 | { 22 | //list=new ArrayList(); 23 | 24 | } 25 | 26 | private void writeObject(ObjectOutputStream oos) throws IOException 27 | { 28 | try 29 | { 30 | // oos.writeInt(this.list.get(0)); 31 | // oos.writeInt(this.list.get(1)); 32 | oos.writeInt(value1); 33 | oos.writeInt(value2); 34 | } 35 | catch(Exception e) 36 | { 37 | e.printStackTrace(); 38 | } 39 | } 40 | 41 | private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException 42 | { 43 | try 44 | { 45 | // if(this.list==null) 46 | // this.list=new ArrayList(); 47 | value1=in.readInt(); 48 | value2=in.readInt(); 49 | // this.list.add(in.readInt()); 50 | // this.list.add(in.readInt()); 51 | } 52 | catch(Exception e) 53 | { 54 | e.printStackTrace(); 55 | } 56 | } 57 | public String toString() 58 | { 59 | return "test"; 60 | } 61 | 62 | @Override 63 | public int compareTo(IntegerSet o2) { 64 | 65 | // int val1=0,val2=0; 66 | // for(int i=0;i 83 | { 84 | public void serialize(SerializerOutput out,IntegerSet obj) throws IOException 85 | { 86 | // for(Integer i: obj.list) 87 | // { 88 | // out.writeInt(i.intValue()); 89 | // } 90 | out.writeInt(obj.value1); 91 | out.writeInt(obj.value2); 92 | } 93 | public IntegerSet deserialize(SerializerInput in) throws IOException, ClassNotFoundException 94 | { 95 | // IntegerSet result=new IntegerSet(); 96 | // int value=0; 97 | // int byteCount=0; 98 | // while((byteCount=in.available())>0) 99 | // { 100 | // value=in.readInt(); 101 | // System.out.println(value); 102 | // 103 | // result.list.add(value); 104 | // } 105 | // return result; 106 | // 107 | // } 108 | 109 | IntegerSet result=new IntegerSet(); 110 | result.value1=in.readInt(); 111 | result.value2=in.readInt(); 112 | return result; 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /CSE562/Extra code/IndexScanner.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | import java.util.Map; 7 | import java.util.Map.Entry; 8 | import java.util.Set; 9 | import java.util.SortedMap; 10 | 11 | import edu.buffalo.cse562.QueryHandler.SQLTable; 12 | import edu.buffalo.cse562.QueryHandler.Tuple; 13 | import edu.buffalo.cse562.QueryHandler.TupleSerializer; 14 | import jdbm.PrimaryMap; 15 | import jdbm.PrimaryTreeMap; 16 | import jdbm.RecordManager; 17 | import jdbm.RecordManagerFactory; 18 | import jdbm.SecondaryKeyExtractor; 19 | import jdbm.SecondaryTreeMap; 20 | import jdbm.btree.BTree; 21 | 22 | 23 | public class IndexScanner 24 | { 25 | static RecordManager recMan; 26 | //static PrimaryTreeMap t1; 27 | static Set ptrKeys; 28 | IndexHandler ih = null; 29 | 30 | public IndexScanner(SQLTable tbl) 31 | { 32 | try 33 | { 34 | recMan = RecordManagerFactory.createRecordManager(tbl.getTableAlias().toUpperCase()); 35 | 36 | long recid = recMan.getNamedObject("orders_primary"); 37 | PrimaryTreeMap t1 = recMan.treeMap( 38 | "orders_primary", 39 | new TupleSerializer(tbl.getSchema(),tbl.getTypes()) 40 | ); 41 | 42 | System.out.println("hey"); 43 | SecondaryTreeMap sMap = t1.secondaryTreeMap("newOrdersSecondary", 44 | new SecondaryKeyExtractor() { 45 | @Override 46 | public String extractSecondaryKey(Integer key, 47 | Tuple value) { 48 | // TODO Auto-generated method stub 49 | return value.valueForField("orderdate"); 50 | } 51 | } 52 | ); 53 | 54 | System.out.println("oho"); 55 | /* PrimaryTreeMap> t3 = recMan.treeMap("orders_datenew");*/ 56 | //System.out.println(tp); 57 | //ptrKeys = t1.keySet(); 58 | //ih = new IndexHandler(recMan, t1,ptrKeys); 59 | //System.out.println(t1.size()); 60 | /*for(Entry entry : t1.entrySet()) 61 | { 62 | System.out.println(entry.getValue().finalPrint()); 63 | }*/ 64 | Set dateKeySet = sMap.keySet(); 65 | SortedMap> tpmap = sMap.subMap("1994-01-01", "1994-02-01"); 66 | for(String st : tpmap.keySet()) 67 | { 68 | Iterable itr = tpmap.get(st); 69 | for(int i : itr) 70 | { 71 | System.out.println(t1.get(i).finalPrint()); 72 | } 73 | //System.out.println("hey"); 74 | } 75 | 76 | } 77 | catch(IOException e) 78 | { 79 | e.printStackTrace(); 80 | } 81 | } 82 | 83 | private Tuple getNextTuple() 84 | { 85 | return null; 86 | } 87 | 88 | public boolean hasNextTuple() 89 | { 90 | return ih.endOfIndex(); 91 | } 92 | 93 | public static void main(String args[]) 94 | { 95 | ArrayList tbl = new ArrayList(); 96 | ArrayList tbltype = new ArrayList(); 97 | tbl.add("orderkey"); 98 | tbltype.add("INT"); 99 | tbl.add("custkey"); 100 | tbltype.add("INT"); 101 | tbl.add("orderstatus"); 102 | tbltype.add("CHAR(1)"); 103 | tbl.add("totalprice"); 104 | tbltype.add("DECIMAL"); 105 | tbl.add("orderdate"); 106 | tbltype.add("DATE"); 107 | tbl.add("orderpriority"); 108 | tbltype.add("CHAR(15)"); 109 | tbl.add("clerk"); 110 | tbltype.add("CHAR(15)"); 111 | tbl.add("shippriority"); 112 | tbltype.add("INT"); 113 | tbl.add("comment"); 114 | tbltype.add("VARCHAR(79)"); 115 | SQLTable tbl1 = new SQLTable("ORDERS",tbl,tbltype); 116 | IndexScanner idS = new IndexScanner(tbl1); 117 | } 118 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Evaluators/ConditionalEvaluators.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Evaluators; 2 | 3 | import net.sf.jsqlparser.expression.Expression; 4 | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; 5 | import net.sf.jsqlparser.expression.operators.conditional.OrExpression; 6 | import edu.buffalo.cse562.AbstractExpressionVisitor; 7 | import edu.buffalo.cse562.QueryHandler.Tuple; 8 | 9 | public class ConditionalEvaluators extends AbstractExpressionVisitor { 10 | 11 | private boolean result; 12 | private Tuple inputTuple; 13 | 14 | public ConditionalEvaluators(Tuple in) 15 | { 16 | inputTuple=in; 17 | } 18 | 19 | public boolean getResult() 20 | { 21 | return result; 22 | } 23 | 24 | 25 | @Override 26 | public void visit(AndExpression ae) { 27 | // throw new UnsupportedOperationException("Not supported yet."); 28 | 29 | Expression leftexp=ae.getLeftExpression(); 30 | Expression rightexp=ae.getRightExpression(); 31 | boolean leftValue=false,rightValue=false; 32 | 33 | if(leftexp instanceof AndExpression || leftexp instanceof OrExpression) 34 | { 35 | ae.getLeftExpression().accept(this); 36 | leftValue=result; 37 | } 38 | else 39 | { 40 | RelationalEvaluator rel=new RelationalEvaluator(inputTuple); 41 | ae.getLeftExpression().accept(rel); 42 | leftValue=rel.getResult(); 43 | } 44 | 45 | 46 | if(rightexp instanceof AndExpression || rightexp instanceof OrExpression) 47 | { 48 | ae.getRightExpression().accept(this); 49 | rightValue=result; 50 | } 51 | else 52 | { 53 | RelationalEvaluator rel=new RelationalEvaluator(inputTuple); 54 | ae.getRightExpression().accept(rel); 55 | rightValue=rel.getResult(); 56 | } 57 | 58 | //System.out.println(leftValue+""+rightValue); 59 | result=leftValue && rightValue; 60 | 61 | } 62 | 63 | @Override 64 | public void visit(OrExpression oe) { 65 | // throw new UnsupportedOperationException("Not supported yet."); 66 | // oe.getLeftExpression().accept(this); 67 | // boolean leftValue = result; 68 | // oe.getRightExpression().accept(this); 69 | // boolean rightValue = result; 70 | 71 | 72 | Expression leftexp=oe.getLeftExpression(); 73 | Expression rightexp=oe.getRightExpression(); 74 | boolean leftValue=false,rightValue=false; 75 | 76 | if(leftexp instanceof AndExpression || leftexp instanceof OrExpression) 77 | { 78 | oe.getLeftExpression().accept(this); 79 | leftValue=result; 80 | } 81 | else 82 | { 83 | RelationalEvaluator rel=new RelationalEvaluator(inputTuple); 84 | oe.getLeftExpression().accept(rel); 85 | leftValue=rel.getResult(); 86 | } 87 | 88 | 89 | if(rightexp instanceof AndExpression || rightexp instanceof OrExpression) 90 | { 91 | oe.getRightExpression().accept(this); 92 | rightValue=result; 93 | } 94 | else 95 | { 96 | RelationalEvaluator rel=new RelationalEvaluator(inputTuple); 97 | oe.getRightExpression().accept(rel); 98 | rightValue=rel.getResult(); 99 | } 100 | 101 | 102 | //System.out.println(leftValue+""+rightValue); 103 | result=leftValue || rightValue; 104 | } 105 | public static boolean isConditionalOperator(Expression ex) 106 | { 107 | return (ex instanceof AndExpression || ex instanceof OrExpression); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /CSE562/Extra code/Evaluator.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | 6 | import net.sf.jsqlparser.schema.Table; 7 | import net.sf.jsqlparser.statement.*; 8 | import net.sf.jsqlparser.statement.create.table.CreateTable; 9 | import net.sf.jsqlparser.statement.select.*; 10 | 11 | /** 12 | * 13 | * @author Niccolo' Meneghetti 14 | */ 15 | 16 | public class Evaluator 17 | { 18 | Statement statement; 19 | 20 | 21 | public Evaluator(Statement statement) 22 | { 23 | this.statement = statement; 24 | } 25 | 26 | public void Statement() 27 | { 28 | if(statement instanceof CreateTable) 29 | CreateStatement(); 30 | else if(statement instanceof Select) 31 | SelectStatement(); 32 | } 33 | 34 | public void CreateStatement() 35 | { 36 | CreateTable createstatement = (CreateTable)statement; 37 | Table table = createstatement.getTable(); 38 | String col_def = createstatement.getColumnDefinitions().toString(); 39 | 40 | //System.out.println("Table====>"+table.getName()); 41 | // System.out.println("Col Def===>"+col_def); 42 | } 43 | 44 | public void SelectStatement() 45 | { 46 | if(statement.toString().contains("UNION") || 47 | statement.toString().contains("Union") || 48 | statement.toString().contains("union")) 49 | UnionStatement(); 50 | else if(statement.toString().contains("JOIN") || 51 | statement.toString().contains("Join") || 52 | statement.toString().contains("join")) 53 | JoinStatement(); 54 | else 55 | PlainSelectStatement((PlainSelect) statement); 56 | } 57 | 58 | public void/*Rows*/ PlainSelectStatement(PlainSelect plainSelect) 59 | { 60 | String fromItem = plainSelect.getFromItem().toString(); 61 | String fieldItems = plainSelect.getSelectItems().toString(); 62 | 63 | //System.out.println("Table : " + fromItem); 64 | //System.out.println("Field Items : " + fieldItems); 65 | if(plainSelect.getWhere() != null) 66 | { 67 | String where = plainSelect.getWhere().toString(); 68 | //System.out.println("Where : " + where); 69 | } 70 | 71 | //Return the resultant Rows 72 | } 73 | 74 | public void/*Rows*/ UnionStatement() 75 | { 76 | Select selectstatement = (Select)statement; 77 | Union union = (Union) selectstatement.getSelectBody(); 78 | 79 | Iterator plainselectitr = union.getPlainSelects().iterator(); 80 | while(plainselectitr.hasNext()) 81 | { 82 | PlainSelect plainselect = (PlainSelect) plainselectitr.next(); 83 | PlainSelectStatement(plainselect); 84 | //System.out.println(plainselect.toString()); 85 | } 86 | 87 | /* 88 | *Get the rows from PlainSelect for each individual select statement. 89 | *Combine the rows here. 90 | *Return rows. 91 | */ 92 | } 93 | 94 | private void/*Rows*/ JoinStatement() 95 | { 96 | Select selectstatement = (Select)statement; 97 | PlainSelect plainselect=(PlainSelect) selectstatement.getSelectBody(); 98 | String fromItem = plainselect.getFromItem().toString(); 99 | String fieldItems = plainselect.getSelectItems().toString(); 100 | 101 | //System.out.println("Table : " + fromItem); 102 | 103 | List joinlist= plainselect.getJoins(); 104 | Iterator joinitr = joinlist.iterator(); 105 | while(joinitr.hasNext()) 106 | { 107 | Join join = joinitr.next(); 108 | Table table = (Table) join.getRightItem(); 109 | System.out.println("Table : " + table.getName()); 110 | } 111 | 112 | System.out.println("Field Items : " + fieldItems); 113 | 114 | /*Create an intermediate table with all Joins. 115 | *Call PlainSelect() on intermedaite table if there is a where clause. 116 | *Print the returned result. 117 | */ 118 | } 119 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/EvaluatorNode.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | 7 | import edu.buffalo.cse562.QueryHandler.Relation; 8 | import edu.buffalo.cse562.QueryHandler.SQLTable; 9 | import edu.buffalo.cse562.QueryHandler.Tuple; 10 | 11 | public class EvaluatorNode extends ASTNode 12 | { 13 | JoinNode jNode = new JoinNode(); 14 | GroupingNode gNode = new GroupingNode(); 15 | OrderbyNode oNode = null; 16 | ProjectNode pNode = null; 17 | 18 | public void evaluate() throws IOException 19 | { 20 | //Does join and group! 21 | if(ASTNode.joinTables != null && !ASTNode.joinTables.isEmpty()) 22 | joinEvaluate(); 23 | 24 | //orders and project! 25 | if(orderFields != null && !orderFields.isEmpty()) 26 | { 27 | orderEvaluate(); 28 | project(); 29 | } 30 | } 31 | 32 | public void joinEvaluate() throws IOException 33 | { 34 | 35 | while(ASTNode.joinTables.size() >= 2) //Condition checks if a join is left or not! 36 | { 37 | /* 38 | * All join functions will get two SQLTable objects table1 and table2. 39 | * Based on various factors it should decide the left table and the right table! 40 | * Also check for the size of jointables to see if it is the last join! 41 | */ 42 | 43 | SQLTable table1 = ASTNode.joinTables.get(0); 44 | SQLTable table2 = ASTNode.joinTables.get(1); 45 | 46 | 47 | try 48 | { 49 | 50 | /* 51 | * tpch07 stack trace after 209s 52 | */ 53 | SQLTable resultTable = jNode.Join(table1, table2); 54 | if(ASTNode.joinTables.size()>2) 55 | resultTable.addToJoinSet(table2.getJoinSet().pollFirst()); 56 | ASTNode.joinTables.remove(table1); 57 | ASTNode.joinTables.remove(table2); 58 | 59 | ASTNode.joinTables.add(0, resultTable); 60 | } 61 | catch(IOException e) 62 | { 63 | e.printStackTrace(); 64 | } 65 | } 66 | 67 | if(groupFields != null && !isOuter) 68 | jNode.finalizeGrouping(); 69 | } 70 | 71 | 72 | public void groupEvaluate() 73 | { 74 | //This is called only when there is no join but directly group by. (Outer query for tpch7) 75 | SQLTable groupTable = joinTables.get(0); 76 | gNode.initializeGrouping(groupTable); 77 | ArrayList tuples = groupTable.getRelation().getTuples(); 78 | 79 | Iterator tupleItr = tuples.iterator(); 80 | 81 | while(tupleItr.hasNext()) 82 | { 83 | gNode.groupTuple(tupleItr.next()); 84 | } 85 | 86 | gNode.processGroupOutput(); 87 | } 88 | 89 | 90 | public void orderEvaluate() 91 | { 92 | oNode = new OrderbyNode(); 93 | Relation orderedRelation = oNode.orderBy(groupOutputTable.getRelation(), orderFields); 94 | orderOutputTable = groupOutputTable; 95 | orderOutputTable.setRelation(orderedRelation); 96 | } 97 | 98 | public void project() 99 | { 100 | //TODO 101 | if(ASTNode.limit>0) 102 | { 103 | Relation rel=new Relation(); 104 | for(int i=0;i projFields = null; 115 | Tuple tuple= (Tuple)itr.next(); 116 | if(outerprojectFields != null && !outerprojectFields.isEmpty()) 117 | projFields = outerprojectFields; 118 | else 119 | projFields = projectFields; 120 | String temp = ProjectNode.projectTuple(tuple, projFields).finalPrint(); 121 | System.out.println(temp); 122 | } 123 | //long endTime = System.currentTimeMillis(); 124 | //long totalTime = endTime - ASTNode.startTime; 125 | //System.out.println(totalTime/100); 126 | 127 | System.exit(0); 128 | } 129 | } -------------------------------------------------------------------------------- /CSE562/Extra code/Extra1/testerMotaBhai.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | 7 | import edu.buffalo.cse562.Miscellaneous.IndexScanOperation; 8 | import edu.buffalo.cse562.Operators.IndexScanner; 9 | import edu.buffalo.cse562.QueryHandler.SQLTable; 10 | import edu.buffalo.cse562.QueryHandler.Tuple; 11 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 12 | 13 | public class testerMotaBhai { 14 | 15 | public testerMotaBhai() throws IOException 16 | { 17 | IndexScanOperation iop = new IndexScanOperation(); 18 | iop.key = "shipdate"; 19 | iop.lowerInclusive = true; 20 | iop.upperInclusive = true; 21 | iop.lowerLimit = "1994-01-01"; 22 | iop.upperLimit = "1994-01-01"; 23 | iop.type = "SECONDARY"; 24 | ArrayList tbl = new ArrayList(); 25 | ArrayList tbltype = new ArrayList(); 26 | ArrayList field = new ArrayList(); 27 | tbl.add("orderkey"); 28 | tbltype.add("INT"); 29 | tbl.add("partkey"); 30 | tbltype.add("INT"); 31 | tbl.add("suppkey"); 32 | tbltype.add("INT"); 33 | tbl.add("linenumber"); 34 | tbltype.add("INT"); 35 | tbl.add("quantity"); 36 | tbltype.add("DECIMAL"); 37 | tbl.add("extendedprice"); 38 | tbltype.add("DECIMAL"); 39 | tbl.add("discount"); 40 | tbltype.add("DECIMAL"); 41 | tbl.add("tax"); 42 | tbltype.add("DECIMAL"); 43 | tbl.add("returnflag"); 44 | tbltype.add("CHAR"); 45 | tbl.add("linestatus"); 46 | tbltype.add("CHAR"); 47 | tbl.add("shipdate"); 48 | tbltype.add("DATE"); 49 | tbl.add("commitdate"); 50 | tbltype.add("DATE"); 51 | tbl.add("receiptdate"); 52 | tbltype.add("DATE"); 53 | tbl.add("shipinstruct"); 54 | tbltype.add("VARCHAR(29)"); 55 | tbl.add("shipmode"); 56 | tbltype.add("VARCHAR(79)"); 57 | Iterator itr1 = tbl.iterator(); 58 | Iterator itr2 = tbltype.iterator(); 59 | while(itr1.hasNext() && itr2.hasNext()) 60 | { 61 | String str1 = itr1.next(); 62 | String str2 = itr2.next(); 63 | fieldDetails f = new fieldDetails(str1, str2); 64 | field.add(f); 65 | } 66 | SQLTable tbl1 = new SQLTable("LINEITEM",field); 67 | IndexScanner idX1 = IndexScanner.getInstance("C:/index"); 68 | IndexScanner.InnerScanner InrScanner = idX1.returnClass(tbl1); 69 | InrScanner.run(iop); 70 | while(InrScanner.hasNextTuple()) 71 | { 72 | Tuple tpl = InrScanner.readNextTuple(); 73 | System.out.println(tpl.finalPrint()); 74 | } 75 | IndexScanOperation iop1 = new IndexScanOperation(); 76 | iop1.key = "nationkey"; 77 | iop1.type = "PRIMARY"; 78 | ArrayList tblnew = new ArrayList(); 79 | ArrayList tbltype1 = new ArrayList(); 80 | ArrayList field1= new ArrayList(); 81 | tblnew.add("nationkey"); 82 | tbltype1.add("INT"); 83 | tblnew.add("name"); 84 | tbltype1.add("VARCHAR(79)"); 85 | tblnew.add("regionkey"); 86 | tbltype1.add("INT"); 87 | tbl.add("comment"); 88 | tbltype.add("VARCHAR(152)"); 89 | Iterator itr3 = tblnew.iterator(); 90 | Iterator itr4 = tbltype1.iterator(); 91 | while(itr3.hasNext() && itr3.hasNext()) 92 | { 93 | String str1 = itr3.next(); 94 | String str2 = itr4.next(); 95 | fieldDetails f = new fieldDetails(str1, str2); 96 | field1.add(f); 97 | } 98 | SQLTable tblnew1 = new SQLTable("NATION",field1); 99 | 100 | IndexScanner idS1 = IndexScanner.getInstance("C:/index"); 101 | IndexScanner.InnerScanner IndexScannerInner = idS1.returnClass(tblnew1); 102 | IndexScannerInner.run(iop1); 103 | while(IndexScannerInner.hasNextTuple()) 104 | { 105 | Tuple newtup = IndexScannerInner.readNextTuple(); 106 | System.out.println(newtup.finalPrint()); 107 | } 108 | } 109 | 110 | public static void main(String args[]) throws IOException 111 | { 112 | testerMotaBhai tmB = new testerMotaBhai(); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/ScannerNode.java: -------------------------------------------------------------------------------- 1 | 2 | package edu.buffalo.cse562.Operators; 3 | 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileReader; 7 | import java.text.DecimalFormat; 8 | import java.util.ArrayList; 9 | import java.util.Arrays; 10 | import java.util.Iterator; 11 | 12 | import net.sf.jsqlparser.expression.Expression; 13 | import net.sf.jsqlparser.expression.Function; 14 | import edu.buffalo.cse562.Evaluators.ArithmeticEvaluator; 15 | import edu.buffalo.cse562.IOHandler.FileHandler; 16 | import edu.buffalo.cse562.QueryHandler.Relation; 17 | import edu.buffalo.cse562.QueryHandler.SQLTable; 18 | import edu.buffalo.cse562.QueryHandler.Tuple; 19 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 20 | import edu.buffalo.cse562.Operators.SelectNode; 21 | 22 | public class ScannerNode extends ASTNode 23 | { 24 | String basePath; 25 | SQLTable t; 26 | boolean isMultipleSource; 27 | FileHandler fh; 28 | 29 | public ScannerNode() 30 | {} 31 | 32 | public ScannerNode(String basePath,SQLTable t, String fileName, boolean isMultipleSource) 33 | { 34 | this.basePath = basePath; 35 | this.t = t; 36 | this.isMultipleSource = isMultipleSource; 37 | String filePath; 38 | fh=new FileHandler(); 39 | //brokenLine=new String(); 40 | 41 | if(fileName == null) 42 | filePath = basePath+"/"+ t.getTableName() +".dat"; 43 | else 44 | filePath = fileName; 45 | 46 | fh.openReadFile(filePath); 47 | //incompleteLine=false; 48 | } 49 | 50 | private String getNextLine() { 51 | String line = fh.readFile(); 52 | return line; 53 | } 54 | 55 | public boolean hasNextTuple() 56 | { 57 | if(!fh.endOfFile()) 58 | return true; 59 | else 60 | return false; 61 | } 62 | 63 | public Tuple readNextTuple() 64 | { 65 | String line = getNextLine(); 66 | ArrayList components=new ArrayList(Arrays.asList(line.split("\\|"))); 67 | Tuple tup=new Tuple(t); 68 | tup = t.createNewTupleWithValues(components); 69 | return tup; 70 | } 71 | 72 | public void closeScanner() 73 | { 74 | fh.closeFile(); 75 | } 76 | 77 | public void handleAlias(Tuple t) 78 | { 79 | if(aliasMap != null && !aliasMap.isEmpty()) 80 | { 81 | Iterator it=aliasMap.keySet().iterator(); 82 | while(it.hasNext()) 83 | { 84 | String aName=it.next().toString(); 85 | Expression exp=aliasMap.get(aName); 86 | if(aName.equals(exp.toString()) || exp instanceof Function) 87 | continue; 88 | 89 | if(ArithmeticEvaluator.isArithMeticOperator(exp)) 90 | { 91 | ArithmeticEvaluator aeva=new ArithmeticEvaluator(t); 92 | exp.accept(aeva); 93 | 94 | // To drop the trailing zeroes 95 | double result = aeva.getResult(); 96 | DecimalFormat df=new DecimalFormat("###.#"); 97 | 98 | //Add Filed Type 99 | t.setValue(aName, t.getOwner().getTypeFormFieldName(aName), df.format(result)); 100 | } 101 | else // normal field alias 102 | { 103 | //Add Filed Type 104 | t.setValue(aName, t.getOwner().getTypeFormFieldName(aName), t.valueForField(exp.toString())); 105 | } 106 | } 107 | } 108 | } 109 | 110 | public Tuple validateTuple(Tuple newTuple, ArrayList filters) { 111 | SelectNode selectnode = new SelectNode(); 112 | selectnode.setAliasMap(aliasMap); 113 | boolean isValidTuple = true; 114 | 115 | if(filters != null && !filters.isEmpty()) 116 | { 117 | Iterator expressions = filters.iterator(); 118 | Expression expression; 119 | 120 | while(expressions.hasNext()) 121 | { 122 | expression = expressions.next(); 123 | if(!selectnode.filterTuple(newTuple, expression)) 124 | { 125 | isValidTuple=false; 126 | break; 127 | } 128 | } 129 | } 130 | if(isValidTuple) 131 | return newTuple; 132 | else 133 | return null; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/QueryHandler/TupleSerializer.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.QueryHandler; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | 7 | import jdbm.Serializer; 8 | import jdbm.SerializerInput; 9 | import jdbm.SerializerOutput; 10 | // Used in indexing 11 | public class TupleSerializer implements Serializer{ 12 | 13 | ArrayList fields; 14 | SQLTable parentTable; 15 | public TupleSerializer(SQLTable table) 16 | { 17 | this.parentTable=table; 18 | fields = table.getSchema(); 19 | } 20 | 21 | /* 22 | public TupleSerializer(String type) 23 | { 24 | String[] temp = null; 25 | String[] temp1 = null; 26 | temp[0] = type; 27 | temp1[0] = field; 28 | this.typeArr = (ArrayList) Arrays.asList(temp); 29 | this.fieldArr = (ArrayList) Arrays.asList(temp1); 30 | System.out.println(fieldArr.toString()); 31 | System.out.println(typeArr.toString()); 32 | } 33 | */ 34 | 35 | @Override 36 | public Tuple deserialize(SerializerInput in) throws IOException, ClassNotFoundException 37 | { 38 | //Tuple tup = new Tuple(parentTable); 39 | Iterator fieldIterator = fields.iterator(); 40 | ArrayList values=new ArrayList(); 41 | 42 | while(fieldIterator.hasNext()) 43 | { 44 | fieldDetails field = fieldIterator.next(); 45 | String fieldType = field.getType(); 46 | 47 | switch(fieldType) 48 | { 49 | case "int": 50 | values.add(String.valueOf(in.readInt())); 51 | //tup.setValue(field.getName(), fieldType, String.valueOf(in.readInt())); 52 | break; 53 | case "float": 54 | values.add(String.valueOf(in.readFloat())); 55 | //tup.setValue(field.getName(), fieldType, String.valueOf(in.readFloat())); 56 | break; 57 | case "dec": 58 | values.add(String.valueOf(in.readDouble())); 59 | //tup.setValue(field.getName(), fieldType, String.valueOf(in.readDouble())); 60 | break; 61 | case "bool": 62 | values.add(String.valueOf(in.readBoolean())); 63 | 64 | // tup.setValue(field.getName(), fieldType, String.valueOf(in.readBoolean())); 65 | break; 66 | case "date": 67 | values.add(String.valueOf(in.readUTF())); 68 | //NOT SURE WHAT TO DO HERE, LEFT BLANK INTENTIONALLY 69 | //tup.setValue(field.getName(), fieldType, String.valueOf(in.readUTF())); 70 | break; 71 | case "char": 72 | values.add(String.valueOf(in.readChar())); 73 | //tup.setValue(field.getName(), fieldType, in.readUTF()); 74 | break; 75 | case "str": 76 | //System.out.println("hey"); 77 | values.add(in.readUTF()); 78 | //tup.setValue(field.getName(), fieldType, in.readUTF()); 79 | break; 80 | default: 81 | throw new IOException("Unhandled type!!: " + fieldType); 82 | } 83 | 84 | } 85 | return parentTable.createNewTupleWithValues(values); 86 | } 87 | 88 | @Override 89 | public void serialize(SerializerOutput out, Tuple tup) throws IOException { 90 | int index = 0; 91 | ArrayList values = tup.values(); 92 | Iterator fieldIterator = fields.iterator(); 93 | 94 | while(fieldIterator.hasNext()) 95 | { 96 | fieldDetails field = fieldIterator.next(); 97 | String fieldType = field.getType(); 98 | switch(fieldType) 99 | { 100 | case "int": 101 | out.writeInt(Integer.parseInt(values.get(index))); 102 | break; 103 | case "float": 104 | out.writeFloat(Float.parseFloat(values.get(index))); 105 | break; 106 | case "dec": 107 | out.writeDouble(Double.parseDouble(values.get(index))); 108 | break; 109 | case "bool": 110 | out.writeBoolean(Boolean.parseBoolean(values.get(index))); 111 | break; 112 | case "date": 113 | //String value=; 114 | 115 | out.writeUTF(String.valueOf(values.get(index))); 116 | break; 117 | case "char": 118 | out.writeChar(values.get(index).charAt(0)); 119 | //out.writeUTF(); 120 | break; 121 | case "str": 122 | out.writeUTF(String.valueOf(values.get(index))); 123 | break; 124 | default: 125 | throw new IOException("Unhandled type!!: " + fieldType); 126 | } 127 | index++; 128 | } 129 | 130 | 131 | } 132 | } -------------------------------------------------------------------------------- /CSE562/Extra code/Extra1/testerBhai.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | import java.util.Map; 7 | import java.util.Map.Entry; 8 | import java.util.Set; 9 | import java.util.SortedMap; 10 | 11 | import edu.buffalo.cse562.Miscellaneous.SpecialStringComparator; 12 | import edu.buffalo.cse562.QueryHandler.SQLTable; 13 | import edu.buffalo.cse562.QueryHandler.Tuple; 14 | import edu.buffalo.cse562.QueryHandler.TupleSerializer; 15 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 16 | import jdbm.PrimaryMap; 17 | import jdbm.PrimaryTreeMap; 18 | import jdbm.RecordManager; 19 | import jdbm.RecordManagerFactory; 20 | import jdbm.SecondaryKeyExtractor; 21 | import jdbm.SecondaryTreeMap; 22 | import jdbm.btree.BTree; 23 | 24 | 25 | public class testerBhai 26 | { 27 | static RecordManager recMan; 28 | //static PrimaryTreeMap t1; 29 | static Set ptrKeys; 30 | //IndexHandler ih = null; 31 | 32 | public testerBhai(SQLTable tbl) 33 | { 34 | try 35 | { 36 | recMan = RecordManagerFactory.createRecordManager(tbl.getTableAlias().toUpperCase()); 37 | 38 | long recid = recMan.getNamedObject("orders_primary"); 39 | RecordManager lRecordManager = RecordManagerFactory.createRecordManager("C:/index/lineitem"); 40 | Tuple tpl; 41 | String primaryMapName=tbl.getTableAlias().toLowerCase()+"_primary"; 42 | PrimaryTreeMap primaryMap=lRecordManager.treeMap(primaryMapName, 43 | // new SpecialStringComparator(), 44 | new TupleSerializer(tbl) 45 | ); 46 | 47 | 48 | //System.out.println("hey"); 49 | // SecondaryTreeMap sMap = t1.secondaryTreeMap("newOrdersSecondary", 50 | // new SecondaryKeyExtractor() { 51 | // @Override 52 | // public String extractSecondaryKey(Integer key, 53 | // Tuple value) { 54 | // // TODO Auto-generated method stub 55 | // return value.valueForField("orderdate"); 56 | // } 57 | // } 58 | // ); 59 | 60 | System.out.println("oho"); 61 | /* PrimaryTreeMap> t3 = recMan.treeMap("orders_datenew");*/ 62 | //System.out.println(tp); 63 | //ptrKeys = t1.keySet(); 64 | //ih = new IndexHandler(recMan, t1,ptrKeys); 65 | //System.out.println(t1.size()); 66 | for(Entry entry : primaryMap.entrySet()) 67 | { 68 | System.out.println(entry.getValue().finalPrint()); 69 | } 70 | /*Set dateKeySet = sMap.keySet(); 71 | SortedMap> tpmap = sMap.subMap("1994-01-01", "1994-02-01"); 72 | for(String st : tpmap.keySet()) 73 | { 74 | Iterable itr = tpmap.get(st); 75 | for(int i : itr) 76 | { 77 | System.out.println(t1.get(i).finalPrint()); 78 | } 79 | //System.out.println("hey"); 80 | }*/ 81 | } 82 | catch(IOException e) 83 | { 84 | e.printStackTrace(); 85 | } 86 | } 87 | 88 | private Tuple getNextTuple() 89 | { 90 | return null; 91 | } 92 | 93 | public boolean hasNextTuple() 94 | { 95 | return false; 96 | //return ih.endOfIndex(); 97 | } 98 | 99 | public static void main(String args[]) 100 | { 101 | ArrayList tbl = new ArrayList(); 102 | ArrayList tbltype = new ArrayList(); 103 | ArrayList field = new ArrayList(); 104 | tbl.add("orderkey"); 105 | tbltype.add("INT"); 106 | tbl.add("partkey"); 107 | tbltype.add("INT"); 108 | tbl.add("suppkey"); 109 | tbltype.add("INT"); 110 | tbl.add("linenumber"); 111 | tbltype.add("INT"); 112 | tbl.add("quantity"); 113 | tbltype.add("DECIMAL"); 114 | tbl.add("extendedprice"); 115 | tbltype.add("DECIMAL"); 116 | tbl.add("discount"); 117 | tbltype.add("DECIMAL"); 118 | tbl.add("tax"); 119 | tbltype.add("DECIMAL"); 120 | tbl.add("returnflag"); 121 | tbltype.add("CHAR"); 122 | tbl.add("linestatus"); 123 | tbltype.add("CHAR"); 124 | tbl.add("shipdate"); 125 | tbltype.add("DATE"); 126 | tbl.add("commitdate"); 127 | tbltype.add("DATE"); 128 | tbl.add("receiptdate"); 129 | tbltype.add("DATE"); 130 | tbl.add("shipinstruct"); 131 | tbltype.add("VARCHAR(29)"); 132 | tbl.add("shipmode"); 133 | tbltype.add("VARCHAR(79)"); 134 | Iterator itr1 = tbl.iterator(); 135 | Iterator itr2 = tbltype.iterator(); 136 | while(itr1.hasNext() && itr2.hasNext()) 137 | { 138 | String str1 = itr1.next(); 139 | String str2 = itr2.next(); 140 | fieldDetails f = new fieldDetails(str1, str2); 141 | field.add(f); 142 | } 143 | SQLTable tbl1 = new SQLTable("LINEITEM",field); 144 | testerBhai idS = new testerBhai(tbl1); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /CSE562/Extra code/TestIndex.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import java.io.IOException; 4 | import java.io.Serializable; 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.Comparator; 8 | import java.util.Properties; 9 | 10 | import com.sun.rowset.internal.Row; 11 | 12 | import jdbm.PrimaryMap; 13 | import jdbm.PrimaryStoreMap; 14 | import jdbm.PrimaryTreeMap; 15 | import jdbm.RecordManager; 16 | import jdbm.RecordManagerFactory; 17 | import jdbm.SecondaryKeyExtractor; 18 | import jdbm.SecondaryTreeMap; 19 | import net.sf.jsqlparser.schema.Column; 20 | import net.sf.jsqlparser.schema.Table; 21 | import edu.buffalo.cse562.Operators.ScannerNode; 22 | import edu.buffalo.cse562.QueryHandler.CollectionSerializer; 23 | import edu.buffalo.cse562.QueryHandler.SQLTable; 24 | import edu.buffalo.cse562.QueryHandler.Tuple; 25 | import edu.buffalo.cse562.QueryHandler.TupleSerializer; 26 | 27 | public class TestIndex 28 | { 29 | String basePath = "C:/Users/Shail/Downloads/LittleBigDataEvaluation"; 30 | static String FileName = "C:/Users/Shail/Desktop/tpch-dataset/ORDERS.dat"; 31 | public static void IndexBuilderClass(SQLTable tbl,Column col,Column col1) 32 | { 33 | try { 34 | RecordManager recMan = RecordManagerFactory.createRecordManager(tbl.getTableAlias().toUpperCase()); 35 | // PrimaryTreeMap t1 = recMan.treeMap("odr_primary"); 36 | Tuple tpl; 37 | PrimaryTreeMap t1 = recMan.treeMap("orders_primary", 38 | new IntegerComparator(), 39 | new TupleSerializer(tbl.getSchema(),tbl.getTypes())); 40 | SecondaryTreeMap sMap = t1.secondaryTreeMap("newOrdersSecondary", 41 | new SecondaryKeyExtractor() { 42 | @Override 43 | public String extractSecondaryKey(Integer key, 44 | Tuple value) { 45 | // TODO Auto-generated method stub 46 | return value.valueForField("orderdate"); 47 | } 48 | } 49 | ); 50 | /*PrimaryTreeMap t2 = recMan.treeMap("orders_date", 51 | new StringComparator(), 52 | new TupleSerializer(tbl.getSchema(),tbl.getTypes()) 53 | ); 54 | */ 55 | //PrimaryTreeMap> t3 = recMan.treeMap("orders_datenew", new CollectionSerializer()); 56 | //PrimaryMap t3 = recMan.treeMap("orders_date"); 57 | 58 | /*PrimaryTreeMap t1 = recMan.treeMap 59 | ( 60 | tbl.getTableAlias()+"_primary", 61 | new StringComparator(tpl.valueForField("orderkey")), 62 | new TupleSerializer(tbl.getSchema(),tbl.getTypes()) 63 | ); 64 | */ 65 | 66 | // SQLTable tbl = new SQLTable(); 67 | ScannerNode scn = new ScannerNode 68 | ( 69 | null,tbl,FileName,false 70 | ); 71 | int count = 0; 72 | while(scn.hasNextTuple()) 73 | { 74 | // Optimization possible, Tuple to Integer 75 | tpl = scn.readNextTuple(); 76 | String key = tpl.valueForField(col.getColumnName()); 77 | //String keyforSecond = tpl.valueForField(col1.getColumnName()); 78 | int key1 = Integer.parseInt(key); 79 | t1.put(key1,tpl); 80 | //t2.put(keyforSecond, tpl); 81 | /*if(t3.containsKey(keyforSecond)) 82 | { 83 | ArrayList arr = t3.get(keyforSecond); 84 | arr.add(tpl); 85 | t3.put(keyforSecond, arr); 86 | } 87 | else 88 | { 89 | ArrayList arr = new ArrayList(); 90 | arr.add(tpl); 91 | t3.put(keyforSecond, arr); 92 | }*/ 93 | count++; 94 | if(count % 5000 == 0) //depends on how many tuples you want to commit. 95 | { 96 | recMan.commit(); 97 | } 98 | //System.out.println(count); 99 | } 100 | recMan.commit(); 101 | 102 | } 103 | catch (IOException e) 104 | { 105 | // TODO Auto-generated catch block 106 | e.printStackTrace(); 107 | } 108 | } 109 | public static void main(String args[]) 110 | { 111 | Column c1 = new Column(); 112 | c1.setColumnName("orderkey"); 113 | Column c2 = new Column(); 114 | c2.setColumnName("orderdate"); 115 | ArrayList tbl = new ArrayList(); 116 | ArrayList tbltype = new ArrayList(); 117 | tbl.add("orderkey"); 118 | tbltype.add("INT"); 119 | tbl.add("custkey"); 120 | tbltype.add("INT"); 121 | tbl.add("orderstatus"); 122 | tbltype.add("CHAR(1)"); 123 | tbl.add("totalprice"); 124 | tbltype.add("DECIMAL"); 125 | tbl.add("orderdate"); 126 | tbltype.add("DATE"); 127 | tbl.add("orderpriority"); 128 | tbltype.add("CHAR(15)"); 129 | tbl.add("clerk"); 130 | tbltype.add("CHAR(15)"); 131 | tbl.add("shippriority"); 132 | tbltype.add("INT"); 133 | tbl.add("comment"); 134 | tbltype.add("VARCHAR(79)"); 135 | SQLTable tbl1 = new SQLTable("ORDERS",tbl,tbltype); 136 | long startTime = System.currentTimeMillis(); 137 | IndexBuilderClass(tbl1,c1,c2); 138 | long endTime = System.currentTimeMillis(); 139 | long totalTime = endTime - startTime; 140 | System.out.println(totalTime/1000); 141 | } 142 | 143 | } 144 | 145 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/QueryHandler/Tuple.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.QueryHandler; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | 6 | public class Tuple implements Serializable, Comparable{ 7 | 8 | // Since we do not need to support insert/update queries, we can safely keep the type of values open. EDIT: Kill the person who wrote the previous comment. EDIT: That was me 9 | 10 | /** 11 | * 12 | */ 13 | private static final long serialVersionUID = 1L; 14 | //private ArrayList schema; 15 | private ArrayList values; 16 | private SQLTable ownerTable; 17 | 18 | // //Constructors 19 | 20 | public Tuple(SQLTable table) 21 | { 22 | ownerTable = table; 23 | values=new ArrayList(); 24 | } 25 | 26 | public Tuple(SQLTable table,ArrayList val) 27 | { 28 | //Values=new LinkedHashMap(); 29 | //schema=new ArrayList(); 30 | this.ownerTable=table; 31 | if(val==null) 32 | this.values=new ArrayList(); 33 | else 34 | this.values=val; 35 | 36 | } 37 | 38 | 39 | public Tuple(Tuple t) 40 | { 41 | //Values= new LinkedHashMap(); 42 | //this.readValuesForSchema(t.fields(), t.values(), null); 43 | //this.schema=new ArrayList(t.fields()); 44 | this.values=new ArrayList(t.values()); 45 | this.ownerTable=t.ownerTable; 46 | } 47 | // public Tuple(String[] schema) {} 48 | // 49 | // // Other member functions 50 | // public void setSchema(String[] schema) {} 51 | // public void readValues(String line){} 52 | // public void readValues(String[] values) {} 53 | // public void readValuesForSchema(ArrayList schema, ArrayList values, String table_name) 54 | // { 55 | //// for(int i=0;i fields() 137 | // { 138 | // return schema; 139 | //// Object[] k= Values.keySet().toArray(); 140 | //// ArrayList keys=new ArrayList(); 141 | //// for (Object o:k) 142 | //// { 143 | //// keys.add(o.toString()); 144 | //// } 145 | //// 146 | //// return keys; 147 | // } 148 | 149 | public ArrayList values() 150 | { 151 | return values; 152 | 153 | // Object[] v= Values.values().toArray(); 154 | // ArrayList values=new ArrayList(); 155 | // for (Object o:v) 156 | // { 157 | // values.add(o.toString()); 158 | // } 159 | // 160 | // return values; 161 | } 162 | public String convertToString() 163 | { 164 | 165 | String output=new String(); 166 | for( Object value : values) 167 | { 168 | // if(!start) 169 | // output=output+"|"; 170 | // else 171 | // start=false; 172 | 173 | output=output+value+"|"; 174 | } 175 | // 176 | // if(output.trim().endsWith("|")) 177 | // output = output.substring(0, output.length()-1); 178 | 179 | return output; 180 | } 181 | 182 | public String finalPrint() 183 | { 184 | String output = convertToString(); 185 | 186 | if(output.endsWith("|")) 187 | output = output.substring(0, output.length()-1); 188 | 189 | return output; 190 | } 191 | 192 | @Override 193 | public int compareTo(Tuple arg0) { 194 | // TODO Auto-generated method stub 195 | boolean b1 = this.equals(arg0); 196 | if(b1 == true) 197 | return 1; 198 | else 199 | return 0; 200 | } 201 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/IOHandler/IndexBuilder.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.IOHandler; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | 6 | import jdbm.PrimaryTreeMap; 7 | import jdbm.RecordManager; 8 | import jdbm.RecordManagerFactory; 9 | import jdbm.SecondaryKeyExtractor; 10 | import jdbm.SecondaryTreeMap; 11 | import edu.buffalo.cse562.Operators.ScannerNode; 12 | import edu.buffalo.cse562.QueryHandler.SQLTable; 13 | import edu.buffalo.cse562.QueryHandler.Tuple; 14 | import edu.buffalo.cse562.QueryHandler.TupleSerializer; 15 | 16 | public class IndexBuilder { 17 | 18 | String indexDir; 19 | String dataDir; 20 | static RecordManager lRecordManager; 21 | static int IndexDumpLimit = 40000; 22 | 23 | public IndexBuilder(String ddir,String idir) throws IOException 24 | { 25 | indexDir=idir; 26 | dataDir=ddir; 27 | lRecordManager = RecordManagerFactory.createRecordManager(indexDir+"/Index"); 28 | } 29 | 30 | public void buildIndexOnMultiplePrimaryKey(SQLTable parentTable) 31 | { 32 | try { 33 | //RecordManager lRecordManager = RecordManagerFactory.createRecordManager(indexDir+"/"+parentTable.getTableAlias().toLowerCase()); 34 | 35 | Tuple tpl; 36 | String primaryMapName=parentTable.getTableAlias().toLowerCase()+"_primary"; 37 | // System.out.println("Building for::"+primaryMapName); 38 | // long startTime = System.currentTimeMillis(); 39 | PrimaryTreeMap primaryMap=(PrimaryTreeMap) lRecordManager.treeMap(primaryMapName, 40 | //new SpecialStringComparator(), 41 | new TupleSerializer(parentTable) 42 | ); 43 | 44 | ArrayList>nonclusteredIndexList=new ArrayList<>(); 45 | for(String fKey:parentTable.getIndexField()) 46 | { 47 | String fKeyMapName=parentTable.getTableAlias().toLowerCase()+"_"+fKey; 48 | nonclusteredIndexList.add(primaryMap.secondaryTreeMap(fKeyMapName,new CustomSecondaryKeyExtractor(fKey))); 49 | 50 | } 51 | 52 | 53 | String basePath=dataDir+"/"+parentTable.getTableName()+".dat"; 54 | ScannerNode scn = new ScannerNode(null,parentTable,basePath,false); 55 | int count = 0; 56 | while(scn.hasNextTuple()) 57 | { 58 | // Optimization possible, Tuple to Integer 59 | 60 | tpl = scn.readNextTuple(); 61 | // IntegerSet primaryKey=new IntegerSet(); 62 | int value1=Integer.parseInt(tpl.valueForField(parentTable.getPrimaryField().get(0))); 63 | int value2=Integer.parseInt(tpl.valueForField(parentTable.getPrimaryField().get(1))); 64 | // for(String pkey : parentTable.getPrimaryField()) 65 | // { 66 | // 67 | // 68 | // primaryKey.list.add(Integer.parseInt(tpl.valueForField(pkey))); 69 | // } 70 | String key = value1+"#"+value2; 71 | primaryMap.put((PrimaryKeyType) key,tpl); 72 | count++; 73 | if(count % IndexDumpLimit == 0) //depends on how many tuples you want to commit. 74 | { 75 | lRecordManager.commit(); 76 | lRecordManager.defrag(); 77 | } 78 | //System.out.println(count); 79 | } 80 | lRecordManager.commit(); 81 | lRecordManager.defrag(); 82 | // long endTime = System.currentTimeMillis(); 83 | // System.out.println("Operation Took: "+(endTime-startTime)/1000+"s"); 84 | } 85 | catch (IOException e) 86 | { 87 | e.printStackTrace(); 88 | } 89 | } 90 | 91 | 92 | public void buildIndexOnSinglePrimaryKey(SQLTable parentTable) 93 | { 94 | try { 95 | //RecordManager lRecordManager = RecordManagerFactory.createRecordManager(indexDir+"/"+parentTable.getTableAlias().toLowerCase()); 96 | //lRecordManager = RecordManagerFactory.createRecordManager(indexDir+"/SHAIL"); 97 | Tuple tpl; 98 | String primaryMapName=parentTable.getTableAlias().toLowerCase()+"_primary"; 99 | // System.out.println("Building for::"+primaryMapName); 100 | // long startTime = System.currentTimeMillis(); 101 | PrimaryTreeMap primaryMap=(PrimaryTreeMap) lRecordManager.treeMap(primaryMapName, new TupleSerializer(parentTable)); 102 | 103 | 104 | ArrayList>nonclusteredIndexList=new ArrayList<>(); 105 | for(String fKey:parentTable.getIndexField()) 106 | { 107 | String fKeyMapName=parentTable.getTableAlias().toLowerCase()+"_"+fKey; 108 | nonclusteredIndexList.add(primaryMap.secondaryTreeMap(fKeyMapName,new CustomSecondaryKeyExtractor(fKey))); 109 | } 110 | 111 | 112 | String basePath=dataDir+"/"+parentTable.getTableName()+".dat"; 113 | ScannerNode scn = new ScannerNode(null,parentTable,basePath,false); 114 | int count = 0; 115 | while(scn.hasNextTuple()) 116 | { 117 | // Optimization possible, Tuple to Integer 118 | 119 | tpl = scn.readNextTuple(); 120 | String primaryField = parentTable.getPrimaryField().get(0); 121 | String primaryVal = tpl.valueForField(primaryField); 122 | //Integer primaryKey=Integer.parseInt(primaryVal); 123 | primaryMap.put((PrimaryKeyType) primaryVal,tpl); 124 | count++; 125 | if(count % IndexDumpLimit == 0) //depends on how many tuples you want to commit. 126 | { 127 | lRecordManager.commit(); 128 | lRecordManager.defrag(); 129 | } 130 | //System.out.println(count); 131 | } 132 | 133 | lRecordManager.commit(); 134 | lRecordManager.defrag(); 135 | // long endTime = System.currentTimeMillis(); 136 | // System.out.println("Operation Took: "+(endTime-startTime)/1000+"s"); 137 | } 138 | catch (IOException e) 139 | { 140 | e.printStackTrace(); 141 | } 142 | } 143 | 144 | public class CustomSecondaryKeyExtractor implements SecondaryKeyExtractor 145 | { 146 | private String fKey; 147 | public CustomSecondaryKeyExtractor(String key) 148 | { 149 | fKey=key; 150 | } 151 | public ForeignKeyType extractSecondaryKey(Object key,Object value) 152 | { 153 | return (ForeignKeyType)((Tuple)value).valueForField(fKey); 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/AbstractExpressionVisitor.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | 3 | import net.sf.jsqlparser.expression.*; 4 | import net.sf.jsqlparser.expression.operators.arithmetic.*; 5 | import net.sf.jsqlparser.expression.operators.conditional.*; 6 | import net.sf.jsqlparser.expression.operators.relational.*; 7 | import net.sf.jsqlparser.schema.Column; 8 | import net.sf.jsqlparser.statement.select.SubSelect; 9 | 10 | /** 11 | * 12 | * Default behavior for ExpressionVisitors: raise UnsupportedOperationException 13 | * 14 | * @author Niccolo' Meneghetti 15 | */ 16 | public class AbstractExpressionVisitor implements ExpressionVisitor 17 | { 18 | 19 | @Override 20 | public void visit(NullValue nv) { 21 | throw new UnsupportedOperationException("Not supported yet."); 22 | } 23 | 24 | @Override 25 | public void visit(Function fnctn) { 26 | throw new UnsupportedOperationException("Not supported yet."); 27 | } 28 | 29 | @Override 30 | public void visit(InverseExpression ie) { 31 | throw new UnsupportedOperationException("Not supported yet."); 32 | } 33 | 34 | @Override 35 | public void visit(JdbcParameter jp) { 36 | throw new UnsupportedOperationException("Not supported yet."); 37 | } 38 | 39 | @Override 40 | public void visit(DoubleValue dv) { 41 | throw new UnsupportedOperationException("Not supported yet."); 42 | } 43 | 44 | @Override 45 | public void visit(LongValue lv) { 46 | throw new UnsupportedOperationException("Not supported yet."); 47 | } 48 | 49 | @Override 50 | public void visit(DateValue dv) { 51 | throw new UnsupportedOperationException("Not supported yet."); 52 | } 53 | 54 | @Override 55 | public void visit(TimeValue tv) { 56 | throw new UnsupportedOperationException("Not supported yet."); 57 | } 58 | 59 | @Override 60 | public void visit(TimestampValue tv) { 61 | throw new UnsupportedOperationException("Not supported yet."); 62 | } 63 | 64 | @Override 65 | public void visit(Parenthesis prnths) { 66 | throw new UnsupportedOperationException("Not supported yet."); 67 | } 68 | 69 | @Override 70 | public void visit(StringValue sv) { 71 | throw new UnsupportedOperationException("Not supported yet."); 72 | } 73 | 74 | @Override 75 | public void visit(Addition adtn) { 76 | throw new UnsupportedOperationException("Not supported yet."); 77 | } 78 | 79 | @Override 80 | public void visit(Division dvsn) { 81 | throw new UnsupportedOperationException("Not supported yet."); 82 | } 83 | 84 | @Override 85 | public void visit(Multiplication m) { 86 | throw new UnsupportedOperationException("Not supported yet."); 87 | } 88 | 89 | @Override 90 | public void visit(Subtraction s) { 91 | throw new UnsupportedOperationException("Not supported yet."); 92 | } 93 | 94 | @Override 95 | public void visit(AndExpression ae) { 96 | throw new UnsupportedOperationException("Not supported yet."); 97 | } 98 | 99 | @Override 100 | public void visit(OrExpression oe) { 101 | throw new UnsupportedOperationException("Not supported yet."); 102 | } 103 | 104 | @Override 105 | public void visit(Between btwn) { 106 | throw new UnsupportedOperationException("Not supported yet."); 107 | } 108 | 109 | @Override 110 | public void visit(EqualsTo et) { 111 | throw new UnsupportedOperationException("Not supported yet."); 112 | } 113 | 114 | @Override 115 | public void visit(GreaterThan gt) { 116 | throw new UnsupportedOperationException("Not supported yet."); 117 | } 118 | 119 | @Override 120 | public void visit(GreaterThanEquals gte) { 121 | throw new UnsupportedOperationException("Not supported yet."); 122 | } 123 | 124 | @Override 125 | public void visit(InExpression ie) { 126 | throw new UnsupportedOperationException("Not supported yet."); 127 | } 128 | 129 | @Override 130 | public void visit(IsNullExpression ine) { 131 | throw new UnsupportedOperationException("Not supported yet."); 132 | } 133 | 134 | @Override 135 | public void visit(LikeExpression le) { 136 | throw new UnsupportedOperationException("Not supported yet."); 137 | } 138 | 139 | @Override 140 | public void visit(MinorThan mt) { 141 | throw new UnsupportedOperationException("Not supported yet."); 142 | } 143 | 144 | @Override 145 | public void visit(MinorThanEquals mte) { 146 | throw new UnsupportedOperationException("Not supported yet."); 147 | } 148 | 149 | @Override 150 | public void visit(NotEqualsTo net) { 151 | throw new UnsupportedOperationException("Not supported yet."); 152 | } 153 | 154 | @Override 155 | public void visit(Column column) { 156 | throw new UnsupportedOperationException("Not supported yet."); 157 | } 158 | 159 | @Override 160 | public void visit(SubSelect ss) { 161 | throw new UnsupportedOperationException("Not supported yet."); 162 | } 163 | 164 | @Override 165 | public void visit(CaseExpression ce) { 166 | throw new UnsupportedOperationException("Not supported yet."); 167 | } 168 | 169 | @Override 170 | public void visit(WhenClause wc) { 171 | throw new UnsupportedOperationException("Not supported yet."); 172 | } 173 | 174 | @Override 175 | public void visit(ExistsExpression ee) { 176 | throw new UnsupportedOperationException("Not supported yet."); 177 | } 178 | 179 | @Override 180 | public void visit(AllComparisonExpression ace) { 181 | throw new UnsupportedOperationException("Not supported yet."); 182 | } 183 | 184 | @Override 185 | public void visit(AnyComparisonExpression ace) { 186 | throw new UnsupportedOperationException("Not supported yet."); 187 | } 188 | 189 | @Override 190 | public void visit(Concat concat) { 191 | throw new UnsupportedOperationException("Not supported yet."); 192 | } 193 | 194 | @Override 195 | public void visit(Matches mtchs) { 196 | throw new UnsupportedOperationException("Not supported yet."); 197 | } 198 | 199 | @Override 200 | public void visit(BitwiseAnd ba) { 201 | throw new UnsupportedOperationException("Not supported yet."); 202 | } 203 | 204 | @Override 205 | public void visit(BitwiseOr bo) { 206 | throw new UnsupportedOperationException("Not supported yet."); 207 | } 208 | 209 | @Override 210 | public void visit(BitwiseXor bx) { 211 | throw new UnsupportedOperationException("Not supported yet."); 212 | } 213 | 214 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Evaluators/ArithmeticEvaluator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package edu.buffalo.cse562.Evaluators; 5 | 6 | import net.sf.jsqlparser.expression.DoubleValue; 7 | import net.sf.jsqlparser.expression.Expression; 8 | import net.sf.jsqlparser.expression.Parenthesis; 9 | import net.sf.jsqlparser.expression.operators.arithmetic.Addition; 10 | import net.sf.jsqlparser.expression.operators.arithmetic.Division; 11 | import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication; 12 | import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction; 13 | import net.sf.jsqlparser.schema.Column; 14 | import edu.buffalo.cse562.AbstractExpressionVisitor; 15 | import edu.buffalo.cse562.QueryHandler.Tuple; 16 | 17 | /** 18 | * @author Sumit Agarwal 19 | * 20 | */ 21 | public class ArithmeticEvaluator extends AbstractExpressionVisitor { 22 | private double result; 23 | private Tuple inputTuple; 24 | 25 | public double getResult() 26 | { 27 | return result; 28 | } 29 | public ArithmeticEvaluator(Tuple in) 30 | { 31 | inputTuple = in; 32 | } 33 | 34 | //@Override 35 | // public void visit(double dv) 36 | // { 37 | // result=dv.getValue(); 38 | // } 39 | 40 | 41 | @Override 42 | public void visit(Addition adtn) 43 | { 44 | Expression leftex=adtn.getLeftExpression(); 45 | Expression rightex=adtn.getRightExpression(); 46 | double leftValue = 0; 47 | double rightValue = 0; 48 | 49 | if(leftex instanceof Column) 50 | leftValue = Double.parseDouble(inputTuple.valueForField(((Column) leftex).toString()));//Double.valueOf(); 51 | else if(isArithemetic(leftex)) 52 | { 53 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 54 | leftex.accept(ae); 55 | leftValue=ae.getResult(); 56 | } 57 | else 58 | leftValue= Double.parseDouble(leftex.toString()); 59 | 60 | if(rightex instanceof Column) 61 | rightValue= Double.parseDouble(inputTuple.valueForField(((Column) rightex).toString()));//.valueOf();//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 62 | else if(isArithemetic(rightex)) 63 | { 64 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 65 | rightex.accept(ae); 66 | rightValue=ae.getResult(); 67 | } 68 | else 69 | rightValue=Double.parseDouble(rightex.toString());//Double.valueOf(rightex.toString()); 70 | 71 | result = leftValue + rightValue;//(leftValue+rightValue); 72 | 73 | } 74 | 75 | @Override 76 | public void visit(Subtraction s) 77 | { 78 | Expression leftex=s.getLeftExpression(); 79 | Expression rightex=s.getRightExpression(); 80 | double leftValue = 0; 81 | double rightValue = 0; 82 | 83 | if(leftex instanceof Column) 84 | leftValue= Double.parseDouble(inputTuple.valueForField(((Column) leftex).toString())); 85 | else if(isArithemetic(leftex)) 86 | { 87 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 88 | leftex.accept(ae); 89 | leftValue=ae.getResult(); 90 | } 91 | else 92 | leftValue= Double.parseDouble(leftex.toString()); 93 | 94 | if(rightex instanceof Column) 95 | rightValue=Double.parseDouble(inputTuple.valueForField(((Column) rightex).toString()));//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 96 | else if(isArithemetic(rightex)) 97 | { 98 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 99 | rightex.accept(ae); 100 | rightValue=ae.getResult(); 101 | } 102 | else 103 | rightValue= Double.parseDouble(rightex.toString()); 104 | 105 | result = leftValue - rightValue;//leftValue-rightValue); 106 | 107 | } 108 | 109 | @Override 110 | public void visit(Multiplication mul) 111 | { 112 | Expression leftex=mul.getLeftExpression(); 113 | Expression rightex=mul.getRightExpression(); 114 | double leftValue = 0; 115 | double rightValue = 0; 116 | 117 | if(leftex instanceof Column) 118 | leftValue=Double.parseDouble(inputTuple.valueForField(((Column) leftex).toString()));//.valueOf(); 119 | else if(isArithemetic(leftex)) 120 | { 121 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 122 | leftex.accept(ae); 123 | leftValue=ae.getResult(); 124 | } 125 | else 126 | leftValue=Double.parseDouble(leftex.toString());//.valueOf(); 127 | 128 | if(rightex instanceof Column) 129 | rightValue=Double.parseDouble(inputTuple.valueForField(((Column) rightex).toString()));//.valueOf();//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 130 | else if(isArithemetic(rightex)) 131 | { 132 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 133 | rightex.accept(ae); 134 | rightValue=ae.getResult(); 135 | } 136 | else 137 | rightValue= Double.parseDouble(rightex.toString());//.valueOf(); 138 | 139 | result= leftValue * rightValue;//(leftValue*rightValue); 140 | } 141 | 142 | @Override 143 | public void visit(Division div) 144 | { 145 | Expression leftex=div.getLeftExpression(); 146 | Expression rightex=div.getRightExpression(); 147 | double leftValue = 0; 148 | double rightValue = 0; 149 | 150 | if(leftex instanceof Column) 151 | leftValue=Double.parseDouble(inputTuple.valueForField(((Column) leftex).toString()));//.valueOf(); 152 | else if(isArithemetic(leftex)) 153 | { 154 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 155 | leftex.accept(ae); 156 | leftValue=ae.getResult(); 157 | } 158 | else 159 | leftValue=Double.parseDouble(leftex.toString());//.valueOf(); 160 | 161 | if(rightex instanceof Column) 162 | rightValue=Double.parseDouble(inputTuple.valueForField(((Column) rightex).toString()));//.valueOf();//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 163 | else if(isArithemetic(rightex)) 164 | { 165 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 166 | rightex.accept(ae); 167 | rightValue=ae.getResult(); 168 | } 169 | else 170 | rightValue= Double.parseDouble(rightex.toString());//.valueOf(); 171 | 172 | result = leftValue / rightValue; 173 | } 174 | 175 | @Override 176 | public void visit(Parenthesis prnths) 177 | { 178 | Expression exp=prnths.getExpression(); 179 | //prnths.getExpression().accept(this); 180 | ArithmeticEvaluator aeva=new ArithmeticEvaluator(inputTuple); 181 | exp.accept(aeva); 182 | result=aeva.getResult(); 183 | 184 | 185 | } 186 | 187 | public void visit(Column column) { 188 | result=Double.parseDouble(inputTuple.valueForField(column.toString()));;//.valueOf(); 189 | // throw new UnsupportedOperationException("Not supported yet."); 190 | } 191 | private boolean isArithemetic(Expression e) 192 | { 193 | return (e instanceof Addition || e instanceof Subtraction || e instanceof Multiplication || e instanceof Division || e instanceof Parenthesis); 194 | } 195 | 196 | public static boolean isArithMeticOperator(Expression e) 197 | { 198 | return (e instanceof Addition || e instanceof Subtraction || e instanceof Multiplication || e instanceof Division); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/QueryHandler/Indexer.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.QueryHandler; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | import java.util.Collection; 9 | 10 | //import com.sun.7ml.internal.ws.policy.privateutil.PolicyUtils.Collections; 11 | 12 | //Inspired from the implementation of B+ tree in wikipedia (Subhranil) 13 | public class Indexer, Value> 14 | { 15 | /** Pointer to the root node. It may be a leaf or an inner node, but it is never null. */ 16 | private Node root; 17 | /** the maximum number of keys in the leaf node, M must be > 0 */ 18 | //private final int M; 19 | /** the maximum number of keys in inner node, the number of pointer is N+1, N must be > 2 */ 20 | private final int MaxKeyCount; 21 | 22 | /** Create a new empty tree. */ 23 | public Indexer(int n) { 24 | MaxKeyCount=n; 25 | root=new LeafNode(); 26 | } 27 | 28 | 29 | public void insert(Key key, Value value) 30 | { 31 | System.out.println("insert key=" + key); 32 | SplitNode result = root.insert(key, value); 33 | if(result!=null) 34 | { 35 | // The old root was splitted in two parts. 36 | // We have to create a new root pointing to them 37 | NonLeafNode newRoot = new NonLeafNode(); 38 | newRoot.keycount=1; 39 | newRoot.keys[0]=result.key; 40 | newRoot.children[0]= result.leftChild; 41 | newRoot.children[1]= result.rightChild; 42 | root=newRoot; 43 | } 44 | } 45 | 46 | 47 | 48 | // public void rangeLookup(Key minValue,Key maxValue,Value[] offsets) 49 | // { 50 | // offsets[0]=search(minValue); 51 | // offsets[1]=search(maxValue); 52 | // } 53 | // 54 | // public Value search(Key key) 55 | // { 56 | // Node node = root; 57 | // while(node instanceof Indexer.NonLeafNode) 58 | // { // need to traverse down to the leaf 59 | // NonLeafNode inner=(NonLeafNode) node; 60 | // int index=inner.findLocation(key); 61 | // node=inner.children[index]; 62 | // } 63 | // 64 | // 65 | // LeafNode leaf = (LeafNode) node; 66 | // int index=leaf.findLocation(key); 67 | // if(index> values; 95 | private LeafNode successor; 96 | private LeafNode predecessor; 97 | 98 | public LeafNode() 99 | { 100 | keys= (Key[]) new Comparable[MaxKeyCount]; 101 | values=new ArrayList>(MaxKeyCount); 102 | predecessor=null; 103 | successor=null; 104 | //values= (ArrayList[]) new Object[MaxKeyCount]; // offset values for the different keys 105 | // for(int i=0;i()); 108 | // } 109 | 110 | } 111 | 112 | // Returns the proper bucket for adding the key 113 | public int findLocation(Key key) 114 | { 115 | 116 | //Implementeed as linear search . optimize(subhro) 117 | for (int i= 0;i=0) 120 | { 121 | return i; 122 | } 123 | } 124 | return keycount; 125 | } 126 | 127 | public SplitNode insert(Key key, Value value) 128 | { 129 | 130 | int index = findLocation(key); 131 | if(index>(values.subList(mid, MaxKeyCount-1)); 154 | values=new ArrayList>(values.subList(0, mid-1)); 155 | 156 | keycount=mid-1; 157 | if (index()); 179 | values.get(index).add(value); 180 | keycount++; 181 | } 182 | public void dump() 183 | { 184 | System.out.println("LeafNode h==0"); 185 | for (int i=0;i0) 211 | { 212 | return i; 213 | } 214 | } 215 | return keycount; 216 | } 217 | 218 | public SplitNode insert(Key key, Value value) 219 | { 220 | 221 | if(keycount==MaxKeyCount) // splitting (may not be necessary) but just for safety 222 | { // Split 223 | int mid=(keycount+1)/2; 224 | int siblingSize=keycount-mid; 225 | 226 | NonLeafNode sibling = new NonLeafNode(); 227 | sibling.keycount=siblingSize; 228 | 229 | 230 | System.arraycopy(keys,mid,sibling.keys,0,siblingSize); 231 | System.arraycopy(children,mid,sibling.children,0,siblingSize+1); 232 | 233 | 234 | keycount=mid-1;//this is important, so the middle one elevate to next depth(height), inner node's key don't repeat itself 235 | 236 | // Set up the return variable 237 | SplitNode result = new SplitNode(this.keys[mid-1],this, sibling); 238 | 239 | // Now insert in the appropriate sibling 240 | if (key.compareTo(result.key)<0) { 241 | insertNonfull(key,value); 242 | } else { 243 | sibling.insertNonfull(key, value); 244 | } 245 | return result; 246 | 247 | } 248 | else 249 | { // No split 250 | insertNonfull(key, value); 251 | return null; 252 | } 253 | } 254 | 255 | private void insertNonfull(Key key, Value value) { 256 | // Simple linear search 257 | int index=findLocation(key); 258 | SplitNode result=children[index].insert(key,value); 259 | 260 | if(result!=null) // the child node was split , handle pointers accordingly 261 | { 262 | if(index==keycount) 263 | { 264 | // Insertion at the rightmost key 265 | keys[index]=result.key; 266 | children[index]=result.leftChild; 267 | children[index+1]=result.rightChild; 268 | keycount++; 269 | } 270 | else 271 | { 272 | // Insertion not at the rightmost key 273 | //shift i>idx to the right 274 | System.arraycopy(keys, index, keys,index+1,keycount-index); 275 | System.arraycopy(children,index,children,index+1,keycount-index+1); 276 | 277 | children[index]=result.leftChild; 278 | children[index+1]=result.rightChild; 279 | keys[index]=result.key; 280 | keycount++; 281 | } 282 | } // else the current node is not affected 283 | } 284 | 285 | /** 286 | * This one only dump integer key 287 | */ 288 | public void dump() { 289 | System.out.println("NonLeafNode h==?"); 290 | for (int i=0;i'); 293 | System.out.println(keys[i]); 294 | } 295 | children[keycount].dump(); 296 | } 297 | } 298 | 299 | class SplitNode 300 | { 301 | public final Key key; 302 | public final Node leftChild; 303 | public final Node rightChild; 304 | 305 | public SplitNode(Key k, Node l, Node r) 306 | { 307 | key= k; 308 | leftChild = l; 309 | rightChild = r; 310 | } 311 | } 312 | 313 | public int binarySearch(Key[] keyList,Key K) 314 | { 315 | 316 | int low=0,high=keyList.length-1; 317 | int index=0; 318 | while(low indexer=new Indexer(5); 373 | 374 | int off=0; 375 | for(String str:lines) 376 | { 377 | 378 | Integer i=Integer.parseInt(str); 379 | indexer.insert(i, off); 380 | off+=str.length(); 381 | } 382 | 383 | Integer[] finalValue=new Integer[2]; 384 | //indexer.rangeLookup(Integer.valueOf(50),Integer.valueOf(61),finalValue); 385 | System.out.println(finalValue[0]+"_______"+finalValue[1]); 386 | 387 | } 388 | 389 | 390 | } 391 | 392 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/IndexScanner.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import java.io.IOException; 4 | import java.text.DecimalFormat; 5 | import java.util.ArrayList; 6 | import java.util.Iterator; 7 | import java.util.SortedMap; 8 | import java.util.TreeMap; 9 | 10 | import jdbm.PrimaryTreeMap; 11 | import jdbm.RecordManager; 12 | import jdbm.RecordManagerFactory; 13 | import jdbm.SecondaryTreeMap; 14 | import net.sf.jsqlparser.expression.Expression; 15 | import net.sf.jsqlparser.expression.Function; 16 | import net.sf.jsqlparser.schema.Column; 17 | import edu.buffalo.cse562.Evaluators.ArithmeticEvaluator; 18 | import edu.buffalo.cse562.Miscellaneous.CustomSecondaryKeyExtractor; 19 | import edu.buffalo.cse562.Miscellaneous.IndexScanOperation; 20 | import edu.buffalo.cse562.QueryHandler.SQLTable; 21 | import edu.buffalo.cse562.QueryHandler.Tuple; 22 | import edu.buffalo.cse562.QueryHandler.TupleSerializer; 23 | 24 | public class IndexScanner extends ASTNode{ 25 | private static IndexScanner idS; 26 | static RecordManager lRecordManager; 27 | static String indexDir; 28 | protected IndexScanner(String dir) throws IOException 29 | { 30 | indexDir = dir; 31 | lRecordManager = RecordManagerFactory.createRecordManager(indexDir+"/Index"); 32 | } 33 | public static IndexScanner getInstance(String dir) throws IOException 34 | { 35 | if(idS == null && indexDir == null) 36 | { 37 | idS = new IndexScanner(dir); 38 | indexDir = dir; 39 | } 40 | return idS; 41 | } 42 | 43 | public InnerScanner returnClass(SQLTable parent) 44 | { 45 | return new InnerScanner(parent); 46 | } 47 | public class InnerScanner 48 | { 49 | SQLTable parentTable; 50 | IndexScanOperation operation; 51 | Iterator primaryTupleIterator; 52 | Iterator secondaryIterator; 53 | Iterator primaryTableIterator; 54 | SortedMap> resultMap; 55 | PrimaryTreeMap primaryMap; 56 | int flag = 0; 57 | boolean shouldContinue; 58 | public InnerScanner(SQLTable parent) 59 | { 60 | parentTable=parent; 61 | primaryTupleIterator=null; 62 | secondaryIterator=null; 63 | primaryTableIterator = null; 64 | resultMap=null; 65 | 66 | String primaryMapName=parentTable.getTableName().toLowerCase()+"_primary"; 67 | primaryMap=(PrimaryTreeMap) lRecordManager.treeMap(primaryMapName, 68 | new TupleSerializer(parentTable)); 69 | } 70 | 71 | public void run(IndexScanOperation op) 72 | { 73 | try 74 | { 75 | //parentTable=table; 76 | shouldContinue=true; 77 | operation=op; 78 | if(operation == null) 79 | { 80 | return; 81 | } 82 | if(operation.type.equals("SCAN")) 83 | { 84 | flag = 2; //help I am primary! 85 | /* 86 | * 0 : Secondary 87 | * 1 : Primary 88 | * 2 : Scan 89 | */ 90 | } 91 | else if(operation.type.equals("PRIMARY")) 92 | { 93 | flag = 1; 94 | } 95 | //RecordManager lRecordManager=RecordManagerFactory.createRecordManager(indexDir+"/"+parentTable.getTableName().toLowerCase()); 96 | 97 | //long tp = lRecordManager.getNamedObject(primaryMapName); 98 | 99 | 100 | // for(PrimaryKeyType s:primaryMap.keySet()) 101 | // { 102 | // System.out.println(s+"--->"+primaryMap.get(s).convertToString()); 103 | // } 104 | // 105 | String fKeyMapName=parentTable.getTableName().toLowerCase()+"_"+operation.key; 106 | SecondaryTreeMap secondaryMap=primaryMap.secondaryTreeMap(fKeyMapName,new CustomSecondaryKeyExtractor(operation.key)); 107 | SortedMap> tempResult=secondaryMap.subMap(String.valueOf(operation.lowerLimit), String.valueOf(operation.upperLimit)); 108 | TreeMap> tempResultOne = new TreeMap>(); 109 | if(operation.upperInclusive) 110 | { 111 | Iterable result = secondaryMap.get(String.valueOf(operation.upperLimit)); 112 | if(result != null) 113 | tempResultOne.put(String.valueOf(operation.upperLimit), result); //Copy upper limit. 114 | //This is tested and working!!! 115 | 116 | } 117 | /* 118 | * This is actually not required, but just to keep the type of map consistent, and to display in the loop below. 119 | */ 120 | /*SortedMap> tempResultOnenew = (SortedMap>) tempResultOne; 121 | for(Entry> entry : tempResultOnenew.entrySet()) 122 | { 123 | Iterable itr = entry.getValue(); 124 | for(PrimaryKeyType p : itr) 125 | { 126 | System.out.println(primaryMap.find(p).finalPrint()); 127 | //primaryMap.find(p); 128 | } 129 | } 130 | resultMap=tempResultOnenew;*/ 131 | tempResultOne.putAll(tempResult); //Copy rest of values from tempResult 132 | resultMap=(SortedMap>)tempResultOne; 133 | primaryTupleIterator=resultMap.keySet().iterator(); 134 | primaryTableIterator = (Iterator) primaryMap.keySet().iterator(); 135 | if(operation.lowerInclusive==false) 136 | { 137 | if(!primaryTupleIterator.hasNext()) // null set return 138 | return; 139 | 140 | primaryTupleIterator.next(); 141 | } 142 | 143 | if(flag == 0 && primaryTupleIterator.hasNext()) 144 | secondaryIterator=resultMap.get(primaryTupleIterator.next()).iterator(); 145 | else 146 | { 147 | //primary map scan 148 | resultMap.clear(); 149 | } 150 | secondaryMap=null; 151 | //tempResult.clear(); 152 | tempResult=null; 153 | tempResultOne=null; 154 | //tempResultOne.clear(); 155 | System.gc(); 156 | } 157 | catch(Exception e) 158 | { 159 | e.printStackTrace(); 160 | } 161 | } 162 | public boolean hasNextTuple() 163 | { 164 | boolean value=primaryTupleIterator.hasNext() || (secondaryIterator!=null && secondaryIterator.hasNext()) || (primaryTableIterator.hasNext() && flag == 2) || (!primaryMap.isEmpty() && flag == 1 && shouldContinue); 165 | if(!value) 166 | { 167 | // primaryMap.clear();primaryMap=null; 168 | resultMap.clear(); 169 | //resultMap=null; 170 | 171 | } 172 | return value; 173 | } 174 | 175 | public Tuple readNextTuple() 176 | { 177 | try { 178 | if(secondaryIterator != null && secondaryIterator.hasNext()) 179 | //Subhranil, yahan se comparator call ho raha hai. 180 | { 181 | 182 | //TODO 183 | /* 184 | * Integer being casted to String here!!!! 185 | * Causing crash in tpch10!!! 186 | */ 187 | String val=(String)secondaryIterator.next(); 188 | Tuple tupe=primaryMap.get(val); 189 | return tupe; 190 | } 191 | if(hasNextTuple()) 192 | { 193 | if(flag == 1 && !primaryMap.isEmpty()) 194 | { 195 | //System.out.println(primaryMap.keySet().getClass()); 196 | 197 | 198 | // for(PrimaryKeyType k:primaryMap.keySet()) 199 | // { 200 | // System.out.println(k.getClass()); 201 | // break; 202 | // } 203 | //System.out.println(primaryMap.find((PrimaryKeyType)).convertToString()); 204 | if(primaryMap.containsKey((PrimaryKeyType)operation.lowerLimit)) 205 | { 206 | 207 | if(operation.lowerLimit != operation.upperLimit) 208 | throw new Exception("ABEY EQUAL NAHI HAI!"); 209 | 210 | 211 | Tuple t= primaryMap.get(operation.lowerLimit); 212 | boolean b = t.setOwner(parentTable); 213 | shouldContinue=false; 214 | return t; 215 | } 216 | else 217 | return null; 218 | } 219 | else if(flag == 0 && primaryTupleIterator.hasNext() ) 220 | { 221 | secondaryIterator=resultMap.get(primaryTupleIterator.next()).iterator(); 222 | return readNextTuple(); 223 | } 224 | else if(primaryTableIterator.hasNext() && flag == 2) 225 | { 226 | // String key=null; 227 | // if(!primaryTableIterator.hasNext()) 228 | // System.out.println("asdsd"); 229 | // //else 230 | // //key=.getClass().toString(); 231 | Tuple t=primaryMap.get(primaryTableIterator.next()); 232 | boolean b = t.setOwner(parentTable); 233 | return t; 234 | } 235 | } 236 | } 237 | catch(Exception e) 238 | { 239 | e.printStackTrace(); 240 | } 241 | return null; 242 | } 243 | public void close() 244 | { 245 | primaryMap.clear(); 246 | primaryMap=null; 247 | System.gc(); 248 | } 249 | public Tuple validateTuple(Tuple newTuple, ArrayList filters) { 250 | SelectNode selectnode = new SelectNode(); 251 | selectnode.setAliasMap(aliasMap); 252 | boolean isValidTuple = true; 253 | 254 | if(filters != null && !filters.isEmpty()) 255 | { 256 | Iterator expressions = filters.iterator(); 257 | Expression expression; 258 | 259 | while(expressions.hasNext()) 260 | { 261 | expression = expressions.next(); 262 | if(!selectnode.filterTuple(newTuple, expression)) 263 | { 264 | isValidTuple=false; 265 | break; 266 | } 267 | } 268 | } 269 | if(isValidTuple) 270 | return newTuple; 271 | else 272 | return null; 273 | } 274 | } 275 | 276 | public void handleAlias(Tuple t) 277 | { 278 | //System.out.println("Handle Alias!"); 279 | 280 | //System.out.println("Value Size: " + t.values().size()); 281 | //System.out.println("Schema Size: " + t.getOwner().getSchema().size()); 282 | 283 | if(aliasMap != null && !aliasMap.isEmpty()) 284 | { 285 | if(true) 286 | { 287 | Iterator it=aliasMap.keySet().iterator(); 288 | while(it.hasNext()) //&& t.values().size() == t.getOwner().getSchema().size()) 289 | { 290 | String aName=it.next().toString(); 291 | Expression exp = aliasMap.get(aName); 292 | if(aName.equals(exp.toString()) || exp instanceof Function) 293 | continue; 294 | 295 | if(ArithmeticEvaluator.isArithMeticOperator(exp)) 296 | { 297 | ArithmeticEvaluator aeva=new ArithmeticEvaluator(t); 298 | exp.accept(aeva); 299 | 300 | // To drop the trailing zeroes 301 | double result = aeva.getResult(); 302 | DecimalFormat df=new DecimalFormat("###.#"); 303 | 304 | //Add Filed Type 305 | t.setValue(aName, "dec", df.format(result)); 306 | } 307 | else // normal field alias 308 | { 309 | //Add Filed Type 310 | 311 | Expression exp1 = aliasMap.get(aName); 312 | String type = null; 313 | if(exp1 instanceof Column) { 314 | type = "str"; 315 | } 316 | else { 317 | type = "dec"; 318 | } 319 | t.setValue(aName, type, t.valueForField(exp.toString())); 320 | } 321 | } 322 | } 323 | } 324 | } 325 | 326 | public Tuple validateTuple(Tuple newTuple, ArrayList filters) { 327 | SelectNode selectnode = new SelectNode(); 328 | selectnode.setAliasMap(aliasMap); 329 | boolean isValidTuple = true; 330 | 331 | if(filters != null && !filters.isEmpty()) 332 | { 333 | Iterator expressions = filters.iterator(); 334 | Expression expression; 335 | 336 | while(expressions.hasNext()) 337 | { 338 | expression = expressions.next(); 339 | if(!selectnode.filterTuple(newTuple, expression)) 340 | { 341 | isValidTuple=false; 342 | break; 343 | } 344 | } 345 | } 346 | if(isValidTuple) 347 | return newTuple; 348 | else 349 | return null; 350 | } 351 | } 352 | -------------------------------------------------------------------------------- /CSE562/Extra code/newclass.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562; 2 | import java.util.List; 3 | import java.io.IOException; 4 | import java.io.StringReader; 5 | import java.util.ArrayList; 6 | import java.util.Collection; 7 | import java.util.HashMap; 8 | import java.util.Iterator; 9 | import java.util.Properties; 10 | import java.util.Set; 11 | 12 | import net.sf.jsqlparser.JSQLParserException; 13 | import net.sf.jsqlparser.expression.Expression; 14 | import net.sf.jsqlparser.expression.Function; 15 | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; 16 | import net.sf.jsqlparser.expression.operators.conditional.OrExpression; 17 | import net.sf.jsqlparser.expression.operators.relational.EqualsTo; 18 | import net.sf.jsqlparser.expression.operators.relational.ExpressionList; 19 | import net.sf.jsqlparser.expression.operators.relational.GreaterThan; 20 | import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals; 21 | import net.sf.jsqlparser.expression.operators.relational.MinorThan; 22 | import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals; 23 | import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo; 24 | import net.sf.jsqlparser.parser.CCJSqlParserManager; 25 | import net.sf.jsqlparser.schema.Column; 26 | import net.sf.jsqlparser.schema.Table; 27 | import net.sf.jsqlparser.statement.create.table.CreateTable; 28 | import net.sf.jsqlparser.statement.create.table.Index; 29 | import net.sf.jsqlparser.statement.select.FromItem; 30 | import net.sf.jsqlparser.statement.select.PlainSelect; 31 | import net.sf.jsqlparser.statement.select.Select; 32 | import net.sf.jsqlparser.statement.select.SelectExpressionItem; 33 | import jdbm.PrimaryTreeMap; 34 | import jdbm.RecordManager; 35 | import jdbm.RecordManagerFactory; 36 | import jdbm.recman.*; 37 | 38 | public class newclass 39 | { 40 | CCJSqlParserManager parserManager = new CCJSqlParserManager(); 41 | 42 | @SuppressWarnings("null") 43 | public newclass() throws JSQLParserException 44 | { 45 | String statement = "CREATE TABLE LINEITEM (orderkey INT, PRIMARY KEY (orderkey, linenumber),INDEX shipidx (shipdate));"; 46 | /* 47 | Properties p1 = new Properties(); 48 | try { 49 | RecordManager recMan = RecordManagerFactory.createRecordManager("OMFG"); 50 | RecordManager recMan1 = RecordManagerFactory.createRecordManager("Shail", p1); 51 | PrimaryTreeMap treeMap = recMan.treeMap("heya"); 52 | recMan.commit(); 53 | System.out.println(treeMap.keySet()); 54 | treeMap.remove(2); 55 | System.out.println(treeMap.keySet()); 56 | recMan.rollback(); 57 | System.out.println(treeMap.keySet()); 58 | } catch (IOException e) { 59 | // TODO Auto-generated catch block 60 | e.printStackTrace(); 61 | } 62 | */ 63 | CreateTable st1 = (CreateTable) (parserManager.parse(new StringReader(statement))); 64 | List ls = st1.getIndexes(); 65 | Iterator itr = ls.iterator(); 66 | while(itr.hasNext()) 67 | { 68 | Index indx = itr.next(); 69 | if(indx.getType().equals("INDEX")) 70 | { 71 | System.out.println(); 72 | } 73 | } 74 | /*PlainSelect st = (PlainSelect) ((Select) parserManager.parse(new StringReader(statement))).getSelectBody(); 75 | 76 | 77 | //while(true) 78 | //{ 79 | ExpressionList el = new ExpressionList(); 80 | ArrayList l1 = new ArrayList (); 81 | Expression s = st.getWhere(); 82 | JoinChecker (s); 83 | FromItem fr1 = st.getFromItem(); 84 | if(fr1 instanceof Table) 85 | { 86 | System.out.println(fr1.toString()); 87 | } 88 | */ //parseJoins(st.getWhere()); 89 | 90 | 91 | 92 | //break; 93 | //} 94 | /*String[] tokens = null; 95 | String str = st.toString(); 96 | HashMap map1 = new HashMap (); 97 | int tokenflag = 0; 98 | tokens = str.split(" "); 99 | 100 | for(int i = 0;i result = parseAlias(st); 154 | if(result == null) 155 | { 156 | System.out.println("NULL"); 157 | } 158 | Set keys = result.keySet(); 159 | for(String key : keys) 160 | { 161 | Expression value = result.get(key); 162 | System.out.println(key+" "+value.toString()); 163 | }*/ 164 | 165 | } 166 | public static void main(String[] args) throws JSQLParserException 167 | { 168 | 169 | new newclass(); 170 | 171 | } 172 | public void JoinChecker(Expression p) 173 | { 174 | if(p instanceof AndExpression) 175 | { 176 | parseJoins((AndExpression) p); 177 | } 178 | else if(p instanceof MinorThan) 179 | { 180 | parseJoins((MinorThan) p); 181 | } 182 | else if(p instanceof MinorThanEquals) 183 | { 184 | parseJoins((MinorThanEquals) p); 185 | } 186 | else if(p instanceof GreaterThan) 187 | { 188 | parseJoins((GreaterThan) p); 189 | } 190 | else if(p instanceof OrExpression) 191 | { 192 | parseJoins((OrExpression) p); 193 | } 194 | else if(p instanceof GreaterThanEquals) 195 | { 196 | parseJoins((GreaterThanEquals) p); 197 | } 198 | else if(p instanceof EqualsTo) 199 | { 200 | parseJoins((EqualsTo) p); 201 | } 202 | else if(p instanceof NotEqualsTo) 203 | { 204 | parseJoins((NotEqualsTo) p); 205 | } 206 | } 207 | private void parseJoins(NotEqualsTo p) { 208 | // TODO Auto-generated method stub 209 | p.getRightExpression(); 210 | Expression e = p.getLeftExpression(); 211 | System.out.println(p.getRightExpression().toString()); 212 | JoinChecker(e); 213 | } 214 | private void parseJoins(EqualsTo p) { 215 | // TODO Auto-generated method stub 216 | p.getRightExpression(); 217 | Expression e = p.getLeftExpression(); 218 | System.out.println(p.getRightExpression().toString()); 219 | JoinChecker(e); 220 | } 221 | private void parseJoins(GreaterThanEquals p) { 222 | // TODO Auto-generated method stub 223 | p.getRightExpression(); 224 | Expression e = p.getLeftExpression(); 225 | System.out.println(p.getRightExpression().toString()); 226 | JoinChecker(e); 227 | } 228 | private void parseJoins(OrExpression p) { 229 | // TODO Auto-generated method stub 230 | p.getRightExpression(); 231 | Expression e = p.getLeftExpression(); 232 | System.out.println(p.getRightExpression().toString()); 233 | JoinChecker(e); 234 | } 235 | private void parseJoins(GreaterThan p) { 236 | // TODO Auto-generated method stub 237 | p.getRightExpression(); 238 | Expression e = p.getLeftExpression(); 239 | System.out.println(p.getRightExpression().toString()); 240 | JoinChecker(e); 241 | } 242 | private void parseJoins(MinorThan p) { 243 | // TODO Auto-generated method stub 244 | p.getRightExpression(); 245 | Expression e = p.getLeftExpression(); 246 | System.out.println(p.getRightExpression().toString()); 247 | JoinChecker(e); 248 | } 249 | private void parseJoins(MinorThanEquals p) { 250 | // TODO Auto-generated method stub 251 | p.getRightExpression(); 252 | Expression e = p.getLeftExpression(); 253 | System.out.println(p.getRightExpression().toString()); 254 | JoinChecker(e); 255 | } 256 | public void parseJoins(AndExpression p) 257 | { 258 | p.getRightExpression(); 259 | Expression e = p.getLeftExpression(); 260 | System.out.println(p.getRightExpression().toString()); 261 | JoinChecker(e); 262 | } 263 | public HashMap parseAlias(PlainSelect st) 264 | { 265 | HashMap map1 = new HashMap(); 266 | String str=st.toString(); 267 | //String[] tokens = null; 268 | int tokenflag = 0; 269 | //tokens = str.split(" "); 270 | try 271 | { 272 | /* 273 | for(int i = 0;i schema; 18 | private ArrayList filters; 19 | private long totalTupleCount; 20 | private int aliasflag; 21 | private TreeSet joinSet; 22 | private boolean isJoinoutput; 23 | private ArrayList projectedFields; 24 | private Relation tuples; 25 | private String filePath; 26 | private boolean isDumpedInFile; 27 | private ArrayList primaryField; 28 | private ArrayList indexField; 29 | private IndexScanOperation indexLookup; 30 | 31 | public SQLTable() 32 | { 33 | this.tablename=null; 34 | this.schema=new ArrayList(); 35 | //this.tempSchema=new ArrayList(t.getSchema()); 36 | this.filters=new ArrayList(); 37 | this.tableAlias=tablename; 38 | this.totalTupleCount=0; 39 | this.isDumpedInFile=false; 40 | this.filePath=""; 41 | this.indexLookup=null; 42 | joinSet=new TreeSet(new Comparator() { 43 | public int compare(JoinDetails s1,JoinDetails s2) 44 | { 45 | int rValue=(int)(Parser.tableSizes.get(s1.table) - Parser.tableSizes.get(s2.table)); 46 | if(rValue==0) 47 | return s1.table.compareTo(s2.table); 48 | else 49 | return rValue; 50 | } 51 | }); 52 | 53 | this.primaryField = new ArrayList(); 54 | this.indexField = new ArrayList(); 55 | this.tuples=new Relation(); 56 | } 57 | public SQLTable(String name,ArrayList sch) 58 | { 59 | tablename=name; 60 | schema=sch; 61 | //tempSchema=new ArrayList(sch); 62 | filters=new ArrayList(); 63 | tableAlias=tablename; 64 | aliasflag = 0; 65 | totalTupleCount=-1; 66 | isJoinoutput=false; 67 | 68 | joinSet=new TreeSet(new Comparator() { 69 | public int compare(JoinDetails s1,JoinDetails s2) 70 | { 71 | int value=(int)(Parser.tableSizes.get(s1.table) - Parser.tableSizes.get(s2.table)); 72 | if(value==0) 73 | return s1.table.compareTo(s2.table); 74 | else 75 | return value; 76 | } 77 | }); 78 | 79 | this.primaryField = new ArrayList(); 80 | this.indexField = new ArrayList(); 81 | projectedFields=new ArrayList(sch); 82 | this.tuples=new Relation(); 83 | } 84 | 85 | public SQLTable(SQLTable t) // copy constructor 86 | { 87 | this.tablename=t.getTableName(); 88 | this.schema=new ArrayList(t.getSchema()); 89 | this.filters=new ArrayList(); 90 | this.tableAlias=tablename; 91 | this.totalTupleCount=t.getTupleCount(); 92 | this.indexLookup=t.getIndexLookupOperation(); 93 | 94 | joinSet=new TreeSet(new Comparator() { 95 | public int compare(JoinDetails s1,JoinDetails s2) 96 | { 97 | int rValue=(int)(Parser.tableSizes.get(s1.table) - Parser.tableSizes.get(s2.table)); 98 | if(rValue==0) 99 | return s1.table.compareTo(s2.table); 100 | else 101 | return rValue; 102 | } 103 | }); 104 | this.isJoinoutput=t.isJoinoutput(); 105 | this.filePath=t.filePath; 106 | this.isDumpedInFile=t.isDumpedInFile; 107 | 108 | this.primaryField = new ArrayList(); 109 | this.indexField = new ArrayList(); 110 | setPrimaryField(t.getPrimaryField()); 111 | setIndexField(t.getIndexField()); 112 | 113 | projectedFields=new ArrayList(t.getProjectedFields()); 114 | } 115 | 116 | public SQLTable(String tableName, String[] columns, String[] columntypes) 117 | { 118 | this.tablename = tableName; 119 | this.schema=new ArrayList(); 120 | //this.tempSchema=new ArrayList(t.getSchema()); 121 | this.filters=new ArrayList(); 122 | this.tableAlias=tablename; 123 | this.totalTupleCount=0; 124 | this.isDumpedInFile=false; 125 | this.filePath=""; 126 | joinSet=new TreeSet(new Comparator() { 127 | public int compare(JoinDetails s1,JoinDetails s2) 128 | { 129 | int rValue=(int)(Parser.tableSizes.get(s1.table) - Parser.tableSizes.get(s2.table)); 130 | if(rValue==0) 131 | return s1.table.compareTo(s2.table); 132 | else 133 | return rValue; 134 | } 135 | }); 136 | 137 | this.primaryField = new ArrayList(); 138 | this.indexField = new ArrayList(); 139 | this.tuples=new Relation(); 140 | 141 | int counter = 0; 142 | while(counter < columns.length) 143 | { 144 | this.schema.add(new fieldDetails(columns[counter], columntypes[counter])); 145 | counter++; 146 | } 147 | 148 | projectedFields=new ArrayList(schema); 149 | } 150 | 151 | public void setJoinSet(TreeSet treeSet) 152 | { 153 | joinSet = treeSet; 154 | } 155 | public String getTableName() 156 | { 157 | return tablename; 158 | } 159 | 160 | public ArrayList getSchema() 161 | { 162 | return schema; 163 | } 164 | 165 | public void setSchema(ArrayList sch) 166 | { 167 | schema=sch; 168 | } 169 | public ArrayList getProjectedFields() 170 | { 171 | return projectedFields; 172 | } 173 | 174 | public void setProjectedFields(ArrayList sch) 175 | { 176 | projectedFields=new ArrayList(sch); 177 | } 178 | 179 | public void addField(String fieldName, String fieldType) { 180 | schema.add(new fieldDetails(fieldName, fieldType)); 181 | } 182 | 183 | public void addField(fieldDetails field) { 184 | schema.add(field); 185 | } 186 | 187 | public void removeField(String fieldName) { 188 | int index = getIndexFromFieldName(fieldName); 189 | schema.remove(index); 190 | } 191 | 192 | public void removeField(fieldDetails field) { 193 | schema.remove(field); 194 | } 195 | 196 | public ArrayList getFilters() { 197 | return filters; 198 | } 199 | 200 | public void setFilters(ArrayList filters) { 201 | this.filters = filters; 202 | } 203 | 204 | public void addFilter(Expression e) { 205 | this.filters.add(e); 206 | } 207 | 208 | public String getTableAlias() { 209 | return tableAlias; 210 | } 211 | 212 | public void setAlias(String a) { 213 | tableAlias=a; 214 | aliasflag = 1; 215 | } 216 | 217 | public long getTupleCount() { 218 | if(!isDumpedInFile) 219 | return tuples.tupleCount(); 220 | 221 | return totalTupleCount; 222 | } 223 | 224 | public void setTupleCount(long s) { 225 | totalTupleCount=s; 226 | } 227 | 228 | public String isRelated(SQLTable t) { 229 | ArrayList sch1=this.getSchema(); 230 | ArrayList sch2=t.getSchema(); 231 | Iterator field=sch1.iterator(); 232 | // Assumption , each pair of relations have only one common attribute 233 | while(field.hasNext()) 234 | { 235 | fieldDetails f=field.next(); 236 | if((sch2.indexOf(f))!=-1) 237 | return f.getName(); 238 | } 239 | 240 | return null; 241 | } 242 | 243 | public boolean hasExistingAlias() { 244 | if(aliasflag == 1) 245 | return true; 246 | else 247 | return false; 248 | } 249 | 250 | public TreeSet getJoinSet() { 251 | return joinSet; 252 | } 253 | 254 | public void addToJoinSet(JoinDetails t) { 255 | if(joinSet==null) 256 | System.out.println("nullllllllllllllllllll"); 257 | else 258 | joinSet.add(t); 259 | } 260 | 261 | public void removeFromJoinSet(JoinDetails t) { 262 | joinSet.remove(t); 263 | } 264 | 265 | public boolean isJoinoutput() { 266 | return isJoinoutput; 267 | } 268 | 269 | public void setisJoinoutput(boolean v) { 270 | isJoinoutput=v; 271 | } 272 | 273 | 274 | public JoinDetails searchForTableInJoinSet(String k) { 275 | Iterator it=joinSet.iterator(); 276 | while(it.hasNext()) 277 | { 278 | JoinDetails j=it.next(); 279 | if(j.table.equals(k)) 280 | return j; 281 | } 282 | return null; 283 | } 284 | 285 | public void searchForTableAndRemoveFromJoinSet(String k) { 286 | joinSet.remove(searchForTableInJoinSet(k)); 287 | } 288 | // 289 | // public ArrayList getTempSchema() 290 | // { 291 | // //return tempSchema; 292 | // } 293 | 294 | public void removefromprojectionlist(String key) { 295 | projectedFields.remove(key); 296 | } 297 | 298 | public Tuple createNewTupleWithValues(ArrayList values) { 299 | Tuple tuple=new Tuple(this,values); 300 | //tuples.addTuple(tuple); 301 | return tuple; 302 | } 303 | 304 | public int getIndexFromFieldName(String fieldName) { 305 | fieldName = fieldName.trim(); 306 | Iterator fieldDetailIterator = this.schema.iterator(); 307 | int index = 0; 308 | 309 | while(fieldDetailIterator.hasNext()) 310 | { 311 | fieldDetails fieldDetail = fieldDetailIterator.next(); 312 | if(fieldDetail.getName().equals(fieldName)) 313 | return index; 314 | 315 | index++; 316 | } 317 | return -1; 318 | } 319 | 320 | public String getTypeFormFieldName(String fieldName) { 321 | Iterator fieldDetailIterator = this.schema.iterator(); 322 | 323 | while(fieldDetailIterator.hasNext()) 324 | { 325 | fieldDetails fieldDetail = fieldDetailIterator.next(); 326 | if(fieldDetail.getName().equals(fieldName)) 327 | return fieldDetail.getType(); 328 | } 329 | return null; 330 | } 331 | 332 | public String getTypeFormExpression(Expression e) 333 | { 334 | String leftField= ((BinaryExpression)e).getLeftExpression().toString(); 335 | String rightField=((BinaryExpression)e).getRightExpression().toString(); 336 | int lIndex=this.getIndexFromFieldName(leftField); 337 | int rIndex=this.getIndexFromFieldName(rightField); 338 | if(lIndex!=-1) 339 | return schema.get(lIndex).getType(); 340 | if(rIndex!=-1) 341 | return schema.get(rIndex).getType(); 342 | 343 | return ""; 344 | } 345 | 346 | public ArrayList getPrimaryField() { 347 | return this.primaryField; 348 | } 349 | 350 | public ArrayList getPrimaryFieldDetails() { 351 | ArrayList primaryFieldDetails = new ArrayList(); 352 | Iterator primaryIterator = this.primaryField.iterator(); 353 | 354 | while(primaryIterator.hasNext()) 355 | { 356 | String fieldName = primaryIterator.next(); 357 | primaryFieldDetails.add(new fieldDetails(fieldName, getTypeFormFieldName(fieldName))); 358 | } 359 | 360 | return primaryFieldDetails; 361 | } 362 | 363 | 364 | public void setPrimaryField(ArrayList primaryField) { 365 | if(primaryField!=null) 366 | this.primaryField.addAll(primaryField); 367 | } 368 | 369 | public void addPrimaryField(String primaryField) { 370 | if(primaryField!=null) 371 | this.primaryField.add(primaryField); 372 | } 373 | 374 | public void removePrimaryField(String primaryField) { 375 | if(primaryField!=null) 376 | this.primaryField.remove(primaryField); 377 | } 378 | 379 | public ArrayList getIndexField() { 380 | return indexField; 381 | } 382 | 383 | public ArrayList getIndexFieldDetails() { 384 | ArrayList indexFieldDetails = new ArrayList(); 385 | Iterator indexIterator = this.indexField.iterator(); 386 | 387 | while(indexIterator.hasNext()) 388 | { 389 | String fieldName = indexIterator.next(); 390 | indexFieldDetails.add(new fieldDetails(fieldName, getTypeFormFieldName(fieldName))); 391 | } 392 | 393 | return indexFieldDetails; 394 | } 395 | public void setIndexField(ArrayList indexField) { 396 | if(indexField!=null) 397 | this.indexField.addAll(indexField); 398 | } 399 | 400 | public void addIndexField(String indexField) { 401 | if(indexField!=null && !this.indexField.contains(indexField)) 402 | this.indexField.add(indexField); 403 | } 404 | 405 | public void removeIndexField(String indexField) { 406 | this.indexField.remove(indexField); 407 | } 408 | 409 | public void setDumpedInFile(boolean isDumpedInFile) { 410 | this.isDumpedInFile = isDumpedInFile; 411 | } 412 | 413 | public boolean getDumpedInFile() { 414 | return this.isDumpedInFile; 415 | } 416 | 417 | public void setFilePath( String filePath) { 418 | this.filePath = filePath; 419 | } 420 | 421 | public String getFilePath() { 422 | return this.filePath; 423 | } 424 | 425 | public IndexScanOperation getIndexLookupOperation() 426 | { 427 | return indexLookup; 428 | } 429 | public void setIndexLookupOperation(IndexScanOperation in) 430 | { 431 | indexLookup=in; 432 | } 433 | 434 | public Relation getRelation() { 435 | return this.tuples; 436 | } 437 | 438 | public void setRelation(Relation tuples) { 439 | this.tuples = tuples; 440 | } 441 | 442 | public boolean hasUsableIndex() 443 | { 444 | return (this.getPrimaryField().size()>0 || this.getIndexField().size()>0) && (this.getIndexLookupOperation()==null); 445 | } 446 | 447 | public boolean isInSchema(String fieldName) 448 | { 449 | boolean isPresent = false; 450 | 451 | for(fieldDetails fields : this.schema) 452 | { 453 | if(fieldName.equals(fields.getName())) 454 | { 455 | isPresent = true; 456 | break; 457 | } 458 | } 459 | 460 | return isPresent; 461 | } 462 | } 463 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Evaluators/RelationalEvaluator.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Evaluators; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | import net.sf.jsqlparser.expression.Expression; 8 | import net.sf.jsqlparser.expression.ExpressionVisitor; 9 | import net.sf.jsqlparser.expression.Parenthesis; 10 | import net.sf.jsqlparser.expression.StringValue; 11 | import net.sf.jsqlparser.expression.operators.arithmetic.Addition; 12 | import net.sf.jsqlparser.expression.operators.arithmetic.Division; 13 | import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication; 14 | import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction; 15 | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; 16 | import net.sf.jsqlparser.expression.operators.conditional.OrExpression; 17 | import net.sf.jsqlparser.expression.operators.relational.Between; 18 | import net.sf.jsqlparser.expression.operators.relational.EqualsTo; 19 | import net.sf.jsqlparser.expression.operators.relational.GreaterThan; 20 | import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals; 21 | import net.sf.jsqlparser.expression.operators.relational.MinorThan; 22 | import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals; 23 | import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo; 24 | import net.sf.jsqlparser.schema.Column; 25 | import edu.buffalo.cse562.AbstractExpressionVisitor; 26 | import edu.buffalo.cse562.QueryHandler.Tuple; 27 | import net.sf.jsqlparser.expression.Function; 28 | 29 | public class RelationalEvaluator extends AbstractExpressionVisitor { 30 | 31 | private boolean result; 32 | private Tuple inputTuple; 33 | 34 | public RelationalEvaluator(Tuple in) 35 | { 36 | inputTuple=in; 37 | } 38 | public boolean getResult() 39 | { 40 | return result; 41 | } 42 | @Override 43 | public void visit(Between btwn) { 44 | throw new UnsupportedOperationException("Not supported yet."); 45 | } 46 | 47 | @Override 48 | public void visit(EqualsTo et) { 49 | //throw new UnsupportedOperationException("Not supported yet."); 50 | Expression leftex=et.getLeftExpression(); 51 | Expression rightex=et.getRightExpression(); 52 | 53 | int r=evaluateExpression(leftex, rightex); 54 | result=(r==0); 55 | 56 | // if(leftex instanceof Column) 57 | // { 58 | // long leftvalue= Long.valueOf(inputTuple.valueForField(((Column) leftex).getColumnName())).longValue(); 59 | // //gt.getRightExpression().accept(this); 60 | // long rightValue=0; 61 | // if(rightex instanceof Column) 62 | // rightValue=Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 63 | // else 64 | // rightValue=Long.valueOf(rightex.toString()).longValue(); 65 | // result = leftvalue == rightValue; 66 | // } 67 | } 68 | 69 | @Override 70 | public void visit(GreaterThan gt) { 71 | //throw new UnsupportedOperationException("Not supported yet."); 72 | Expression leftex=gt.getLeftExpression(); 73 | Expression rightex=gt.getRightExpression(); 74 | 75 | int r=evaluateExpression(leftex, rightex); 76 | result=(r>0); 77 | 78 | // if(leftex instanceof Column) // assuming that the left exp will always be a column 79 | // { 80 | // // For greter than the value on the RHS has to be an integer/long 81 | // long leftvalue= Long.valueOf(inputTuple.valueForField(((Column) leftex).getColumnName())).longValue(); 82 | // //gt.getRightExpression().accept(this); 83 | // 84 | // long rightValue=0; 85 | // if(rightex instanceof Column) 86 | // rightValue=Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 87 | // else 88 | // rightValue=Long.valueOf(rightex.toString()).longValue(); 89 | // //System.out.println(leftValue+""+rightValue); 90 | // result=leftvalue > rightValue; 91 | // } 92 | // 93 | 94 | 95 | } 96 | 97 | @Override 98 | public void visit(GreaterThanEquals gte) { 99 | //throw new UnsupportedOperationException("Not supported yet."); 100 | Expression leftex=gte.getLeftExpression(); 101 | Expression rightex=gte.getRightExpression(); 102 | int r=evaluateExpression(leftex, rightex); 103 | result=(r>=0); 104 | 105 | 106 | // if(leftex instanceof Column) // assuming that the left exp will always be a column 107 | // { 108 | // // For greter than the value on the RHS has to be an integer/long 109 | // double leftvalue=Double.valueOf(inputTuple.valueForField(((Column) leftex).getColumnName()));//Long.valueOf().longValue(); 110 | // //gt.getRightExpression().accept(this); 111 | // double rightValue=0.0f; 112 | // if(rightex instanceof Column) 113 | // rightValue=Double.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName()));//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 114 | // else 115 | // rightValue=Double.valueOf(rightex.toString());//Long.valueOf(rightex.toString()).longValue(); 116 | // //System.out.println(leftValue+""+rightValue); 117 | // int r=Double.compare(leftvalue, rightValue); 118 | // result=(r>=0); 119 | // } 120 | // else // assuming that the only two possible types for the lhs are column or double. note for possible longvalues 121 | // { 122 | // double leftValue=Double.valueOf(leftex.toString()); 123 | // double rightValue=0.0f; 124 | // if(rightex instanceof Column) 125 | // rightValue=Double.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName()));//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 126 | // else 127 | // rightValue=Double.valueOf(rightex.toString()); 128 | // } 129 | 130 | } 131 | 132 | @Override 133 | public void visit(MinorThan mt) { 134 | //throw new UnsupportedOperationException("Not supported yet."); 135 | Expression leftex=mt.getLeftExpression(); 136 | Expression rightex=mt.getRightExpression(); 137 | int r=evaluateExpression(leftex, rightex); 138 | result=(r<0); 139 | 140 | // if(leftex instanceof Column) // assuming that the left exp will always be a column 141 | // { 142 | // // For greter than the value on the RHS has to be an integer/long 143 | // long leftvalue= Long.valueOf(inputTuple.valueForField(((Column) leftex).getColumnName())).longValue(); 144 | // //gt.getRightExpression().accept(this); 145 | // long rightValue=0; 146 | // if(rightex instanceof Column) 147 | // rightValue=Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 148 | // else 149 | // rightValue=Long.valueOf(rightex.toString()).longValue(); 150 | // //System.out.println(leftValue+""+rightValue); 151 | // result=leftvalue < rightValue; 152 | // } 153 | 154 | } 155 | 156 | @Override 157 | public void visit(MinorThanEquals mte) { 158 | // throw new UnsupportedOperationException("Not supported yet."); 159 | Expression leftex=mte.getLeftExpression(); 160 | Expression rightex=mte.getRightExpression(); 161 | int r=evaluateExpression(leftex, rightex); 162 | result=(r<=0); 163 | 164 | // if(leftex instanceof Column) // assuming that the left exp will always be a column 165 | // { 166 | // // For greter than the value on the RHS has to be an integer/long 167 | // long leftvalue= Long.valueOf(inputTuple.valueForField(((Column) leftex).getColumnName())).longValue(); 168 | // //gt.getRightExpression().accept(this); 169 | // long rightValue=0; 170 | // if(rightex instanceof Column) 171 | // rightValue=Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 172 | // else 173 | // rightValue=Long.valueOf(rightex.toString()).longValue(); 174 | // //System.out.println(leftValue+""+rightValue); 175 | // result=leftvalue <= rightValue; 176 | // } 177 | 178 | } 179 | 180 | @Override 181 | public void visit(NotEqualsTo net) { 182 | // throw new UnsupportedOperationException("Not supported yet."); 183 | Expression leftex=net.getLeftExpression(); 184 | Expression rightex=net.getRightExpression(); 185 | int r=evaluateExpression(leftex, rightex); 186 | result=(r!=0); 187 | 188 | 189 | // if(leftex instanceof Column) // assuming that the left exp will always be a column 190 | // { 191 | // // For greter than the value on the RHS has to be an integer/long 192 | // long leftvalue= Long.valueOf(inputTuple.valueForField(((Column) leftex).getColumnName())).longValue(); 193 | // //gt.getRightExpression().accept(this); 194 | // long rightValue=0; 195 | // if(rightex instanceof Column) 196 | // rightValue=Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 197 | // else 198 | // rightValue=Long.valueOf(rightex.toString()).longValue(); 199 | // //System.out.println(leftValue+""+rightValue); 200 | // result=leftvalue != rightValue; 201 | // } 202 | 203 | 204 | } 205 | 206 | public void visit(Parenthesis prnths) 207 | { 208 | Expression exp=prnths.getExpression(); 209 | if(ArithmeticEvaluator.isArithMeticOperator(exp)) 210 | { 211 | ArithmeticEvaluator aeva=new ArithmeticEvaluator(inputTuple); 212 | exp.accept(aeva); 213 | } 214 | else if(ConditionalEvaluators.isConditionalOperator(exp)) 215 | { 216 | ConditionalEvaluators aeva=new ConditionalEvaluators(inputTuple); 217 | exp.accept(aeva); 218 | } 219 | else if(RelationalEvaluator.isRelationalOperator(exp)) 220 | { 221 | RelationalEvaluator aeva=new RelationalEvaluator(inputTuple); 222 | exp.accept(aeva); 223 | } 224 | 225 | } 226 | @Override 227 | public void visit(Column column) { 228 | throw new UnsupportedOperationException("Not supported yet."); 229 | } 230 | 231 | private boolean isArithemetic(Expression e) 232 | { 233 | return (e instanceof Addition || e instanceof Subtraction || e instanceof Multiplication || e instanceof Division); 234 | } 235 | 236 | 237 | public static boolean isRelationalOperator(Expression e) 238 | { 239 | return (e instanceof Between || e instanceof EqualsTo || e instanceof GreaterThan || e instanceof GreaterThanEquals || e instanceof MinorThan || e instanceof MinorThanEquals || e instanceof NotEqualsTo); 240 | } 241 | 242 | private int evaluateExpression(Expression leftex,Expression rightex) 243 | { 244 | int comparisonType=-1; //0-->Strings 1---> Double 2----> dates 245 | String rValue=null; 246 | 247 | if(rightex instanceof Function) 248 | comparisonType=2; 249 | else if(rightex instanceof StringValue) 250 | { 251 | comparisonType=0; 252 | rValue=((StringValue)rightex).getValue(); 253 | 254 | } 255 | else if(isArithemetic(rightex) ) 256 | comparisonType=1; 257 | else if(rightex instanceof Column) 258 | { 259 | String rightexp=rightex.toString(); 260 | String type=inputTuple.getOwner().getTypeFormFieldName(rightexp); 261 | rValue = inputTuple.valueForField(rightexp); 262 | // if(isNumeric(rValue)) 263 | // comparisonType=1; 264 | // else if(isValidDate(rValue,"yyyy-MM-dd")) 265 | // comparisonType=2; 266 | // else 267 | // comparisonType=0; 268 | 269 | if(type.equals("int")|| type.equals("dec")) 270 | comparisonType=1; 271 | else 272 | comparisonType=0; 273 | 274 | } 275 | 276 | 277 | 278 | //if(comparisonType==2) 279 | // { 280 | // String dateright=null; 281 | // if(rightex instanceof Function) 282 | // { 283 | // String pa = ((Function) rightex).getName(); 284 | // 285 | // if((pa.equals("date")||pa.equals("DATE"))) 286 | // { 287 | // 288 | // dateright= dr.substring(2, (dr.length()-2)); 289 | // } 290 | // } 291 | // else 292 | // dateright=rValue; 293 | // 294 | // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 295 | // 296 | // String dateleft = inputTuple.valueForField(leftex.toString()); 297 | // try { 298 | // Date dateleft1 = sdf.parse(dateleft); 299 | // Date dateright1 = sdf.parse(dateright); 300 | // return dateleft1.compareTo(dateright1); 301 | // 302 | // } catch (ParseException e) { 303 | // // TODO Auto-generated catch block 304 | // e.printStackTrace(); 305 | // } 306 | // } 307 | 308 | 309 | if(comparisonType==0) 310 | { 311 | String value=inputTuple.valueForField(((Column) leftex).toString()); 312 | return value.compareTo(rValue); 313 | } 314 | 315 | 316 | double leftValue=0.0f,rightValue=0.0f; 317 | if(leftex instanceof Column) 318 | leftValue=Double.valueOf(inputTuple.valueForField(((Column) leftex).toString())); 319 | else if(isArithemetic(leftex)) 320 | { 321 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 322 | leftex.accept(ae); 323 | leftValue=ae.getResult(); 324 | } 325 | else 326 | leftValue=Double.valueOf(leftex.toString()); 327 | 328 | if(rightex instanceof Column) 329 | rightValue=Double.valueOf(inputTuple.valueForField(((Column) rightex).toString()));//Long.valueOf(inputTuple.valueForField(((Column) rightex).getColumnName())).longValue(); 330 | else if(isArithemetic(rightex)) 331 | { 332 | ArithmeticEvaluator ae= new ArithmeticEvaluator(inputTuple); 333 | rightex.accept(ae); 334 | rightValue=ae.getResult(); 335 | } 336 | else 337 | rightValue=Double.valueOf(rightex.toString()); 338 | 339 | return Double.compare(leftValue, rightValue); 340 | } 341 | // public boolean isNumeric(String s) 342 | // { 343 | // try 344 | // { 345 | // double d = Double.parseDouble(s); 346 | // } 347 | // catch(NumberFormatException nfe) 348 | // { 349 | // return false; 350 | // } 351 | // return true; 352 | // } 353 | // public boolean isValidDate(String dateToValidate, String dateFromat) 354 | // { 355 | // if(dateToValidate == null) 356 | // return false; 357 | // 358 | // SimpleDateFormat sdf = new SimpleDateFormat(dateFromat); 359 | // sdf.setLenient(false); 360 | // try { 361 | // Date date = sdf.parse(dateToValidate); 362 | // } 363 | // catch (Exception e){ 364 | // return false; 365 | // } 366 | // 367 | // return true; 368 | // } 369 | } -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/JoinNode.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.Iterator; 7 | import net.sf.jsqlparser.expression.BinaryExpression; 8 | import net.sf.jsqlparser.expression.Expression; 9 | import net.sf.jsqlparser.expression.Function; 10 | import edu.buffalo.cse562.Miscellaneous.OrderDetails; 11 | import edu.buffalo.cse562.QueryHandler.Relation; 12 | import edu.buffalo.cse562.QueryHandler.SQLTable; 13 | import edu.buffalo.cse562.QueryHandler.Tuple; 14 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 15 | 16 | public class JoinNode extends ASTNode{ 17 | 18 | long joinOutputTupleCount; 19 | private HashMap rightSideHash; 20 | private GroupingNode gNode; 21 | 22 | public JoinNode() 23 | { 24 | gNode=new GroupingNode(); 25 | rightSideHash=new HashMap(); 26 | } 27 | 28 | private Tuple mergeTuples(Tuple t1,Tuple t2,SQLTable parentTable) 29 | { 30 | ArrayList values=new ArrayList(); 31 | Iterator schemaItr = parentTable.getSchema().iterator(); 32 | while(schemaItr.hasNext()) 33 | { 34 | fieldDetails field = schemaItr.next(); 35 | if(t1.getOwner().getSchema().contains(field)) 36 | values.add(t1.valueForField(field.getName())); 37 | else if(t2.getOwner().getSchema().contains(field)) 38 | values.add(t2.valueForField(field.getName())); 39 | } 40 | 41 | Tuple newTuple=parentTable.createNewTupleWithValues(values); 42 | 43 | if(ASTNode.joinTables.size()==2 && ASTNode.isNestedQuery==true) 44 | { 45 | try 46 | { 47 | IndexScanner sc=IndexScanner.getInstance(ASTNode.indexDir); 48 | sc.setAliasMap(aliasMap); 49 | sc.handleAlias(newTuple); 50 | } 51 | catch(Exception e) 52 | { 53 | System.out.println("Merge Tuple Crashed!"); 54 | e.printStackTrace(); 55 | } 56 | 57 | } 58 | 59 | return newTuple; 60 | } 61 | 62 | /* 63 | * Add this SQLTable to the tables HashMap in Parser.java 64 | */ 65 | private SQLTable createSQLTable(SQLTable t1, String k1, SQLTable t2, String k2) 66 | { 67 | ArrayList newSchema = new ArrayList();//.getSchema();//t1.getProjectedFields(); 68 | newSchema.addAll(t1.getSchema()); 69 | newSchema.addAll(t2.getSchema()); 70 | 71 | SQLTable newTable = new SQLTable(t1.getTableAlias()+t2.getTableAlias(), newSchema); 72 | 73 | if(ASTNode.joinTables.size()==2 && ASTNode.isNestedQuery==true) 74 | { 75 | ArrayList finalSchema=new ArrayList(); 76 | for(String s:ASTNode.innerProjectFields) 77 | { 78 | Expression exp=aliasMap.get(s); 79 | if(exp instanceof Function || exp instanceof BinaryExpression) 80 | finalSchema.add(new fieldDetails(s, newTable.getTypeFormExpression(exp))); 81 | else 82 | finalSchema.add(new fieldDetails(s, newTable.getTypeFormFieldName(exp.toString()))); 83 | 84 | } 85 | 86 | newSchema.addAll(finalSchema); 87 | newTable.setSchema(newSchema); 88 | } 89 | //newSql.setTupleCount(joinOutputTupleCount); 90 | 91 | ArrayList filteredSchema = filterFields(t1,t2); 92 | newTable.setSchema(filteredSchema); 93 | //newTable.setSchema(newSchema); 94 | newTable.setisJoinoutput(true); 95 | return newTable; 96 | } 97 | 98 | 99 | private ArrayList filterFields(SQLTable t1, SQLTable t2) 100 | { 101 | ArrayList oldSchema = new ArrayList(t1.getSchema()); 102 | oldSchema.addAll(t2.getSchema()); 103 | 104 | ArrayList newSchema = new ArrayList(); 105 | 106 | Iterator schemaItr = oldSchema.iterator(); 107 | while(schemaItr.hasNext()) 108 | { 109 | fieldDetails field = schemaItr.next(); 110 | 111 | if(distinctField != null && distinctField.equals(field.getName())) 112 | { 113 | if(!newSchema.contains(field)) 114 | { 115 | newSchema.add(field); 116 | continue; 117 | } 118 | } 119 | 120 | else if(projectFields != null) 121 | { 122 | if(projectFields.contains(field.getName())) 123 | { 124 | if(!newSchema.contains(field)) 125 | { 126 | newSchema.add(field); 127 | continue; 128 | } 129 | } 130 | 131 | else 132 | { 133 | for(String alias : projectFields) 134 | { 135 | if(aliasMap.containsKey(alias)) 136 | { 137 | String exp = aliasMap.get(alias).toString(); 138 | if(exp.contains(field.getName())) 139 | { 140 | if(!newSchema.contains(field)) 141 | { 142 | newSchema.add(field); 143 | continue; 144 | } 145 | } 146 | } 147 | } 148 | } 149 | } 150 | 151 | if(innerProjectFields != null && innerProjectFields.contains(field.getName())) 152 | { 153 | if(!newSchema.contains(field)) 154 | { 155 | newSchema.add(field); 156 | continue; 157 | } 158 | 159 | } 160 | 161 | if(groupFields != null && !groupFields.isEmpty() && groupFields.contains(field.getName())) 162 | { 163 | if(!newSchema.contains(field)) 164 | { 165 | newSchema.add(field); 166 | continue; 167 | } 168 | } 169 | 170 | if(orderFields != null && !orderFields.isEmpty() && orderFields.contains(new OrderDetails(field.getName(), "ASC")) || orderFields.contains(new OrderDetails(field.getName(), "DESC"))) 171 | { 172 | if(!newSchema.contains(field)) 173 | { 174 | newSchema.add(field); 175 | continue; 176 | } 177 | } 178 | 179 | if(joinPredicates != null && !joinPredicates.isEmpty()) 180 | { 181 | Iterator joinItr = joinPredicates.iterator(); 182 | while(joinItr.hasNext()) 183 | { 184 | String joinExp = joinItr.next().toString(); 185 | if(joinExp.contains(field.getName())) 186 | { 187 | if(!newSchema.contains(field)) 188 | { 189 | newSchema.add(field); 190 | continue; 191 | } 192 | } 193 | } 194 | } 195 | 196 | if(extraPredicates != null && !extraPredicates.isEmpty()) 197 | { 198 | Iterator joinItr = extraPredicates.iterator(); 199 | while(joinItr.hasNext()) 200 | { 201 | String joinExp = joinItr.next().toString(); 202 | if(joinExp.contains(field.getName())) 203 | { 204 | if(!newSchema.contains(field)) 205 | { 206 | newSchema.add(field); 207 | continue; 208 | } 209 | } 210 | } 211 | } 212 | } 213 | 214 | return newSchema; 215 | } 216 | 217 | 218 | public String projectWithoutDistinct(Tuple tup,ArrayList projectCols, HashMap aMap) 219 | { 220 | String output=""; 221 | Iterator it=projectCols.iterator(); 222 | while(it.hasNext()) 223 | { 224 | String field=it.next(); 225 | String field1 = aMap.get(field).toString(); 226 | if(!field1.toUpperCase().contains("DISTINCT")) 227 | output=output+tup.valueForField(field)+"|"; 228 | } 229 | return output; 230 | } 231 | 232 | /* 233 | * This function will be called by all the join methods! 234 | * Used to find the join key! 235 | */ 236 | 237 | private String findRightJoinKey(String leftKey) 238 | { 239 | Iterator joinItr = joinPredicates.iterator(); 240 | while(joinItr.hasNext()) 241 | { 242 | String joinExp = joinItr.next().toString(); 243 | if(joinExp.contains(leftKey))// && joinExp.contains(tableName2)) 244 | { 245 | String[] comp=joinExp.split("="); 246 | if(comp[0].trim().equals(leftKey)) 247 | return comp[1].trim(); 248 | else 249 | return comp[0].trim(); 250 | } 251 | } 252 | return null; 253 | } 254 | 255 | public SQLTable Join(SQLTable table1, SQLTable table2) throws IOException { 256 | 257 | SQLTable resultTable = null; 258 | 259 | String key1=table1.getJoinSet().first().key; 260 | String key2=findRightJoinKey(key1); 261 | 262 | String fKey=null; 263 | if(!table2.getJoinSet().isEmpty()) 264 | fKey=table2.getJoinSet().first().key; 265 | if(!rightSideHash.isEmpty()) 266 | resultTable = HashJoin(table2,key2, table1,key1,fKey); 267 | else 268 | resultTable = HashJoin(table1,key1, table2,key2,fKey); 269 | 270 | return resultTable; 271 | } 272 | 273 | private SQLTable HashJoin(SQLTable table1,String key1, SQLTable table2,String key2,String futureKey) // the right hand side table has to be the smaller table or the previous join output 274 | { 275 | 276 | SQLTable resultTable=createSQLTable(table1, key1, table2, key2);//new SQLTable(table1.getTableAlias()+table2.getTableAlias(),newSchema); 277 | if(ASTNode.joinTables.size()==2 && groupFields != null) 278 | initializeGrouping(resultTable); 279 | 280 | //HashMap rightSideHash=new HashMap(); 281 | SQLTable lTable=table1,rTable=table2; 282 | String lKey=key1,rKey=key2; 283 | if(table1.getTupleCount().InnerScanner iscnInner=null; 296 | Iterator tupleIt=null; 297 | ScannerNode scn1=null; 298 | 299 | if(rightSideHash.isEmpty()) 300 | { 301 | 302 | if(rTable.getIndexLookupOperation()!=null) 303 | { 304 | 305 | IndexScanneriscn=IndexScanner.getInstance(ASTNode.indexDir); 306 | iscnInner=iscn.returnClass(rTable); 307 | 308 | iscnInner.run( rTable.getIndexLookupOperation()); 309 | } 310 | else 311 | scn1=new ScannerNode(ASTNode.baseDir, rTable, null, false); 312 | //else 313 | //tupleIt=rTable.getRelation().getTuples().iterator(); 314 | while((iscnInner!=null && iscnInner.hasNextTuple()) || (scn1!=null && scn1.hasNextTuple()) ||(tupleIt!=null &&tupleIt.hasNext() )) 315 | { 316 | Tuple tuple=null; 317 | if(rTable.getIndexLookupOperation()!=null) 318 | { 319 | tuple=iscnInner.readNextTuple(); 320 | tuple = iscnInner.validateTuple(tuple, rTable.getFilters()); 321 | } 322 | else 323 | { 324 | tuple=scn1.readNextTuple(); 325 | tuple = scn1.validateTuple(tuple, rTable.getFilters()); 326 | } 327 | 328 | // tuple = scn1.validateTuple(tuple, rTable.getFilters()); 329 | if(tuple==null) 330 | continue; 331 | 332 | String value=tuple.valueForField(rKey); 333 | if(!rightSideHash.containsKey(value)) 334 | rightSideHash.put(value, new Relation()); 335 | 336 | rightSideHash.get(value).addTuple(tuple); 337 | } 338 | 339 | } 340 | 341 | //System.out.println(""); 342 | //scanning the left relation 343 | 344 | ScannerNode scn2=null; 345 | if(lTable.getIndexLookupOperation()!=null && !lTable.getTableName().equals("LINEITEM")) 346 | { 347 | 348 | IndexScanneriscn=IndexScanner.getInstance(ASTNode.indexDir); 349 | iscnInner=iscn.returnClass(lTable); 350 | iscnInner.run(lTable.getIndexLookupOperation()); 351 | } 352 | else 353 | scn2=new ScannerNode(ASTNode.baseDir, lTable, null,false); 354 | 355 | HashMaptempResult=new HashMap(); 356 | while((iscnInner!=null && iscnInner.hasNextTuple())|| (scn2 !=null && scn2.hasNextTuple()) || (tupleIt!=null && tupleIt.hasNext())) 357 | { 358 | Tuple tuple=null; 359 | if(iscnInner!=null && lTable.getIndexLookupOperation()!=null) 360 | { 361 | tuple=iscnInner.readNextTuple(); 362 | tuple = iscnInner.validateTuple(tuple, lTable.getFilters()); 363 | } 364 | else 365 | { 366 | tuple=scn2.readNextTuple(); 367 | if(lTable.getIndexLookupOperation()!=null)// spl case for lineitem 368 | { 369 | String value=tuple.valueForField(lTable.getTableAlias()+"."+lTable.getIndexLookupOperation().key); 370 | String low=lTable.getIndexLookupOperation().lowerLimit; 371 | String high=lTable.getIndexLookupOperation().upperLimit; 372 | boolean finalResult=false; 373 | if(lTable.getIndexLookupOperation().lowerInclusive) 374 | finalResult=(value.compareTo(low)>=0); 375 | else 376 | finalResult=(value.compareTo(low)>0); 377 | 378 | if(finalResult) 379 | { 380 | if(lTable.getIndexLookupOperation().upperInclusive) 381 | finalResult=finalResult && (value.compareTo(high)<=0); 382 | else 383 | finalResult= finalResult && (value.compareTo(high)<0); 384 | } 385 | if(!finalResult) 386 | continue; 387 | } 388 | 389 | tuple = scn2.validateTuple(tuple, lTable.getFilters()); 390 | } 391 | 392 | if(tuple==null) 393 | continue; 394 | String value=tuple.valueForField(lKey); 395 | if(rightSideHash.containsKey(value)) 396 | { 397 | Iterator itRel=rightSideHash.get(value).getTuples().iterator(); 398 | while(itRel.hasNext()) 399 | { 400 | Tuple tup=mergeTuples(tuple,(Tuple)itRel.next(),resultTable); 401 | if(tup!=null) 402 | //output += tup.convertToString() + "\n"; 403 | { 404 | 405 | if(ASTNode.joinTables.size()==2 && groupFields != null) // last join 406 | { 407 | // System.out.println(tup.finalPrint()); 408 | if(ASTNode.distinctField==null) 409 | gNode.groupTuple(tup); 410 | else 411 | gNode.addToMap(tup); 412 | } 413 | else 414 | { 415 | //resultTable.getRelation().addTuple(tup); 416 | String value1=tup.valueForField(futureKey); 417 | if(!tempResult.containsKey(value1)) 418 | tempResult.put(value1, new Relation()); 419 | 420 | tempResult.get(value1).addTuple(tup); 421 | 422 | } 423 | 424 | joinOutputTupleCount++; 425 | } 426 | } 427 | } 428 | } 429 | //End of join 430 | rightSideHash.clear(); 431 | rightSideHash=null; 432 | System.gc(); 433 | rightSideHash=tempResult; 434 | } 435 | catch(Exception e) 436 | { 437 | System.out.println("HashJoin Causes Exception!" + table1.getTableName() + " " + table2.getTableName()); 438 | e.printStackTrace(); 439 | } 440 | return resultTable; 441 | } 442 | 443 | private void initializeGrouping(SQLTable resultTable) 444 | { 445 | gNode=new GroupingNode(); 446 | gNode.initializeGrouping(resultTable); 447 | } 448 | public void finalizeGrouping() 449 | { 450 | gNode.processGroupOutput(); 451 | } 452 | } 453 | 454 | 455 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/Operators/GroupingNode.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.Operators; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Comparator; 6 | import java.util.HashMap; 7 | import java.util.Iterator; 8 | import java.util.Map.Entry; 9 | import java.util.TreeMap; 10 | import java.util.TreeSet; 11 | 12 | import net.sf.jsqlparser.expression.BinaryExpression; 13 | import net.sf.jsqlparser.expression.Expression; 14 | import net.sf.jsqlparser.expression.Function; 15 | import net.sf.jsqlparser.expression.operators.relational.ExpressionList; 16 | import net.sf.jsqlparser.schema.Column; 17 | import edu.buffalo.cse562.Evaluators.ArithmeticEvaluator; 18 | import edu.buffalo.cse562.QueryHandler.Relation; 19 | import edu.buffalo.cse562.QueryHandler.SQLTable; 20 | import edu.buffalo.cse562.QueryHandler.Tuple; 21 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 22 | 23 | public class GroupingNode extends ASTNode{ 24 | 25 | String inDirectory; 26 | String outDirectory; 27 | String groupDir; 28 | SQLTable table; //join output , input to groupby 29 | int bucketsDumped; 30 | 31 | boolean jugaad; 32 | 33 | ScannerNode scn; 34 | 35 | public GroupingNode() 36 | { 37 | scn=null; 38 | } 39 | 40 | public double evaluateEXpressionOnTuple(Tuple tup,Expression exp ) 41 | { 42 | 43 | ArithmeticEvaluator aeva=new ArithmeticEvaluator(tup); 44 | exp.accept(aeva); 45 | double s=aeva.getResult(); 46 | return s; 47 | } 48 | public double evaluateSum(Relation input,ExpressionList expl) 49 | { 50 | double sum = 0; 51 | 52 | Iterator it=expl.getExpressions().iterator(); 53 | Expression sumParam =(Expression)it.next(); 54 | Iterator relIt=input.getTuples().iterator(); 55 | 56 | 57 | while(relIt.hasNext()) 58 | { 59 | sum=sum + evaluateEXpressionOnTuple((Tuple)relIt.next(), sumParam); 60 | 61 | 62 | } 63 | return sum; 64 | } 65 | 66 | 67 | public String evaluateAggregateOnTuple(String key,String value,Tuple input) 68 | { 69 | //Add the fields for all alias 70 | String previousResult=key; 71 | 72 | if(value==null) 73 | { 74 | value=""; 75 | //previousResult=new String(); 76 | Iterator it=ASTNode.groupOutputTable.getSchema().iterator(); 77 | while(it.hasNext()) 78 | { 79 | 80 | fieldDetails field=it.next(); 81 | if(input.getOwner().getIndexFromFieldName(field.getName())==-1) // new fields for the agg 82 | value+="0|"; 83 | } 84 | } 85 | previousResult=previousResult+value; 86 | value=""; 87 | 88 | // iterate over all the aliases and evaluate the functions 89 | Iterator itAlias=aliasMap.keySet().iterator(); 90 | //Tuple newTuple= ASTNode.baseDir ;//new Tuple(); 91 | while(itAlias.hasNext()) 92 | { 93 | String aKey=itAlias.next().toString(); 94 | Expression exp=aliasMap.get(aKey); 95 | //DecimalFormat df=new DecimalFormat("$0.00"); 96 | if(exp instanceof Function) 97 | { 98 | ArrayList values=new ArrayList(Arrays.asList(previousResult.split("\\|"))); 99 | Tuple tup=ASTNode.groupOutputTable.createNewTupleWithValues(values); 100 | // tup.readValuesForSchema(t.getProjectedFields(), values, null); 101 | 102 | String fName=((Function)exp).getName(); 103 | ExpressionList param=((Function)exp).getParameters(); 104 | if(fName.toUpperCase().equals("COUNT")) 105 | { 106 | 107 | // The following code is commented since it will never get called .. count(distinct) queries are handled seperately 108 | if(((Function) exp).isDistinct()) 109 | {} 110 | else 111 | { 112 | int count=Integer.parseInt(tup.valueForField(aKey))+1; 113 | tup.setValue(aKey,"int",String.valueOf(count)); // count will always be an integer 114 | } 115 | } 116 | 117 | else if(fName.toUpperCase().equals("SUM")) 118 | { 119 | Iterator it=param.getExpressions().iterator(); 120 | Expression sumParam =(Expression)it.next(); 121 | 122 | double value1 = Double.parseDouble(tup.valueForField(aKey)); 123 | double nValue=evaluateEXpressionOnTuple(input,sumParam); 124 | value1 = value1 + nValue; 125 | 126 | value+=String.valueOf(value1)+"|"; 127 | tup.setValue(aKey, "decimal",String.valueOf(value1)); 128 | 129 | } 130 | else if(fName.toUpperCase().equals("AVG")) 131 | { 132 | String[] value1; 133 | if(tup.valueForField(aKey).equals("0")) 134 | { 135 | value1=new String[2]; 136 | value1[0]="0"; 137 | value1[1]="0"; 138 | } 139 | else 140 | value1=tup.valueForField(aKey).split(","); 141 | 142 | Iterator it=param.getExpressions().iterator(); 143 | Expression sumParam =(Expression)it.next(); 144 | 145 | double newSum=Double.parseDouble(value1[0]) + evaluateEXpressionOnTuple(input, sumParam);//+Double.valueOf(); 146 | int newCount=Integer.parseInt(value1[1])+1; 147 | tup.setValue(aKey,"decimal", String.valueOf(newSum)+","+String.valueOf(newCount)); 148 | } 149 | } 150 | } 151 | return (value!=null)?value:""; 152 | } 153 | 154 | public void initializeGrouping(SQLTable t) 155 | { 156 | if(ASTNode.distinctField==null || ASTNode.distinctField.isEmpty()) 157 | { 158 | if(outerprojectFields == null || outerprojectFields.isEmpty()) 159 | ASTNode.groupBucketsAgg = new HashMap(); 160 | else 161 | { 162 | ASTNode.groupBucketsAggOuter = new TreeMap(); 163 | } 164 | } 165 | else 166 | { 167 | if(!orderFields.equals(groupFields)) 168 | ASTNode.groupBucketsDis = new HashMap>(); 169 | else 170 | ASTNode.groupBucketsDisOuter = new TreeMap>(); 171 | } 172 | 173 | this.table = t; 174 | ArrayList groupTableschema=new ArrayList(); 175 | //schema.addAll(ASTNode.groupFields); 176 | //Addthe grouping cols from the input table 177 | 178 | for(fieldDetails col:this.table.getSchema()) 179 | { 180 | if(ASTNode.groupFields.contains(col.getName())) 181 | { 182 | if(!groupTableschema.contains(col)) 183 | groupTableschema.add(col); 184 | } 185 | } 186 | 187 | 188 | // search for aliases 189 | Iterator> aliasItr = aliasMap.entrySet().iterator(); 190 | while(aliasItr.hasNext()) 191 | { 192 | Entry map = aliasItr.next(); 193 | Expression exp = map.getValue(); 194 | String colAlias = map.getKey(); 195 | boolean isAdded = false; 196 | 197 | for(fieldDetails fields : groupTableschema) 198 | { 199 | if(colAlias.equals(fields.getName())) 200 | { 201 | isAdded = true; 202 | break; 203 | } 204 | } 205 | 206 | if(!isAdded) 207 | { 208 | if(exp instanceof Function) // || exp instanceof BinaryExpression) 209 | { 210 | //String type=this.table.getTypeFormExpression(exp); 211 | String type=null; 212 | String fName=((Function)exp).getName(); 213 | if(fName.toUpperCase().equals("COUNT")) 214 | type="int"; 215 | else if(fName.toUpperCase().equals("SUM")) 216 | type="decimal"; 217 | 218 | if(!groupTableschema.contains(new fieldDetails(colAlias, type))) 219 | groupTableschema.add(new fieldDetails(colAlias, type)); 220 | } 221 | 222 | else if(exp instanceof Column) 223 | groupTableschema.add(new fieldDetails(colAlias, "str")); 224 | 225 | else if(exp instanceof BinaryExpression) 226 | groupTableschema.add(new fieldDetails(colAlias, "dec")); 227 | } 228 | } 229 | ASTNode.groupOutputTable=new SQLTable("GroupByOutput",groupTableschema); 230 | } 231 | 232 | public void groupTuple(Tuple tuple) 233 | { 234 | 235 | if(scn==null) 236 | scn=new ScannerNode(); 237 | 238 | if(ASTNode.extraPredicates!=null && !ASTNode.extraPredicates.isEmpty()) 239 | tuple=scn.validateTuple(tuple,ASTNode.extraPredicates); 240 | 241 | if(tuple != null) 242 | { 243 | Iterator it1 = ASTNode.groupOutputTable.getSchema().iterator(); 244 | String key=""; 245 | 246 | while(it1.hasNext()) 247 | { 248 | String field=it1.next().getName(); 249 | if(ASTNode.groupFields.contains(field)) 250 | key=key+tuple.valueForField(field)+"|"; 251 | } 252 | 253 | //check if we already have a bucket with this key 254 | if(outerprojectFields == null || outerprojectFields.isEmpty()) 255 | { 256 | if(!ASTNode.groupBucketsAgg.containsKey(key)) 257 | groupBucketsAgg.put(key, evaluateAggregateOnTuple(key,null,tuple)); 258 | else 259 | groupBucketsAgg.put(key, evaluateAggregateOnTuple(key,groupBucketsAgg.get(key),tuple)); 260 | } 261 | 262 | else 263 | { 264 | for(String projFields : outerprojectFields) 265 | { 266 | if(aliasMap.get(projFields) instanceof Function) 267 | { 268 | ArrayList schema = tuple.getOwner().getSchema(); 269 | boolean isPresent = false; 270 | fieldDetails lastField = schema.get(schema.size()-1); 271 | if(lastField.getName().equals(projFields)) 272 | isPresent = true; 273 | if(!isPresent) 274 | { 275 | tuple.getOwner().getSchema().add(new fieldDetails(projFields, "dec")); 276 | break; 277 | } 278 | } 279 | } 280 | 281 | if(!ASTNode.groupBucketsAggOuter.containsKey(key)) 282 | { 283 | groupBucketsAggOuter.put(key, evaluateAggregateOnTupleNestedQuery(null, tuple)); 284 | } 285 | else 286 | groupBucketsAggOuter.put(key, evaluateAggregateOnTupleNestedQuery(groupBucketsAggOuter.get(key),tuple)); 287 | } 288 | } 289 | } 290 | 291 | private Tuple evaluateAggregateOnTupleNestedQuery(Tuple previousTuple, Tuple tuple) 292 | { 293 | ArrayList oldSchema = tuple.getOwner().getSchema(); 294 | String aliasField = oldSchema.get(oldSchema.size()-1).getName(); 295 | Expression exp = aliasMap.get(aliasField); 296 | String sumparam = exp.toString(); 297 | if(exp instanceof Function) 298 | sumparam = ((Function)exp).getParameters().toString(); 299 | sumparam = sumparam.replace("(", ""); 300 | sumparam = sumparam.replace(")", ""); 301 | 302 | if(previousTuple == null) 303 | { 304 | tuple.setValue(aliasField, "dec", tuple.valueForField(sumparam)); 305 | } 306 | 307 | else 308 | { 309 | double oldVal = Double.parseDouble(previousTuple.valueForField(aliasField)); 310 | double newVal = Double.parseDouble(tuple.valueForField(sumparam)); 311 | newVal += oldVal; 312 | tuple.setValue(aliasField, "dec", String.valueOf(newVal)); 313 | } 314 | 315 | 316 | return tuple; 317 | } 318 | 319 | public void processGroupOutput() 320 | { 321 | if(distinctField != null && !distinctField.isEmpty()) 322 | { 323 | if(groupBucketsDisOuter == null || groupBucketsDisOuter.isEmpty()) 324 | { 325 | Iterator>> distinctItr = groupBucketsDis.entrySet().iterator(); 326 | while(distinctItr.hasNext()) 327 | { 328 | Entry> valTupSet = distinctItr.next(); 329 | ArrayList values = new ArrayList<>(); 330 | 331 | String[] temp = null; 332 | temp = valTupSet.getKey().split("\\|"); 333 | 334 | int count = 0; 335 | while(count < temp.length) 336 | { 337 | values.add(temp[count]); 338 | count++; 339 | } 340 | 341 | Integer val = valTupSet.getValue().size(); 342 | //System.out.println(val); 343 | values.add(val.toString()); 344 | 345 | Tuple tuple = groupOutputTable.createNewTupleWithValues(values); 346 | groupOutputTable.getRelation().addTuple(tuple); 347 | } 348 | } 349 | 350 | else 351 | { 352 | Iterator>> distinctItr = groupBucketsDisOuter.entrySet().iterator(); 353 | while(distinctItr.hasNext()) 354 | { 355 | Entry> valTupSet = distinctItr.next(); 356 | Integer val = valTupSet.getValue().size(); 357 | String val1 = val.toString(); 358 | 359 | System.out.println(valTupSet.getKey() + val1); 360 | } 361 | System.exit(0); 362 | } 363 | } 364 | else 365 | { 366 | if(outerprojectFields == null || outerprojectFields.isEmpty()) 367 | { 368 | ArrayList keySet= new ArrayList<>(ASTNode.groupBucketsAgg.keySet()); 369 | //Relation groupOutput=new Relation(); 370 | for(String s : keySet) 371 | { 372 | ArrayList values=new ArrayList(Arrays.asList((s+groupBucketsAgg.get(s)).split("\\|"))); 373 | Tuple tup=ASTNode.groupOutputTable.createNewTupleWithValues(values); 374 | ASTNode.groupOutputTable.getRelation().addTuple(tup); 375 | 376 | ASTNode.groupBucketsAgg.remove(s); 377 | //groupOutput.addTuple(tup); 378 | } 379 | } 380 | 381 | else 382 | { 383 | Iterator itr = groupBucketsAggOuter.values().iterator(); 384 | 385 | while(itr.hasNext()) 386 | System.out.println(ProjectNode.projectTuple(itr.next(), outerprojectFields).finalPrint()); 387 | // long endTime = System.currentTimeMillis(); 388 | // long totalTime = endTime - ASTNode.startTime; 389 | // System.out.println(totalTime/100); 390 | System.exit(0); 391 | } 392 | } 393 | 394 | groupBucketsDis.clear(); 395 | groupBucketsAgg.clear(); 396 | groupBucketsAggOuter.clear(); 397 | System.gc(); 398 | return; 399 | } 400 | 401 | public String projectWithoutDistinct(Tuple tup,ArrayList projectCols, HashMap aMap) 402 | { 403 | String output=""; 404 | Iterator it=projectCols.iterator(); 405 | while(it.hasNext()) 406 | { 407 | String field=it.next(); 408 | String field1 = aMap.get(field).toString(); 409 | if(!field1.toUpperCase().contains("DISTINCT")) 410 | output=output+tup.valueForField(field)+"|"; 411 | } 412 | return output; 413 | } 414 | 415 | /* 416 | * Methods to Handle Distinct on Groups! 417 | */ 418 | 419 | public void addToMap(Tuple tuple) 420 | { 421 | ScannerNode scanner = new ScannerNode(); 422 | tuple=scanner.validateTuple(tuple, extraPredicates); 423 | 424 | if(tuple != null) 425 | { 426 | Iterator it1=groupFields.iterator(); 427 | String key=""; 428 | 429 | while(it1.hasNext()) 430 | key=key+tuple.valueForField(it1.next().toString())+"|"; 431 | 432 | if(orderFields.equals(groupFields)) 433 | { 434 | if(groupBucketsDis.containsKey(key)) 435 | { 436 | ArrayList distinctValues = groupBucketsDis.get(key); 437 | if(!distinctValues.contains(tuple.valueForField(distinctField))) 438 | distinctValues.add(tuple.valueForField(distinctField)); 439 | groupBucketsDis.put(key, distinctValues); 440 | } 441 | 442 | else 443 | { 444 | ArrayList distinctValues = new ArrayList(); 445 | distinctValues.add(tuple.valueForField(distinctField)); 446 | groupBucketsDis.put(key,distinctValues); 447 | } 448 | } 449 | 450 | else 451 | { 452 | if(groupBucketsDisOuter.containsKey(key)) 453 | { 454 | ArrayList distinctValues = groupBucketsDisOuter.get(key); 455 | if(!distinctValues.contains(tuple.valueForField(distinctField))) 456 | distinctValues.add(tuple.valueForField(distinctField)); 457 | groupBucketsDisOuter.put(key, distinctValues); 458 | } 459 | 460 | else 461 | { 462 | ArrayList distinctValues = new ArrayList(); 463 | distinctValues.add(tuple.valueForField(distinctField)); 464 | groupBucketsDisOuter.put(key,distinctValues); 465 | } 466 | } 467 | } 468 | } 469 | } 470 | -------------------------------------------------------------------------------- /CSE562/src/edu/buffalo/cse562/IOHandler/Parser.java: -------------------------------------------------------------------------------- 1 | package edu.buffalo.cse562.IOHandler; 2 | 3 | import java.io.File; 4 | import java.io.FileReader; 5 | import java.io.StringReader; 6 | import java.util.ArrayList; 7 | import java.util.Comparator; 8 | import java.util.HashMap; 9 | import java.util.Iterator; 10 | import java.util.List; 11 | import java.util.TreeSet; 12 | 13 | import net.sf.jsqlparser.JSQLParserException; 14 | import net.sf.jsqlparser.expression.BinaryExpression; 15 | import net.sf.jsqlparser.expression.Expression; 16 | import net.sf.jsqlparser.expression.Function; 17 | import net.sf.jsqlparser.expression.Parenthesis; 18 | import net.sf.jsqlparser.expression.StringValue; 19 | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; 20 | import net.sf.jsqlparser.expression.operators.relational.EqualsTo; 21 | import net.sf.jsqlparser.expression.operators.relational.ExpressionList; 22 | import net.sf.jsqlparser.expression.operators.relational.GreaterThan; 23 | import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals; 24 | import net.sf.jsqlparser.expression.operators.relational.MinorThan; 25 | import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals; 26 | import net.sf.jsqlparser.parser.CCJSqlParser; 27 | import net.sf.jsqlparser.parser.CCJSqlParserManager; 28 | import net.sf.jsqlparser.parser.ParseException; 29 | import net.sf.jsqlparser.schema.Column; 30 | import net.sf.jsqlparser.schema.Table; 31 | import net.sf.jsqlparser.statement.Statement; 32 | import net.sf.jsqlparser.statement.create.table.CreateTable; 33 | import net.sf.jsqlparser.statement.create.table.Index; 34 | import net.sf.jsqlparser.statement.select.FromItem; 35 | import net.sf.jsqlparser.statement.select.Join; 36 | import net.sf.jsqlparser.statement.select.PlainSelect; 37 | import net.sf.jsqlparser.statement.select.Select; 38 | import net.sf.jsqlparser.statement.select.SelectBody; 39 | import net.sf.jsqlparser.statement.select.SelectExpressionItem; 40 | import net.sf.jsqlparser.statement.select.SubSelect; 41 | import net.sf.jsqlparser.statement.select.Union; 42 | import edu.buffalo.cse562.Evaluators.ArithmeticEvaluator; 43 | import edu.buffalo.cse562.Evaluators.ConditionalEvaluators; 44 | import edu.buffalo.cse562.Evaluators.RelationalEvaluator; 45 | import edu.buffalo.cse562.Operators.ASTNode; 46 | import edu.buffalo.cse562.Miscellaneous.IndexScanOperation; 47 | import edu.buffalo.cse562.Miscellaneous.JoinDetails; 48 | import edu.buffalo.cse562.Operators.JoinNode; 49 | import edu.buffalo.cse562.Miscellaneous.OrderDetails; 50 | import edu.buffalo.cse562.Operators.EvaluatorNode; 51 | import edu.buffalo.cse562.Operators.ScannerNode; 52 | import edu.buffalo.cse562.QueryHandler.Relation; 53 | import edu.buffalo.cse562.QueryHandler.SQLTable; 54 | import edu.buffalo.cse562.QueryHandler.fieldDetails; 55 | 56 | public class Parser { 57 | private HashMap tables; 58 | private String baseDir; 59 | private String swapDir; 60 | private String indexDir; 61 | boolean hasSingleSource; 62 | private ArrayList joinList = new ArrayList(); 63 | private ArrayList additionalPredicates= new ArrayList(); 64 | private String nestedAlias; 65 | ArrayList sortedTables; 66 | public static HashMap tableSizes; 67 | private HashMap fromItemsAliasMap; 68 | private HashMap allFieldsMap; 69 | public static HashMap foreignIndexMap = new HashMap(); 70 | 71 | private boolean isInIndexPhase; 72 | public Parser() 73 | { 74 | sortedTables=new ArrayList(); 75 | Parser.tableSizes=new HashMap(); 76 | nestedAlias=null; 77 | new HashMap(); 78 | fromItemsAliasMap=new HashMap(); 79 | allFieldsMap= new HashMap(); 80 | isInIndexPhase=false; 81 | Parser.tableSizes.put("CUSTOMER", 300000L); 82 | Parser.tableSizes.put("LINEITEM", 11997996L); 83 | Parser.tableSizes.put("NATION",25L); 84 | Parser.tableSizes.put("ORDERS", 3000000L); 85 | Parser.tableSizes.put("PART", 400000L); 86 | Parser.tableSizes.put("PARTSUPP", 1600000L); 87 | Parser.tableSizes.put("REGION", 5L); 88 | Parser.tableSizes.put("SUPPLIER",20000L); 89 | Parser.foreignIndexMap.put("CUSTOMER",new String []{"nationkey"}); 90 | Parser.foreignIndexMap.put("LINEITEM",new String []{"partkey","suppkey","receiptdate","shipdate"}); 91 | Parser.foreignIndexMap.put("NATION",new String []{"regionkey"}); 92 | Parser.foreignIndexMap.put("ORDERS", new String []{"custkey","orderdate"}); 93 | Parser.foreignIndexMap.put("PART", new String []{"name"}); //This threw a biiig error. Fuck you part! 94 | Parser.foreignIndexMap.put("PARTSUPP", new String []{"suppkey"}); 95 | Parser.foreignIndexMap.put("REGION", new String []{"name"}); 96 | Parser.foreignIndexMap.put("SUPPLIER",new String []{"nationkey","suppkey"}); 97 | 98 | } 99 | 100 | public void parseCmdLineArguments(String args[]) 101 | { 102 | tables=new HashMap<>(); // we will maintain our list of the tables of the concerned here 103 | ArrayList sqlFiles=new ArrayList(); 104 | baseDir=null; 105 | for(int i=0;i idb= new IndexBuilder(baseDir, indexDir); 151 | //String for multiple primary keys 152 | //IndexBuilder idb1 = new IndexBuilder(baseDir, indexDir); 153 | //Integer for single primary keys 154 | //We can take multiple index builders here. one each for integer and string. 155 | Iterator keyIt=tables.keySet().iterator(); 156 | while(keyIt.hasNext()) 157 | { 158 | SQLTable table=tables.get(keyIt.next()); 159 | //keyIt.remove(); 160 | if(table.getPrimaryField().size()>1) 161 | { 162 | idb.buildIndexOnMultiplePrimaryKey(table); 163 | } 164 | else 165 | idb.buildIndexOnSinglePrimaryKey(table); 166 | } 167 | } 168 | //sc.close(); 169 | } 170 | } 171 | } 172 | catch(Exception e) 173 | { 174 | System.out.println(e.getMessage()+e.getStackTrace()); 175 | } 176 | } 177 | } 178 | 179 | // Author:Kaushal 180 | 181 | public Statement parseSQLExpression(String exprStr) throws ParseException, JSQLParserException 182 | { 183 | CCJSqlParserManager pm = new CCJSqlParserManager(); 184 | return pm.parse(new StringReader(exprStr)); 185 | } 186 | 187 | public void parseCreate(Statement statement, HashMap tables) // parse and store value in the tables map. 188 | { 189 | CreateTable createstatement = (CreateTable)statement; 190 | String tableName = createstatement.getTable().getName(); 191 | Object [] col_def=createstatement.getColumnDefinitions().toArray(); 192 | String[] columns=new String[col_def.length]; 193 | String[] columntypes = new String[col_def.length]; 194 | //Extracting the field names and storing as string 195 | for(int i=0;i ls = createstatement.getIndexes(); 218 | if(ls != null) 219 | { 220 | /* 221 | * Ideally primary key information should be provided before we start building indexes, 222 | * so that if we sort on primary key and then make a B+ Tree index on it, we can easily 223 | * make a clustered B+ Tree index. 224 | */ 225 | Iterator itr = ls.iterator(); 226 | while(itr.hasNext()) 227 | { 228 | Index indx = itr.next(); 229 | if(indx.getType().equals("INDEX")) //checks statement for INDEX option. 230 | { 231 | //Add indexes to the SQL Table! 232 | Iterator itrFields = indx.getColumnsNames().iterator(); 233 | 234 | while(itrFields.hasNext()) 235 | table.addIndexField(itrFields.next()); 236 | 237 | //Decide what to do here. (Suggestion: go to index building code. Working on that right now) 238 | } 239 | else if(indx.getType().equals("PRIMARY KEY")) //checks statement for PRIMARY KEY option. 240 | { 241 | Iterator itrFields = indx.getColumnsNames().iterator(); 242 | 243 | while(itrFields.hasNext()) 244 | table.addPrimaryField(itrFields.next()); 245 | //Decide what to do here. (Suggestion: go to sorting code, provided we need data sorted by primary key) 246 | } 247 | } 248 | String[] itrIndex = foreignIndexMap.get(table.getTableName()); 249 | for(String itrFields:itrIndex) 250 | table.addIndexField(itrFields); 251 | } 252 | } 253 | 254 | public void parseSelect(Statement st ) 255 | { 256 | SelectBody stb=((Select)st).getSelectBody(); 257 | if(stb instanceof Union) 258 | parseUnion((Union)stb); 259 | else if(stb instanceof PlainSelect) 260 | { 261 | parsePlainSelect((PlainSelect)stb); 262 | } 263 | 264 | } 265 | 266 | private void parseUnion(Union stb) { 267 | System.out.println("Not Implemented!"); 268 | } 269 | 270 | public void parsePlainSelect(PlainSelect st) 271 | { 272 | HashMap aliasMap = parseAlias(st); 273 | 274 | try 275 | { 276 | String fromSource=st.getFromItem().toString(); 277 | FromItem fr1 = st.getFromItem(); 278 | List extrasources=st.getJoins(); 279 | String selectalias = null; 280 | if(fr1 instanceof SubSelect) 281 | { 282 | selectalias = fr1.getAlias(); 283 | fr1.setAlias(null); 284 | fr1.toString(); 285 | } 286 | else if(fr1 instanceof Table) 287 | { 288 | parseTableAlias(fr1); 289 | for(Join table:extrasources) 290 | { 291 | FromItem f=table.getRightItem(); 292 | parseTableAlias(f); 293 | } 294 | } 295 | Expression whereClause=st.getWhere(); 296 | fromSource = fr1.toString(); 297 | 298 | parseOperators(st,aliasMap); 299 | 300 | if(fromSource.startsWith("(") && fromSource.endsWith(")")) 301 | { 302 | ASTNode.outerprojectFields = parseSelectionList(st.getSelectItems(),aliasMap); 303 | fromSource=fromSource.substring(1, fromSource.length()-1); 304 | Statement nestedSt=parseSQLExpression(fromSource); 305 | if(nestedSt!=null) 306 | { 307 | if(selectalias == null) 308 | nestedAlias = "defautkey"; 309 | else 310 | nestedAlias = selectalias; 311 | 312 | ASTNode.isNestedQuery = true; 313 | parsePlainSelect((PlainSelect)(((Select)nestedSt).getSelectBody())); 314 | } 315 | } 316 | 317 | else 318 | { 319 | traverseExpressionTree(whereClause); 320 | translateExpressionsToIndexLookups(); 321 | queryRewritePlan(); 322 | 323 | if(additionalPredicates!=null && !additionalPredicates.isEmpty()) 324 | ASTNode.extraPredicates=additionalPredicates; 325 | 326 | EvaluatorNode evaluator = new EvaluatorNode(); 327 | evaluator.evaluate(); 328 | 329 | } 330 | } 331 | catch(Exception e) 332 | { 333 | System.out.println(e.getStackTrace()); 334 | } 335 | } 336 | 337 | private void queryRewritePlan() 338 | { 339 | TreeSet orderedJoinTables= new TreeSet<>(new Comparator() { 340 | public int compare(String s1,String s2) 341 | { 342 | 343 | int rValue= (int)(Parser.tableSizes.get(tables.get(s1).getTableName().toUpperCase()) - Parser.tableSizes.get(tables.get(s2).getTableName().toUpperCase())); 344 | if(rValue==0) 345 | return s1.compareTo(s2); 346 | else 347 | return rValue; 348 | } 349 | }); 350 | 351 | Iterator it=joinList.iterator(); 352 | while(it.hasNext()) 353 | { 354 | BinaryExpression exp=(BinaryExpression)it.next(); 355 | String lhs=exp.getLeftExpression().toString(); 356 | String rhs=exp.getRightExpression().toString(); 357 | 358 | String table1=Parser.getTableNameFromKey(lhs); 359 | String table2=Parser.getTableNameFromKey(rhs); 360 | 361 | Parser.getFieldFromKey(lhs); 362 | 363 | SQLTable t1 = tables.get(table1); 364 | SQLTable t2 = tables.get(table2); 365 | t1.addToJoinSet(new JoinDetails(table2,lhs)); 366 | t2.addToJoinSet(new JoinDetails(table1, rhs)); 367 | orderedJoinTables.add(table1); 368 | orderedJoinTables.add(table2); 369 | } 370 | 371 | ArrayList joinFields = new ArrayList<>(); 372 | joinFields.add(tables.get(orderedJoinTables.pollFirst())); 373 | 374 | SQLTable prev = joinFields.get(0); 375 | 376 | while(true) 377 | { 378 | //SQLTable next = tables.get(prev.getJoinSet().pollFirst().table); 379 | SQLTable next = tables.get(prev.getJoinSet().first().table); 380 | 381 | if(!joinFields.contains(next)) 382 | joinFields.add(next); 383 | else 384 | { 385 | prev.getJoinSet().pollFirst(); 386 | if(prev.getJoinSet().size()<=0) 387 | break; 388 | 389 | next = tables.get(prev.getJoinSet().first().table); 390 | if(!joinFields.contains(next)) 391 | joinFields.add(next); 392 | else 393 | break; 394 | } 395 | prev = next; 396 | } 397 | 398 | if(!joinFields.contains(prev)) 399 | joinFields.add(prev); 400 | ASTNode.joinTables.addAll(joinFields); 401 | } 402 | 403 | private void parseOperators(PlainSelect st, HashMap aliasMap) 404 | { 405 | long limitval; 406 | String distinctField; 407 | ASTNode.indexDir=indexDir; 408 | ASTNode.baseDir=baseDir; 409 | ASTNode.swapDir=swapDir; 410 | 411 | ASTNode.aliasMap = aliasMap; 412 | if(ASTNode.groupFields==null || ASTNode.groupFields.size()==0) //optimisation for nested query 413 | ASTNode.groupFields = parseGroupingList(st.getGroupByColumnReferences(),aliasMap); 414 | 415 | if(ASTNode.orderFields==null || ASTNode.orderFields.size()==0) 416 | ASTNode.orderFields = parseOrderByList(st.getOrderByElements(),aliasMap); 417 | 418 | ASTNode.projectFields = parseSelectionList(st.getSelectItems(),aliasMap); 419 | //ASTNode.joinFields = parseJoinList(st.getJoins()); 420 | distinctField = hasDistinctInQuery(aliasMap); 421 | if(distinctField != null && !distinctField.isEmpty()) 422 | { 423 | if(distinctField.contains("(")) 424 | distinctField = distinctField.replace("(", ""); 425 | if(distinctField.contains(")")) 426 | distinctField = distinctField.replace(")", ""); 427 | ASTNode.distinctField = distinctField.trim(); 428 | } 429 | else 430 | distinctField = ""; 431 | if(st.getLimit() != null) 432 | limitval = st.getLimit().getRowCount(); 433 | else 434 | limitval = -1; 435 | 436 | ASTNode.limit = limitval; 437 | } 438 | 439 | 440 | private ArrayList parseJoinList(List joins) 441 | { 442 | ArrayList joinList = new ArrayList(); 443 | int i = 0; 444 | while(joins != null && i parseSelectionList(List fields, HashMap aliasMap) 459 | { 460 | ArrayList result=new ArrayList(); 461 | //String[] result=new String[]; 462 | Iterator it=fields.iterator(); 463 | 464 | while(it.hasNext()) 465 | { 466 | SelectExpressionItem set=(SelectExpressionItem)it.next(); 467 | if(set.getAlias() != null) 468 | result.add(set.getAlias()); 469 | else 470 | result.add(set.toString()); 471 | } 472 | 473 | return result; 474 | } 475 | 476 | private ArrayList parseGroupingList(List fields, HashMap aliasMap) 477 | { 478 | if(fields==null) 479 | return null; 480 | ArrayList result=new ArrayList(); 481 | //String[] result=new String[]; 482 | Iterator it=fields.iterator(); 483 | 484 | while(it.hasNext()) 485 | { 486 | result.add(it.next().toString()); 487 | } 488 | 489 | return result; 490 | } 491 | private ArrayList parseOrderByList(List l, HashMap aliasMap) 492 | { 493 | ArrayList output=new ArrayList(); 494 | if(l==null || l.isEmpty()) 495 | return output; 496 | Iterator it=l.iterator(); 497 | String type="DESC"; 498 | while(it.hasNext()) 499 | { 500 | String value=it.next().toString(); 501 | if(value.contains(type)) 502 | { 503 | OrderDetails oe = new OrderDetails(value.split(" ")[0], "DESC"); 504 | output.add(oe); 505 | } 506 | else 507 | { 508 | OrderDetails oe = new OrderDetails(value.split(" ")[0], "ASC"); 509 | output.add(oe); 510 | } 511 | } 512 | return output; 513 | } 514 | 515 | private String hasDistinctInQuery(HashMapaMap) 516 | { 517 | Iterator it=aMap.keySet().iterator(); 518 | while(it.hasNext()) 519 | { 520 | String key=it.next(); 521 | Expression exp=aMap.get(key); 522 | if(exp instanceof Function) 523 | { 524 | String fname=((Function) exp).getName(); 525 | if(fname.toUpperCase().equals("COUNT") && ((Function) exp).isDistinct()) 526 | return ((Function) exp).getParameters().toString(); 527 | } 528 | } 529 | return null; 530 | } 531 | 532 | public ArrayList aggregateFunctions(PlainSelect st) 533 | { 534 | ArrayList expressions = new ArrayList(); 535 | for(int o=0 ; o parseAlias(PlainSelect st) 547 | { 548 | HashMap map1 = new HashMap(); 549 | 550 | if(ASTNode.aliasMap != null && !ASTNode.aliasMap.isEmpty()) 551 | map1 = ASTNode.aliasMap; 552 | 553 | try 554 | { 555 | for(int p=0;p it=tables.keySet().iterator(); 689 | while(it.hasNext()) 690 | { 691 | SQLTable t= tables.get(it.next()); 692 | if(t.getTableName().equals(tablename.toUpperCase())) 693 | { 694 | // copy object and add. It is necessary to clone coz ther emight be seperate filters for both aliases 695 | tables.put(aliasStr,new SQLTable(t)); 696 | break; 697 | } 698 | } 699 | 700 | } 701 | else 702 | { 703 | 704 | SQLTable t=new SQLTable(tables.get(tablename.toUpperCase())); 705 | 706 | t.setAlias(aliasStr); 707 | //tables.remove(tablename.toUpperCase()); 708 | tables.put(aliasStr, t); 709 | 710 | } 711 | 712 | 713 | fromItemsAliasMap.put(aliasStr,tablename); 714 | Parser.tableSizes.put(aliasStr, Parser.tableSizes.get(tablename.toUpperCase())); 715 | alterTable(tables.get(aliasStr)); 716 | } 717 | else if(aliasfrom instanceof SubSelect) 718 | { 719 | fromItemsAliasMap.put(aliasStr, aliasfrom.toString()); 720 | } 721 | 722 | } 723 | else // no alias 724 | { 725 | String tablename=aliasfrom.toString(); 726 | SQLTable t=tables.get(tablename.toUpperCase()); 727 | t.setAlias(tablename); // alias = tablename 728 | 729 | // The table names in the select query might be in lower case and so the next two lines do this conversion in the tables map. 730 | tables.remove(tablename.toUpperCase()); 731 | tables.put(tablename,t); 732 | alterTable(tables.get(tablename)); 733 | Parser.tableSizes.put(tablename, Parser.tableSizes.get(tablename.toUpperCase())); 734 | fromItemsAliasMap.put(tablename, tablename); 735 | 736 | } 737 | 738 | } 739 | public void alterTable(SQLTable t) 740 | { 741 | if(hasSingleSource) 742 | return; 743 | 744 | ArrayList schema = new ArrayList(); 745 | 746 | Iterator schIt=t.getSchema().iterator(); 747 | while(schIt.hasNext()) 748 | { 749 | fieldDetails oldFieldDetail=schIt.next(); 750 | String newKey = t.getTableAlias() + "." + oldFieldDetail.getName(); 751 | fieldDetails newFieldDetails = new fieldDetails(newKey, oldFieldDetail.getType()) ; 752 | schema.add(newFieldDetails); 753 | 754 | allFieldsMap.put(newKey, t.getTableAlias()); 755 | } 756 | t.setSchema(schema); 757 | } 758 | 759 | 760 | public void removeFromprojectFields(String key) 761 | { 762 | key=key. replace("(",""); 763 | key=key.replace(")",""); 764 | if(allFieldsMap.containsKey(key)) 765 | allFieldsMap.remove(key); 766 | } 767 | 768 | // Optimisations 769 | public void combineFilters() 770 | { 771 | 772 | 773 | for(SQLTable table:tables.values()) 774 | { 775 | ArrayList filters=table.getFilters(); 776 | if(filters.size()<=1) 777 | continue; 778 | 779 | Iterator expIt=filters.iterator(); 780 | AndExpression finalExp=null; 781 | 782 | while(expIt.hasNext()) 783 | { 784 | 785 | if(finalExp==null) 786 | finalExp=new AndExpression(expIt.next(),expIt.next()); 787 | else 788 | finalExp=new AndExpression(finalExp,expIt.next()); 789 | } 790 | table.getFilters().clear(); 791 | table.getFilters().add(finalExp); 792 | } 793 | } 794 | public void translateExpressionsToIndexLookups() 795 | { 796 | // Assumptions :1) Each query will comprise only a single range lookup on a table 2) REange lookup predicates usually occure together 797 | Iterator keyIt=tables.keySet().iterator(); 798 | while(keyIt.hasNext()) 799 | { 800 | SQLTable table=tables.get(keyIt.next()); 801 | ArrayList filters=table.getFilters(); 802 | Iterator expIt=filters.iterator(); 803 | IndexScanOperation scan=null; 804 | Expression exp1=null,exp2=null; 805 | while(expIt.hasNext()) 806 | { 807 | exp1=expIt.next(); 808 | 809 | if(exp1 instanceof MinorThan || exp1 instanceof MinorThanEquals || exp1 instanceof GreaterThan || exp1 instanceof GreaterThanEquals) 810 | { 811 | 812 | if(((BinaryExpression)exp1).getRightExpression() instanceof Column) 813 | continue; 814 | 815 | scan=new IndexScanOperation(); 816 | if(exp1 instanceof MinorThan || exp1 instanceof MinorThanEquals) 817 | { 818 | scan.key=Parser.getFieldFromKey(((BinaryExpression)exp1).getLeftExpression().toString()); 819 | scan.upperLimit=((BinaryExpression)exp1).getRightExpression().toString(); 820 | scan.upperLimit = scan.upperLimit.substring(1,scan.upperLimit.length()-1); 821 | if(exp1 instanceof MinorThanEquals) 822 | scan.upperInclusive=true; 823 | else 824 | scan.upperInclusive=false; 825 | 826 | exp2=expIt.next(); 827 | 828 | 829 | scan.lowerLimit=((BinaryExpression)exp2).getRightExpression().toString(); 830 | scan.lowerLimit = scan.lowerLimit.substring(1,scan.lowerLimit.length()-1); 831 | if(exp2 instanceof GreaterThanEquals) 832 | scan.lowerInclusive=true; 833 | else 834 | scan.lowerInclusive=false; 835 | 836 | 837 | } 838 | else 839 | { 840 | scan.key=Parser.getFieldFromKey(((BinaryExpression)exp1).getLeftExpression().toString()); 841 | scan.lowerLimit=((BinaryExpression)exp1).getRightExpression().toString(); 842 | scan.lowerLimit = scan.lowerLimit.substring(1,scan.lowerLimit.length()-1); 843 | if(exp1 instanceof GreaterThanEquals) 844 | scan.lowerInclusive=true; 845 | else 846 | scan.lowerInclusive=false; 847 | 848 | 849 | 850 | exp2=expIt.next(); 851 | 852 | scan.upperLimit=((BinaryExpression)exp2).getRightExpression().toString(); 853 | scan.upperLimit = scan.upperLimit.substring(1,scan.upperLimit.length()-1); 854 | if(exp2 instanceof MinorThanEquals) 855 | scan.upperInclusive=true; 856 | else 857 | scan.upperInclusive=false; 858 | 859 | } 860 | 861 | 862 | } 863 | 864 | 865 | } 866 | if(scan!=null) 867 | { 868 | scan.type="SECONDARY"; 869 | table.setIndexLookupOperation(scan); 870 | table.getFilters().remove(exp1); 871 | table.getFilters().remove(exp2); 872 | } 873 | 874 | } 875 | 876 | } 877 | 878 | // Extra Functions ******************************************************************************* 879 | public static String getTableNameFromKey(String key) 880 | { 881 | return key.split("\\.")[0]; 882 | } 883 | public static String getFieldFromKey(String key) 884 | { 885 | return key.split("\\.")[1]; 886 | } 887 | } 888 | --------------------------------------------------------------------------------