-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubMain.java
124 lines (109 loc) · 4.51 KB
/
SubMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.PrimitiveValue;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.select.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Mugdha on 7/3/2017.
*/
public class SubMain {
public PlainSelect plainSelect;
public HashMap<String, CreateTable> createTableMap;
public Column[] schema;
public Column[] newSchema;
public SubMain(PlainSelect plainSelect, HashMap createTableMap){
this.plainSelect = plainSelect;
this.createTableMap = createTableMap;
}
public ArrayList execute(){
HashMap<String, HashMap<String, ColumnIdType>> databaseMap = new HashMap<>();
HashMap<PrimitiveValue,ArrayList<PrimitiveValue[]>> groupByMap = new HashMap<>();
ArrayList<PrimitiveValue[]> outputTupleList = new ArrayList<>();
HashMap<String, String> aliasHashMap;
HashMap<String, Operator> operatorMap;
List<Join> joinList;
Operator oper = null;
FromScanner fromscan = new FromScanner(createTableMap);
plainSelect.getFromItem().accept(fromscan);
if(plainSelect.getJoins() != null) {
joinList = plainSelect.getJoins();
for(Join join : joinList) {
join.getRightItem().accept(fromscan);
}
} else {
}
aliasHashMap = fromscan.aliasHasMap;
operatorMap = fromscan.operatorMap;
schema = new Column[fromscan.schemaList.size()];
schema = fromscan.schemaList.toArray(schema);
createTableMap = fromscan.createTableMap;
if(plainSelect.getWhere() != null) {
oper = new SelectionOperator(
databaseMap,
operatorMap,
schema,
plainSelect.getWhere(),
aliasHashMap,
createTableMap
);
} else {
for (String key : operatorMap.keySet()) {
oper = operatorMap.get(key);
}
}
//group by
if(plainSelect.getGroupByColumnReferences()!=null){
List<Column> groupByColumns = plainSelect.getGroupByColumnReferences();
PrimitiveValue[] tuple = oper.readOneTuple();
GroupByOperator groupByOperator = new GroupByOperator(schema, groupByColumns, aliasHashMap);
while(tuple!=null){
groupByOperator.groupTuples(tuple);
tuple = oper.readOneTuple();
}
groupByMap = groupByOperator.groupByMap;
outputTupleList = groupByOperator.getGroupByOutput();
}
else{
PrimitiveValue[] tuple = oper.readOneTuple();
while(tuple!=null){
outputTupleList.add(tuple);
tuple = oper.readOneTuple();
}
}
//Check for aggregate condition in SELECT
// Having
if(plainSelect.getHaving()!=null){
Expression condition = plainSelect.getHaving();
HavingOperator havingOperator = new HavingOperator(condition, groupByMap, aliasHashMap, schema);
havingOperator.filterGroupedEle();
outputTupleList = havingOperator.getHavingOutput();
groupByMap = havingOperator.getMap();
}
//Order By
if(plainSelect.getOrderByElements()!=null){
List<OrderByElement> orderByList = plainSelect.getOrderByElements();
OrderByOperator orderByOperator = new OrderByOperator(
groupByMap, aliasHashMap, schema, plainSelect, outputTupleList
);
orderByOperator.orderTuples(orderByList);
outputTupleList = orderByOperator.getOrderByOutput();
groupByMap = orderByOperator.groupByMap;
}
List<SelectItem> selectItemList = plainSelect.getSelectItems();
ProjectionOperator projectionOperator = new ProjectionOperator(
outputTupleList, selectItemList, schema, aliasHashMap
);
outputTupleList = projectionOperator.getProjectedOutput();
newSchema = projectionOperator.newSchema;
Distinct distinct = plainSelect.getDistinct();
if(distinct != null){
DistinctOperator distinctOperator = new DistinctOperator(outputTupleList);
outputTupleList = distinctOperator.execute();
}
return outputTupleList;
}
}