-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.java
119 lines (106 loc) · 4.01 KB
/
Evaluator.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
import net.sf.jsqlparser.eval.Eval;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.BooleanValue;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.PrimitiveValue;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.PrimitiveType;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
/**
* Created by Mugdha on 7/1/2017.
*/
public class Evaluator extends Eval {
PrimitiveValue[] tuple;
Column[] schema;
HashMap<String, String> aliasHashMap;
public Evaluator(PrimitiveValue[] tuple, Column[] schema, HashMap<String, String> aliasHashMap){
this.schema = schema;
this.tuple = tuple;
this.aliasHashMap = aliasHashMap;
}
public PrimitiveValue eval(Column c) throws SQLException {
String colName = c.getColumnName().toLowerCase();
String tableName = c.getTable().getName();
int index = -1;
if(tableName == null) {
for(int i = 0; i < schema.length; i++) {
if(schema[i].getColumnName().toLowerCase().equals(colName)) {
index = i;
break;
}
}
} else {
tableName = tableName.toLowerCase();
String fullTableName = aliasHashMap.get(tableName);
for(int i = 0; i < schema.length; i++) {
if(schema[i].getColumnName().toLowerCase().equals(c.getColumnName().toLowerCase())) {
if(schema[i].getTable().getName().toLowerCase().equals(fullTableName)) {
index = i;
}
}
}
}
return tuple[index];
}
public PrimitiveType escalateNumeric(PrimitiveType lhs, PrimitiveType rhs)
throws SQLException
{
if((lhs == PrimitiveType.DATE)||(rhs == PrimitiveType.DATE)) {
return PrimitiveType.DATE;
}
if( (assertNumeric(lhs) == PrimitiveType.DOUBLE)
||(assertNumeric(rhs) == PrimitiveType.DOUBLE)){
return PrimitiveType.DOUBLE;
} else {
return PrimitiveType.LONG;
}
}
public PrimitiveValue cmp(BinaryExpression e, CmpOp op)
throws SQLException
{
try {
PrimitiveValue lhs = eval(e.getLeftExpression());
PrimitiveValue rhs = eval(e.getRightExpression());
if(lhs == null || rhs == null) return null;
boolean ret;
switch(escalateNumeric(getPrimitiveType(lhs), getPrimitiveType(rhs))){
case DOUBLE:
ret = op.op(lhs.toDouble(), rhs.toDouble());
break;
case LONG:
ret = op.op(lhs.toLong(), rhs.toLong());
break;
case DATE: {
String rhs1 = rhs.toRawString();
Date d = null;
try{
d = new SimpleDateFormat("yyyy-MM-dd").parse(rhs1);
}
catch (ParseException p){
p.printStackTrace();
}
DateValue dlhs = (DateValue)lhs;
//drhs = (DateValue)d;
ret = op.op(
dlhs.getYear()*10000+
dlhs.getMonth()*100+
dlhs.getDate(),
d.getYear()*10000+
d.getMonth()*100+
d.getDate()
);
}
break;
default:
throw new SQLException("Invalid PrimitiveType escalation");
}
return ret ? BooleanValue.TRUE : BooleanValue.FALSE;
} catch(PrimitiveValue.InvalidPrimitive ex) {
throw new SQLException("Invalid leaf value", ex);
}
}
}