aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/main/java/org/apache/drill/exec/expr/EqualityVisitor.java
blob: 94f8b79489fbf64bd56b8dffa65ffd4f7f329690 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.drill.exec.expr;

import com.google.common.collect.Lists;
import org.apache.drill.common.expression.BooleanOperator;
import org.apache.drill.common.expression.CastExpression;
import org.apache.drill.common.expression.ConvertExpression;
import org.apache.drill.common.expression.FunctionCall;
import org.apache.drill.common.expression.FunctionHolderExpression;
import org.apache.drill.common.expression.IfExpression;
import org.apache.drill.common.expression.LogicalExpression;
import org.apache.drill.common.expression.NullExpression;
import org.apache.drill.common.expression.SchemaPath;
import org.apache.drill.common.expression.TypedNullConstant;
import org.apache.drill.common.expression.ValueExpressions.BooleanExpression;
import org.apache.drill.common.expression.ValueExpressions.DateExpression;
import org.apache.drill.common.expression.ValueExpressions.Decimal18Expression;
import org.apache.drill.common.expression.ValueExpressions.Decimal28Expression;
import org.apache.drill.common.expression.ValueExpressions.Decimal38Expression;
import org.apache.drill.common.expression.ValueExpressions.Decimal9Expression;
import org.apache.drill.common.expression.ValueExpressions.DoubleExpression;
import org.apache.drill.common.expression.ValueExpressions.FloatExpression;
import org.apache.drill.common.expression.ValueExpressions.IntExpression;
import org.apache.drill.common.expression.ValueExpressions.IntervalDayExpression;
import org.apache.drill.common.expression.ValueExpressions.IntervalYearExpression;
import org.apache.drill.common.expression.ValueExpressions.LongExpression;
import org.apache.drill.common.expression.ValueExpressions.QuotedString;
import org.apache.drill.common.expression.ValueExpressions.TimeExpression;
import org.apache.drill.common.expression.ValueExpressions.TimeStampExpression;
import org.apache.drill.common.expression.ValueExpressions.VarDecimalExpression;
import org.apache.drill.common.expression.visitors.AbstractExprVisitor;

import java.util.List;

class EqualityVisitor extends AbstractExprVisitor<Boolean,LogicalExpression,RuntimeException> {

