Skip to content

Commit 57bb68d

Browse files
committed
CDAP-15550 Netezza db plugin enhacements: all data types support + proper test coverage
1 parent 8fca100 commit 57bb68d

File tree

3 files changed

+242
-200
lines changed

3 files changed

+242
-200
lines changed

netezza-plugin/src/test/java/io/cdap/plugin/netezza/NetezzaPluginTestBase.java

Lines changed: 66 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.base.Throwables;
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.common.collect.Sets;
23+
2324
import io.cdap.cdap.api.artifact.ArtifactSummary;
2425
import io.cdap.cdap.api.plugin.PluginClass;
2526
import io.cdap.cdap.datapipeline.DataPipelineApp;
@@ -31,20 +32,25 @@
3132
import io.cdap.plugin.db.batch.DatabasePluginTestBase;
3233
import io.cdap.plugin.db.batch.sink.ETLDBOutputFormat;
3334
import io.cdap.plugin.db.batch.source.DataDrivenETLDBInputFormat;
35+
3436
import org.junit.AfterClass;
3537
import org.junit.BeforeClass;
3638
import org.junit.ClassRule;
3739

3840
import java.math.BigDecimal;
41+
import java.math.MathContext;
3942
import java.sql.Connection;
4043
import java.sql.Date;
4144
import java.sql.Driver;
4245
import java.sql.DriverManager;
4346
import java.sql.PreparedStatement;
47+
import java.sql.ResultSet;
48+
import java.sql.ResultSetMetaData;
4449
import java.sql.SQLException;
4550
import java.sql.Statement;
4651
import java.sql.Time;
4752
import java.sql.Timestamp;
53+
import java.util.Arrays;
4854
import java.util.Calendar;
4955
import java.util.Collections;
5056
import java.util.Map;
@@ -59,7 +65,7 @@ public class NetezzaPluginTestBase extends DatabasePluginTestBase {
5965

6066
protected static String connectionUrl;
6167
protected static final int YEAR;
62-
protected static final int PRECISION = 10;
68+
protected static final int PRECISION = 16;
6369
protected static final int SCALE = 6;
6470
protected static boolean tearDown = true;
6571
private static int startCount;
@@ -91,9 +97,9 @@ public static void setupTest() throws Exception {
9197
setupBatchArtifacts(DATAPIPELINE_ARTIFACT_ID, DataPipelineApp.class);
9298

9399
addPluginArtifact(NamespaceId.DEFAULT.artifact(JDBC_DRIVER_NAME, "1.0.0"),
94-
DATAPIPELINE_ARTIFACT_ID,
95-
NetezzaSource.class, NetezzaSink.class, DBRecord.class, ETLDBOutputFormat.class,
96-
DataDrivenETLDBInputFormat.class, DBRecord.class, NetezzaPostAction.class, NetezzaAction.class);
100+
DATAPIPELINE_ARTIFACT_ID,
101+
NetezzaSource.class, NetezzaSink.class, DBRecord.class, ETLDBOutputFormat.class,
102+
DataDrivenETLDBInputFormat.class, DBRecord.class, NetezzaPostAction.class, NetezzaAction.class);
97103

98104
connectionUrl = "jdbc:netezza://" + BASE_PROPS.get(ConnectionConfig.HOST) + ":" +
99105
BASE_PROPS.get(ConnectionConfig.PORT) + "/" + BASE_PROPS.get(ConnectionConfig.DATABASE);
@@ -102,11 +108,11 @@ public static void setupTest() throws Exception {
102108

103109
// add netezza 3rd party plugin
104110
PluginClass netezzaDriver = new PluginClass(ConnectionConfig.JDBC_PLUGIN_TYPE, JDBC_DRIVER_NAME,
105-
"netezza driver class", aClass.getCanonicalName(),
106-
null, Collections.emptyMap());
111+
"netezza driver class", aClass.getCanonicalName(),
112+
null, Collections.emptyMap());
107113
addPluginArtifact(NamespaceId.DEFAULT.artifact("netezza-jdbc-connector", "1.0.0"),
108-
DATAPIPELINE_ARTIFACT_ID,
109-
Sets.newHashSet(netezzaDriver), aClass);
114+
DATAPIPELINE_ARTIFACT_ID,
115+
Sets.newHashSet(netezzaDriver), aClass);
110116

111117
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
112118

