-
Notifications
You must be signed in to change notification settings - Fork 614
[JDBC-V2, Client-V2] Fixes data type conversion to string #2580
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
61d2b5f
added data type converter and fixed problem with IP address as string
chernser 3d1e3ad
Merge branch 'main' into fix_ip_address_to_string
chernser 9f7be1d
Merge branch 'main' into fix_ip_address_to_string
chernser 4e6a6bd
implemnted base collection writer to iterate over nested arrays and c…
chernser 0b5dddb
added more tests for string conversion
chernser e78db38
fixed converter for dynamic types and make tool to enquote lists
chernser e1cd563
fix sql.Time?
chernser 9cb45a5
fixed minor issues
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
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
218 changes: 218 additions & 0 deletions
218
client-v2/src/main/java/com/clickhouse/client/api/internal/BaseCollectionConverter.java
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 |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| package com.clickhouse.client.api.internal; | ||
|
|
||
| import com.clickhouse.client.api.ClickHouseException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Array; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.List; | ||
|
|
||
| public abstract class BaseCollectionConverter<ACC, LIST> { | ||
| public static final String ARRAY_START = "["; | ||
| public static final String ARRAY_END = "]"; | ||
|
|
||
| private final String itemDelimiter; | ||
|
|
||
| protected BaseCollectionConverter(String itemDelimiter) { | ||
| this.itemDelimiter = itemDelimiter; | ||
| } | ||
|
|
||
| protected abstract void setAccumulator(ACC acc); | ||
|
|
||
| protected abstract void append(String str); | ||
|
|
||
| protected abstract String buildString(); | ||
|
|
||
| protected abstract void onStart(ListConversionState<LIST> state); | ||
|
|
||
| protected abstract void onEnd(ListConversionState<LIST> state); | ||
|
|
||
| protected abstract void onItem(Object item, ListConversionState<LIST> state); | ||
|
|
||
| protected abstract String onEmptyCollection(); | ||
|
|
||
| protected abstract boolean isEmpty(LIST list); | ||
|
|
||
| protected abstract boolean isSubList(Object list); | ||
|
|
||
| protected abstract int listSize(LIST list); | ||
|
|
||
| protected abstract Object getNext(ListConversionState<LIST> state); | ||
|
|
||
| public final String convert(LIST value, ACC acc) { | ||
| if (isEmpty(value)) { | ||
| return onEmptyCollection(); | ||
| } | ||
| setAccumulator(acc); | ||
|
|
||
| Deque<ListConversionState<LIST>> stack = new ArrayDeque<>(); | ||
| ListConversionState<LIST> state = new ListConversionState<>(value, listSize(value)); | ||
| while (state != null) { | ||
| if (state.isFirst()) { | ||
| onStart(state); | ||
| } | ||
| if (state.hasNext()) { | ||
| Object item = getNext(state); | ||
| state.incPos(); | ||
| if (isSubList(item)) { | ||
| stack.push(state); | ||
| LIST list = (LIST) item; | ||
| state = new ListConversionState<>(list, listSize(list)); | ||
| } else { | ||
| onItem(item, state); | ||
| if (state.hasNext()) { | ||
| append(itemDelimiter); | ||
| } | ||
| } | ||
| } else { | ||
| onEnd(state); | ||
| state = stack.isEmpty() ? null : stack.pop(); | ||
| if (state != null && state.hasNext()) { | ||
| append(itemDelimiter); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return buildString(); | ||
| } | ||
|
|
||
| public static final class ListConversionState<LIST> { | ||
|
|
||
| final LIST list; | ||
| int position; | ||
| int size; | ||
|
|
||
| private ListConversionState(LIST list, int size) { | ||
| this.list = list; | ||
| this.position = 0; | ||
| this.size = size; | ||
| } | ||
|
|
||
| public LIST getList() { | ||
| return list; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public void incPos() { | ||
| this.position++; | ||
| } | ||
|
|
||
| public boolean hasNext() { | ||
| return position < size; | ||
| } | ||
|
|
||
| public boolean isFirst() { | ||
| return position == 0; | ||
| } | ||
| } | ||
|
|
||
| public abstract static class BaseArrayWriter extends BaseCollectionWriter<Object> { | ||
|
|
||
| protected BaseArrayWriter() { | ||
| super(", "); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean isEmpty(Object objects) { | ||
| return listSize(objects) == 0; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean isSubList(Object list) { | ||
| return list != null && list.getClass().isArray(); | ||
| } | ||
|
|
||
| @Override | ||
| protected int listSize(Object objects) { | ||
| return Array.getLength(objects); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object getNext(ListConversionState<Object> state) { | ||
| return Array.get(state.getList(), state.getPosition()); | ||
| } | ||
| } | ||
|
|
||
| public abstract static class BaseListWriter | ||
| extends BaseCollectionWriter<List<?>> { | ||
| protected BaseListWriter() { | ||
| super(", "); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean isEmpty(List<?> objects) { | ||
| return objects.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean isSubList(Object list) { | ||
| return list instanceof List<?>; | ||
| } | ||
|
|
||
| @Override | ||
| protected int listSize(List<?> objects) { | ||
| return objects.size(); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object getNext(ListConversionState<List<?>> state) { | ||
| return state.getList().get(state.getPosition()); | ||
| } | ||
| } | ||
|
|
||
| public abstract static class BaseCollectionWriter<T> extends | ||
| BaseCollectionConverter<Appendable, T> { | ||
|
|
||
| protected Appendable appendable; | ||
|
|
||
| protected BaseCollectionWriter(String itemDelimiter) { | ||
| super(itemDelimiter); | ||
| } | ||
|
|
||
| @Override | ||
| protected void setAccumulator(Appendable appendable) { | ||
| this.appendable = appendable; | ||
| } | ||
|
|
||
| @Override | ||
| protected void append(String str) { | ||
| try { | ||
| appendable.append(str); | ||
| } catch (IOException e) { | ||
| throw new ClickHouseException(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected String buildString() { | ||
| return appendable.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onStart(ListConversionState<T> state) { | ||
| try { | ||
| appendable.append(ARRAY_START); | ||
| } catch (IOException e) { | ||
| throw new ClickHouseException(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void onEnd(ListConversionState<T> state) { | ||
| try { | ||
| appendable.append(ARRAY_END); | ||
| } catch (IOException e) { | ||
| throw new ClickHouseException(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected String onEmptyCollection() { | ||
| return ARRAY_START + ARRAY_END; | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.