diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java index 11408b3..425078a 100755 --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java @@ -161,8 +161,8 @@ public ByteWritable evaluate(Text i) { return null; } else { try { - byteWritable - .set(LazyByte.parseByte(i.getBytes(), 0, i.getLength(), 10)); + float f = Float.valueOf(i.toString()); + byteWritable.set((byte)f); return byteWritable; } catch (NumberFormatException e) { // MySQL returns 0 if the string is not a well-formed numeric value. diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java index 3498db5..35a07c5 100755 --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java @@ -161,8 +161,8 @@ public IntWritable evaluate(Text i) { return null; } else { try { - intWritable.set(LazyInteger - .parseInt(i.getBytes(), 0, i.getLength(), 10)); + float f = Float.valueOf(i.toString()); + intWritable.set((int)f); return intWritable; } catch (NumberFormatException e) { // MySQL returns 0 if the string is not a well-formed numeric value. diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java index 87c2288..69b311b 100755 --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java @@ -172,8 +172,8 @@ public LongWritable evaluate(Text i) { return null; } else { try { - longWritable - .set(LazyLong.parseLong(i.getBytes(), 0, i.getLength(), 10)); + double d = Double.valueOf(i.toString()); + longWritable.set((long)d); return longWritable; } catch (NumberFormatException e) { // MySQL returns 0 if the string is not a well-formed numeric value. diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java index 5a96723..62cb996 100644 --- ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java @@ -161,8 +161,8 @@ public ShortWritable evaluate(Text i) { return null; } else { try { - shortWritable.set(LazyShort.parseShort(i.getBytes(), 0, i.getLength(), - 10)); + float f = Float.valueOf(i.toString()); + shortWritable.set((short)f); return shortWritable; } catch (NumberFormatException e) { // MySQL returns 0 if the string is not a well-formed numeric value. diff --git ql/src/test/queries/clientpositive/cast_to_int.q ql/src/test/queries/clientpositive/cast_to_int.q new file mode 100644 index 0000000..a180e3d --- /dev/null +++ ql/src/test/queries/clientpositive/cast_to_int.q @@ -0,0 +1,28 @@ +-- cast string floats to integer types +select + cast('1' as float), + cast('1.4' as float), + cast('1.6' as float), + cast('1' as int), + cast('1.4' as int), + cast('1.6' as int), + cast('1' as tinyint), + cast('1.4' as tinyint), + cast('1.6' as tinyint), + cast('1' as smallint), + cast('1.4' as smallint), + cast('1.6' as smallint), + cast('1' as bigint), + cast('1.4' as bigint), + cast('1.6' as bigint), + cast (cast('1' as float) as int), + cast(cast ('1.4' as float) as int), + cast(cast ('1.6' as float) as int), + cast('+1e5' as int), + cast('2147483647' as int), + cast('-2147483648' as int), + cast('32767' as smallint), + cast('-32768' as smallint), + cast('-128' as tinyint), + cast('127' as tinyint) +from src limit 1; diff --git ql/src/test/results/clientpositive/cast_to_int.q.out ql/src/test/results/clientpositive/cast_to_int.q.out new file mode 100644 index 0000000..9a987bd --- /dev/null +++ ql/src/test/results/clientpositive/cast_to_int.q.out @@ -0,0 +1,63 @@ +PREHOOK: query: -- cast string floats to integer types +select + cast('1' as float), + cast('1.4' as float), + cast('1.6' as float), + cast('1' as int), + cast('1.4' as int), + cast('1.6' as int), + cast('1' as tinyint), + cast('1.4' as tinyint), + cast('1.6' as tinyint), + cast('1' as smallint), + cast('1.4' as smallint), + cast('1.6' as smallint), + cast('1' as bigint), + cast('1.4' as bigint), + cast('1.6' as bigint), + cast (cast('1' as float) as int), + cast(cast ('1.4' as float) as int), + cast(cast ('1.6' as float) as int), + cast('+1e5' as int), + cast('2147483647' as int), + cast('-2147483648' as int), + cast('32767' as smallint), + cast('-32768' as smallint), + cast('-128' as tinyint), + cast('127' as tinyint) +from src limit 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +#### A masked pattern was here #### +POSTHOOK: query: -- cast string floats to integer types +select + cast('1' as float), + cast('1.4' as float), + cast('1.6' as float), + cast('1' as int), + cast('1.4' as int), + cast('1.6' as int), + cast('1' as tinyint), + cast('1.4' as tinyint), + cast('1.6' as tinyint), + cast('1' as smallint), + cast('1.4' as smallint), + cast('1.6' as smallint), + cast('1' as bigint), + cast('1.4' as bigint), + cast('1.6' as bigint), + cast (cast('1' as float) as int), + cast(cast ('1.4' as float) as int), + cast(cast ('1.6' as float) as int), + cast('+1e5' as int), + cast('2147483647' as int), + cast('-2147483648' as int), + cast('32767' as smallint), + cast('-32768' as smallint), + cast('-128' as tinyint), + cast('127' as tinyint) +from src limit 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +#### A masked pattern was here #### +1.0 1.4 1.6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 100000 2147483647 -2147483648 32767 -32768 -128 127