-
Notifications
You must be signed in to change notification settings - Fork 614
[client-v2, jdbc-v2] Configurable type hinting #2476
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa857ed
Implemeted configuration for default type hint
chernser 0aaaf96
formatted test
chernser fa9f5a1
Merge branch 'main' into jdbc_fix_nested_types
chernser 765524b
fixed tests
chernser 15d30e0
Merge branch 'main' into jdbc_fix_nested_types
chernser bc4c13b
added a test for toKeyValuePairs()
chernser 38991f1
fixed primitive arrays. added more tests
chernser 1661177
fixed array tests
chernser 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package com.clickhouse.client.api; | ||
|
|
||
| import com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader; | ||
| import com.clickhouse.client.api.internal.ClickHouseLZ4OutputStream; | ||
| import com.clickhouse.data.ClickHouseDataType; | ||
| import com.clickhouse.data.ClickHouseFormat; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -9,11 +11,14 @@ | |
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.Map; | ||
| import java.util.TimeZone; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
|
|
@@ -168,6 +173,11 @@ public Object parseValue(String value) { | |
|
|
||
| BINARY_READER_USE_PREALLOCATED_BUFFERS("client_allow_binary_reader_to_reuse_buffers", Boolean.class, "false"), | ||
|
|
||
| /** | ||
| * Defines mapping between ClickHouse data type and target Java type | ||
| * Used by binary readers to convert values into desired Java type. | ||
| */ | ||
| TYPE_HINT_MAPPING("type_hint_mapping", Map.class), | ||
| ; | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ClientConfigProperties.class); | ||
|
|
@@ -278,6 +288,10 @@ public Object parseValue(String value) { | |
| return TimeZone.getTimeZone(value); | ||
| } | ||
|
|
||
| if (valueType.equals(Map.class)) { | ||
| return toKeyValuePairs(value); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
|
|
@@ -301,7 +315,15 @@ public static Map<String, Object> parseConfigMap(Map<String, String> configMap) | |
| for (ClientConfigProperties config : ClientConfigProperties.values()) { | ||
| String value = tmpMap.remove(config.getKey()); | ||
| if (value != null) { | ||
| parsedConfig.put(config.getKey(), config.parseValue(value)); | ||
| Object parsedValue; | ||
| switch (config) { | ||
| case TYPE_HINT_MAPPING: | ||
| parsedValue = translateTypeHintMapping(value); | ||
| break; | ||
| default: | ||
| parsedValue = config.parseValue(value); | ||
| } | ||
| parsedConfig.put(config.getKey(), parsedValue); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -317,4 +339,90 @@ public static Map<String, Object> parseConfigMap(Map<String, String> configMap) | |
|
|
||
| return parsedConfig; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Converts given string to key value pairs. | ||
| * This is very simple implementation that do not handle edge cases like | ||
| * {@code k1=v1, ,k2=v2} | ||
| * | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be great if we have a simple example of the string |
||
| * @param str string | ||
| * @return non-null key value pairs | ||
| */ | ||
| public static Map<String, String> toKeyValuePairs(String str) { | ||
| if (str == null || str.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| Map<String, String> map = new LinkedHashMap<>(); | ||
| String key = null; | ||
| StringBuilder builder = new StringBuilder(); | ||
| for (int i = 0, len = str.length(); i < len; i++) { | ||
| char ch = str.charAt(i); | ||
| if (ch == '\\' && i + 1 < len) { | ||
| ch = str.charAt(++i); | ||
| builder.append(ch); | ||
| continue; | ||
| } | ||
|
|
||
| if (Character.isWhitespace(ch)) { | ||
| if (builder.length() > 0) { | ||
| builder.append(ch); | ||
| } | ||
| } else if (ch == '=' && key == null) { | ||
| key = builder.toString().trim(); | ||
| builder.setLength(0); | ||
| } else if (ch == ',' && key != null) { | ||
| String value = builder.toString().trim(); | ||
| builder.setLength(0); | ||
| if (!key.isEmpty() && !value.isEmpty()) { | ||
| map.put(key, value); | ||
| } | ||
| key = null; | ||
| } else { | ||
| builder.append(ch); | ||
| } | ||
| } | ||
|
|
||
| if (key != null && builder.length() > 0) { | ||
| String value = builder.toString().trim(); | ||
| if (!key.isEmpty() && !value.isEmpty()) { | ||
| map.put(key, value); | ||
| } | ||
| } | ||
|
|
||
| return Collections.unmodifiableMap(map); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public static String mapToString(Map<?,?> map, Function<Object, String> valueConverter) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (Map.Entry<?, ?> entry : map.entrySet()) { | ||
| sb.append(entry.getKey()).append("=").append(valueConverter.apply(entry.getValue())).append(","); | ||
| } | ||
|
|
||
| if (sb.length() > 0) { | ||
| sb.setLength(sb.length() - 1); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| public static Map<ClickHouseDataType, Class<?>> translateTypeHintMapping(String mappingStr) { | ||
| if (mappingStr == null || mappingStr.isEmpty()) { | ||
| return AbstractBinaryFormatReader.NO_TYPE_HINT_MAPPING; | ||
| } | ||
|
|
||
| Map<String, String> mapping= ClientConfigProperties.toKeyValuePairs(mappingStr); | ||
| Map<ClickHouseDataType, Class<?>> hintMapping = new HashMap<>(); | ||
| try { | ||
| for (Map.Entry<String, String> entry : mapping.entrySet()) { | ||
| hintMapping.put(ClickHouseDataType.of(entry.getKey()), | ||
| Class.forName(entry.getValue())); | ||
| } | ||
| } catch (ClassNotFoundException e) { | ||
| throw new ClientMisconfigurationException("Failed to translate type-hint mapping", e); | ||
| } | ||
| return hintMapping; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
The
typeHintMappingfield is initialized in the constructor but there's no null check when passing it to the binary format readers. If a client is created without specifying type hints, this could potentially cause NullPointerExceptions in the binary readers that receive this parameter.