1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.expreval.expr.instmt;
22
23 import org.apache.expreval.client.NullColumnValueException;
24 import org.apache.expreval.client.ResultMissingColumnException;
25 import org.apache.expreval.expr.ExpressionType;
26 import org.apache.expreval.expr.NotValue;
27 import org.apache.expreval.expr.node.BooleanValue;
28 import org.apache.expreval.expr.node.GenericValue;
29 import org.apache.hadoop.hbase.filter.WritableByteArrayComparable;
30 import org.apache.hadoop.hbase.hbql.client.HBqlException;
31 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
32 import org.apache.hadoop.hbase.hbql.impl.InvalidServerFilterException;
33
34 import java.util.List;
35
36 public abstract class GenericInStmt extends NotValue<GenericInStmt> implements BooleanValue {
37
38 protected GenericInStmt(final GenericValue arg0, final boolean not, final List<GenericValue> inList) {
39 super(ExpressionType.INSTMT, not, arg0, inList);
40 }
41
42 protected abstract boolean evaluateInList(final Object object) throws HBqlException,
43 ResultMissingColumnException,
44 NullColumnValueException;
45
46 protected List<GenericValue> getInList() {
47 return this.getSubArgs(1);
48 }
49
50 public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
51 ResultMissingColumnException,
52 NullColumnValueException {
53 final boolean retval = this.evaluateInList(object);
54 return (this.isNot()) ? !retval : retval;
55 }
56
57 public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
58 final boolean allowCollections) throws HBqlException {
59 return BooleanValue.class;
60 }
61
62 public String asString() {
63 final StringBuilder sbuf = new StringBuilder(this.getExprArg(0).asString() + notAsString() + " IN (");
64
65 boolean first = true;
66 for (final GenericValue valueExpr : this.getInList()) {
67 if (!first)
68 sbuf.append(", ");
69 sbuf.append(valueExpr.asString());
70 first = false;
71 }
72 sbuf.append(")");
73 return sbuf.toString();
74 }
75
76 private boolean inListIsConstant() {
77 for (final GenericValue valueExpr : this.getInList()) {
78 if (!valueExpr.isAConstant())
79 return false;
80 }
81 return true;
82 }
83
84 protected abstract static class GenericInComparable<T> extends WritableByteArrayComparable {
85
86 private List<T> inValues;
87
88 protected List<T> getInValues() {
89 return this.inValues;
90 }
91
92 protected void setInValue(final List<T> inValues) {
93 this.inValues = inValues;
94 }
95 }
96
97 protected void validateArgsForInFilter() throws InvalidServerFilterException {
98 if (this.getExprArg(0).isAColumnReference() && this.inListIsConstant())
99 return;
100
101 throw new InvalidServerFilterException("Filter requires a column reference and list of constants");
102 }
103 }