Skip to content

Commit c57aadb

Browse files
Merge pull request #312 from data-integrations/feature/PLUGIN-1433-OracleNumericScaleChanges
* Using the scale from logical schema instead of the number value. * Added Warning logs when detecting precision less Oracle numbers. * Added Unit Tests for precision less Number.
2 parents c6116bd + d4d7e05 commit c57aadb

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSourceDBRecord.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,23 @@ private void handleOracleSpecificType(ResultSet resultSet, StructuredRecord.Buil
214214
if (Double.class.getTypeName().equals(resultSet.getMetaData().getColumnClassName(columnIndex))) {
215215
recordBuilder.set(field.getName(), resultSet.getDouble(columnIndex));
216216
} else {
217-
// For a Number type without specified precision and scale, precision will be 0 and scale will be -127
218-
if (precision == 0) {
219-
// reference : https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1832
220-
scale = 0;
221-
}
222217
// It's required to pass 'scale' parameter since in the case of Oracle, scale of 'BigDecimal' depends on the
223-
// scale of actual value. For example for value '77.12' scale will be '2' even if sql scale is '6'
224-
BigDecimal decimal = resultSet.getBigDecimal(columnIndex, scale);
218+
// scale set in the logical schema. For example for value '77.12' if the scale set in the logical schema is
219+
// set to 4 then the number will change to '77.1200'. Also if the value is '22.1274' and the logical schema
220+
// scale is set to 2 then the decimal value used will be '22.13' after rounding.
221+
BigDecimal decimal = resultSet.getBigDecimal(columnIndex, getScale(field.getSchema()));
225222
recordBuilder.setDecimal(field.getName(), decimal);
226223
}
227224
}
228225
}
229226

227+
/**
228+
* Get the scale set in Non-nullable schema associated with the schema
229+
* */
230+
private int getScale(Schema schema) {
231+
return schema.isNullable() ? schema.getNonNullable().getScale() : schema.getScale();
232+
}
233+
230234
private boolean isLongOrLongRaw(int columnType) {
231235
return columnType == OracleSourceSchemaReader.LONG || columnType == OracleSourceSchemaReader.LONG_RAW;
232236
}

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSourceSchemaReader.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.common.collect.ImmutableSet;
2020
import io.cdap.cdap.api.data.schema.Schema;
2121
import io.cdap.plugin.db.CommonSchemaReader;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2224

2325
import java.sql.ResultSetMetaData;
2426
import java.sql.SQLException;
@@ -42,6 +44,11 @@ public class OracleSourceSchemaReader extends CommonSchemaReader {
4244
public static final int LONG = -1;
4345
public static final int LONG_RAW = -4;
4446

47+
/**
48+
* Logger instance for Oracle Schema reader.
49+
*/
50+
private static final Logger LOG = LoggerFactory.getLogger(OracleSourceSchemaReader.class);
51+
4552
public static final Set<Integer> ORACLE_TYPES = ImmutableSet.of(
4653
INTERVAL_DS,
4754
INTERVAL_YM,
@@ -100,6 +107,11 @@ public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLExcepti
100107
// reference : https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1832
101108
precision = 38;
102109
scale = 0;
110+
LOG.warn(String.format("%s type with undefined precision and scale is detected, "
111+
+ "there may be a precision loss while running the pipeline. "
112+
+ "Please define an output precision and scale for field '%s' to avoid precision loss.",
113+
metadata.getColumnTypeName(index),
114+
metadata.getColumnName(index)));
103115
}
104116
return Schema.decimalOf(precision, scale);
105117
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright © 2019 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.oracle;
18+
19+
import io.cdap.cdap.api.data.format.StructuredRecord;
20+
import io.cdap.cdap.api.data.schema.Schema;
21+
import org.junit.Assert;
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.math.BigDecimal;
28+
import java.sql.ResultSet;
29+
import java.sql.ResultSetMetaData;
30+
import java.sql.Types;
31+
32+
import static org.mockito.ArgumentMatchers.eq;
33+
import static org.mockito.Mockito.when;
34+
35+
/**
36+
* Unit Test class for the OracleSourceDBRecord
37+
*/
38+
@RunWith(MockitoJUnitRunner.class)
39+
public class OracleSourceDBRecordUnitTest {
40+
41+
private static final int DEFAULT_PRECISION = 38;
42+
43+
@Mock
44+
ResultSet resultSet;
45+
46+
@Mock
47+
ResultSetMetaData resultSetMetaData;
48+
49+
/**
50+
* Validate the precision less Numbers handling against following use cases.
51+
* 1. Ensure that for Number(0,-127) non nullable type a Number(38,0) is returned by default.
52+
* 2. Ensure that for Number(0,-127) non nullable type a Number(38,4) is returned,
53+
* if schema defined this as Number(38,4).
54+
* 3. Ensure that for Number(0,-127) nullable type a Number(38,0) is returned by default.
55+
* 4. Ensure that for Number(0,-127) nullable type a Number(38,4) is returned,
56+
* if schema defined this as Number(38,4).
57+
* @throws Exception
58+
*/
59+
@Test
60+
public void validatePrecisionLessDecimalParsing() throws Exception {
61+
Schema.Field field1 = Schema.Field.of("ID1", Schema.decimalOf(DEFAULT_PRECISION));
62+
Schema.Field field2 = Schema.Field.of("ID2", Schema.decimalOf(DEFAULT_PRECISION, 4));
63+
Schema.Field field3 = Schema.Field.of("ID3", Schema.nullableOf(Schema.decimalOf(DEFAULT_PRECISION)));
64+
Schema.Field field4 = Schema.Field.of("ID4", Schema.nullableOf(Schema.decimalOf(DEFAULT_PRECISION, 4)));
65+
66+
Schema schema = Schema.recordOf(
67+
"dbRecord",
68+
field1,
69+
field2,
70+
field3,
71+
field4
72+
);
73+
74+
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
75+
when(resultSet.getBigDecimal(eq(1), eq(0))).thenReturn(new BigDecimal("123"));
76+
when(resultSet.getBigDecimal(eq(2), eq(4))).thenReturn(new BigDecimal("123.4568"));
77+
when(resultSet.getBigDecimal(eq(3), eq(0))).thenReturn(new BigDecimal("123"));
78+
when(resultSet.getBigDecimal(eq(4), eq(4))).thenReturn(new BigDecimal("123.4568"));
79+
80+
StructuredRecord.Builder builder = StructuredRecord.builder(schema);
81+
OracleSourceDBRecord dbRecord = new OracleSourceDBRecord(null, null);
82+
dbRecord.handleField(resultSet, builder, field1, 1, Types.NUMERIC, 0, -127);
83+
dbRecord.handleField(resultSet, builder, field2, 2, Types.NUMERIC, 0, -127);
84+
dbRecord.handleField(resultSet, builder, field3, 3, Types.NUMERIC, 0, -127);
85+
dbRecord.handleField(resultSet, builder, field4, 4, Types.NUMERIC, 0, -127);
86+
87+
StructuredRecord record = builder.build();
88+
Assert.assertEquals(record.getDecimal("ID1").toPlainString(), "123");
89+
Assert.assertEquals(record.getDecimal("ID2").toPlainString(), "123.4568");
90+
Assert.assertEquals(record.getDecimal("ID3").toPlainString(), "123");
91+
Assert.assertEquals(record.getDecimal("ID4").toPlainString(), "123.4568");
92+
}
93+
}

0 commit comments

Comments
 (0)