Skip to content

Commit c652e04

Browse files
authored
Merge pull request #117 from data-integrations/PLUGIN-702-fix-decimal-validation
Fixed validation issue with decimal values with precision 0
2 parents e220003 + c7272fe commit c652e04

File tree

4 files changed

+213
-8
lines changed

4 files changed

+213
-8
lines changed

database-commons/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
<version>${mockrunner.version}</version>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>org.mockito</groupId>
70+
<artifactId>mockito-core</artifactId>
71+
<version>${mockito.version}</version>
72+
<scope>test</scope>
73+
</dependency>
6874
</dependencies>
6975

7076
<build>

database-commons/src/main/java/io/cdap/plugin/db/CommonSchemaReader.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,10 @@ public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLExcepti
9090
case Types.DECIMAL:
9191
int precision = metadata.getPrecision(index); // total number of digits
9292
int scale = metadata.getScale(index); // digits after the decimal point
93-
// decimal type with scale 0 is not supported
94-
// possible cases are:
95-
// - scale is set to 0
96-
// - only precision is set - by default scale is 0
97-
// - precision and scale are set as dynamic - by default scale is 0
98-
if (scale == 0) {
99-
throw new SQLException(new UnsupportedTypeException(String.format("Unsupported SQL Type: %s with scale 0.",
100-
metadata.getColumnTypeName(index))));
93+
// decimal type with precision 0 is not supported
94+
if (precision == 0) {
95+
throw new SQLException(new UnsupportedTypeException(
96+
String.format("Unsupported SQL Type: %s with precision 0.", metadata.getColumnTypeName(index))));
10197
}
10298
return Schema.decimalOf(precision, scale);
10399

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Copyright © 2021 Cask Data, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.cdap.plugin.db;
18+
19+
import io.cdap.cdap.api.data.schema.Schema;
20+
import org.junit.Assert;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.junit.MockitoJUnitRunner;
26+
27+
import java.sql.ResultSetMetaData;
28+
import java.sql.SQLException;
29+
import java.sql.Types;
30+
31+
import static org.mockito.ArgumentMatchers.eq;
32+
import static org.mockito.Mockito.when;
33+
34+
@RunWith(MockitoJUnitRunner.class)
35+
public class CommonSchemaReaderTest {
36+
37+
CommonSchemaReader reader;
38+
39+
@Mock
40+
ResultSetMetaData metadata;
41+
42+
@Before
43+
public void before() {
44+
reader = new CommonSchemaReader();
45+
}
46+
47+
@Test
48+
public void testGetSchemaHandlesNull() throws SQLException {
49+
when(metadata.getColumnType(eq(1))).thenReturn(Types.NULL);
50+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.NULL));
51+
}
52+
53+
@Test
54+
public void testGetSchemaHandlesRowID() throws SQLException {
55+
when(metadata.getColumnType(eq(1))).thenReturn(Types.ROWID);
56+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.STRING));
57+
}
58+
59+
@Test
60+
public void testGetSchemaHandlesBoolean() throws SQLException {
61+
when(metadata.getColumnType(eq(1))).thenReturn(Types.BOOLEAN);
62+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.BOOLEAN));
63+
64+
when(metadata.getColumnType(eq(2))).thenReturn(Types.BIT);
65+
Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.BOOLEAN));
66+
}
67+
68+
@Test
69+
public void testGetSchemaHandlesInt() throws SQLException {
70+
when(metadata.getColumnType(eq(1))).thenReturn(Types.TINYINT);
71+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.INT));
72+
73+
when(metadata.getColumnType(eq(2))).thenReturn(Types.SMALLINT);
74+
Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.INT));
75+
76+
when(metadata.getColumnType(eq(3))).thenReturn(Types.INTEGER);
77+
Assert.assertEquals(reader.getSchema(metadata, 3), Schema.of(Schema.Type.INT));
78+
}
79+
80+
@Test
81+
public void testGetSchemaHandlesLong() throws SQLException {
82+
when(metadata.getColumnType(eq(1))).thenReturn(Types.BIGINT);
83+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.LONG));
84+
}
85+
86+
@Test
87+
public void testGetSchemaHandlesFloat() throws SQLException {
88+
when(metadata.getColumnType(eq(1))).thenReturn(Types.REAL);
89+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.FLOAT));
90+
91+
when(metadata.getColumnType(eq(2))).thenReturn(Types.FLOAT);
92+
Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.FLOAT));
93+
}
94+
95+
@Test
96+
public void testGetSchemaHandlesNumeric() throws SQLException {
97+
when(metadata.getColumnType(eq(1))).thenReturn(Types.NUMERIC);
98+
when(metadata.getPrecision(eq(1))).thenReturn(10);
99+
when(metadata.getScale(eq(1))).thenReturn(0);
100+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.decimalOf(10, 0));
101+
102+
when(metadata.getColumnType(eq(2))).thenReturn(Types.DECIMAL);
103+
when(metadata.getPrecision(eq(2))).thenReturn(10);
104+
when(metadata.getScale(eq(2))).thenReturn(1);
105+
Assert.assertEquals(reader.getSchema(metadata, 2), Schema.decimalOf(10, 1));
106+
}
107+
108+
@Test
109+
public void testGetSchemaHandlesDouble() throws SQLException {
110+
when(metadata.getColumnType(eq(1))).thenReturn(Types.DOUBLE);
111+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.DOUBLE));
112+
}
113+
114+
@Test
115+
public void testGetSchemaHandlesDate() throws SQLException {
116+
when(metadata.getColumnType(eq(1))).thenReturn(Types.DATE);
117+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.LogicalType.DATE));
118+
}
119+
120+
@Test
121+
public void testGetSchemaHandlesTime() throws SQLException {
122+
when(metadata.getColumnType(eq(1))).thenReturn(Types.TIME);
123+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.LogicalType.TIME_MICROS));
124+
}
125+
126+
@Test
127+
public void testGetSchemaHandlesTimestamp() throws SQLException {
128+
when(metadata.getColumnType(eq(1))).thenReturn(Types.TIMESTAMP);
129+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.LogicalType.TIMESTAMP_MICROS));
130+
}
131+
132+
@Test
133+
public void testGetSchemaHandlesBytes() throws SQLException {
134+
when(metadata.getColumnType(eq(1))).thenReturn(Types.BINARY);
135+
Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.BYTES));
136+
137+
when(metadata.getColumnType(eq(2))).thenReturn(Types.VARBINARY);
138+
Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.BYTES));
139+
140+
when(metadata.getColumnType(eq(3))).thenReturn(Types.LONGVARBINARY);
141+
Assert.assertEquals(reader.getSchema(metadata, 3), Schema.of(Schema.Type.BYTES));
142+
143+
when(metadata.getColumnType(eq(4))).thenReturn(Types.BLOB);
144+
Assert.assertEquals(reader.getSchema(metadata, 4), Schema.of(Schema.Type.BYTES));
145+
}
146+
147+
@Test(expected = SQLException.class)
148+
public void testGetSchemaThrowsExceptionOnNumericWithZeroPrecision() throws SQLException {
149+
when(metadata.getColumnType(eq(1))).thenReturn(Types.NUMERIC);
150+
when(metadata.getPrecision(eq(1))).thenReturn(0);
151+
when(metadata.getScale(eq(1))).thenReturn(10);
152+
reader.getSchema(metadata, 1);
153+
}
154+
155+
@Test(expected = SQLException.class)
156+
public void testGetSchemaThrowsExceptionOnArray() throws SQLException {
157+
when(metadata.getColumnType(eq(1))).thenReturn(Types.ARRAY);
158+
reader.getSchema(metadata, 1);
159+
}
160+
161+
@Test(expected = SQLException.class)
162+
public void testGetSchemaThrowsExceptionOnDatalink() throws SQLException {
163+
when(metadata.getColumnType(eq(1))).thenReturn(Types.DATALINK);
164+
reader.getSchema(metadata, 1);
165+
}
166+
167+
@Test(expected = SQLException.class)
168+
public void testGetSchemaThrowsExceptionOnDistinct() throws SQLException {
169+
when(metadata.getColumnType(eq(1))).thenReturn(Types.DISTINCT);
170+
reader.getSchema(metadata, 1);
171+
}
172+
173+
@Test(expected = SQLException.class)
174+
public void testGetSchemaThrowsExceptionOnJavaObject() throws SQLException {
175+
when(metadata.getColumnType(eq(1))).thenReturn(Types.JAVA_OBJECT);
176+
reader.getSchema(metadata, 1);
177+
}
178+
179+
@Test(expected = SQLException.class)
180+
public void testGetSchemaThrowsExceptionOnOther() throws SQLException {
181+
when(metadata.getColumnType(eq(1))).thenReturn(Types.OTHER);
182+
reader.getSchema(metadata, 1);
183+
}
184+
185+
@Test(expected = SQLException.class)
186+
public void testGetSchemaThrowsExceptionOnRef() throws SQLException {
187+
when(metadata.getColumnType(eq(1))).thenReturn(Types.REF);
188+
reader.getSchema(metadata, 1);
189+
}
190+
191+
@Test(expected = SQLException.class)
192+
public void testGetSchemaThrowsExceptionOnSQLXML() throws SQLException {
193+
when(metadata.getColumnType(eq(1))).thenReturn(Types.SQLXML);
194+
reader.getSchema(metadata, 1);
195+
}
196+
197+
@Test(expected = SQLException.class)
198+
public void testGetSchemaThrowsExceptionOnStruct() throws SQLException {
199+
when(metadata.getColumnType(eq(1))).thenReturn(Types.STRUCT);
200+
reader.getSchema(metadata, 1);
201+
}
202+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<hsql.version>2.2.4</hsql.version>
6868
<junit.version>4.11</junit.version>
6969
<mockrunner.version>2.0.1</mockrunner.version>
70+
<mockito.version>3.3.3</mockito.version>
7071
<slf4j.version>1.7.5</slf4j.version>
7172
<twill.version>0.9.0</twill.version>
7273
<!-- For plugins that don't require jdbc driver for tests -->

0 commit comments

Comments
 (0)