asterixdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ti...@apache.org
Subject [1/2] asterixdb git commit: Clean up numeric type conversion code
Date Sat, 03 Jun 2017 18:17:50 GMT
Repository: asterixdb
Updated Branches:
  refs/heads/master d26141d6b -> ce062ba29


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt16TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt16TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt16TypeConvertComputer.java
index f610bfe..1c69dc4 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt16TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt16TypeConvertComputer.java
@@ -21,30 +21,75 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.ADouble;
+import org.apache.asterix.om.base.AInt16;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.DoublePointable;
 
 public class DoubleToInt16TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final DoubleToInt16TypeConvertComputer INSTANCE = new DoubleToInt16TypeConvertComputer();
+    private static final DoubleToInt16TypeConvertComputer INSTANCE_STRICT = new DoubleToInt16TypeConvertComputer(true);
 
-    private DoubleToInt16TypeConvertComputer() {
+    private static final DoubleToInt16TypeConvertComputer INSTANCE_LAX = new DoubleToInt16TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private DoubleToInt16TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static DoubleToInt16TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
         double sourceValue = DoublePointable.getDouble(data, start);
+        short targetValue = convert(sourceValue);
+        out.writeByte(ATypeTag.SMALLINT.serialize());
+        out.writeShort(targetValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        double sourceValue = ((ADouble) sourceObject).getDoubleValue();
+        short targetValue = convert(sourceValue);
+        return new AInt16(targetValue);
+    }
+
+    private short convert(double sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Short.MAX_VALUE || sourceValue < Short.MIN_VALUE) {
-            throw new IOException("Cannot convert Double to SMALLINT - Double value " + sourceValue
-                    + " is out of range that SMALLINT type can hold: SMALLINT.MAX_VALUE:" + Short.MAX_VALUE
-                    + ", SMALLINT.MIN_VALUE: " + Short.MIN_VALUE);
+        if (Double.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return 0;
+            }
         }
+        if (sourceValue > Short.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Short.MAX_VALUE;
+            }
+        } else if (sourceValue < Short.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Short.MIN_VALUE;
+            }
+        }
+
         // Math.floor to truncate decimal portion
-        short targetValue = (short) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.SMALLINT.serialize());
-        out.writeShort(targetValue);
+        return (short) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryCheckException(double sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.SMALLINT,
+                Short.MAX_VALUE, Short.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt32TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt32TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt32TypeConvertComputer.java
index da974bb..7d2f121 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt32TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt32TypeConvertComputer.java
@@ -21,30 +21,78 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.ADouble;
+import org.apache.asterix.om.base.AInt32;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.DoublePointable;
 
 public class DoubleToInt32TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final DoubleToInt32TypeConvertComputer INSTANCE = new DoubleToInt32TypeConvertComputer();
+    private static final DoubleToInt32TypeConvertComputer INSTANCE_STRICT = new DoubleToInt32TypeConvertComputer(true);
 
-    private DoubleToInt32TypeConvertComputer() {
+    private static final DoubleToInt32TypeConvertComputer INSTANCE_LAX = new DoubleToInt32TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private DoubleToInt32TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static DoubleToInt32TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
+        int targetValue = convertType(data, start);
+        out.writeByte(ATypeTag.INTEGER.serialize());
+        out.writeInt(targetValue);
+    }
+
+    int convertType(byte[] data, int start) throws HyracksDataException {
         double sourceValue = DoublePointable.getDouble(data, start);
+        return convert(sourceValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        double sourceValue = ((ADouble) sourceObject).getDoubleValue();
+        int targetValue = convert(sourceValue);
+        return new AInt32(targetValue);
+    }
+
+    private int convert(double sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Integer.MAX_VALUE || sourceValue < Integer.MIN_VALUE) {
-            throw new IOException("Cannot convert Double to INTEGER - Double value " + sourceValue
-                    + " is out of range that INTEGER type can hold: INTEGER.MAX_VALUE:" + Integer.MAX_VALUE
-                    + ", INTEGER.MIN_VALUE: " + Integer.MIN_VALUE);
+        if (Double.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return 0;
+            }
+        } else if (sourceValue > Integer.MAX_VALUE) {
+            if (strict) {
+                return raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Integer.MAX_VALUE;
+            }
+        } else if (sourceValue < Integer.MIN_VALUE) {
+            if (strict) {
+                return raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Integer.MIN_VALUE;
+            }
         }
+
         // Math.floor to truncate decimal portion
-        int targetValue = (int) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.INTEGER.serialize());
-        out.writeInt(targetValue);
+        return (int) Math.floor(sourceValue);
     }
 
