Xikui Wang has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/2621

Change subject: [ASTERIXDB-2375][RT] Evaluate constant experession in SELECT 
only once
......................................................................

[ASTERIXDB-2375][RT] Evaluate constant experession in SELECT only once

- user model changes: none
- storage format changes: none
- interface changes: new method added to ComparisonHelper to handle
cached value.

Currently, in datascan case, the constant value is evaluated on per
tuple bases. We could cache the value to avoid the deserialization of
the constant value each time.

This patch also fixes the FLOAT and DOUBLE comparison issue due to the
precision promotion.

Change-Id: Iae4e78928da2bd63b2984b3624b88baed9b7cd73
---
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.1.ddl.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.2.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.3.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.4.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.5.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.6.update.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.1.adm
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.2.adm
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.3.adm
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.4.adm
M asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
M 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/AbstractComparisonEvaluator.java
M 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/ComparisonHelper.java
13 files changed, 294 insertions(+), 86 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/21/2621/1

diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.1.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.1.ddl.sqlpp
new file mode 100644
index 0000000..9083866
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.1.ddl.sqlpp
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+drop  dataverse tpch if exists;
+create  dataverse tpch;
+
+use tpch;
+
+create type tpch.SupplierType as closed {
+  s_suppkey : int32,
+  s_name : string,
+  s_address : string,
+  s_nationkey : int32,
+  s_phone : string,
+  s_acctbal : float,
+  s_comment : string
+};
+
+create  dataset Supplier(SupplierType) primary key s_name;
+
+
+load  dataset Supplier using localfs 
((`path`=`asterix_nc1://data/tpch0.001/supplier.tbl`),(`format`=`delimited-text`),(`delimiter`=`|`));
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.2.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.2.query.sqlpp
new file mode 100644
index 0000000..128f64f
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.2.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+use tpch;
+
+select value s from Supplier s where 1 = s.s_suppkey;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.3.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.3.query.sqlpp
new file mode 100644
index 0000000..f7ed2d7
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.3.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+use tpch;
+
+select value s from Supplier s where s.s_suppkey <= 5 order by s_suppkey;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.4.query.sqlpp
new file mode 100644
index 0000000..ffa8a0f
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.4.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+use tpch;
+
+select value s from Supplier s where s.s_acctbal = -283.84;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.5.query.sqlpp
new file mode 100644
index 0000000..6412265
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.5.query.sqlpp
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+use tpch;
+
+select value s from Supplier s where s.s_acctbal < 4500 order by s_suppkey;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.6.update.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.6.update.sqlpp
new file mode 100644
index 0000000..21d5a43
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/comparison/secondary_idx_lookup/secondary_idx_lookup.6.update.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+drop dataverse tpch;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.1.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.1.adm
new file mode 100644
index 0000000..519d3ab
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.1.adm
@@ -0,0 +1 @@
+{ "s_suppkey": 1, "s_name": "Supplier#000000001", "s_address": " N kD4on9OM 
Ipw3,gf0JBoQDd7tgrzrddZ", "s_nationkey": 17, "s_phone": "27-918-335-1736", 
"s_acctbal": 5755.94, "s_comment": "each slyly above the careful" }
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.2.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.2.adm
new file mode 100644
index 0000000..dda08f9
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.2.adm
@@ -0,0 +1,5 @@
+{ "s_suppkey": 1, "s_name": "Supplier#000000001", "s_address": " N kD4on9OM 
Ipw3,gf0JBoQDd7tgrzrddZ", "s_nationkey": 17, "s_phone": "27-918-335-1736", 
"s_acctbal": 5755.94, "s_comment": "each slyly above the careful" }
+{ "s_suppkey": 2, "s_name": "Supplier#000000002", "s_address": 
"89eJ5ksX3ImxJQBvxObC,", "s_nationkey": 5, "s_phone": "15-679-861-2259", 
"s_acctbal": 4032.68, "s_comment": " slyly bold instructions. idle dependen" }
+{ "s_suppkey": 3, "s_name": "Supplier#000000003", "s_address": 
"q1,G3Pj6OjIuUYfUoH18BFTKP5aU9bEV3", "s_nationkey": 1, "s_phone": 
"11-383-516-1199", "s_acctbal": 4192.4, "s_comment": "blithely silent requests 
after the express dependencies are sl" }
+{ "s_suppkey": 4, "s_name": "Supplier#000000004", "s_address": 
"Bk7ah4CK8SYQTepEmvMkkgMwg", "s_nationkey": 15, "s_phone": "25-843-787-7479", 
"s_acctbal": 4641.08, "s_comment": "riously even requests above the exp" }
+{ "s_suppkey": 5, "s_name": "Supplier#000000005", "s_address": 
"Gcdm2rJRzl5qlTVzc", "s_nationkey": 11, "s_phone": "21-151-690-3663", 
"s_acctbal": -283.84, "s_comment": ". slyly regular pinto bea" }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.3.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.3.adm
new file mode 100644
index 0000000..69756c0
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.3.adm
@@ -0,0 +1 @@
+{ "s_suppkey": 5, "s_name": "Supplier#000000005", "s_address": 
"Gcdm2rJRzl5qlTVzc", "s_nationkey": 11, "s_phone": "21-151-690-3663", 
"s_acctbal": -283.84, "s_comment": ". slyly regular pinto bea" }
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.4.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.4.adm
new file mode 100644
index 0000000..49ed112
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/comparison/secondary_idx_lookup/secondary_idx_lookup.4.adm
@@ -0,0 +1,5 @@
+{ "s_suppkey": 2, "s_name": "Supplier#000000002", "s_address": 
"89eJ5ksX3ImxJQBvxObC,", "s_nationkey": 5, "s_phone": "15-679-861-2259", 
"s_acctbal": 4032.68, "s_comment": " slyly bold instructions. idle dependen" }
+{ "s_suppkey": 3, "s_name": "Supplier#000000003", "s_address": 
"q1,G3Pj6OjIuUYfUoH18BFTKP5aU9bEV3", "s_nationkey": 1, "s_phone": 
"11-383-516-1199", "s_acctbal": 4192.4, "s_comment": "blithely silent requests 
after the express dependencies are sl" }
+{ "s_suppkey": 5, "s_name": "Supplier#000000005", "s_address": 
"Gcdm2rJRzl5qlTVzc", "s_nationkey": 11, "s_phone": "21-151-690-3663", 
"s_acctbal": -283.84, "s_comment": ". slyly regular pinto bea" }
+{ "s_suppkey": 6, "s_name": "Supplier#000000006", "s_address": "tQxuVm7s7CnK", 
"s_nationkey": 14, "s_phone": "24-696-997-4969", "s_acctbal": 1365.79, 
"s_comment": "final accounts. regular dolphins use against the furiously ironic 
decoys. " }
+{ "s_suppkey": 10, "s_name": "Supplier#000000010", "s_address": 
"Saygah3gYWMp72i PY", "s_nationkey": 24, "s_phone": "34-852-489-8585", 
"s_acctbal": 3891.91, "s_comment": "ing waters. regular requests ar" }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml 
b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 270d81a..c760034 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -1006,6 +1006,11 @@
   </test-group>
   <test-group name="comparison">
     <test-case FilePath="comparison">
+      <compilation-unit name="secondary_idx_lookup">
+        <output-dir compare="Text">secondary_idx_lookup</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="comparison">
       <compilation-unit name="year_month_duration_order">
         <output-dir compare="Text">year_month_duration_order</output-dir>
       </compilation-unit>
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/AbstractComparisonEvaluator.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/AbstractComparisonEvaluator.java
index 620c543..84bf3b8 100644
--- 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/AbstractComparisonEvaluator.java
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/AbstractComparisonEvaluator.java
@@ -30,6 +30,7 @@
 import org.apache.asterix.runtime.exceptions.UnsupportedTypeException;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+import org.apache.hyracks.algebricks.runtime.evaluators.ConstantEvalFactory;
 import org.apache.hyracks.api.context.IHyracksTaskContext;
 import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -50,6 +51,7 @@
     protected IScalarEvaluator evalLeft;
     protected IScalarEvaluator evalRight;
     private ComparisonHelper ch = new ComparisonHelper();
+    private Object leftValue, rightValue;
 
     @SuppressWarnings("unchecked")
     protected ISerializerDeserializer<ABoolean> serde =
@@ -57,11 +59,18 @@
     @SuppressWarnings("unchecked")
     protected ISerializerDeserializer<ANull> nullSerde =
             
SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);
+    protected boolean leftIsConstant, rightIsConstant;
 
     public AbstractComparisonEvaluator(IScalarEvaluatorFactory evalLeftFactory,
             IScalarEvaluatorFactory evalRightFactory, IHyracksTaskContext 
context) throws HyracksDataException {
         this.evalLeft = evalLeftFactory.createScalarEvaluator(context);
         this.evalRight = evalRightFactory.createScalarEvaluator(context);
+        if (evalLeftFactory instanceof ConstantEvalFactory) {
+            leftIsConstant = true;
+        } else if (evalRightFactory instanceof ConstantEvalFactory) {
+            rightIsConstant = true;
+        }
+        leftValue = rightValue = null;
     }
 
     @Override
@@ -129,9 +138,15 @@
     }
 
     protected int compareResults() throws HyracksDataException {
-        int result = 
ch.compare(EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(argLeft.getTag()),
-                
EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(argRight.getTag()), outLeft, 
outRight);
-        return result;
+        ATypeTag leftTypeTag = 
EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(argLeft.getTag());
+        ATypeTag rightTypeTag = 
EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(argRight.getTag());
+        if (leftIsConstant && leftValue == null) {
+            leftValue = ch.getCachableValue(leftTypeTag, outLeft);
+        }
+        if (rightIsConstant && rightValue == null) {
+            rightValue = ch.getCachableValue(rightTypeTag, outRight);
+        }
+        return ch.compare(leftTypeTag, rightTypeTag, outLeft, outRight, 
leftValue, rightValue);
     }
 
 }
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/ComparisonHelper.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/ComparisonHelper.java
index 163bd9f..35fe58a 100644
--- 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/ComparisonHelper.java
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/comparisons/ComparisonHelper.java
@@ -44,8 +44,6 @@
 import org.apache.hyracks.data.std.accessors.PointableBinaryComparatorFactory;
 import org.apache.hyracks.data.std.api.IPointable;
 import org.apache.hyracks.data.std.primitive.ByteArrayPointable;
