forked from apache/carbondata
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataTypeUtilTest.java
120 lines (100 loc) · 5 KB
/
DataTypeUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* 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.
*/
package org.apache.carbondata.core.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.apache.carbondata.core.metadata.datatype.DataTypes;
import org.apache.carbondata.core.metadata.schema.table.column.CarbonMeasure;
import org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.apache.carbondata.core.util.DataTypeUtil.bigDecimalToByte;
import static org.apache.carbondata.core.util.DataTypeUtil.byteToBigDecimal;
import static org.apache.carbondata.core.util.DataTypeUtil.getDataBasedOnDataType;
import static org.apache.carbondata.core.util.DataTypeUtil.getMeasureValueBasedOnDataType;
import static org.apache.carbondata.core.util.DataTypeUtil.normalizeIntAndLongValues;
public class DataTypeUtilTest {
@Test public void testGetColumnDataTypeDisplayName() {
String expected = DataTypes.INT.getName();
String result = "INT";
assertEquals(expected, result);
}
@Test public void testByteToBigDecimal() {
byte[] byteArr = { 0, 0 };
byte[] unscale = new byte[byteArr.length - 1];
BigInteger bigInteger = new BigInteger(unscale);
BigDecimal expected = new BigDecimal(bigInteger, 0);
BigDecimal result = byteToBigDecimal(byteArr);
assertEquals(expected, result);
}
@Test public void testBigDecimalToByte() {
byte[] result = bigDecimalToByte(BigDecimal.ONE);
assertTrue(result == result);
}
@Test public void testGetDataBasedOnDataType() throws NumberFormatException {
String data = " ";
if (data.isEmpty()) {
assertEquals(getDataBasedOnDataType(data, DataTypes.INT), null);
}
assertEquals(getDataBasedOnDataType("1", DataTypes.INT), 1);
assertEquals(getDataBasedOnDataType(" ", DataTypes.INT), null);
assertEquals(getDataBasedOnDataType("0", DataTypes.DOUBLE), 0.0d);
assertEquals(getDataBasedOnDataType("0", DataTypes.LONG), 0L);
java.math.BigDecimal javaDecVal = new java.math.BigDecimal(1);
scala.math.BigDecimal scalaDecVal = new scala.math.BigDecimal(javaDecVal);
assertEquals(getDataBasedOnDataType("1", DataTypes.createDefaultDecimalType()),
DataTypeUtil.getDataTypeConverter().convertToDecimal(scalaDecVal));
assertEquals(getDataBasedOnDataType("default", DataTypes.NULL),
DataTypeUtil.getDataTypeConverter().convertFromStringToUTF8String("default"));
assertEquals(getDataBasedOnDataType((String) null, DataTypes.NULL), null);
}
@Test public void testGetMeasureValueBasedOnDataType() {
ColumnSchema columnSchema = new ColumnSchema();
CarbonMeasure carbonMeasure = new CarbonMeasure(columnSchema, 1);
Object resultInt = getMeasureValueBasedOnDataType("1", DataTypes.INT, carbonMeasure);
Object expectedInt = Double.valueOf(1).intValue();
assertEquals(expectedInt, resultInt);
Object resultLong = getMeasureValueBasedOnDataType("1", DataTypes.LONG, carbonMeasure);
Object expectedLong = Long.valueOf(1);
assertEquals(expectedLong, resultLong);
Object resultDefault = getMeasureValueBasedOnDataType("1", DataTypes.DOUBLE, carbonMeasure);
Double expectedDefault = Double.valueOf(1);
assertEquals(expectedDefault, resultDefault);
}
@Test public void testNormalizeIntAndLongValues() throws NumberFormatException {
assertEquals(null, normalizeIntAndLongValues("INT", DataTypes.INT));
assertEquals("1", normalizeIntAndLongValues("1", DataTypes.STRING));
}
@Test public void testGetDataBasedOnDataTypeForNoDictionaryColumn() {
Object result = DataTypeUtil.getDataBasedOnDataTypeForNoDictionaryColumn(new byte[0],
DataTypes.INT);
assert (result == null);
result = DataTypeUtil.getDataBasedOnDataTypeForNoDictionaryColumn(new byte[0],
DataTypes.SHORT);
assert (result == null);
result = DataTypeUtil.getDataBasedOnDataTypeForNoDictionaryColumn(new byte[0],
DataTypes.LONG);
assert (result == null);
result = DataTypeUtil.getDataBasedOnDataTypeForNoDictionaryColumn(new byte[0],
DataTypes.TIMESTAMP);
assert (result == null);
result = DataTypeUtil.getDataBasedOnDataTypeForNoDictionaryColumn(new byte[0],
DataTypes.STRING);
assert (result != null);
}
}