+    private int raiseBoundaryCheckException(double sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.INTEGER,
+                Integer.MAX_VALUE, Integer.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt64TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt64TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt64TypeConvertComputer.java
index 26daa94..6098afa 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt64TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt64TypeConvertComputer.java
@@ -21,30 +21,83 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.ADouble;
+import org.apache.asterix.om.base.AInt64;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.DoublePointable;
 
 public class DoubleToInt64TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final DoubleToInt64TypeConvertComputer INSTANCE = new DoubleToInt64TypeConvertComputer();
+    private static final DoubleToInt64TypeConvertComputer INSTANCE_STRICT = new DoubleToInt64TypeConvertComputer(true);
 
-    private DoubleToInt64TypeConvertComputer() {
+    private static final DoubleToInt64TypeConvertComputer INSTANCE_LAX = new DoubleToInt64TypeConvertComputer(false);
 
+    static DoubleToInt64TypeConvertComputer getInstance() {
+        return INSTANCE_STRICT;
+    }
+
+    private final boolean strict;
+
+    private DoubleToInt64TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static DoubleToInt64TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
+        long targetValue = convertType(data, start);
+        out.writeByte(ATypeTag.BIGINT.serialize());
+        out.writeLong(targetValue);
+    }
+
+    long convertType(byte[] data, int start) throws HyracksDataException {
         double sourceValue = DoublePointable.getDouble(data, start);
+        return convert(sourceValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        double sourceValue = ((ADouble) sourceObject).getDoubleValue();
+        long targetValue = convert(sourceValue);
+        return new AInt64(targetValue);
+    }
+
+    private long convert(double sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Long.MAX_VALUE || sourceValue < Long.MIN_VALUE) {
-            throw new IOException("Cannot convert Double to BIGINT - Double value " + sourceValue
-                    + " is out of range that BIGINT type can hold: BIGINT.MAX_VALUE:" + Long.MAX_VALUE
-                    + ", BIGINT.MIN_VALUE: " + Long.MIN_VALUE);
+        if (Double.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return 0;
+            }
+        }
+        if (sourceValue > Long.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Long.MAX_VALUE;
+            }
+        } else if (sourceValue < Long.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Long.MIN_VALUE;
+            }
         }
+
         // Math.floor to truncate decimal portion
-        long targetValue = (long) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.BIGINT.serialize());
-        out.writeLong(targetValue);
+        return (long) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryCheckException(double sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.BIGINT,
+                Long.MAX_VALUE, Long.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt8TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt8TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt8TypeConvertComputer.java
index 9b9fe12..bb1ccc3 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt8TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/DoubleToInt8TypeConvertComputer.java
@@ -21,30 +21,75 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.ADouble;
+import org.apache.asterix.om.base.AInt8;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.DoublePointable;
 
 public class DoubleToInt8TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final DoubleToInt8TypeConvertComputer INSTANCE = new DoubleToInt8TypeConvertComputer();
+    private static final DoubleToInt8TypeConvertComputer INSTANCE_STRICT = new DoubleToInt8TypeConvertComputer(true);
 
-    private DoubleToInt8TypeConvertComputer() {
+    private static final DoubleToInt8TypeConvertComputer INSTANCE_LAX = new DoubleToInt8TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private DoubleToInt8TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static DoubleToInt8TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
         double sourceValue = DoublePointable.getDouble(data, start);
+        byte targetValue = convert(sourceValue);
+        out.writeByte(ATypeTag.TINYINT.serialize());
+        out.writeByte(targetValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        double sourceValue = ((ADouble) sourceObject).getDoubleValue();
+        byte targetValue = convert(sourceValue);
+        return new AInt8(targetValue);
+    }
+
+    private byte convert(double sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Byte.MAX_VALUE || sourceValue < Byte.MIN_VALUE) {
-            throw new IOException("Cannot convert Double to TINYINT - Double value " + sourceValue
-                    + " is out of range that TINYINT type can hold: TINYINT.MAX_VALUE:" + Byte.MAX_VALUE
-                    + ", TINYINT.MIN_VALUE: " + Byte.MIN_VALUE);
+        if (Double.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return 0;
+            }
         }
+        if (sourceValue > Byte.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Byte.MAX_VALUE;
+            }
+        } else if (sourceValue < Byte.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Byte.MIN_VALUE;
+            }
+        }
+
         // Math.floor to truncate decimal portion
