Skip to content

Commit db07a0b

Browse files
committed
PLUGIN-1893 : Adding flag via field for backward compatibility of precisionless number to decimal
1 parent b9444d0 commit db07a0b

File tree

7 files changed

+79
-14
lines changed

7 files changed

+79
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ protected DBConnectorPath getDBConnectorPath(String path) {
112112

113113
@Override
114114
protected SchemaReader getSchemaReader(String sessionID) {
115-
return new OracleSourceSchemaReader(sessionID, config.getTreatAsOldTimestampConn());
115+
return new OracleSourceSchemaReader(sessionID, config.getTreatAsOldTimestampConn(),
116+
config.getTreatPrecisionlessNumAsDeciConn());
116117
}
117118

118119
@Override

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ public OracleConnectorConfig(String host, int port, String user, String password
4141

4242
public OracleConnectorConfig(String host, int port, String user, String password, String jdbcPluginName,
4343
String connectionArguments, String connectionType, String database) {
44-
this(host, port, user, password, jdbcPluginName, connectionArguments, connectionType, database, null, null, null);
44+
this(host, port, user, password, jdbcPluginName, connectionArguments, connectionType, database, null, null, null,
45+
null);
4546
}
4647

4748
public OracleConnectorConfig(String host, int port, String user, String password, String jdbcPluginName,
4849
String connectionArguments, String connectionType, String database,
49-
String role, Boolean useSSL, String treatAsOldTimestampConn) {
50+
String role, Boolean useSSL, String treatAsOldTimestampConn,
51+
String treatPrecisionlessNumAsDeciConn) {
5052

5153
this.host = host;
5254
this.port = port;
@@ -59,6 +61,7 @@ public OracleConnectorConfig(String host, int port, String user, String password
5961
this.role = role;
6062
this.useSSL = useSSL;
6163
this.treatAsOldTimestampConn = treatAsOldTimestampConn;
64+
this.treatPrecisionlessNumAsDeciConn = treatPrecisionlessNumAsDeciConn;
6265
}
6366

6467
@Override
@@ -90,6 +93,11 @@ public String getConnectionString() {
9093
@Nullable
9194
public String treatAsOldTimestampConn;
9295

96+
@Name(OracleConstants.TREAT_PRECISIONLESSNUM_AS_DECI_CONN)
97+
@Description("A hidden field to handle precision less number as CDAP's decimal per old behavior.")
98+
@Nullable
99+
public String treatPrecisionlessNumAsDeciConn;
100+
93101
@Override
94102
protected int getDefaultPort() {
95103
return 1521;
@@ -116,6 +124,10 @@ public Boolean getTreatAsOldTimestampConn() {
116124
return Boolean.parseBoolean(treatAsOldTimestampConn);
117125
}
118126

127+
public Boolean getTreatPrecisionlessNumAsDeciConn() {
128+
return Boolean.parseBoolean(treatPrecisionlessNumAsDeciConn);
129+
}
130+
119131
@Override
120132
public Properties getConnectionArgumentsProperties() {
121133
Properties prop = super.getConnectionArgumentsProperties();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ private OracleConstants() {
4545
public static final String USE_SSL = "useSSL";
4646
public static final String TREAT_AS_OLD_TIMESTAMP = "treatAsOldTimestamp";
4747
public static final String TREAT_AS_OLD_TIMESTAMP_CONN = "treatAsOldTimestampConn";
48+
public static final String TREAT_PRECISIONLESSNUM_AS_DECI = "treatPrecisionlessNumAsDeci";
49+
public static final String TREAT_PRECISIONLESSNUM_AS_DECI_CONN = "treatPrecisionlessNumAsDeciConn";
4850

4951
/**
5052
* Constructs the Oracle connection string based on the provided connection type, host, port, and database.

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ protected SchemaReader getSchemaReader() {
7474
treatAsOldTimestamp = Boolean.parseBoolean(sourceConfig.getTreatAsOldTimestamp());
7575
}
7676

77-
return new OracleSourceSchemaReader(null, treatAsOldTimestamp);
77+
boolean treatPrecisionlessNumAsDeci = oracleSourceConfig.getConnection().getTreatPrecisionlessNumAsDeciConn();
78+
if (sourceConfig.getTreatPrecisionlessNumAsDeci().equalsIgnoreCase("true") ||
79+
sourceConfig.getTreatPrecisionlessNumAsDeci().equalsIgnoreCase("false")) {
80+
treatPrecisionlessNumAsDeci = Boolean.parseBoolean(sourceConfig.getTreatPrecisionlessNumAsDeci());
81+
}
82+
83+
return new OracleSourceSchemaReader(null, treatAsOldTimestamp, treatPrecisionlessNumAsDeci);
7884
}
7985

8086
@Override
@@ -139,14 +145,21 @@ public static class OracleSourceConfig extends AbstractDBSpecificSourceConfig {
139145
@Nullable
140146
private String treatAsOldTimestamp;
141147

148+
@Name(OracleConstants.TREAT_PRECISIONLESSNUM_AS_DECI)
149+
@Description("A hidden field to handle precision less number as CDAP's decimal per old behavior.")
150+
@Nullable
151+
public String treatPrecisionlessNumAsDeci;
152+
142153
public OracleSourceConfig(String host, int port, String user, String password, String jdbcPluginName,
143154
String connectionArguments, String connectionType, String database, String role,
144155
int defaultBatchValue, int defaultRowPrefetch,
145156
String importQuery, Integer numSplits, int fetchSize,
146157
String boundingQuery, String splitBy, Boolean useSSL, String treatAsOldTimestampConn,
147-
String treatAsOldTimestamp) {
158+
String treatAsOldTimestamp, String treatPrecisionlessNumAsDeciConn,
159+
String treatPrecisionlessNumAsDeci) {
148160
this.connection = new OracleConnectorConfig(host, port, user, password, jdbcPluginName, connectionArguments,
149-
connectionType, database, role, useSSL, treatAsOldTimestampConn);
161+
connectionType, database, role, useSSL, treatAsOldTimestampConn,
162+
treatPrecisionlessNumAsDeciConn);
150163
this.defaultBatchValue = defaultBatchValue;
151164
this.defaultRowPrefetch = defaultRowPrefetch;
152165
this.fetchSize = fetchSize;
@@ -155,6 +168,7 @@ public OracleSourceConfig(String host, int port, String user, String password, S
155168
this.boundingQuery = boundingQuery;
156169
this.splitBy = splitBy;
157170
this.treatAsOldTimestamp = treatAsOldTimestamp;
171+
this.treatPrecisionlessNumAsDeci = treatPrecisionlessNumAsDeci;
158172
}
159173

160174
@Override
@@ -185,6 +199,10 @@ public String getTreatAsOldTimestamp() {
185199
return treatAsOldTimestamp == null ? "" : treatAsOldTimestamp;
186200
}
187201

202+
public String getTreatPrecisionlessNumAsDeci() {
203+
return treatPrecisionlessNumAsDeci == null ? "" : treatPrecisionlessNumAsDeci;
204+
}
205+
188206
@Override
189207
public void validate(FailureCollector collector) {
190208
ConfigUtil.validateConnection(this, useConnection, connection, collector);

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ public class OracleSourceSchemaReader extends CommonSchemaReader {
6666

6767
private final String sessionID;
6868
private final Boolean isTimestampOldBehavior;
69+
private final Boolean isPrecisionlessNumAsDecimal;
6970

7071
public OracleSourceSchemaReader() {
71-
this(null, false);
72+
this(null, false, false);
7273
}
73-
public OracleSourceSchemaReader(String sessionID, boolean isTimestampOldBehavior) {
74+
public OracleSourceSchemaReader(String sessionID, boolean isTimestampOldBehavior,
75+
boolean isPrecisionlessNumAsDecimal) {
7476
this.sessionID = sessionID;
7577
this.isTimestampOldBehavior = isTimestampOldBehavior;
78+
this.isPrecisionlessNumAsDecimal = isPrecisionlessNumAsDecimal;
7679
}
7780

7881
@Override
@@ -109,12 +112,24 @@ public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLExcepti
109112
// For a Number type without specified precision and scale, precision will be 0 and scale will be -127
110113
if (precision == 0) {
111114
// reference : https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1832
112-
LOG.warn(String.format("Field '%s' is a %s type without precision and scale, "
113-
+ "converting into STRING type to avoid any precision loss.",
114-
metadata.getColumnName(index),
115-
metadata.getColumnTypeName(index),
116-
metadata.getColumnName(index)));
117-
return Schema.of(Schema.Type.STRING);
115+
if (isPrecisionlessNumAsDecimal) {
116+
precision = 38;
117+
scale = 0;
118+
LOG.warn(String.format("%s type with undefined precision and scale is detected, "
119+
+ "there may be a precision loss while running the pipeline. "
120+
+ "Please define an output precision and scale for field '%s' to avoid "
121+
+ "precision loss.",
122+
metadata.getColumnTypeName(index),
123+
metadata.getColumnName(index)));
124+
return Schema.decimalOf(precision, scale);
125+
} else {
126+
LOG.warn(String.format("Field '%s' is a %s type without precision and scale, "
127+
+ "converting into STRING type to avoid any precision loss.",
128+
metadata.getColumnName(index),
129+
metadata.getColumnTypeName(index),
130+
metadata.getColumnName(index)));
131+
return Schema.of(Schema.Type.STRING);
132+
}
118133
}
119134
return Schema.decimalOf(precision, scale);
120135
}

oracle-plugin/widgets/Oracle-batchsource.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@
255255
"widget-attributes": {
256256
"default": "default-no"
257257
}
258+
},
259+
{
260+
"widget-type": "hidden",
261+
"label": "Treat precision less number as Decimal(old behavior)",
262+
"name": "treatPrecisionlessNumAsDeci",
263+
"description": "If edited, then this will take precedence over other properties and the edited value shall be true or false only",
264+
"widget-attributes": {
265+
"default": "default-no"
266+
}
258267
}
259268
]
260269
}

oracle-plugin/widgets/Oracle-connector.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@
137137
"widget-attributes": {
138138
"default": "false"
139139
}
140+
},
141+
{
142+
"widget-type": "hidden",
143+
"label": "Treat precision less number as Decimal(old behavior)",
144+
"name": "treatPrecisionlessNumAsDeciConn",
145+
"widget-attributes": {
146+
"default": "false"
147+
}
140148
}
141149
]
142150
},

0 commit comments

Comments
 (0)