diff --git hplsql/src/main/java/org/apache/hive/hplsql/Var.java hplsql/src/main/java/org/apache/hive/hplsql/Var.java index 480d97c..7b43925 100644 --- hplsql/src/main/java/org/apache/hive/hplsql/Var.java +++ hplsql/src/main/java/org/apache/hive/hplsql/Var.java @@ -178,7 +178,11 @@ else if (type == Type.STRING) { } else if (type == Type.BIGINT) { if (val.type == Type.STRING) { - value = Long.parseLong((String)val.value); + // String can contain the fractional part that must be discarded i.e. 10.3 -> 10 + value = new Long(new BigDecimal((String)val.value).longValue()); + } + else if (val.type == Type.DECIMAL || val.type == Type.DOUBLE) { + value = new Long(val.longValue()); } } else if (type == Type.DECIMAL) { @@ -512,6 +516,12 @@ public long longValue() { if (type == Type.BIGINT) { return ((Long)value).longValue(); } + else if (type == Type.DECIMAL) { + return ((BigDecimal)value).longValue(); + } + else if (type == Type.DOUBLE) { + return ((Double)value).longValue(); + } throw new NumberFormatException(); } diff --git hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java index 8692661..61d25c5 100644 --- hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java +++ hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java @@ -72,8 +72,18 @@ public void testCast() throws Exception { public void testCast2() throws Exception { run("cast2"); } + + @Test + public void testCast3() throws Exception { + run("cast3"); + } @Test + public void testCast4() throws Exception { + run("cast4"); + } + + @Test public void testChar() throws Exception { run("char"); } diff --git hplsql/src/test/queries/local/cast3.sql hplsql/src/test/queries/local/cast3.sql new file mode 100644 index 0000000..8bf94bc --- /dev/null +++ hplsql/src/test/queries/local/cast3.sql @@ -0,0 +1,8 @@ +print cast('10.0' as integer); +print cast('10.3' as integer); +print cast('10.0' as int); +print cast('10.3' as int); +print cast('10.0' as smallint); +print cast('10.3' as smallint); +print cast('10.0' as bigint); +print cast('10.3' as bigint); \ No newline at end of file diff --git hplsql/src/test/queries/local/cast4.sql hplsql/src/test/queries/local/cast4.sql new file mode 100644 index 0000000..9b8a4ac --- /dev/null +++ hplsql/src/test/queries/local/cast4.sql @@ -0,0 +1,50 @@ +declare s string default '1.1' +declare i int default 11; +declare num decimal default 1.3; +declare f float default 3.7; +declare db double default 7.1; + +print 'String: ' || s; +print cast(s as string); +print cast(s as int); +print cast(s as decimal); +print cast(s as float); +print cast(s as double); + +print 'Integer: ' || i; +print cast(i as string); +print cast(i as int); +print cast(i as decimal); +print cast(i as float); +print cast(i as double); + +print 'Decimal: ' || num; +print cast(num as string); +print cast(num as int); +print cast(num as decimal); +print cast(num as float); +print cast(num as double); + +print 'Float: ' || f; +print cast(f as string); +print cast(f as int); +print cast(f as decimal); +print cast(f as float); +print cast(f as double); + +print 'Double: ' || db; +print cast(db as string); +print cast(db as int); +print cast(db as decimal); +print cast(db as float); +print cast(db as double); + +declare i2 int; +i2 = s; +print i - i2; +i2 = num; +print i - i2; +i2 = f; +print i - i2; +i2 = db; +print i - i2; diff --git hplsql/src/test/results/local/cast3.out.txt hplsql/src/test/results/local/cast3.out.txt new file mode 100644 index 0000000..cceb582 --- /dev/null +++ hplsql/src/test/results/local/cast3.out.txt @@ -0,0 +1,16 @@ +Ln:1 PRINT +10 +Ln:2 PRINT +10 +Ln:3 PRINT +10 +Ln:4 PRINT +10 +Ln:5 PRINT +10 +Ln:6 PRINT +10 +Ln:7 PRINT +10 +Ln:8 PRINT +10 \ No newline at end of file diff --git hplsql/src/test/results/local/cast4.out.txt hplsql/src/test/results/local/cast4.out.txt new file mode 100644 index 0000000..abffdd1 --- /dev/null +++ hplsql/src/test/results/local/cast4.out.txt @@ -0,0 +1,78 @@ +Ln:1 DECLARE s string = '1.1' +Ln:2 DECLARE i int = 11 +Ln:3 DECLARE num decimal = 1.3 +Ln:4 DECLARE f float = 3.7 +Ln:5 DECLARE db double = 7.1 +Ln:7 PRINT +String: 1.1 +Ln:8 PRINT +1.1 +Ln:9 PRINT +1 +Ln:10 PRINT +1.1 +Ln:11 PRINT +1.1 +Ln:12 PRINT +1.1 +Ln:14 PRINT +Integer: 11 +Ln:15 PRINT +11 +Ln:16 PRINT +11 +Ln:17 PRINT +11 +Ln:18 PRINT +11.0 +Ln:19 PRINT +11.0 +Ln:21 PRINT +Decimal: 1.3 +Ln:22 PRINT +1.3 +Ln:23 PRINT +1 +Ln:24 PRINT +1.3 +Ln:25 PRINT +1.3 +Ln:26 PRINT +1.3 +Ln:28 PRINT +Float: 3.7 +Ln:29 PRINT +3.7 +Ln:30 PRINT +3 +Ln:31 PRINT +3.7 +Ln:32 PRINT +3.7 +Ln:33 PRINT +3.7 +Ln:35 PRINT +Double: 7.1 +Ln:36 PRINT +7.1 +Ln:37 PRINT +7 +Ln:38 PRINT +7.1 +Ln:39 PRINT +7.1 +Ln:40 PRINT +7.1 +Ln:42 DECLARE i2 int +Ln:43 SET i2 = 1 +Ln:44 PRINT +10 +Ln:45 SET i2 = 1 +Ln:46 PRINT +10 +Ln:47 SET i2 = 3 +Ln:48 PRINT +8 +Ln:49 SET i2 = 7 +Ln:50 PRINT +4 \ No newline at end of file