-        byte targetValue = (byte) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.TINYINT.serialize());
-        out.writeByte(targetValue);
+        return (byte) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryCheckException(double sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.TINYINT,
+                Byte.MAX_VALUE, Byte.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToDoubleTypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToDoubleTypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToDoubleTypeConvertComputer.java
index 3695174..eaddfff 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToDoubleTypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToDoubleTypeConvertComputer.java
@@ -21,23 +21,39 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.om.base.ADouble;
+import org.apache.asterix.om.base.AFloat;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.FloatPointable;
 import org.apache.hyracks.dataflow.common.data.marshalling.DoubleSerializerDeserializer;
 
 public class FloatToDoubleTypeConvertComputer implements ITypeConvertComputer {
 
-    public static final FloatToDoubleTypeConvertComputer INSTANCE = new FloatToDoubleTypeConvertComputer();
+    private static final FloatToDoubleTypeConvertComputer INSTANCE = new FloatToDoubleTypeConvertComputer();
 
     private FloatToDoubleTypeConvertComputer() {
+    }
 
+    public static FloatToDoubleTypeConvertComputer getInstance() {
+        return INSTANCE;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
+        double targetValue = convertType(data, start);
         out.writeByte(ATypeTag.DOUBLE.serialize());
-        DoubleSerializerDeserializer.INSTANCE.serialize((double) (FloatPointable.getFloat(data, start)),
-                out);
+        DoubleSerializerDeserializer.INSTANCE.serialize(targetValue, out);
     }
 
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        double targetValue = ((AFloat) sourceObject).getFloatValue();
+        return new ADouble(targetValue);
+    }
+
+    double convertType(byte[] data, int start) {
+        return FloatPointable.getFloat(data, start);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt16TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt16TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt16TypeConvertComputer.java
index 6ed8d06..ed570fc 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt16TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt16TypeConvertComputer.java
@@ -21,30 +21,75 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.AFloat;
+import org.apache.asterix.om.base.AInt16;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.FloatPointable;
 
 public class FloatToInt16TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final FloatToInt16TypeConvertComputer INSTANCE = new FloatToInt16TypeConvertComputer();
+    private static final FloatToInt16TypeConvertComputer INSTANCE_STRICT = new FloatToInt16TypeConvertComputer(true);
 
-    private FloatToInt16TypeConvertComputer() {
+    private static final FloatToInt16TypeConvertComputer INSTANCE_LAX = new FloatToInt16TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private FloatToInt16TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static FloatToInt16TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
         float sourceValue = FloatPointable.getFloat(data, start);
+        short targetValue = convert(sourceValue);
+        out.writeByte(ATypeTag.SMALLINT.serialize());
+        out.writeShort(targetValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        float sourceValue = ((AFloat) sourceObject).getFloatValue();
+        short targetValue = convert(sourceValue);
+        return new AInt16(targetValue);
+    }
+
+    private short convert(float sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Short.MAX_VALUE || sourceValue < Short.MIN_VALUE) {
-            throw new IOException("Cannot convert Float to SMALLINT - Float value " + sourceValue
-                    + " is out of range that SMALLINT type can hold: SMALLINT.MAX_VALUE:" + Short.MAX_VALUE
-                    + ", SMALLINT.MIN_VALUE: " + Short.MIN_VALUE);
+        if (Float.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryException(sourceValue);
+            } else {
+                return 0;
+            }
         }
+        if (sourceValue > Short.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryException(sourceValue);
+            } else {
+                return Short.MAX_VALUE;
+            }
+        } else if (sourceValue < Short.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryException(sourceValue);
+            } else {
+                return Short.MIN_VALUE;
+            }
+        }
+
         // Math.floor to truncate decimal portion
-        short targetValue = (short) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.SMALLINT.serialize());
-        out.writeShort(targetValue);
+        return (short) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryException(float sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.SMALLINT,
+                Short.MAX_VALUE, Short.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt32TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt32TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt32TypeConvertComputer.java
index 6c4751b..2c81c8e 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt32TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt32TypeConvertComputer.java
@@ -21,30 +21,78 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.AFloat;
+import org.apache.asterix.om.base.AInt32;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.FloatPointable;
 
 public class FloatToInt32TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final FloatToInt32TypeConvertComputer INSTANCE = new FloatToInt32TypeConvertComputer();
+    private static final FloatToInt32TypeConvertComputer INSTANCE_STRICT = new FloatToInt32TypeConvertComputer(true);
 
-    private FloatToInt32TypeConvertComputer() {
+    private static final FloatToInt32TypeConvertComputer INSTANCE_LAX = new FloatToInt32TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private FloatToInt32TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static FloatToInt32TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
+        int targetValue = convertType(data, start);
+        out.writeByte(ATypeTag.INTEGER.serialize());
+        out.writeInt(targetValue);
+    }
+
+    int convertType(byte data[], int start) throws HyracksDataException {
         float sourceValue = FloatPointable.getFloat(data, start);
+        return convert(sourceValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        float sourceValue = ((AFloat) sourceObject).getFloatValue();
+        int targetValue = convert(sourceValue);
+        return new AInt32(targetValue);
+    }
+
+    private int convert(float sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Integer.MAX_VALUE || sourceValue < Integer.MIN_VALUE) {
-            throw new IOException("Cannot convert Float to INTEGER - Float value " + sourceValue
-                    + " is out of range that INTEGER type can hold: INTEGER.MAX_VALUE:" + Integer.MAX_VALUE
-                    + ", INTEGER.MIN_VALUE: " + Integer.MIN_VALUE);
+        if (Float.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return 0;
+            }
+        } else if (sourceValue > Integer.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Integer.MAX_VALUE;
+            }
+        } else if (sourceValue < Integer.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Integer.MIN_VALUE;
+            }
         }
