Skip to content

Commit 510072d

Browse files
committed
[21756][core] flink-sql double 类型转换问题
1 parent 7dbad64 commit 510072d

File tree

7 files changed

+225
-819
lines changed

7 files changed

+225
-819
lines changed

core/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@
116116
<version>${flink.version}</version>
117117
</dependency>
118118

119+
<dependency>
120+
<groupId>junit</groupId>
121+
<artifactId>junit</artifactId>
122+
<version>4.12</version>
123+
</dependency>
124+
119125
</dependencies>
120126

121127
<build>

core/src/main/java/com/dtstack/flink/sql/util/MathUtil.java

+187-30
Original file line numberDiff line numberDiff line change
@@ -21,82 +21,239 @@
2121
package com.dtstack.flink.sql.util;
2222

2323
import java.math.BigDecimal;
24+
import java.math.BigInteger;
25+
import java.sql.Date;
26+
import java.sql.Timestamp;
27+
import java.text.ParseException;
28+
import java.text.SimpleDateFormat;
2429

2530
/**
31+
* Convert val to specified numeric type
2632
* Date: 2017/4/21
2733
* Company: www.dtstack.com
2834
* @author xuchao
2935
*/
3036

3137
public class MathUtil {
3238

33-
public static Long getLongVal(Object obj){
34-
if(obj == null){
39+
public static Long getLongVal(Object obj) {
40+
if (obj == null) {
3541
return null;
3642
}
3743

38-
if(obj instanceof String){
44+
if (obj instanceof String) {
3945
return Long.valueOf((String) obj);
40-
}else if(obj instanceof Long){
46+
} else if (obj instanceof Long) {
4147
return (Long) obj;
42-
}else if(obj instanceof Integer){
48+
} else if (obj instanceof Integer) {
4349
return Long.valueOf(obj.toString());
50+
} else if (obj instanceof BigDecimal) {
51+
return ((BigDecimal) obj).longValue();
52+
} else if (obj instanceof BigInteger) {
53+
return ((BigInteger) obj).longValue();
4454
}
55+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Long.");
56+
}
4557

46-
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Long." );
58+
public static Long getLongVal(Object obj, long defaultVal) {
59+
if (obj == null) {
60+
return defaultVal;
61+
}
62+
63+
return getLongVal(obj);
4764
}
4865

49-
public static Integer getIntegerVal(Object obj){
50-
if(obj == null){
66+
public static Integer getIntegerVal(Object obj) {
67+
if (obj == null) {
5168
return null;
5269
}
5370

54-
if(obj instanceof String){
71+
if (obj instanceof String) {
5572
return Integer.valueOf((String) obj);
56-
} else if (obj instanceof Integer){
73+
} else if (obj instanceof Integer) {
5774
return (Integer) obj;
58-
} else if (obj instanceof Long){
59-
return ((Long)obj).intValue();
60-
} else if(obj instanceof Double){
61-
return ((Double)obj).intValue();
62-
} else if(obj instanceof BigDecimal){
63-
return ((BigDecimal)obj).intValue();
75+
} else if (obj instanceof Long) {
76+
return ((Long) obj).intValue();
77+
} else if (obj instanceof Double) {
78+
return ((Double) obj).intValue();
79+
} else if (obj instanceof BigDecimal) {
80+
return ((BigDecimal) obj).intValue();
81+
} else if (obj instanceof BigInteger) {
82+
return ((BigInteger) obj).intValue();
6483
}
6584

66-
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Integer." );
85+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Integer.");
6786
}
6887

69-
public static Boolean getBoolean(Object obj, boolean defaultVal){
70-
if(obj == null){
88+
public static Integer getIntegerVal(Object obj, int defaultVal) {
89+
if (obj == null) {
7190
return defaultVal;
7291
}
7392

74-
return getBoolean(obj);
93+
return getIntegerVal(obj);
94+
}
95+
96+
public static Float getFloatVal(Object obj) {
97+
if (obj == null) {
98+
return null;
99+
}
100+
101+
if (obj instanceof String) {
102+
return Float.valueOf((String) obj);
103+
} else if (obj instanceof Float) {
104+
return (Float) obj;
105+
} else if (obj instanceof BigDecimal) {
106+
return ((BigDecimal) obj).floatValue();
107+
}
108+
109+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Float.");
110+
}
111+
112+
public static Float getFloatVal(Object obj, float defaultVal) {
113+
if (obj == null) {
114+
return defaultVal;
115+
}
116+
117+
return getFloatVal(obj);
75118
}
76119

77-
public static Boolean getBoolean(Object obj){
78-
if(obj == null){
120+
public static Double getDoubleVal(Object obj) {
121+
if (obj == null) {
79122
return null;
80123
}
81124

82-
if(obj instanceof String){
125+
if (obj instanceof String) {
126+
return Double.valueOf((String) obj);
127+
} else if (obj instanceof Float) {
128+
return ((Float) obj).doubleValue();
129+
} else if (obj instanceof Double){
130+
return (Double) obj;
131+
}else if (obj instanceof BigDecimal) {
132+
return ((BigDecimal) obj).doubleValue();
133+
}else if (obj instanceof Integer){
134+
return ((Integer)obj).doubleValue();
135+
}
136+
137+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Double.");
138+
}
139+
140+
public static Double getDoubleVal(Object obj, double defaultVal) {
141+
if (obj == null) {
142+
return defaultVal;
143+
}
144+
145+
return getDoubleVal(obj);
146+
}
147+
148+
149+
public static Boolean getBoolean(Object obj) {
150+
if (obj == null) {
151+
return null;
152+
}
153+
154+
if (obj instanceof String) {
83155
return Boolean.valueOf((String) obj);
84-
}else if(obj instanceof Boolean){
156+
} else if (obj instanceof Boolean) {
85157
return (Boolean) obj;
86158
}
87159

88-
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Boolean." );
160+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Boolean.");
161+
}
162+
163+
public static Boolean getBoolean(Object obj, boolean defaultVal) {
164+
if (obj == null) {
165+
return defaultVal;
166+
}
167+
168+
return getBoolean(obj);
89169
}
90170

91-
public static String getString(Object obj){
92-
if(obj == null){
171+
public static String getString(Object obj) {
172+
if (obj == null) {
93173
return null;
94174
}
95175

96-
if(obj instanceof String){
176+
if (obj instanceof String) {
97177
return (String) obj;
98-
}else {
99-
return obj.toString();
100178
}
179+
180+
return obj.toString();
181+
}
182+
183+
public static Byte getByte(Object obj) {
184+
if (obj == null) {
185+
return null;
186+
}
187+
188+
if (obj instanceof String) {
189+
return Byte.valueOf((String) obj);
190+
} else if (obj instanceof Byte) {
191+
return (Byte) obj;
192+
}
193+
194+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Byte.");
195+
}
196+
197+
public static Short getShort(Object obj) {
198+
if (obj == null) {
199+
return null;
200+
}
201+
202+
if (obj instanceof String) {
203+
return Short.valueOf((String) obj);
204+
} else if (obj instanceof Short) {
205+
return (Short) obj;
206+
}
207+
208+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Short.");
209+
}
210+
211+
public static BigDecimal getBigDecimal(Object obj) {
212+
if (obj == null) {
213+
return null;
214+
}
215+
if (obj instanceof String) {
216+
return new BigDecimal((String) obj);
217+
} else if (obj instanceof BigDecimal) {
218+
return (BigDecimal) obj;
219+
} else if (obj instanceof BigInteger) {
220+
return new BigDecimal((BigInteger) obj);
221+
} else if (obj instanceof Number) {
222+
return new BigDecimal(((Number) obj).doubleValue());
223+
}
224+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to BigDecimal.");
225+
}
226+
227+
public static Date getDate(Object obj) {
228+
if (obj == null) {
229+
return null;
230+
}
231+
if (obj instanceof String) {
232+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
233+
try {
234+
return new Date(format.parse((String) obj).getTime());
235+
} catch (ParseException e) {
236+
throw new RuntimeException("String convert to Date fail.");
237+
}
238+
} else if (obj instanceof Timestamp) {
239+
return new Date(((Timestamp) obj).getTime());
240+
} else if (obj instanceof Date) {
241+
return (Date) obj;
242+
}
243+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date.");
244+
}
245+
246+
public static Timestamp getTimestamp(Object obj) {
247+
if (obj == null) {
248+
return null;
249+
}
250+
if (obj instanceof Timestamp) {
251+
return (Timestamp) obj;
252+
} else if (obj instanceof Date) {
253+
return new Timestamp(((Date) obj).getTime());
254+
} else if (obj instanceof String) {
255+
return new Timestamp(getDate(obj).getTime());
256+
}
257+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date.");
101258
}
102259
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.dtstack.flink.sql.side;
2+
3+
import com.dtstack.flink.sql.util.MathUtil;
4+
import org.junit.Test;
5+
6+
import java.math.BigDecimal;
7+
8+
/**
9+
* Reason:
10+
* Date: 2020/1/6
11+
* Company: www.dtstack.com
12+
* @author xuchao
13+
*/
14+
15+
public class MathUtilTest {
16+
17+
@Test
18+
public void testGetDoubleVal(){
19+
Double doubleVal = 1.1D;
20+
Float floatVal = 1.1F;
21+
Integer intVal = 1;
22+
String strVal = "1";
23+
BigDecimal bigDecimalVal = new BigDecimal(1.1);
24+
25+
MathUtil.getDoubleVal(doubleVal);
26+
MathUtil.getDoubleVal(floatVal);
27+
MathUtil.getDoubleVal(intVal);
28+
MathUtil.getDoubleVal(strVal);
29+
MathUtil.getDoubleVal(bigDecimalVal);
30+
}
31+
}

0 commit comments

Comments
 (0)