Skip to content

Commit

Permalink
Convert string to primitive array
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Feb 6, 2022
1 parent 2f10655 commit cd2dc12
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

Expand Down Expand Up @@ -1066,6 +1067,91 @@ public static int readEnumValues(String args, int startIndex, int len, Map<Strin
return len;
}

public static List<String> readValueArray(String args, int startIndex, int len) {
List<String> list = new LinkedList<>();
readValueArray(args, startIndex, len, list::add);
return list.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(list);
}

public static int readValueArray(String args, int startIndex, int len, Consumer<String> func) {
char closeBracket = ']';
StringBuilder builder = new StringBuilder();
for (int i = startIndex; i < len; i++) {
char ch = args.charAt(i);
if (ch == '[') {
startIndex = i + 1;
break;
} else if (Character.isWhitespace(ch)) {
continue;
} else if (i + 1 < len) {
char nextCh = args.charAt(i + 1);
if (ch == '-' && nextCh == '-') {
i = skipSingleLineComment(args, i + 2, len) - 1;
} else if (ch == '/' && nextCh == '*') {
i = skipMultiLineComment(args, i + 2, len) - 1;
} else {
startIndex = i;
break;
}
} else {
startIndex = i;
break;
}
}

boolean hasNext = false;
for (int i = startIndex; i < len; i++) {
char ch = args.charAt(i);
if (Character.isWhitespace(ch)) {
continue;
} else if (ch == '\'') { // string
hasNext = false;
int endIndex = readNameOrQuotedString(args, i, len, builder);
func.accept(unescape(args.substring(i, endIndex)));
builder.setLength(0);
i = endIndex + 1;
} else if (ch == '[') { // array
hasNext = false;
int endIndex = skipContentsUntil(args, i + 1, len, ']');
func.accept(args.substring(i, endIndex));
builder.setLength(0);
i = endIndex;
} else if (ch == '(') { // tuple
hasNext = false;
int endIndex = skipContentsUntil(args, i + 1, len, ')');
func.accept(args.substring(i, endIndex));
builder.setLength(0);
i = endIndex;
} else if (ch == closeBracket) {
len = i + 1;
break;
} else if (ch == ',') {
hasNext = true;
String str = builder.toString();
func.accept(str.isEmpty() || ClickHouseValues.NULL_EXPR.equalsIgnoreCase(str) ? null : str);
builder.setLength(0);
} else if (i + 1 < len) {
char nextCh = args.charAt(i + 1);
if (ch == '-' && nextCh == '-') {
i = skipSingleLineComment(args, i + 2, len) - 1;
} else if (ch == '/' && nextCh == '*') {
i = skipMultiLineComment(args, i + 2, len) - 1;
} else {
builder.append(ch);
}
} else {
builder.append(ch);
}
}

if (hasNext || builder.length() > 0) {
String str = builder.toString();
func.accept(str.isEmpty() || ClickHouseValues.NULL_EXPR.equalsIgnoreCase(str) ? null : str);
}

return len;
}

