-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DBZ-7362 Support DECFLOAT in Db2 connector #129
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/test/java/io/debezium/connector/db2/DatatypesFromSnapshotIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright Debezium Authors. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package io.debezium.connector.db2; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.SQLException; | ||
|
||
import org.apache.kafka.connect.data.Struct; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import io.debezium.config.Configuration; | ||
import io.debezium.connector.db2.Db2ConnectorConfig.SnapshotMode; | ||
import io.debezium.connector.db2.util.TestHelper; | ||
import io.debezium.data.VariableScaleDecimal; | ||
import io.debezium.embedded.AbstractConnectorTest; | ||
import io.debezium.relational.RelationalDatabaseConnectorConfig.DecimalHandlingMode; | ||
import io.debezium.util.Testing; | ||
|
||
/** | ||
* Test fror Db2 datatypes. | ||
* | ||
* @author Jiri Pechanec | ||
*/ | ||
public class DatatypesFromSnapshotIT extends AbstractConnectorTest { | ||
|
||
private Db2Connection connection; | ||
|
||
private static final String[] CREATE_TABLES = { | ||
"CREATE TABLE dt_numeric (id int not null, df decfloat, df16 decfloat(16), df34 decfloat(34), primary key (id))" | ||
}; | ||
private static final String[] INSERT_DATA = { | ||
"INSERT INTO dt_numeric VALUES(1, 1, 3.123456789012345678, 3.012345678901234567890123456789)" | ||
}; | ||
|
||
@Before | ||
public void before() throws SQLException { | ||
connection = TestHelper.testConnection(); | ||
connection.execute("DELETE FROM ASNCDC.IBMSNAP_REGISTER"); | ||
connection.execute("DROP TABLE IF EXISTS dt_numeric"); | ||
connection.execute(CREATE_TABLES); | ||
connection.execute(INSERT_DATA); | ||
|
||
TestHelper.enableTableCdc(connection, "DT_NUMERIC"); | ||
initializeConnectorTestFramework(); | ||
Testing.Files.delete(TestHelper.DB_HISTORY_PATH); | ||
Testing.Print.enable(); | ||
} | ||
|
||
@After | ||
public void after() throws SQLException { | ||
if (connection != null) { | ||
TestHelper.disableDbCdc(connection); | ||
TestHelper.disableTableCdc(connection, "DT_NUMERIC"); | ||
connection.execute("DROP TABLE dt_numeric"); | ||
connection.execute("DELETE FROM ASNCDC.IBMSNAP_REGISTER"); | ||
connection.execute("DELETE FROM ASNCDC.IBMQREP_COLVERSION"); | ||
connection.execute("DELETE FROM ASNCDC.IBMQREP_TABVERSION"); | ||
connection.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void numericTypesPrecise() throws Exception { | ||
final Configuration config = TestHelper.defaultConfig() | ||
.with(Db2ConnectorConfig.SNAPSHOT_MODE, SnapshotMode.INITIAL) | ||
.with(Db2ConnectorConfig.TABLE_INCLUDE_LIST, "db2inst1.dt_numeric") | ||
.build(); | ||
|
||
start(Db2Connector.class, config); | ||
assertConnectorIsRunning(); | ||
|
||
// Wait for snapshot completion | ||
final SourceRecords records = consumeRecordsByTopic(1); | ||
final SourceRecord rec = records.allRecordsInOrder().get(0); | ||
|
||
assertVariableScaleDecimal(((Struct) rec.value()).getStruct("after").get("DF"), new BigDecimal("1")); | ||
// Loss of precision | ||
assertVariableScaleDecimal(((Struct) rec.value()).getStruct("after").get("DF16"), new BigDecimal("3.123456789012346")); | ||
assertVariableScaleDecimal(((Struct) rec.value()).getStruct("after").get("DF34"), new BigDecimal("3.012345678901234567890123456789")); | ||
stopConnector(); | ||
} | ||
|
||
@Test | ||
public void numericTypesDouble() throws Exception { | ||
final Configuration config = TestHelper.defaultConfig() | ||
.with(Db2ConnectorConfig.SNAPSHOT_MODE, SnapshotMode.INITIAL) | ||
.with(Db2ConnectorConfig.TABLE_INCLUDE_LIST, "db2inst1.dt_numeric") | ||
.with(Db2ConnectorConfig.DECIMAL_HANDLING_MODE, DecimalHandlingMode.DOUBLE) | ||
.build(); | ||
|
||
start(Db2Connector.class, config); | ||
assertConnectorIsRunning(); | ||
|
||
// Wait for snapshot completion | ||
final SourceRecords records = consumeRecordsByTopic(1); | ||
final SourceRecord rec = records.allRecordsInOrder().get(0); | ||
|
||
Assertions.assertThat(((Struct) rec.value()).getStruct("after").get("DF")) | ||
.isEqualTo(Double.valueOf(1)); | ||
// Loss of precision | ||
Assertions.assertThat(((Struct) rec.value()).getStruct("after").get("DF16")) | ||
.isEqualTo(Double.valueOf(3.123456789012346)); | ||
Assertions.assertThat(((Struct) rec.value()).getStruct("after").get("DF34")) | ||
.isEqualTo(Double.valueOf(3.012345678901234567890123456789)); | ||
|
||
stopConnector(); | ||
} | ||
|
||
@Test | ||
public void numericTypesString() throws Exception { | ||
final Configuration config = TestHelper.defaultConfig() | ||
.with(Db2ConnectorConfig.SNAPSHOT_MODE, SnapshotMode.INITIAL) | ||
.with(Db2ConnectorConfig.TABLE_INCLUDE_LIST, "db2inst1.dt_numeric") | ||
.with(Db2ConnectorConfig.DECIMAL_HANDLING_MODE, DecimalHandlingMode.STRING) | ||
.build(); | ||
|
||
start(Db2Connector.class, config); | ||
assertConnectorIsRunning(); | ||
|
||
// Wait for snapshot completion | ||
final SourceRecords records = consumeRecordsByTopic(1); | ||
final SourceRecord rec = records.allRecordsInOrder().get(0); | ||
|
||
Assertions.assertThat(((Struct) rec.value()).getStruct("after").get("DF")) | ||
.isEqualTo("1"); | ||
// Loss of precision | ||
Assertions.assertThat(((Struct) rec.value()).getStruct("after").get("DF16")) | ||
.isEqualTo("3.123456789012346"); | ||
Assertions.assertThat(((Struct) rec.value()).getStruct("after").get("DF34")) | ||
.isEqualTo("3.012345678901234567890123456789"); | ||
|
||
stopConnector(); | ||
} | ||
|
||
private void assertVariableScaleDecimal(Object actual, BigDecimal expected) { | ||
final Struct v = (Struct) actual; | ||
Assertions.assertThat(v.get(VariableScaleDecimal.SCALE_FIELD)).isEqualTo(expected.scale()); | ||
Assertions.assertThat(v.get(VariableScaleDecimal.VALUE_FIELD)).isEqualTo(expected.unscaledValue().toByteArray()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not reuse the convertDecimal function? I think the contents of convertDecimal and convertDecfloat are the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, it is not. This is taken form PostgreSQL to hadnle potential issue with trailing zeroes. We don't know if it is problem here but it is kind of defensive measure.