@@ -122,92 +128,64 @@ protected static void createTestTables(Connection conn) throws SQLException {
122128
// create a table that the action will truncate at the end of the run
123129
stmt.execute("CREATE TABLE post_action_test (x int, day varchar(10))");
124130

125-
stmt.execute("CREATE TABLE my_table (" +
126-
"ID INT NOT NULL," +
127-
"NAME VARCHAR(40) NOT NULL," +
128-
"SCORE REAL," +
129-
"GRADUATED BOOLEAN," +
130-
"NOT_IMPORTED VARCHAR(30)," +
131-
"BYTEINT_COL BYTEINT," +
132-
"SMALLINT_COL SMALLINT," +
133-
"INTEGER_COL INTEGER," +
134-
"BIGINT_COL BIGINT," +
135-
"CHAR_COL CHARACTER(40)," +
136-
"VARCHAR_COL CHARACTER VARYING(40)," +
137-
"NCHAR_COL NATIONAL CHARACTER(40)," +
138-
"DATE_COL DATE," +
139-
"TIME_COL TIME," +
140-
"TIME_WITH_TIME_ZONE_COL TIME WITH TIME ZONE," +
141-
"TIMESTAMP_COL TIMESTAMP," +
142-
"INTERVAL_COL INTERVAL," +
143-
"DOUBLE_PRECISION_COL DOUBLE PRECISION," +
144-
"NUMERIC_COL NUMERIC(" + PRECISION + "," + SCALE + ")," +
145-
"NVARCHAR_COL NATIONAL CHARACTER VARYING(40)," +
146-
"REAL_COL REAL," +
147-
"ST_GEOMETRY_COL ST_GEOMETRY(10)," +
148-
"VARBINARY_COL BINARY VARYING(10)," +
149-
"DECIMAL_COL DECIMAL(" + PRECISION + "," + SCALE + ")," +
150-
"FLOAT_COL FLOAT(6))");
151-
stmt.execute("CREATE TABLE MY_DEST_TABLE AS " +
152-
"SELECT * FROM my_table");
153-
stmt.execute("CREATE TABLE your_table AS " +
154-
"SELECT * FROM my_table");
131+
stmt.execute("create table MY_TABLE (INTEGER_COL INTEGER, BYTEINT_COL BYTEINT, SMALLINT_COL SMALLINT, " +
132+
"BIGINT_COL BIGINT, REAL_COL REAL, REAL_FLOAT_COL FLOAT(1), DOUBLE_FLOAT_COL FLOAT(7), " +
133+
"DOUBLE_PRECISION_COL DOUBLE PRECISION, NUMERIC_COL NUMERIC(" + PRECISION + "," + SCALE + "), " +
134+
"DECIMAL_COL DECIMAL(" + PRECISION + "," + SCALE + "), CHAR_COL CHAR(40), VARCHAR_COL VARCHAR(40), " +
135+
"NCHAR_COL NCHAR(40), NVARCHAR_COL NVARCHAR(40), VARBINARY_COL BINARY VARYING(10), " +
136+
"ST_GEOMETRY_COL ST_GEOMETRY(10), DATE_COL DATE, TIME_COL TIME, TIMETZ_COL TIMETZ, TIMESTAMP_COL TIMESTAMP, " +
137+
"INTERVAL_COL INTERVAL, BOOLEAN_COL BOOLEAN)");
138+
139+
stmt.execute("CREATE TABLE MY_DEST_TABLE AS SELECT * FROM my_table");
140+
stmt.execute("CREATE TABLE YOUR_TABLE AS SELECT * FROM my_table");
155141
}
156142
}
157143