-import org.apache.hyracks.data.std.primitive.FloatPointable;
-import org.apache.hyracks.data.std.primitive.IntegerPointable;
 
 public class ComparisonHelper implements Serializable {
     private static final long serialVersionUID = 1L;
@@ -74,21 +72,21 @@
     private final IBinaryComparator byteArrayComparator =
             new 
PointableBinaryComparatorFactory(ByteArrayPointable.FACTORY).createBinaryComparator();
 
-    public int compare(ATypeTag typeTag1, ATypeTag typeTag2, IPointable arg1, 
IPointable arg2)
+    public int compare(ATypeTag typeTag1, ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
             throws HyracksDataException {
         switch (typeTag1) {
             case TINYINT:
-                return compareInt8WithArg(typeTag2, arg1, arg2);
+                return compareInt8WithArg(typeTag2, arg1, arg2, obj1, obj2);
             case SMALLINT:
-                return compareInt16WithArg(typeTag2, arg1, arg2);
+                return compareInt16WithArg(typeTag2, arg1, arg2, obj1, obj2);
             case INTEGER:
-                return compareInt32WithArg(typeTag2, arg1, arg2);
+                return compareInt32WithArg(typeTag2, arg1, arg2, obj1, obj2);
             case BIGINT:
-                return compareInt64WithArg(typeTag2, arg1, arg2);
+                return compareInt64WithArg(typeTag2, arg1, arg2, obj1, obj2);
             case FLOAT:
-                return compareFloatWithArg(typeTag2, arg1, arg2);
+                return compareFloatWithArg(typeTag2, arg1, arg2, obj1, obj2);
             case DOUBLE:
-                return compareDoubleWithArg(typeTag2, arg1, arg2);
+                return compareDoubleWithArg(typeTag2, arg1, arg2, obj1, obj2);
             case STRING:
                 return compareStringWithArg(typeTag2, arg1, arg2);
             case BOOLEAN:
@@ -96,6 +94,11 @@
             default:
                 return compareStrongTypedWithArg(typeTag1, typeTag2, arg1, 
arg2);
         }
+    }
+
+    public int compare(ATypeTag typeTag1, ATypeTag typeTag2, IPointable arg1, 
IPointable arg2)
+            throws HyracksDataException {
+        return compare(typeTag1, typeTag2, arg1, arg2, null, null);
     }
 
     private int compareStrongTypedWithArg(ATypeTag expectedTypeTag, ATypeTag 
actualTypeTag, IPointable arg1,
@@ -182,26 +185,27 @@
         throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_STRING_TYPE_TAG, typeTag2.serialize());
     }
 
-    private int compareDoubleWithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2) throws HyracksDataException {
+    private int compareDoubleWithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
+            throws HyracksDataException {
         byte[] leftBytes = arg1.getByteArray();
         int leftOffset = arg1.getStartOffset();
         byte[] rightBytes = arg2.getByteArray();
         int rightOffset = arg2.getStartOffset();
 
-        double s = ADoubleSerializerDeserializer.getDouble(leftBytes, 
leftOffset);
+        double s = getOrDeserializeDouble(leftBytes, leftOffset, obj1);
         switch (typeTag2) {
             case TINYINT:
-                return compareDouble(s, 
AInt8SerializerDeserializer.getByte(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeTinyInt(rightBytes, 
rightOffset, obj2));
             case SMALLINT:
-                return compareDouble(s, 
AInt16SerializerDeserializer.getShort(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeSmallInt(rightBytes, 
rightOffset, obj2));
             case INTEGER:
-                return compareDouble(s, 
AInt32SerializerDeserializer.getInt(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeInt(rightBytes, 
rightOffset, obj2));
             case BIGINT:
-                return compareDouble(s, 
AInt64SerializerDeserializer.getLong(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeBigInt(rightBytes, 
rightOffset, obj2));
             case FLOAT:
-                return compareDouble(s, 
AFloatSerializerDeserializer.getFloat(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeFloat(rightBytes, 
rightOffset, obj2));
             case DOUBLE:
-                return compareDouble(s, 
ADoubleSerializerDeserializer.getDouble(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeDouble(rightBytes, 
rightOffset, obj2));
             default: {
                 throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG,
                         typeTag2.serialize());
@@ -209,89 +213,89 @@
         }
     }
 
-    private int compareFloatWithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2) throws HyracksDataException {
+    private int compareFloatWithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
+            throws HyracksDataException {
         byte[] leftBytes = arg1.getByteArray();
         int leftOffset = arg1.getStartOffset();
         byte[] rightBytes = arg2.getByteArray();
         int rightOffset = arg2.getStartOffset();
 
-        float s = FloatPointable.getFloat(leftBytes, leftOffset);
+        float s = getOrDeserializeFloat(leftBytes, leftOffset, obj1);
         switch (typeTag2) {
             case TINYINT:
-                return compareFloat(s, 
AInt8SerializerDeserializer.getByte(rightBytes, rightOffset));
+                return compareFloat(s, getOrDeserializeTinyInt(rightBytes, 
rightOffset, obj2));
             case SMALLINT:
-                return compareFloat(s, 
AInt16SerializerDeserializer.getShort(rightBytes, rightOffset));
+                return compareFloat(s, getOrDeserializeSmallInt(rightBytes, 
rightOffset, obj2));
             case INTEGER:
-                return compareFloat(s, 
AInt32SerializerDeserializer.getInt(rightBytes, rightOffset));
+                return compareFloat(s, getOrDeserializeInt(rightBytes, 
rightOffset, obj2));
             case BIGINT:
-                return compareFloat(s, 
AInt64SerializerDeserializer.getLong(rightBytes, rightOffset));
+                return compareFloat(s, getOrDeserializeBigInt(rightBytes, 
rightOffset, obj2));
             case FLOAT:
-                return compareFloat(s, 
AFloatSerializerDeserializer.getFloat(rightBytes, rightOffset));
+                return compareFloat(s, getOrDeserializeFloat(rightBytes, 
rightOffset, obj2));
             case DOUBLE:
-                return compareDouble(s, 
ADoubleSerializerDeserializer.getDouble(rightBytes, rightOffset));
+                return compareDouble(Double.valueOf(String.valueOf(s)),
+                        ADoubleSerializerDeserializer.getDouble(rightBytes, 
rightOffset));
+
+            //            return compareDouble(s, 
getOrDeserializeDouble(rightBytes, rightOffset, obj2));
             default:
                 throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_FLOAT_TYPE_TAG,
                         typeTag2.serialize());
         }
     }
 
-    private int compareInt64WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2) throws HyracksDataException {
+    private int compareInt64WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
+            throws HyracksDataException {
         byte[] leftBytes = arg1.getByteArray();
         int leftOffset = arg1.getStartOffset();
         byte[] rightBytes = arg2.getByteArray();
         int rightOffset = arg2.getStartOffset();
 
-        long s = AInt64SerializerDeserializer.getLong(leftBytes, leftOffset);
+        long s = getOrDeserializeBigInt(leftBytes, leftOffset, obj1);
         switch (typeTag2) {
             case TINYINT:
-                return compareLong(s, 
AInt8SerializerDeserializer.getByte(rightBytes, rightOffset));
+                return compareLong(s, getOrDeserializeTinyInt(rightBytes, 
rightOffset, obj2));
             case SMALLINT:
-                return compareLong(s, 
AInt16SerializerDeserializer.getShort(rightBytes, rightOffset));
+                return compareLong(s, getOrDeserializeSmallInt(rightBytes, 
rightOffset, obj2));
             case INTEGER:
-                return compareLong(s, 
AInt32SerializerDeserializer.getInt(rightBytes, rightOffset));
+                return compareLong(s, getOrDeserializeInt(rightBytes, 
rightOffset, obj2));
             case BIGINT:
-                return compareLong(s, 
AInt64SerializerDeserializer.getLong(rightBytes, rightOffset));
+                return compareLong(s, getOrDeserializeBigInt(rightBytes, 
rightOffset, obj2));
             case FLOAT:
-                return compareFloat(s, 
AFloatSerializerDeserializer.getFloat(rightBytes, rightOffset));
+                return compareFloat(s, getOrDeserializeFloat(rightBytes, 
rightOffset, obj2));
             case DOUBLE:
-                return compareDouble(s, 
ADoubleSerializerDeserializer.getDouble(rightBytes, rightOffset));
+                return compareDouble(s, getOrDeserializeDouble(rightBytes, 
rightOffset, obj2));
             default:
                 throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_INT64_TYPE_TAG,
                         typeTag2.serialize());
         }
     }
 
-    private int compareInt32WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2) throws HyracksDataException {
+    private int compareInt32WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
+            throws HyracksDataException {
         byte[] leftBytes = arg1.getByteArray();
         int leftOffset = arg1.getStartOffset();
         byte[] rightBytes = arg2.getByteArray();
         int rightOffset = arg2.getStartOffset();
 
-        int s = IntegerPointable.getInteger(leftBytes, leftOffset);
+        int s = getOrDeserializeInt(leftBytes, leftOffset, obj1);
         switch (typeTag2) {
             case TINYINT: {
-                byte v2 = AInt8SerializerDeserializer.getByte(rightBytes, 
rightOffset);
-                return compareInt(s, v2);
+                return compareInt(s, getOrDeserializeTinyInt(rightBytes, 
rightOffset, obj2));
             }
             case SMALLINT: {
-                short v2 = AInt16SerializerDeserializer.getShort(rightBytes, 
rightOffset);
-                return compareInt(s, v2);
+                return compareInt(s, getOrDeserializeSmallInt(rightBytes, 
rightOffset, obj2));
             }
             case INTEGER: {
-                int v2 = AInt32SerializerDeserializer.getInt(rightBytes, 
rightOffset);
-                return compareInt(s, v2);
+                return compareInt(s, getOrDeserializeInt(rightBytes, 
rightOffset, obj2));
             }
             case BIGINT: {
-                long v2 = AInt64SerializerDeserializer.getLong(rightBytes, 
rightOffset);
-                return compareLong(s, v2);
+                return compareLong(s, getOrDeserializeBigInt(rightBytes, 
rightOffset, obj2));
             }
             case FLOAT: {
-                float v2 = AFloatSerializerDeserializer.getFloat(rightBytes, 
rightOffset);
-                return compareFloat(s, v2);
+                return compareFloat(s, getOrDeserializeFloat(rightBytes, 
rightOffset, obj2));
             }
             case DOUBLE: {
-                double v2 = 
ADoubleSerializerDeserializer.getDouble(rightBytes, rightOffset);
-                return compareDouble(s, v2);
+                return compareDouble(s, getOrDeserializeDouble(rightBytes, 
rightOffset, obj2));
             }
             default:
                 throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_INT32_TYPE_TAG,
@@ -299,37 +303,32 @@
         }
     }
 
-    private int compareInt16WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2) throws HyracksDataException {
+    private int compareInt16WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
+            throws HyracksDataException {
         byte[] leftBytes = arg1.getByteArray();
         int leftOffset = arg1.getStartOffset();
         byte[] rightBytes = arg2.getByteArray();
         int rightOffset = arg2.getStartOffset();
 
-        short s = AInt16SerializerDeserializer.getShort(leftBytes, leftOffset);
+        short s = getOrDeserializeSmallInt(leftBytes, leftOffset, obj1);
         switch (typeTag2) {
             case TINYINT: {
-                byte v2 = AInt8SerializerDeserializer.getByte(rightBytes, 
rightOffset);
-                return compareShort(s, v2);
+                return compareShort(s, getOrDeserializeTinyInt(rightBytes, 
rightOffset, obj2));
             }
             case SMALLINT: {
-                short v2 = AInt16SerializerDeserializer.getShort(rightBytes, 
rightOffset);
-                return compareShort(s, v2);
+                return compareShort(s, getOrDeserializeSmallInt(rightBytes, 
rightOffset, obj2));
             }
             case INTEGER: {
-                int v2 = AInt32SerializerDeserializer.getInt(rightBytes, 
rightOffset);
-                return compareInt(s, v2);
+                return compareInt(s, getOrDeserializeInt(rightBytes, 
rightOffset, obj2));
             }
             case BIGINT: {
-                long v2 = AInt64SerializerDeserializer.getLong(rightBytes, 
rightOffset);
-                return compareLong(s, v2);
+                return compareLong(s, getOrDeserializeBigInt(rightBytes, 
rightOffset, obj2));
             }
             case FLOAT: {
-                float v2 = AFloatSerializerDeserializer.getFloat(rightBytes, 
rightOffset);
-                return compareFloat(s, v2);
+                return compareFloat(s, getOrDeserializeFloat(rightBytes, 
rightOffset, obj2));
             }
             case DOUBLE: {
-                double v2 = 
ADoubleSerializerDeserializer.getDouble(rightBytes, rightOffset);
-                return compareDouble(s, v2);
+                return compareDouble(s, getOrDeserializeDouble(rightBytes, 
rightOffset, obj2));
             }
             default: {
                 throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_INT16_TYPE_TAG,
@@ -338,30 +337,55 @@
         }
     }
 
-    private int compareInt8WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2) throws HyracksDataException {
+    private int compareInt8WithArg(ATypeTag typeTag2, IPointable arg1, 
IPointable arg2, Object obj1, Object obj2)
+            throws HyracksDataException {
         byte[] leftBytes = arg1.getByteArray();
         int leftStart = arg1.getStartOffset();
         byte[] rightBytes = arg2.getByteArray();
         int rightStart = arg2.getStartOffset();
 
-        byte s = AInt8SerializerDeserializer.getByte(leftBytes, leftStart);
+        byte s = getOrDeserializeTinyInt(leftBytes, leftStart, obj1);
         switch (typeTag2) {
             case TINYINT:
-                return compareByte(s, 
AInt8SerializerDeserializer.getByte(rightBytes, rightStart));
+                return compareByte(s, getOrDeserializeTinyInt(rightBytes, 
rightStart, obj2));
             case SMALLINT:
-                return compareShort(s, 
AInt16SerializerDeserializer.getShort(rightBytes, rightStart));
+                return compareShort(s, getOrDeserializeSmallInt(rightBytes, 
rightStart, obj2));
             case INTEGER:
-                return compareInt(s, 
AInt32SerializerDeserializer.getInt(rightBytes, rightStart));
+                return compareInt(s, getOrDeserializeInt(rightBytes, 
rightStart, obj2));
             case BIGINT:
-                return compareLong(s, 
AInt64SerializerDeserializer.getLong(rightBytes, rightStart));
+                return compareLong(s, getOrDeserializeBigInt(rightBytes, 
rightStart, obj2));
             case FLOAT:
-                return compareFloat(s, 
AFloatSerializerDeserializer.getFloat(rightBytes, rightStart));
+                return compareFloat(s, getOrDeserializeFloat(rightBytes, 
rightStart, obj2));
             case DOUBLE:
-                return compareDouble(s, 
ADoubleSerializerDeserializer.getDouble(rightBytes, rightStart));
+                return compareDouble(s, getOrDeserializeDouble(rightBytes, 
rightStart, obj2));
             default:
                 throw new IncompatibleTypeException(COMPARISON, 
ATypeTag.SERIALIZED_INT8_TYPE_TAG,
                         typeTag2.serialize());
         }
+    }
+
+    private final byte getOrDeserializeTinyInt(byte[] bytes, int offset, 
Object obj) {
+        return obj == null ? AInt8SerializerDeserializer.getByte(bytes, 
offset) : (byte) obj;
+    }
+
+    private final short getOrDeserializeSmallInt(byte[] bytes, int offset, 
Object obj) {
+        return obj == null ? AInt16SerializerDeserializer.getShort(bytes, 
offset) : (short) obj;
+    }
+
+    private final int getOrDeserializeInt(byte[] bytes, int offset, Object 
obj) {
+        return obj == null ? AInt32SerializerDeserializer.getInt(bytes, 
offset) : (int) obj;
+    }
+
+    private final long getOrDeserializeBigInt(byte[] bytes, int offset, Object 
obj) {
+        return obj == null ? AInt64SerializerDeserializer.getLong(bytes, 
offset) : (long) obj;
+    }
+
+    private final float getOrDeserializeFloat(byte[] bytes, int offset, Object 
obj) {
+        return obj == null ? AFloatSerializerDeserializer.getFloat(bytes, 
offset) : (float) obj;
+    }
+
+    private final double getOrDeserializeDouble(byte[] bytes, int offset, 
Object obj) {
+        return obj == null ? ADoubleSerializerDeserializer.getDouble(bytes, 
offset) : (double) obj;
     }
 
     private final int compareByte(int v1, int v2) {
@@ -405,23 +429,31 @@
     }
 
     private final int compareFloat(float v1, float v2) {
-        if (v1 == v2) {
-            return 0;
-        } else if (v1 < v2) {
-            return -1;
-        } else {
-            return 1;
-        }
+        return Float.compare(v1, v2);
     }
 
     private final int compareDouble(double v1, double v2) {
-        if (v1 == v2) {
-            return 0;
-        } else if (v1 < v2) {
-            return -1;
-        } else {
-            return 1;
-        }
+        System.out.println("Compare " + v1 + " " + v2);
+        return Double.compare(v1, v2);
     }
 
+    public Object getCachableValue(ATypeTag typeTag, IPointable arg) {
+        byte[] bytes = arg.getByteArray();
+        int offset = arg.getStartOffset();
+        switch (typeTag) {
+            case TINYINT:
+                return AInt8SerializerDeserializer.getByte(bytes, offset);
+            case SMALLINT:
+                return AInt16SerializerDeserializer.getShort(bytes, offset);
+            case INTEGER:
+                return AInt32SerializerDeserializer.getInt(bytes, offset);
+            case BIGINT:
+                return AInt64SerializerDeserializer.getLong(bytes, offset);
+            case FLOAT:
+                return AFloatSerializerDeserializer.getFloat(bytes, offset);
+            case DOUBLE:
+                return ADoubleSerializerDeserializer.getDouble(bytes, offset);
+        }
+        return null;
+    }
 }

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/2621
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iae4e78928da2bd63b2984b3624b88baed9b7cd73
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Xikui Wang <xkk...@gmail.com>

Reply via email to