-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectOperator.java
90 lines (78 loc) · 2.28 KB
/
ProjectOperator.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
package operators;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import helpers.ProjOperatorHelper;
import models.Tuple;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.*;
public class ProjectOperator extends UnaryOperator {
/**
* Generate a projected tuple from the schema.
*
* @return the projected tuple
*/
@Override
public Tuple getNextTuple() {
Tuple tp = child.getNextTuple();
if (tp == null)
return null;
int[] cols = new int[schema.size()];
int i = 0;
for (String attr : schema) {
Long val = ProjOperatorHelper.getAttrVal(tp, attr, child.getSchema());
cols[i++] = val.intValue();
}
return new Tuple(cols);
}
/**
* Construct a project operator. Both AllColumns, AllTableColumns and
* SelectExpressionItem are considered.
*
* @param child
* the child
* @param sis
* the list of selected columns
*/
public ProjectOperator(List<SelectItem> selectItemList, Operator child) {
super(child);
System.out.println("Came inside Project Operator");
List<String> childSchema = child.getSchema();
List<String> tmpScm = new ArrayList<String>();
HashSet<String> allColumnsOfTable = new HashSet<String>();
for (SelectItem selectItem : selectItemList) {
if (selectItem instanceof AllColumns) {
schema = childSchema;
return;
}
if (selectItem instanceof AllTableColumns)
allColumnsOfTable.add(selectItem.toString().split(".")[0]);
else {
Column column = (Column) ((SelectExpressionItem) selectItem).getExpression();
if (column.getTable() != null && (column.getTable().getName() != null)) {
String table = column.getTable().getName();
if (allColumnsOfTable.contains(table))
continue;
tmpScm.add(table + '.' + column.getColumnName());
} else {
String colName = column.getColumnName();
for (String tabCol : childSchema) {
if (ProjOperatorHelper.getNameOfColumn(tabCol).equals(colName)) {
tmpScm.add(tabCol);
break;
}
}
}
}
}
if (allColumnsOfTable.isEmpty())
schema = tmpScm;
else {
for (String tabCol : childSchema) {
String table = tabCol.split(".")[0];
if (allColumnsOfTable.contains(table) || tmpScm.contains(tabCol))
schema.add(tabCol);
}
}
}
}