158144
protected static void prepareTestData(Connection conn) throws SQLException {
159-
try (
160-
Statement stmt = conn.createStatement();
161-
PreparedStatement pStmt1 =
162-
conn.prepareStatement("INSERT INTO my_table " +
163-
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?," +
164-
" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?," +
165-
" ?, ?, ?, ?, ?)");
166-
PreparedStatement pStmt2 =
167-
conn.prepareStatement("INSERT INTO your_table " +
168-
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?," +
169-
" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?," +
170-
" ?, ?, ?, ?, ?)")) {
171-
145+
try (Statement stmt = conn.createStatement()) {
172146
stmt.execute("insert into db_action_test values (1, '1970-01-01')");
173147
stmt.execute("insert into post_action_test values (1, '1970-01-01')");
174148

175-
populateData(pStmt1, pStmt2);
176-
}
177-
}
178-
179-
private static void populateData(PreparedStatement ...stmts) throws SQLException {
180-
// insert the same data into both tables: my_table and your_table
181-
for (PreparedStatement pStmt : stmts) {
182-
for (int i = 1; i <= 5; i++) {
183-
String name = "user" + i;
184-
pStmt.setInt(1, i);
185-
pStmt.setString(2, name);
186-
pStmt.setDouble(3, 123.45 + i);
187-
pStmt.setBoolean(4, (i % 2 == 0));
188-
pStmt.setString(5, "random" + i);
189-
pStmt.setShort(6, (short) i);
190-
pStmt.setShort(7, (short) i);
191-
pStmt.setInt(8, i);
192-
pStmt.setLong(9, (long) i);
193-
pStmt.setString(10, name);
194-
pStmt.setString(11, name);
195-
pStmt.setString(12, name);
196-
pStmt.setDate(13, new Date(CURRENT_TS));
197-
pStmt.setTime(14, new Time(CURRENT_TS));
198-
pStmt.setTime(15, new Time(CURRENT_TS));
199-
pStmt.setTimestamp(16, new Timestamp(CURRENT_TS));
200-
pStmt.setString(17, "2 year 3 month " + i + " day");
201-
pStmt.setDouble(18, 123.45 + i);
202-
pStmt.setBigDecimal(19, new BigDecimal(123.45).add(new BigDecimal(i)));
203-
pStmt.setString(20, name);
204-
pStmt.setDouble(21, 123.45 + i);
205-
pStmt.setBytes(22, name.getBytes(Charsets.UTF_8));
206-
pStmt.setBytes(23, name.getBytes(Charsets.UTF_8));
207-
pStmt.setBigDecimal(24, new BigDecimal(123.45).add(new BigDecimal(i)));
208-
pStmt.setFloat(25, (float) 123.45 + i);
209-
210-
pStmt.executeUpdate();
149+
for (String tableName : Arrays.asList("MY_TABLE", "YOUR_TABLE")) {
150+
for (int i = 1; i <= 5; i++) {
151+
String name = "user" + i;
152+
try (PreparedStatement pStmt = conn.prepareStatement("insert into " + tableName + " values(" +
153+
"?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
154+
pStmt.setString(11, name);
155+
pStmt.setString(12, name);
156+
pStmt.setString(13, name);
157+
pStmt.setString(14, name);
158+
pStmt.setBytes(15, name.getBytes(Charsets.UTF_8));
159+
pStmt.setBytes(16, name.getBytes(Charsets.UTF_8));
160+
161+
pStmt.setInt(1, i);
162+
pStmt.setInt(2, i);
163+
pStmt.setInt(3, i);
164+
pStmt.setLong(4, i);
165+
166+
pStmt.setFloat(5, 123.45f + i);
167+
pStmt.setFloat(6, 123.45f + i);
168+
pStmt.setDouble(7, 123.45 + i);
169+
pStmt.setDouble(8, 123.45 + i);
170+
pStmt.setBigDecimal(
171+
9,
172+
new BigDecimal(123.45, MathContext.DECIMAL64).add(new BigDecimal(i, MathContext.DECIMAL64))
173+
);
174+
pStmt.setBigDecimal(
175+
10,
176+
new BigDecimal(123.45, MathContext.DECIMAL64).add(new BigDecimal(i, MathContext.DECIMAL64))
177+
);
178+
pStmt.setDate(17, new Date(CURRENT_TS));
179+
pStmt.setTime(18, new Time(CURRENT_TS));
180+
pStmt.setString(19, "13:24:16+03");
181+
pStmt.setTimestamp(20, new Timestamp(CURRENT_TS));
182+
pStmt.setString(21, "2 year 3 month " + i + " day");
183+
// other
184+
pStmt.setBoolean(22, (i % 2 == 0));
185+
186+
pStmt.execute();
187+
}
188+
}
211189
}
212190
}
213191
}
@@ -216,7 +194,7 @@ public static Connection createConnection() {
216194
try {
217195
Class.forName(Driver.class.getCanonicalName());
218196
return DriverManager.getConnection(connectionUrl, BASE_PROPS.get(ConnectionConfig.USER),
219-
BASE_PROPS.get(ConnectionConfig.PASSWORD));
197+
BASE_PROPS.get(ConnectionConfig.PASSWORD));
220198
} catch (Exception e) {
221199
throw Throwables.propagate(e);
222200
}

0 commit comments

Comments
 (0)