+
         // Math.floor to truncate decimal portion
-        int targetValue = (int) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.INTEGER.serialize());
-        out.writeInt(targetValue);
+        return (int) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryCheckException(float sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.INTEGER,
+                Integer.MAX_VALUE, Integer.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt64TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt64TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt64TypeConvertComputer.java
index 43adddf..9180785 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt64TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt64TypeConvertComputer.java
@@ -21,30 +21,78 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.AFloat;
+import org.apache.asterix.om.base.AInt64;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.FloatPointable;
 
 public class FloatToInt64TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final FloatToInt64TypeConvertComputer INSTANCE = new FloatToInt64TypeConvertComputer();
+    private static final FloatToInt64TypeConvertComputer INSTANCE_STRICT = new FloatToInt64TypeConvertComputer(true);
 
-    private FloatToInt64TypeConvertComputer() {
+    private static final FloatToInt64TypeConvertComputer INSTANCE_LAX = new FloatToInt64TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private FloatToInt64TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static FloatToInt64TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
+        long targetValue = convertType(data, start);
+        out.writeByte(ATypeTag.BIGINT.serialize());
+        out.writeLong(targetValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        float sourceValue = ((AFloat) sourceObject).getFloatValue();
+        long targetValue = convert(sourceValue);
+        return new AInt64(targetValue);
+    }
+
+    long convertType(byte[] data, int start) throws HyracksDataException {
         float sourceValue = FloatPointable.getFloat(data, start);
+        return convert(sourceValue);
+    }
+
+    private long convert(float sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Long.MAX_VALUE || sourceValue < Long.MIN_VALUE) {
-            throw new IOException("Cannot convert Float to BIGINT - Float value " + sourceValue
-                    + " is out of range that BIGINT type can hold: BIGINT.MAX_VALUE:" + Long.MAX_VALUE
-                    + ", BIGINT.MIN_VALUE: " + Long.MIN_VALUE);
+        if (Float.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return 0;
+            }
+        } else if (sourceValue > Long.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Long.MAX_VALUE;
+            }
+        } else if (sourceValue < Long.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryCheckException(sourceValue);
+            } else {
+                return Long.MIN_VALUE;
+            }
         }
+
         // Math.floor to truncate decimal portion
