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.betweenstmt;
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.ExpressionType;
28 import org.apache.expreval.expr.node.GenericValue;
29 import org.apache.expreval.expr.var.DelegateColumn;
30 import org.apache.expreval.expr.var.GenericColumn;
31 import org.apache.hadoop.hbase.filter.CompareFilter;
32 import org.apache.hadoop.hbase.filter.Filter;
33 import org.apache.hadoop.hbase.hbql.client.HBqlException;
34 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
35 import org.apache.hadoop.hbase.hbql.impl.Utils;
36 import org.apache.hadoop.hbase.hbql.io.IO;
37 import org.apache.hadoop.hbase.hbql.mapping.FieldType;
38
39 import java.io.DataInput;
40 import java.io.DataOutput;
41 import java.io.IOException;
42 import java.util.Date;
43
44 public class DateBetweenStmt extends GenericBetweenStmt {
45
46 private static final Log LOG = LogFactory.getLog(DateBetweenStmt.class);
47
48 public DateBetweenStmt(final GenericValue expr,
49 final boolean not,
50 final GenericValue lower,
51 final GenericValue upper) {
52 super(ExpressionType.DATEBETWEEN, not, expr, lower, upper);
53 }
54
55 public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
56 ResultMissingColumnException,
57 NullColumnValueException {
58 final long dateval = (Long)this.getExprArg(0).getValue(conn, object);
59 final boolean retval = dateval >= (Long)this.getExprArg(1).getValue(conn, object)
60 && dateval <= (Long)this.getExprArg(2).getValue(conn, object);
61
62 return (this.isNot()) ? !retval : retval;
63 }
64
65 public Filter getFilter() throws HBqlException {
66
67 this.validateArgsForBetweenFilter();
68
69 final GenericColumn<? extends GenericValue> column = ((DelegateColumn)this.getExprArg(0)).getTypedColumn();
70 final Long lowerVal = (Long)this.getConstantValue(1);
71 final Long upperVal = (Long)this.getConstantValue(2);
72
73 return this.newSingleColumnValueFilter(column.getColumnAttrib(),
74 CompareFilter.CompareOp.EQUAL,
75 new DateBetweenComparable(lowerVal, upperVal));
76 }
77
78 private static class DateBetweenComparable extends GenericBetweenComparable<Long> {
79
80 public DateBetweenComparable() {
81 }
82
83 public DateBetweenComparable(final Long lowerValue, final Long upperValue) {
84 this.setLowerValue(lowerValue);
85 this.setUpperValue(upperValue);
86 }
87
88 public int compareTo(final byte[] bytes) {
89 try {
90 Date dateValue = (Date)IO.getSerialization().getScalarFromBytes(FieldType.DateType, bytes);
91 long val = dateValue.getTime();
92 final long lowerVal = this.getLowerValue();
93 final long upperVal = this.getUpperValue();
94 return (val >= lowerVal && val <= upperVal) ? 0 : 1;
95 }
96 catch (HBqlException e) {
97 e.printStackTrace();
98 Utils.logException(LOG, e);
99 return 1;
100 }
101 }
102
103 public void write(final DataOutput dataOutput) throws IOException {
104 dataOutput.writeLong(this.getLowerValue());
105 dataOutput.writeLong(this.getUpperValue());
106 }
107
108 public void readFields(final DataInput dataInput) throws IOException {
109 this.setLowerValue(dataInput.readLong());
110 this.setUpperValue(dataInput.readLong());
111 }
112 }
113 }