Skip to content
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

Fix the getVersionColumnForReplacingMergeTree() to support replicated tables properly #343

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.altinity.clickhouse.sink.connector.db;

import static com.altinity.clickhouse.sink.connector.db.ClickHouseDbConstants.CHECK_DB_EXISTS_SQL;
import static com.altinity.clickhouse.sink.connector.db.ClickHouseDbConstants.SIGN_COLUMN;
import static com.altinity.clickhouse.sink.connector.db.ClickHouseDbConstants.VERSION_COLUMN;
import com.clickhouse.jdbc.ClickHouseConnection;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.MutablePair;
Expand All @@ -11,6 +13,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DBMetadata {

Expand Down Expand Up @@ -48,7 +51,6 @@ public MutablePair<TABLE_ENGINE, String> getTableEngine(ClickHouseConnection con

MutablePair<TABLE_ENGINE, String> result;
result = getTableEngineUsingSystemTables(conn, databaseName, tableName);

if(result.left == null) {
result = getTableEngineUsingShowTable(conn, databaseName, tableName);
}
Expand Down Expand Up @@ -142,7 +144,7 @@ public MutablePair<TABLE_ENGINE, String> getTableEngineUsingShowTable(ClickHouse
*/
public String getSignColumnForCollapsingMergeTree(String createDML) {

String signColumn = "sign";
String signColumn = SIGN_COLUMN;

if(createDML.contains(TABLE_ENGINE.COLLAPSING_MERGE_TREE.getEngine())) {
signColumn = StringUtils.substringBetween(createDML, COLLAPSING_MERGE_TREE_SIGN_PREFIX, ")");
Expand All @@ -160,23 +162,25 @@ public String getSignColumnForCollapsingMergeTree(String createDML) {
*/
public String getVersionColumnForReplacingMergeTree(String createDML) {

String versionColumn = "ver";
String versionColumn = VERSION_COLUMN;

if(createDML.contains(TABLE_ENGINE.REPLICATED_REPLACING_MERGE_TREE.getEngine())) {
String parameters = StringUtils.substringBetween(createDML, REPLICATED_REPLACING_MERGE_TREE_VER_PREFIX, ")");
if(parameters != null) {
if (createDML.contains(TABLE_ENGINE.REPLACING_MERGE_TREE.getEngine())) {
String parameters = StringUtils.substringBetween(createDML, REPLACING_MERGE_TREE_VER_PREFIX, ")");
if (parameters != null) {
String[] parameterArray = parameters.split(",");
if(parameterArray != null && parameterArray.length >= 3) {
versionColumn = parameterArray[2].trim();
}
}
}
else if(createDML.contains(TABLE_ENGINE.REPLACING_MERGE_TREE.getEngine())) {
if(createDML != null && createDML.indexOf("(") != -1 && createDML.indexOf(")") != -1) {
String subString = StringUtils.substringBetween(createDML, REPLACING_MERGE_TREE_VER_PREFIX, ")");
if(subString != null) {
versionColumn = subString.trim();
if (parameterArray != null && parameterArray.length > 2) {
// if more than 2 params => it's replicated table
ArrayList<String> rmtColumns = new ArrayList<>();
// Start from third element, because first and second are {shard}, {replica}
for (int i=2; i<parameterArray.length; i++) {
rmtColumns.add(parameterArray[i].trim());
}
versionColumn = String.join(",", rmtColumns);
} else {
versionColumn = String.join(",", parameterArray);
}
} else {
log.error("Error: Could not retrieve params from ReplacingMergeTree");
}
} else {
log.error("Error: Trying to retrieve ver from table that is not ReplacingMergeTree");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void testDefaultGetSignColumnForCollapsingMergeTree() {

String createTableDML = "ReplacingMergeTree() PRIMARY KEY productCode ORDER BY productCode SETTINGS index_granularity = 8192";
String signColumn = metadata.getSignColumnForCollapsingMergeTree(createTableDML);

Assert.assertTrue(signColumn.equalsIgnoreCase("sign"));
Assert.assertTrue(signColumn.equalsIgnoreCase("_sign"));
}

@Test
Expand All @@ -51,6 +50,11 @@ public void testGetVersionColumnForReplacingMergeTree() {

Assert.assertTrue(signColumn.equalsIgnoreCase("versionNo"));

String createTableDMLisDeleted = "ReplacingMergeTree(versionNo, is_deleted) PRIMARY KEY productCode ORDER BY productCode SETTINGS index_granularity = 8192";
String IsDeletedColumn = metadata.getVersionColumnForReplacingMergeTree(createTableDMLisDeleted);

Assert.assertTrue(IsDeletedColumn.equalsIgnoreCase("versionNo, is_deleted"));

}

@Test
Expand Down Expand Up @@ -94,6 +98,14 @@ public void testGetEngineFromResponse() {
Assert.assertTrue(replicatedReplacingMergeTreeResult.getLeft().getEngine().equalsIgnoreCase(DBMetadata.TABLE_ENGINE.REPLACING_MERGE_TREE.getEngine()));


String replicatedReplacingMergeTreeIsDeleted = "ReplicatedReplacingMergeTree('/clickhouse/{cluster}/tables/dashboard_mysql_replication/favourite_products', '{replica}', ver, deleted) ORDER BY id SETTINGS allow_nullable_key = 1, index_granularity = 8192";

MutablePair<DBMetadata.TABLE_ENGINE, String> replicatedReplacingMergeTreeIsDeletedResult = new DBMetadata().getEngineFromResponse(replicatedReplacingMergeTreeIsDeleted);

Assert.assertTrue(replicatedReplacingMergeTreeIsDeletedResult.getRight().equalsIgnoreCase("ver,deleted"));
Assert.assertTrue(replicatedReplacingMergeTreeIsDeletedResult.getLeft().getEngine().equalsIgnoreCase(DBMetadata.TABLE_ENGINE.REPLACING_MERGE_TREE.getEngine()));


}

@ParameterizedTest
Expand Down
Loading