-        long targetValue = (long) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.BIGINT.serialize());
-        out.writeLong(targetValue);
+        return (long) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryCheckException(float sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.BIGINT,
+                Long.MAX_VALUE, Long.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt8TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt8TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt8TypeConvertComputer.java
index 355fe4c..43a5e76 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt8TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/FloatToInt8TypeConvertComputer.java
@@ -21,30 +21,74 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.AFloat;
+import org.apache.asterix.om.base.AInt8;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.primitive.FloatPointable;
 
 public class FloatToInt8TypeConvertComputer implements ITypeConvertComputer {
 
-    public static final FloatToInt8TypeConvertComputer INSTANCE = new FloatToInt8TypeConvertComputer();
+    private static final FloatToInt8TypeConvertComputer INSTANCE_STRICT = new FloatToInt8TypeConvertComputer(true);
 
-    private FloatToInt8TypeConvertComputer() {
+    private static final FloatToInt8TypeConvertComputer INSTANCE_LAX = new FloatToInt8TypeConvertComputer(false);
 
+    private final boolean strict;
+
+    private FloatToInt8TypeConvertComputer(boolean strict) {
+        this.strict = strict;
+    }
+
+    public static FloatToInt8TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
         float sourceValue = FloatPointable.getFloat(data, start);
+        byte targetValue = convert(sourceValue);
+        out.writeByte(ATypeTag.TINYINT.serialize());
+        out.writeByte(targetValue);
+    }
+
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        float sourceValue = ((AFloat) sourceObject).getFloatValue();
+        byte targetValue = convert(sourceValue);
+        return new AInt8(targetValue);
+    }
+
+    private byte convert(float sourceValue) throws HyracksDataException {
         // Boundary check
-        if (sourceValue > Byte.MAX_VALUE || sourceValue < Byte.MIN_VALUE) {
-            throw new IOException("Cannot convert Float to TINYINT - Float value " + sourceValue
-                    + " is out of range that TINYINT type can hold: TINYINT.MAX_VALUE:" + Byte.MAX_VALUE
-                    + ", TINYINT.MIN_VALUE: " + Byte.MIN_VALUE);
+        if (Float.isNaN(sourceValue)) {
+            if (strict) {
+                raiseBoundaryException(sourceValue);
+            } else {
+                return 0;
+            }
+        } else if (sourceValue > Byte.MAX_VALUE) {
+            if (strict) {
+                raiseBoundaryException(sourceValue);
+            } else {
+                return Byte.MAX_VALUE;
+            }
+        } else if (sourceValue < Byte.MIN_VALUE) {
+            if (strict) {
+                raiseBoundaryException(sourceValue);
+            } else {
+                return Byte.MIN_VALUE;
+            }
         }
+
         // Math.floor to truncate decimal portion
-        byte targetValue = (byte) Math.floor(sourceValue);
-        out.writeByte(ATypeTag.TINYINT.serialize());
-        out.writeByte(targetValue);
+        return (byte) Math.floor(sourceValue);
     }
 
+    private void raiseBoundaryException(float sourceValue) throws HyracksDataException {
+        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_OUT_OF_BOUND, sourceValue, ATypeTag.TINYINT,
+                Byte.MAX_VALUE, Byte.MIN_VALUE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/ITypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/ITypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/ITypeConvertComputer.java
index acc7e45..a13878d 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/ITypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/ITypeConvertComputer.java
@@ -21,9 +21,14 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.om.base.IAObject;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+
 public interface ITypeConvertComputer {
 
     // promote or demote a type to a different type
     void convertType(byte[] data, int start, int length, DataOutput out) throws IOException;
 
+    // promote or demote a type to a different type
+    IAObject convertType(IAObject sourceObject) throws HyracksDataException;
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
index 0227937..37f29ed 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
@@ -20,56 +20,78 @@ package org.apache.asterix.om.types.hierachy;
 
 import java.io.DataOutput;
 import java.io.IOException;
+import java.util.Arrays;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.ADouble;
+import org.apache.asterix.om.base.AInt16;
+import org.apache.asterix.om.base.AInt32;
+import org.apache.asterix.om.base.AInt64;
+import org.apache.asterix.om.base.AInt8;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.data.std.primitive.BytePointable;
+import org.apache.hyracks.data.std.primitive.IntegerPointable;
+import org.apache.hyracks.data.std.primitive.LongPointable;
+import org.apache.hyracks.data.std.primitive.ShortPointable;
 import org.apache.hyracks.dataflow.common.data.marshalling.DoubleSerializerDeserializer;
 
 public class IntegerToDoubleTypeConvertComputer implements ITypeConvertComputer {
 
-    public static final IntegerToDoubleTypeConvertComputer INSTANCE = new IntegerToDoubleTypeConvertComputer();
+    private static final IntegerToDoubleTypeConvertComputer INSTANCE = new IntegerToDoubleTypeConvertComputer();
 
     private IntegerToDoubleTypeConvertComputer() {
+    }
 
+    public static IntegerToDoubleTypeConvertComputer getInstance() {
+        return INSTANCE;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
-        long val = 0L;
+        long val = AbstractIntegerTypeConvertComputer.asLong(data, start, length);
+        out.writeByte(ATypeTag.DOUBLE.serialize());
+        DoubleSerializerDeserializer.INSTANCE.serialize((double) val, out);
+    }
 
-        // In order to convert a negative number correctly,
-        // proper casting per INT type is needed.
-        //
-        switch (length) {
-            case 1:
-                // TINYINT
-                val = (data[start] & 0xff);
-                break;
+    double convertType(byte[] data, int start, ATypeTag sourceTypeTag) throws HyracksDataException {
+        switch (sourceTypeTag) {
+            case TINYINT:
+                return BytePointable.getByte(data, start);
+            case SMALLINT:
+                return ShortPointable.getShort(data, start);
+            case INTEGER:
+                return IntegerPointable.getInteger(data, start);
+            case BIGINT:
+                return LongPointable.getLong(data, start);
+            default:
+                throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_INTEGER_SOURCE, Arrays.toString(
+                        new ATypeTag[] { ATypeTag.TINYINT, ATypeTag.SMALLINT, ATypeTag.INTEGER, ATypeTag.BIGINT }));
+        }
+    }
 
-            case 2:
-                // SMALLINT
-                val = (short) ((data[start] << 8) | (data[start + 1] & 0xff));
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        long val;
+        switch (sourceObject.getType().getTypeTag()) {
+            case TINYINT:
+                val = ((AInt8) sourceObject).getByteValue();
                 break;
-
-            case 4:
-                // INTEGER
-                val = (int) (((data[start] & 0xff) << 24) | ((data[start + 1] & 0xff) << 16)
-                        | ((data[start + 2] & 0xff) << 8) | (data[start + 3] & 0xff));
+            case SMALLINT:
+                val = ((AInt16) sourceObject).getShortValue();
                 break;
-
-            case 8:
-                // BIGINT
-                val = (((long) (data[start] & 0xff) << 56) | ((long) (data[start + 1] & 0xff) << 48)
-                        | ((long) (data[start + 2] & 0xff) << 40) | ((long) (data[start + 3] & 0xff) << 32)
-                        | ((long) (data[start + 4] & 0xff) << 24) | ((long) (data[start + 5] & 0xff) << 16)
-                        | ((long) (data[start + 6] & 0xff) << 8) | ((long) (data[start + 7] & 0xff)));
-
+            case INTEGER:
+                val = ((AInt32) sourceObject).getIntegerValue();
                 break;
-
-            default:
+            case BIGINT:
+                val = ((AInt64) sourceObject).getLongValue();
                 break;
+            default:
+                throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_INTEGER_SOURCE, Arrays.toString(
+                        new ATypeTag[] { ATypeTag.TINYINT, ATypeTag.SMALLINT, ATypeTag.INTEGER, ATypeTag.BIGINT }));
         }
-        out.writeByte(ATypeTag.DOUBLE.serialize());
-        DoubleSerializerDeserializer.INSTANCE.serialize(Double.valueOf(val), out);
+        return new ADouble(val);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
index 85c4202..e072dd5 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
@@ -20,56 +20,80 @@ package org.apache.asterix.om.types.hierachy;
 
 import java.io.DataOutput;
 import java.io.IOException;
+import java.util.Arrays;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import org.apache.asterix.om.base.AFloat;
+import org.apache.asterix.om.base.AInt16;
+import org.apache.asterix.om.base.AInt32;
+import org.apache.asterix.om.base.AInt64;
+import org.apache.asterix.om.base.AInt8;
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.data.std.primitive.BytePointable;
+import org.apache.hyracks.data.std.primitive.IntegerPointable;
+import org.apache.hyracks.data.std.primitive.LongPointable;
+import org.apache.hyracks.data.std.primitive.ShortPointable;
 import org.apache.hyracks.dataflow.common.data.marshalling.FloatSerializerDeserializer;
 
 public class IntegerToFloatTypeConvertComputer implements ITypeConvertComputer {
 
-    public static final IntegerToFloatTypeConvertComputer INSTANCE = new IntegerToFloatTypeConvertComputer();
+    private static final IntegerToFloatTypeConvertComputer INSTANCE = new IntegerToFloatTypeConvertComputer();
 
     private IntegerToFloatTypeConvertComputer() {
+    }
 
+    public static IntegerToFloatTypeConvertComputer getInstance() {
+        return INSTANCE;
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput out) throws IOException {
-        float val = 0;
-        // In order to convert a negative number correctly,
-        // proper casting per INT type is needed.
-        //
+        float val;
+        // In order to convert a negative number correctly, proper casting per INT type is needed.
         switch (length) {
-            case 1:
-                // TINYINT
-                val = (data[start] & 0xff);
+            case 1: // TINYINT
+                val = BytePointable.getByte(data, start);
                 break;
-
-            case 2:
-                // SMALLINT
-                val = (short) ((data[start] << 8) | (data[start + 1] & 0xff));
+            case 2: // SMALLINT
+                val = ShortPointable.getShort(data, start);
                 break;
-
-            case 4:
-                // INTEGER
-                val = (int) (((data[start] & 0xff) << 24) | ((data[start + 1] & 0xff) << 16)
-                        | ((data[start + 2] & 0xff) << 8) | (data[start + 3] & 0xff));
+            case 4: // INTEGER
+                val = IntegerPointable.getInteger(data, start);
                 break;
-
-            case 8:
-                // BIGINT
-                val = (((long) (data[start] & 0xff) << 56) | ((long) (data[start + 1] & 0xff) << 48)
-                        | ((long) (data[start + 2] & 0xff) << 40) | ((long) (data[start + 3] & 0xff) << 32)
-                        | ((long) (data[start + 4] & 0xff) << 24) | ((long) (data[start + 5] & 0xff) << 16)
-                        | ((long) (data[start + 6] & 0xff) << 8) | ((long) (data[start + 7] & 0xff)));
-
+            case 8: // BIGINT
+                val = LongPointable.getLong(data, start);
                 break;
-
             default:
-                break;
+                throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_INTEGER_SOURCE, Arrays.toString(
+                        new ATypeTag[] { ATypeTag.TINYINT, ATypeTag.SMALLINT, ATypeTag.INTEGER, ATypeTag.BIGINT }));
         }
         out.writeByte(ATypeTag.FLOAT.serialize());
         FloatSerializerDeserializer.INSTANCE.serialize(val, out);
-
     }
 
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        float val;
+        switch (sourceObject.getType().getTypeTag()) {
+            case TINYINT:
+                val = ((AInt8) sourceObject).getByteValue();
+                break;
+            case SMALLINT:
+                val = ((AInt16) sourceObject).getShortValue();
+                break;
+            case INTEGER:
+                val = ((AInt32) sourceObject).getIntegerValue();
+                break;
+            case BIGINT:
+                val = ((AInt64) sourceObject).getLongValue();
+                break;
+            default:
+                throw new RuntimeDataException(ErrorCode.TYPE_CONVERT_INTEGER_SOURCE, Arrays.toString(
+                        new ATypeTag[] { ATypeTag.TINYINT, ATypeTag.SMALLINT, ATypeTag.INTEGER, ATypeTag.BIGINT }));
+        }
+        return new AFloat(val);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt16TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt16TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt16TypeConvertComputer.java
index 948e554..86363fc 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt16TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt16TypeConvertComputer.java
@@ -21,14 +21,23 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 
 public class IntegerToInt16TypeConvertComputer extends AbstractIntegerTypeConvertComputer {
 
-    public static final IntegerToInt16TypeConvertComputer INSTANCE = new IntegerToInt16TypeConvertComputer();
+    private static final IntegerToInt16TypeConvertComputer INSTANCE_STRICT =
+            new IntegerToInt16TypeConvertComputer(true);
 
-    private IntegerToInt16TypeConvertComputer() {
+    private static final IntegerToInt16TypeConvertComputer INSTANCE_LAX = new IntegerToInt16TypeConvertComputer(false);
 
+    private IntegerToInt16TypeConvertComputer(boolean strict) {
+        super(strict);
+    }
+
+    public static IntegerToInt16TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
@@ -36,4 +45,8 @@ public class IntegerToInt16TypeConvertComputer extends AbstractIntegerTypeConver
         convertIntegerType(data, start, length, out, ATypeTag.SMALLINT, 2);
     }
 
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        return convertIntegerType(sourceObject, ATypeTag.SMALLINT);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt32TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt32TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt32TypeConvertComputer.java
index be370cb..73f16fa 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt32TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt32TypeConvertComputer.java
@@ -21,13 +21,23 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 
 public class IntegerToInt32TypeConvertComputer extends AbstractIntegerTypeConvertComputer {
 
-    public static final IntegerToInt32TypeConvertComputer INSTANCE = new IntegerToInt32TypeConvertComputer();
+    private static final IntegerToInt32TypeConvertComputer INSTANCE_STRICT =
+            new IntegerToInt32TypeConvertComputer(true);
 
-    private IntegerToInt32TypeConvertComputer() {
+    private static final IntegerToInt32TypeConvertComputer INSTANCE_LAX = new IntegerToInt32TypeConvertComputer(false);
+
+    private IntegerToInt32TypeConvertComputer(boolean strict) {
+        super(strict);
+    }
+
+    public static IntegerToInt32TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
@@ -35,4 +45,8 @@ public class IntegerToInt32TypeConvertComputer extends AbstractIntegerTypeConver
         convertIntegerType(data, start, length, out, ATypeTag.INTEGER, 4);
     }
 
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        return convertIntegerType(sourceObject, ATypeTag.INTEGER);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt64TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt64TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt64TypeConvertComputer.java
index 5234b59..bfe1625 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt64TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt64TypeConvertComputer.java
@@ -21,13 +21,20 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 
 public class IntegerToInt64TypeConvertComputer extends AbstractIntegerTypeConvertComputer {
 
-    public static final IntegerToInt64TypeConvertComputer INSTANCE = new IntegerToInt64TypeConvertComputer();
+    private static final IntegerToInt64TypeConvertComputer INSTANCE = new IntegerToInt64TypeConvertComputer();
 
     private IntegerToInt64TypeConvertComputer() {
+        super(true);
+    }
+
+    public static IntegerToInt64TypeConvertComputer getInstance() {
+        return INSTANCE;
     }
 
     @Override
@@ -35,4 +42,8 @@ public class IntegerToInt64TypeConvertComputer extends AbstractIntegerTypeConver
         convertIntegerType(data, start, length, out, ATypeTag.BIGINT, 8);
     }
 
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        return convertIntegerType(sourceObject, ATypeTag.BIGINT);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ce062ba2/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt8TypeConvertComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt8TypeConvertComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt8TypeConvertComputer.java
index 271ad25..32e3d00 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt8TypeConvertComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToInt8TypeConvertComputer.java
@@ -21,14 +21,22 @@ package org.apache.asterix.om.types.hierachy;
 import java.io.DataOutput;
 import java.io.IOException;
 
+import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
 
 public class IntegerToInt8TypeConvertComputer extends AbstractIntegerTypeConvertComputer {
 
-    public static final IntegerToInt8TypeConvertComputer INSTANCE = new IntegerToInt8TypeConvertComputer();
+    private static final IntegerToInt8TypeConvertComputer INSTANCE_STRICT = new IntegerToInt8TypeConvertComputer(true);
 
-    private IntegerToInt8TypeConvertComputer() {
+    private static final IntegerToInt8TypeConvertComputer INSTANCE_LAX = new IntegerToInt8TypeConvertComputer(false);
 
+    private IntegerToInt8TypeConvertComputer(boolean strict) {
+        super(strict);
+    }
+
+    public static IntegerToInt8TypeConvertComputer getInstance(boolean strict) {
+        return strict ? INSTANCE_STRICT : INSTANCE_LAX;
     }
 
     @Override
@@ -36,4 +44,8 @@ public class IntegerToInt8TypeConvertComputer extends AbstractIntegerTypeConvert
         convertIntegerType(data, start, length, out, ATypeTag.TINYINT, 1);
     }
 
+    @Override
+    public IAObject convertType(IAObject sourceObject) throws HyracksDataException {
+        return convertIntegerType(sourceObject, ATypeTag.TINYINT);
+    }
 }


Mime
View raw message