  @Override
  public Boolean visitFunctionCall(FunctionCall call, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof FunctionCall)) {
      return false;
    }
    if (!checkType(call, value)) {
      return false;
    }
    if (!call.getName().equals(((FunctionCall) value).getName())) {
      return false;
    }
    return checkChildren(call, value);
  }

  @Override
  public Boolean visitFunctionHolderExpression(FunctionHolderExpression holder, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof FunctionHolderExpression)) {
      return false;
    }
    if (!checkType(holder, value)) {
      return false;
    }
    if (!holder.getName().equals(((FunctionHolderExpression) value).getName())) {
      return false;
    }
    if (holder.isRandom()) {
      return false;
    }
    return checkChildren(holder, value);
  }

  @Override
  public Boolean visitIfExpression(IfExpression ifExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof IfExpression)) {
      return false;
    }
    return checkChildren(ifExpr, value);
  }

  @Override
  public Boolean visitBooleanOperator(BooleanOperator call, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof BooleanOperator)) {
      return false;
    }
    if (!call.getName().equals(((BooleanOperator) value).getName())) {
      return false;
    }
    return checkChildren(call, value);
  }

  @Override
  public Boolean visitSchemaPath(SchemaPath path, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof SchemaPath)) {
      return false;
    }
    return path.equals(value);
  }

  @Override
  public Boolean visitIntConstant(IntExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof IntExpression)) {
      return false;
    }
    return intExpr.getInt() == ((IntExpression) value).getInt();
  }

  @Override
  public Boolean visitFloatConstant(FloatExpression fExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof FloatExpression)) {
      return false;
    }
    return fExpr.getFloat() == ((FloatExpression) value).getFloat();
  }

  @Override
  public Boolean visitLongConstant(LongExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof LongExpression)) {
      return false;
    }
    return intExpr.getLong() == ((LongExpression) value).getLong();
  }

  @Override
  public Boolean visitDateConstant(DateExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof DateExpression)) {
      return false;
    }
    return intExpr.getDate() == ((DateExpression) value).getDate();
  }

  @Override
  public Boolean visitTimeConstant(TimeExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof TimeExpression)) {
      return false;
    }
    return intExpr.getTime() == ((TimeExpression) value).getTime();
  }

  @Override
  public Boolean visitTimeStampConstant(TimeStampExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof TimeStampExpression)) {
      return false;
    }
    return intExpr.getTimeStamp() == ((TimeStampExpression) value).getTimeStamp();
  }

  @Override
  public Boolean visitIntervalYearConstant(IntervalYearExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof IntervalYearExpression)) {
      return false;
    }
    return intExpr.getIntervalYear() == ((IntervalYearExpression) value).getIntervalYear();
  }

  @Override
  public Boolean visitIntervalDayConstant(IntervalDayExpression intExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof IntervalDayExpression)) {
      return false;
    }
    return intExpr.getIntervalDay() == ((IntervalDayExpression) value).getIntervalDay()
            && intExpr.getIntervalMillis() == ((IntervalDayExpression) value).getIntervalMillis();
  }

  @Override
  public Boolean visitDecimal9Constant(Decimal9Expression decExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof Decimal9Expression)) {
      return false;
    }
    if (decExpr.getIntFromDecimal() != ((Decimal9Expression) value).getIntFromDecimal()) {
      return false;
    }
    if (decExpr.getScale() != ((Decimal9Expression) value).getScale()) {
      return false;
    }
    if (decExpr.getPrecision() != ((Decimal9Expression) value).getPrecision()) {
      return false;
    }
    return true;
  }

  @Override
  public Boolean visitDecimal18Constant(Decimal18Expression decExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof Decimal18Expression)) {
      return false;
    }
    if (decExpr.getLongFromDecimal() != ((Decimal18Expression) value).getLongFromDecimal()) {
      return false;
    }
    if (decExpr.getScale() != ((Decimal18Expression) value).getScale()) {
      return false;
    }
    if (decExpr.getPrecision() != ((Decimal18Expression) value).getPrecision()) {
      return false;
    }
    return true;
  }

  @Override
  public Boolean visitDecimal28Constant(Decimal28Expression decExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof Decimal28Expression)) {
      return false;
    }
    if (decExpr.getBigDecimal() != ((Decimal28Expression) value).getBigDecimal()) {
      return false;
    }
    return true;
  }

  @Override
  public Boolean visitDecimal38Constant(Decimal38Expression decExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof Decimal38Expression)) {
      return false;
    }
    if (decExpr.getBigDecimal() != ((Decimal38Expression) value).getBigDecimal()) {
      return false;
    }
    if (!decExpr.getMajorType().equals(((Decimal38Expression) value).getMajorType())) {
      return false;
    }
    return true;
  }

  @Override
  public Boolean visitVarDecimalConstant(VarDecimalExpression decExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof VarDecimalExpression)) {
      return false;
    }
    if (!decExpr.getMajorType().equals(value.getMajorType())) {
      return false;
    }
    if (!decExpr.getBigDecimal().equals(((VarDecimalExpression) value).getBigDecimal())) {
      return false;
    }
    return true;
  }

  @Override
  public Boolean visitDoubleConstant(DoubleExpression dExpr, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof DoubleExpression)) {
      return false;
    }
    return dExpr.getDouble() == ((DoubleExpression) value).getDouble();
  }

  @Override
  public Boolean visitBooleanConstant(BooleanExpression e, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof BooleanExpression)) {
      return false;
    }
    return e.getBoolean() == ((BooleanExpression) value).getBoolean();
  }

  @Override
  public Boolean visitQuotedStringConstant(QuotedString e, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof QuotedString)) {
      return false;
    }
    return e.getString().equals(((QuotedString) value).getString());
  }

  @Override
  public Boolean visitNullExpression(NullExpression e, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof NullExpression)) {
      return false;
    }
    return e.getMajorType().equals(value.getMajorType());
  }

  @Override
  public Boolean visitCastExpression(CastExpression e, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof CastExpression)) {
      return false;
    }
    if (!e.getMajorType().equals(value.getMajorType())) {
      return false;
    }
    return checkChildren(e, value);
  }

  @Override
  public Boolean visitConvertExpression(ConvertExpression e, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof ConvertExpression)) {
      return false;
    }
    if (!e.getConvertFunction().equals(((ConvertExpression) value).getConvertFunction())) {
      return false;
    }
    return checkChildren(e, value);
  }

  @Override
  public Boolean visitNullConstant(TypedNullConstant e, LogicalExpression value) throws RuntimeException {
    if (!(value instanceof TypedNullConstant)) {
      return false;
    }
    return value.getMajorType().equals(e.getMajorType());
  }

  @Override
  public Boolean visitUnknown(LogicalExpression e, LogicalExpression value) throws RuntimeException {
    if (e instanceof ValueVectorReadExpression && value instanceof ValueVectorReadExpression) {
      return visitValueVectorReadExpression((ValueVectorReadExpression) e, (ValueVectorReadExpression) value);
    }
    return false;
  }

  private Boolean visitValueVectorReadExpression(ValueVectorReadExpression e, ValueVectorReadExpression value) {
    return e.getTypedFieldId().equals(value.getTypedFieldId());
  }


  private boolean checkChildren(LogicalExpression thisExpr, LogicalExpression thatExpr) {
    List<LogicalExpression> theseChildren = Lists.newArrayList(thisExpr);
    List<LogicalExpression> thoseChildren = Lists.newArrayList(thatExpr);

    if (theseChildren.size() != thoseChildren.size()) {
      return false;
    }
    for (int i = 0; i < theseChildren.size(); i++) {
      if (!theseChildren.get(i).accept(this, thoseChildren.get(i))) {
        return false;
      }
    }
    return true;
  }

  private boolean checkType(LogicalExpression e1, LogicalExpression e2) {
    return e1.getMajorType().equals(e2.getMajorType());
  }
}