Skip to content

Commit

Permalink
Merge branch 'main' into fix_password_authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
chernser committed Oct 31, 2024
2 parents fcdda34 + 714dcf8 commit 8533f46
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;

import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.*;
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseClient;
Expand Down Expand Up @@ -49,6 +42,32 @@ public class ClickHouseDriver implements Driver {

static final java.util.logging.Logger parentLogger = java.util.logging.Logger.getLogger("com.clickhouse.jdbc");

public static String frameworksDetected = null;

public static class FrameworksDetection {
private static final List<String> FRAMEWORKS_TO_DETECT = Arrays.asList("apache.spark");
static volatile String frameworksDetected = null;

private FrameworksDetection() {
}
public static String getFrameworksDetected() {
if (frameworksDetected == null) {
Set<String> inferredFrameworks = new LinkedHashSet<>();
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
for (String framework : FRAMEWORKS_TO_DETECT) {
if (ste.toString().contains(framework)) {
inferredFrameworks.add(String.format("(%s)", framework));
}
}
}

frameworksDetected = String.join("; ", inferredFrameworks);
}
return frameworksDetected;
}

}

static {
String str = ClickHouseDriver.class.getPackage().getImplementationVersion();
if (str != null && !str.isEmpty()) {
Expand Down Expand Up @@ -115,7 +134,6 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Properties pro
options.put(o, ClickHouseOption.fromString(e.getValue().toString(), o.getValueType()));
}
}

return options;
}

Expand All @@ -128,7 +146,7 @@ private DriverPropertyInfo create(ClickHouseOption option, Properties props) {

Class<?> clazz = option.getValueType();
if (Boolean.class == clazz || boolean.class == clazz) {
propInfo.choices = new String[] { "true", "false" };
propInfo.choices = new String[]{"true", "false"};
} else if (clazz.isEnum()) {
Object[] values = clazz.getEnumConstants();
String[] names = new String[values.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,11 @@ public ClickHouseConnectionImpl(String url, Properties properties) throws SQLExc
public ClickHouseConnectionImpl(ConnectionInfo connInfo) throws SQLException {
Properties props = connInfo.getProperties();
jvmTimeZone = TimeZone.getDefault();

if (props.get("disable_frameworks_detection") == null || !props.get("disable_frameworks_detection").toString().equalsIgnoreCase("true")) {
ClickHouseDriver.frameworksDetected = ClickHouseDriver.FrameworksDetection.getFrameworksDetected();
if (ClickHouseDriver.frameworksDetected != null)
props.setProperty(ClickHouseClientOption.PRODUCT_NAME.getKey(), props.getProperty(ClickHouseClientOption.PRODUCT_NAME.getKey()) + ClickHouseDriver.frameworksDetected);
}
ClickHouseClientBuilder clientBuilder = ClickHouseClient.builder()
.options(ClickHouseDriver.toClientOptions(props))
.defaultCredentials(connInfo.getDefaultCredentials());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Map<String, Object> getAllSettings() {
* @return
*/
public InsertSettings setDeduplicationToken(String token) {
rawSettings.put("insert_deduplication_token", token);
serverSetting("insert_deduplication_token", token);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,33 @@ public void testLogComment(String logComment) throws Exception {
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment == null ? "" : logComment);
}

@Test(groups = { "integration" })
public void testInsertSettingsDeduplicationToken() throws Exception {
final String tableName = "insert_settings_database_test";
final String createTableSQL = "CREATE TABLE " + tableName + " ( A Int64 ) ENGINE = MergeTree ORDER BY A SETTINGS " +
"non_replicated_deduplication_window = 100";
final String deduplicationToken = RandomStringUtils.randomAlphabetic(36);

dropTable(tableName);
createTable(createTableSQL);

InsertSettings insertSettings = settings.setInputStreamCopyBufferSize(8198 * 2)
.setDeduplicationToken(deduplicationToken);

for (int i = 0; i < 3; ++i) {
ByteArrayOutputStream data = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(data);
writer.printf("%d\n", i);
writer.flush();
InsertResponse response = client.insert(tableName, new ByteArrayInputStream(data.toByteArray()), ClickHouseFormat.TSV, insertSettings)
.get(30, TimeUnit.SECONDS);
response.close();
}

List<GenericRecord> records = client.queryAll("SELECT * FROM " + tableName);
assertEquals(records.size(), 1);
}

@DataProvider( name = "logCommentDataProvider")
public static Object[] logCommentDataProvider() {
return new Object[][] {
Expand Down

0 comments on commit 8533f46

Please sign in to comment.