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.compare;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.expreval.client.NullColumnValueException;
26 import org.apache.expreval.client.ResultMissingColumnException;
27 import org.apache.expreval.expr.Operator;
28 import org.apache.expreval.expr.node.DateValue;
29 import org.apache.expreval.expr.node.GenericValue;
30 import org.apache.expreval.expr.var.DelegateColumn;
31 import org.apache.expreval.expr.var.GenericColumn;
32 import org.apache.hadoop.hbase.filter.CompareFilter;
33 import org.apache.hadoop.hbase.filter.Filter;
34 import org.apache.hadoop.hbase.hbql.client.HBqlException;
35 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
36 import org.apache.hadoop.hbase.hbql.impl.Utils;
37 import org.apache.hadoop.hbase.hbql.io.IO;
38 import org.apache.hadoop.hbase.hbql.mapping.FieldType;
39
40 import java.io.DataInput;
41 import java.io.DataOutput;
42 import java.io.IOException;
43 import java.util.Date;
44
45 public class DateCompare extends GenericCompare {
46
47 private static final Log LOG = LogFactory.getLog(DateCompare.class);
48
49 public DateCompare(final GenericValue arg0, final Operator operator, final GenericValue arg1) {
50 super(arg0, operator, arg1);
51 }
52
53 public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
54 final boolean allowCollections) throws HBqlException {
55 return this.validateType(DateValue.class);
56 }
57
58 public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
59 ResultMissingColumnException,
60 NullColumnValueException {
61
62 final long val0 = (Long)this.getValue(0, conn, object);
63 final long val1 = (Long)this.getValue(1, conn, object);
64
65 switch (this.getOperator()) {
66 case EQ:
67 return val0 == val1;
68 case NOTEQ:
69 return val0 != val1;
70 case GT:
71 return val0 > val1;
72 case GTEQ:
73 return val0 >= val1;
74 case LT:
75 return val0 < val1;
76 case LTEQ:
77 return val0 <= val1;
78 default:
79 throw new HBqlException("Invalid operator: " + this.getOperator());
80 }
81 }
82
83 public Filter getFilter() throws HBqlException {
84
85 this.validateArgsForCompareFilter();
86
87 final GenericColumn<? extends GenericValue> column;
88 final Object constant;
89 final CompareFilter.CompareOp compareOp;
90
91 if (this.getExprArg(0).isAColumnReference()) {
92 column = ((DelegateColumn)this.getExprArg(0)).getTypedColumn();
93 constant = this.getConstantValue(1);
94 compareOp = this.getOperator().getCompareOpLeft();
95 }
96 else {
97 column = ((DelegateColumn)this.getExprArg(1)).getTypedColumn();
98 constant = this.getConstantValue(0);
99 compareOp = this.getOperator().getCompareOpRight();
100 }
101
102 return this.newSingleColumnValueFilter(column.getColumnAttrib(),
103 compareOp,
104 new DateComparable((Long)constant));
105 }
106
107 private static class DateComparable extends GenericComparable<Long> {
108
109 public DateComparable() {
110 }
111
112 public DateComparable(final Long value) {
113 this.setTypedValue(value);
114 }
115
116 public int compareTo(final byte[] bytes) {
117
118 if (this.equalValues(bytes))
119 return 0;
120
121 try {
122 final Date dateValue = (Date)IO.getSerialization().getScalarFromBytes(FieldType.DateType, bytes);
123 final long columnValue = dateValue.getTime();
124 return (columnValue > this.getTypedValue()) ? -1 : 1;
125 }
126 catch (HBqlException e) {
127 e.printStackTrace();
128 Utils.logException(LOG, e);
129 return 1;
130 }
131 }
132
133 public void write(final DataOutput dataOutput) throws IOException {
134 dataOutput.writeLong(this.getTypedValue());
135 }
136
137 public void readFields(final DataInput dataInput) throws IOException {
138 this.setTypedValue(dataInput.readLong());
139
140 this.setValueInBytes(FieldType.LongType, this.getTypedValue());
141 }
142 }
143 }