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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package edu.caltech.nanodb.expressions;
import java.util.ArrayList;
import java.util.List;
import edu.caltech.nanodb.queryast.SelectClause;
import edu.caltech.nanodb.relations.ColumnInfo;
import edu.caltech.nanodb.relations.ColumnType;
import edu.caltech.nanodb.relations.Schema;
import edu.caltech.nanodb.relations.SchemaNameException;
import edu.caltech.nanodb.relations.Tuple;
/**
* This class implements the <tt>expr IN (subquery)</tt> operator. This
* operation may be optimized out of a query, but if it is not, it can still
* be evaluated although it will be slow.
*/
public class InSubqueryOperator extends SubqueryOperator {
/**
* The list of expressions to check against the set on the righthand side of the
* <tt>IN</tt> operator.
*/
private ArrayList<Expression> exprList = new ArrayList<>();
/**
* If this is false, the operator computes <tt>expr IN (subquery)</tt>;
* if true, the operator computes <tt>expr NOT IN (subquery)</tt>.
*/
private boolean invert = false;
public InSubqueryOperator(Expression expr, SelectClause subquery) {
if (expr == null)
throw new IllegalArgumentException("expr cannot be null");
if (subquery == null)
throw new IllegalArgumentException("subquery cannot be null");
exprList.add(expr);
this.subquery = subquery;
}
public InSubqueryOperator(List<Expression> exprList, SelectClause subquery) {
if (exprList == null)
throw new IllegalArgumentException("exprList cannot be null");
if (exprList.isEmpty())
throw new IllegalArgumentException("exprList cannot be empty");
if (subquery == null)
throw new IllegalArgumentException("subquery cannot be null");
this.exprList.addAll(exprList);
this.subquery = subquery;
}
public void setInvert(boolean invert) {
this.invert = invert;
}
public ColumnInfo getColumnInfo(Schema schema) throws SchemaNameException {
// Comparisons always return Boolean values, so just pass a Boolean
// value in to the TypeConverter to get out the corresponding SQL type.
ColumnType colType =
new ColumnType(TypeConverter.getSQLType(Boolean.FALSE));
return new ColumnInfo(colType);
}
/**
* Evaluates this comparison expression and returns either
* {@link java.lang.Boolean#TRUE} or {@link java.lang.Boolean#FALSE}. If
* either the left-hand or right-hand expression evaluates to
* <code>null</code> (representing the SQL <tt>NULL</tt> value), then the
* expression's result is always <code>FALSE</code>.
*
* @design (Donnie) We have to suppress "unchecked operation" warnings on
* this code, since {@link Comparable} is a generic (and thus allows
* us to specify the type of object being compared), but we want to
* use it without specifying any types.
*/
@SuppressWarnings("unchecked")
public Object evaluate(Environment env) throws ExpressionException {
if (subqueryPlan == null)
throw new IllegalStateException("No execution plan for subquery");
// Compute the values that we need to compare to, into a tuple. Then
// we can use the tuple-comparator to see if the subquery produces the
// same tuple-values.
TupleLiteral valueTup = new TupleLiteral();
for (Expression expr : exprList) {
// TODO: Return NULL if one of the values is NULL?
valueTup.addValue(expr.evaluate(env));
}
subqueryPlan.initialize();
while (true) {
Tuple subqueryTup = subqueryPlan.getNextTuple();
if (subqueryTup == null)
break;
if (TupleComparator.areTuplesEqual(valueTup, subqueryTup))
return invert ? false : true;
}
return invert ? true : false;
}
@Override
public Expression traverse(ExpressionProcessor p) {
p.enter(this);
for (int i = 0; i < exprList.size(); i++)
exprList.set(i, exprList.get(i).traverse(p));
// We do not traverse the subquery; it is treated as a "black box"
// by the expression-traversal mechanism.
return p.leave(this);
}
/**
* Returns a string representation of this comparison expression and its
* subexpressions.
*/
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
// Convert all of the components into string representations.
if (exprList.size() == 1) {
buf.append(exprList.get(0).toString());
}
else {
char ch = '(';
for (Expression expr : exprList) {
buf.append(ch);
ch = ',';
buf.append(expr.toString());
}
buf.append(')');
}
if (invert)
buf.append(" NOT");
buf.append(" IN (");
buf.append(subquery.toString());
buf.append(')');
return buf.toString();
}
/**
* Checks if the argument is an expression with the same structure, but not
* necessarily the same references.
*
* @param obj the object to which we are comparing
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof InSubqueryOperator) {
InSubqueryOperator other = (InSubqueryOperator) obj;
return exprList.equals(other.exprList) &&
subquery.equals(other.subquery);
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
for (Expression expr : exprList)
hash = 31 * hash + expr.hashCode();
hash = 31 * hash + subquery.hashCode();
return hash;
}
/**
* Creates a copy of expression. This method is used by the
* {@link Expression#duplicate} method to make a deep copy of an expression
* tree.
*/
@Override
@SuppressWarnings("unchecked")
protected Object clone() throws CloneNotSupportedException {
InSubqueryOperator op = (InSubqueryOperator) super.clone();
// Clone the subexpressions.
op.exprList = (ArrayList<Expression>) exprList.clone();
// Don't clone the subquery, since subqueries currently aren't cloneable.
return op;
}
}