public static int readParameters(String args, int startIndex, int len, List<String> params) {
char closeBracket = ')'; // startIndex points to the openning bracket
Deque<Character> stack = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ public static ClickHouseVersion of(String version) {
return versionCache.get(version);
}

/**
* Creates a new version object using given numbers.
*
* @param yearOrMajor year or major vrsion
* @param more more version numbers if any
* @return version
*/
public static ClickHouseVersion of(int yearOrMajor, int... more) {
int len = more != null ? more.length : 0;
return new ClickHouseVersion(false, yearOrMajor, len > 0 ? more[0] : 0, len > 1 ? more[1] : 0,
len > 2 ? more[2] : 0);
}

/**
* Parses given version without caching.
*
Expand Down Expand Up @@ -460,7 +473,7 @@ public boolean check(String range) {
break;
}

result = nextCh == ')' ? isOlderThan(v2) : isOlderOrEqualTo(v2);
result = nextCh == ')' ? (latest || isOlderThan(v2)) : isOlderOrEqualTo(v2);
if (!result) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseObjectValue;
Expand Down Expand Up @@ -403,7 +404,22 @@ public ClickHouseByteArrayValue update(Map<?, ?> value) {

@Override
public ClickHouseByteArrayValue update(String value) {
return set(new byte[] { Byte.parseByte(value) });
if (ClickHouseChecker.isNullOrBlank(value)) {
set(ClickHouseValues.EMPTY_BYTE_ARRAY);
} else {
List<String> list = ClickHouseUtils.readValueArray(value, 0, value.length());
if (list.isEmpty()) {
set(ClickHouseValues.EMPTY_BYTE_ARRAY);
} else {
byte[] arr = new byte[list.size()];
int index = 0;
for (String v : list) {
arr[index++] = Byte.parseByte(v);
}
set(arr);
}
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseObjectValue;
Expand Down Expand Up @@ -399,7 +400,22 @@ public ClickHouseDoubleArrayValue update(Map<?, ?> value) {

@Override
public ClickHouseDoubleArrayValue update(String value) {
return set(new double[] { Double.parseDouble(value) });
if (ClickHouseChecker.isNullOrBlank(value)) {
set(ClickHouseValues.EMPTY_DOUBLE_ARRAY);
} else {
List<String> list = ClickHouseUtils.readValueArray(value, 0, value.length());
if (list.isEmpty()) {
set(ClickHouseValues.EMPTY_DOUBLE_ARRAY);
} else {
double[] arr = new double[list.size()];
int index = 0;
for (String v : list) {
arr[index++] = Double.parseDouble(v);
}
set(arr);
}
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseObjectValue;
Expand Down Expand Up @@ -399,7 +400,22 @@ public ClickHouseFloatArrayValue update(Map<?, ?> value) {

@Override
public ClickHouseFloatArrayValue update(String value) {
return set(new float[] { Float.parseFloat(value) });
if (ClickHouseChecker.isNullOrBlank(value)) {
set(ClickHouseValues.EMPTY_FLOAT_ARRAY);
} else {
List<String> list = ClickHouseUtils.readValueArray(value, 0, value.length());
if (list.isEmpty()) {
set(ClickHouseValues.EMPTY_FLOAT_ARRAY);
} else {
float[] arr = new float[list.size()];
int index = 0;
for (String v : list) {
arr[index++] = Float.parseFloat(v);
}
set(arr);
}
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseObjectValue;
Expand Down Expand Up @@ -399,7 +400,22 @@ public ClickHouseIntArrayValue update(Map<?, ?> value) {

@Override
public ClickHouseIntArrayValue update(String value) {
return set(new int[] { Integer.parseInt(value) });
if (ClickHouseChecker.isNullOrBlank(value)) {
set(ClickHouseValues.EMPTY_INT_ARRAY);
} else {
List<String> list = ClickHouseUtils.readValueArray(value, 0, value.length());
if (list.isEmpty()) {
set(ClickHouseValues.EMPTY_INT_ARRAY);
} else {
int[] arr = new int[list.size()];
int index = 0;
for (String v : list) {
arr[index++] = Integer.parseInt(v);
}
set(arr);
}
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseObjectValue;
Expand Down Expand Up @@ -399,7 +400,22 @@ public ClickHouseLongArrayValue update(Map<?, ?> value) {

@Override
public ClickHouseLongArrayValue update(String value) {
return set(new long[] { Long.parseLong(value) });
if (ClickHouseChecker.isNullOrBlank(value)) {
set(ClickHouseValues.EMPTY_LONG_ARRAY);
} else {
List<String> list = ClickHouseUtils.readValueArray(value, 0, value.length());
if (list.isEmpty()) {
set(ClickHouseValues.EMPTY_LONG_ARRAY);
} else {
long[] arr = new long[list.size()];
int index = 0;
for (String v : list) {
arr[index++] = Long.parseLong(v);
}
set(arr);
}
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseObjectValue;
Expand Down Expand Up @@ -399,7 +400,22 @@ public ClickHouseShortArrayValue update(Map<?, ?> value) {

@Override
public ClickHouseShortArrayValue update(String value) {
return set(new short[] { Short.parseShort(value) });
if (ClickHouseChecker.isNullOrBlank(value)) {
set(ClickHouseValues.EMPTY_SHORT_ARRAY);
} else {
List<String> list = ClickHouseUtils.readValueArray(value, 0, value.length());
if (list.isEmpty()) {
set(ClickHouseValues.EMPTY_SHORT_ARRAY);
} else {
short[] arr = new short[list.size()];
int index = 0;
for (String v : list) {
arr[index++] = Short.parseShort(v);
}
set(arr);
}
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ public void testProbe() {
// FIXME does not support ClickHouseProtocol.POSTGRESQL for now
ClickHouseProtocol[] protocols = new ClickHouseProtocol[] { ClickHouseProtocol.GRPC, ClickHouseProtocol.HTTP,
ClickHouseProtocol.MYSQL, ClickHouseProtocol.TCP };

ClickHouseVersion serverVersion = ClickHouseVersion.of(System.getProperty("clickhouseVersion", "latest"));
for (ClickHouseProtocol p : protocols) {
if (p == ClickHouseProtocol.GRPC && !serverVersion.check("[21.1,)")) {
continue;
}
ClickHouseNode node = getServer(ClickHouseProtocol.ANY, p.getDefaultPort());
ClickHouseNode probedNode = ClickHouseCluster.probe(node);
Assert.assertNotEquals(probedNode, node);
Expand Down
Loading

0 comments on commit cd2dc12

Please sign in to comment.