Skip to content

Commit

Permalink
Support maven-like version range
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Oct 10, 2021
1 parent 32ca307 commit 19f0c9d
Show file tree
Hide file tree
Showing 16 changed files with 575 additions and 284 deletions.

Large diffs are not rendered by default.

Expand Up @@ -88,7 +88,7 @@ public class ClickHouseServerForTest {
String imageNameWithTag = imageName + imageTag;
String customPackages = ClickHouseUtils.getProperty("additionalPackages", properties);
if (!ClickHouseChecker.isNullOrEmpty(clickhouseVersion)
&& ClickHouseVersion.of(clickhouseVersion).isOlderOrBelongsTo("21.3")) {
&& ClickHouseVersion.check(clickhouseVersion, "(,21.3]")) {
if (ClickHouseChecker.isNullOrEmpty(customPackages)) {
customPackages = "tzdata";
} else if (!customPackages.contains("tzdata")) {
Expand Down

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions clickhouse-jdbc/pom.xml
Expand Up @@ -53,13 +53,17 @@
</properties>

<dependencies>
<!--
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-http-client</artifactId>
<artifactId>clickhouse-client</artifactId>
<version>${revision}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down Expand Up @@ -92,12 +96,6 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-client</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-client</artifactId>
Expand Down
Expand Up @@ -12,12 +12,13 @@
import java.util.Iterator;
import java.util.List;

import com.clickhouse.client.ClickHouseVersion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.yandex.clickhouse.response.ClickHouseColumnInfo;
import ru.yandex.clickhouse.response.ClickHouseResultBuilder;
import ru.yandex.clickhouse.util.ClickHouseVersionNumberUtil;


public class ClickHouseDatabaseMetadata implements DatabaseMetaData {
Expand Down Expand Up @@ -109,7 +110,7 @@ public int getDriverMajorVersion() {
log.warn("Error determining driver major version", sqle);
return 0;
}
return ClickHouseVersionNumberUtil.getMajorVersion(v);
return ClickHouseVersion.of(v).getMajorVersion();
}

@Override
Expand All @@ -121,7 +122,7 @@ public int getDriverMinorVersion() {
log.warn("Error determining driver minor version", sqle);
return 0;
}
return ClickHouseVersionNumberUtil.getMinorVersion(v);
return ClickHouseVersion.of(v).getMinorVersion();
}

@Override
Expand Down Expand Up @@ -801,7 +802,7 @@ private static void buildAndCondition(StringBuilder dest, List<String> condition
@Override
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
StringBuilder query;
if (ClickHouseVersionNumberUtil.compare(connection.getServerVersion(), "18.16") >= 0) {
if (ClickHouseVersion.check(connection.getServerVersion(), "[18.16,)")) {
query = new StringBuilder(
"SELECT database, table, name, type, default_kind as default_type, default_expression, comment ");
} else {
Expand Down Expand Up @@ -1248,14 +1249,12 @@ public int getResultSetHoldability() throws SQLException {

@Override
public int getDatabaseMajorVersion() throws SQLException {
return ClickHouseVersionNumberUtil.getMajorVersion(
connection.getServerVersion());
return ClickHouseVersion.of(connection.getServerVersion()).getMajorVersion();
}

@Override
public int getDatabaseMinorVersion() throws SQLException {
return ClickHouseVersionNumberUtil.getMinorVersion(
connection.getServerVersion());
return ClickHouseVersion.of(connection.getServerVersion()).getMinorVersion();
}

@Override
Expand Down
@@ -1,5 +1,6 @@
package ru.yandex.clickhouse.response;

import com.clickhouse.client.ClickHouseCache;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
Expand All @@ -8,26 +9,20 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.type.TypeFactory;
import ru.yandex.clickhouse.Jackson;
import ru.yandex.clickhouse.util.LRUCache;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

class ArrayToStringDeserializer extends JsonDeserializer<List<String>> {
private static final Map<DeserializationContext, JsonDeserializer<Object>> deserializers = LRUCache.create(1000,
new Function<DeserializationContext, JsonDeserializer<Object>>() {
@Override
public JsonDeserializer<Object> apply(DeserializationContext ctx) {
try {
return ctx.findContextualValueDeserializer(
TypeFactory.defaultInstance().constructType(new TypeReference<List<Object>>() {
}), null);
} catch (JsonMappingException e) {
throw new IllegalStateException(e);
}
private static final ClickHouseCache<DeserializationContext, JsonDeserializer<Object>> deserializers = ClickHouseCache.create(1000, 300,
(ctx) -> {
try {
return ctx.findContextualValueDeserializer(
TypeFactory.defaultInstance().constructType(new TypeReference<List<Object>>() {
}), null);
} catch (JsonMappingException e) {
throw new IllegalStateException(e);
}
});

Expand Down
Expand Up @@ -6,7 +6,12 @@
/**
* Very simple version number parser. It is only needed for ClickHouse driver
* and database version numbers
*
* @deprecated As of release 0.3.2, replaced by
* {@link com.clickhouse.client.ClickHouseVersion} and it will be
* removed in 0.4.0
*/
@Deprecated
public final class ClickHouseVersionNumberUtil {

private static final Pattern VERSION_NUMBER_PATTERN = Pattern.compile("^\\s*(\\d+)\\.(\\d+).*");
Expand Down
Expand Up @@ -6,6 +6,14 @@
import java.util.Objects;
import java.util.function.Function;

/**
* LRU Cache.
*
* @deprecated As of release 0.3.2, replaced by
* {@link com.clickhouse.client.ClickHouseCache} and it will be
* removed in 0.4.0
*/
@Deprecated
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
/**
* Generated serial version UID.
Expand Down
Expand Up @@ -122,9 +122,8 @@ public void testSelectUInt64() throws SQLException {

@Test(groups = "integration")
public void testExternalData() throws SQLException, UnsupportedEncodingException {
String serverVersion = connection.getServerVersion();
ClickHouseStatement stmt = connection.createStatement();
String[] rows = ClickHouseVersion.of(serverVersion).isNewerOrEqualTo("21.3")
String[] rows = ClickHouseVersion.check(connection.getServerVersion(), "[21.3,)")
? new String[] { "1\tGroup\n" }
: new String[] { "1\tGroup", "1\tGroup\n" };

Expand All @@ -150,8 +149,7 @@ public void testExternalData() throws SQLException, UnsupportedEncodingException
// reproduce issue #634
@Test(groups = "integration")
public void testLargeQueryWithExternalData() throws Exception {
String serverVersion = connection.getServerVersion();
String[] rows = ClickHouseVersion.of(serverVersion).isNewerOrEqualTo("21.3")
String[] rows = ClickHouseVersion.check(connection.getServerVersion(), "[21.3)")
? new String[] { "1\tGroup\n" }
: new String[] { "1\tGroup", "1\tGroup\n" };

Expand Down
Expand Up @@ -173,7 +173,7 @@ public void testRoaringBitmap() throws Exception {

@Test(groups = "integration")
public void testRoaringBitmap64() throws Exception {
if (conn == null || ClickHouseVersion.of(conn.getServerVersion()).isOlderOrBelongsTo("20.8")) {
if (conn == null || ClickHouseVersion.check(conn.getServerVersion(), "(,20.8]")) {
return;
}

Expand Down
Expand Up @@ -57,7 +57,7 @@ public void testMetadata() throws Exception {

@Test(groups = "integration")
public void testMetadataColumns() throws Exception {
boolean supportComment = ClickHouseVersion.of(connection.getServerVersion()).isNewerOrEqualTo("18.16");
boolean supportComment = ClickHouseVersion.check(connection.getServerVersion(), "[18.16,)");
connection.createStatement().executeQuery(
"DROP TABLE IF EXISTS testMetadata");
connection.createStatement().executeQuery(
Expand Down Expand Up @@ -158,8 +158,7 @@ public void testToDateTimeTZ() throws Exception {
ResultSetMetaData meta = rs.getMetaData();
Assert.assertEquals(meta.getColumnClassName(1), Timestamp.class.getCanonicalName());
TimeZone timezone = ((ClickHouseConnection) connection).getTimeZone();
ClickHouseVersion version = ClickHouseVersion.of(((ClickHouseConnection) connection).getServerVersion());
if (version.isNewerOrEqualTo("21.6")) {
if (ClickHouseVersion.check(connection.getServerVersion(), "[21.6,)")) {
Assert.assertEquals(meta.getColumnTypeName(1), "DateTime");
} else {
Assert.assertEquals(meta.getColumnTypeName(1), "DateTime('" + timezone.getID() + "')");
Expand Down
Expand Up @@ -59,7 +59,7 @@ public void tearDown() throws Exception {

@Test(groups = "integration")
public void testBigIntSupport() throws SQLException {
if (conn == null || ClickHouseVersion.of(conn.getServerVersion()).isNewerOrEqualTo("21.7")) {
if (conn == null || ClickHouseVersion.check(conn.getServerVersion(), "[21.7,)")) {
return;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public void testDecimal256() throws Exception {
}

// check max scale
if (ClickHouseVersion.of(conn.getServerVersion()).isNewerOrEqualTo("21.9")) {
if (ClickHouseVersion.check(conn.getServerVersion(), "[21.9,)")) {
s.execute("set output_format_decimal_trailing_zeros=1");
}
try (ResultSet rs = s.executeQuery("select d from test_decimal256 order by d")) {
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void testMapSupport() throws SQLException {
+ "drop table if exists system.test_map_support;";
try (ClickHouseConnection conn = newDataSource().getConnection(); Statement s = conn.createStatement()) {
s.execute("set allow_experimental_map_type=0;" + testSql);
if (ClickHouseVersion.of(conn.getServerVersion()).isOlderThan("21.8")) {
if (ClickHouseVersion.check(conn.getServerVersion(), "(,21.8)")) {
fail("Should fail without enabling map support");
}
} catch (SQLException e) {
Expand All @@ -100,7 +100,7 @@ public void testMapSupport() throws SQLException {

params.put(ClickHouseQueryParam.ALLOW_EXPERIMENTAL_MAP_TYPE, "0");
s.executeQuery(testSql, params);
if (ClickHouseVersion.of(conn.getServerVersion()).isOlderThan("21.8")) {
if (ClickHouseVersion.check(conn.getServerVersion(), "(,21.8)")) {
fail("Should fail without enabling map support");
}
} catch (SQLException e) {
Expand All @@ -115,14 +115,14 @@ public void testMaps() throws Exception {
}

ClickHouseVersion version = ClickHouseVersion.of(conn.getServerVersion());
if (version.isOlderOrBelongsTo("21.3")) {
if (version.check("(,21.3]")) {
// https://github.com/ClickHouse/ClickHouse/issues/25026
return;
}
String columns = ", ma Map(Integer, Array(String)), mi Map(Integer, Integer)";
String values = ",{1:['11','12'],2:['22','23']},{1:11,2:22}";
String params = ",?,?";
if (version.isNewerOrEqualTo("21.4") && version.isOlderThan("21.9")) {
if (version.check("[21.4,21.9)")) {
columns = "";
values = "";
params = "";
Expand Down
Expand Up @@ -40,7 +40,7 @@ public void testWrongUser() {
try (Connection connection = newConnection(properties)) {
} catch (Exception e) {
String version = ClickHouseServerForTest.getClickHouseVersion();
if (!version.isEmpty() && ClickHouseVersion.of(version).isOlderOrBelongsTo("19")) {
if (!version.isEmpty() && ClickHouseVersion.check(version, "(,19]")) {
Assert.assertEquals((getClickhouseException(e)).getErrorCode(), 192);
} else {
Assert.assertEquals((getClickhouseException(e)).getErrorCode(), 516);
Expand Down
Expand Up @@ -249,7 +249,7 @@ public void testBitmap() throws Exception {
testBitmap64(32, 0L, 1L);
testBitmap64(32, Long.MAX_VALUE, -1L);

if (ClickHouseVersion.of(connection.getServerVersion()).isBeyond("20.8")) {
if (ClickHouseVersion.check(connection.getServerVersion(), "(20.8,]")) {
testBitmap64(65537, 100000L, 1L); // highToBitmap.size() == 1
testBitmap64(65537, 9223372036854775807L, -1000000000L); // highToBitmap.size() > 1
}
Expand Down
Expand Up @@ -267,8 +267,7 @@ public void ORCInsertCompressedIntoTable() throws SQLException {
// clickhouse-client -q "select number int, toString(number) str, 1/number flt, toDecimal64( 1/(number+1) , 9) dcml,
// toDateTime('2020-01-01 00:00:00') + number time from numbers(100) format ORC"|gzip > test_sample.orc.gz

String version = connection.getServerVersion();
if (ClickHouseVersion.of(version).isOlderThan("20.8")) {
if (ClickHouseVersion.check(connection.getServerVersion(), "(,20.8)")) {
return;
}

Expand Down Expand Up @@ -312,7 +311,7 @@ public void ORCInsertCompressedIntoTable1() throws SQLException {
// clickhouse-client -q "select number int, toString(number) str, 1/number flt, toDecimal64( 1/(number+1) , 9) dcml,
// toDateTime('2020-01-01 00:00:00') + number time from numbers(100) format ORC"|gzip > test_sample.orc.gz

if (ClickHouseVersion.of(connection.getServerVersion()).isOlderThan("20.8")) {
if (ClickHouseVersion.check(connection.getServerVersion(), "(,20.8)")) {
return;
}

Expand Down Expand Up @@ -346,7 +345,7 @@ public void ParquetInsertCompressedIntoTable() throws SQLException {
// clickhouse-client -q "select number int, toString(number) str, 1/number flt, toDecimal64( 1/(number+1) , 9) dcml,
// toDateTime('2020-01-01 00:00:00') + number time from numbers(100) format Parquet"|gzip > test_sample.parquet.gz

if (ClickHouseVersion.of(connection.getServerVersion()).isOlderThan("20.8")) {
if (ClickHouseVersion.check(connection.getServerVersion(), "(,20.8)")) {
return;
}

Expand Down Expand Up @@ -390,7 +389,7 @@ public void ParquetInsertCompressedIntoTable1() throws SQLException {
// clickhouse-client -q "select number int, toString(number) str, 1/number flt, toDecimal64( 1/(number+1) , 9) dcml,
// toDateTime('2020-01-01 00:00:00') + number time from numbers(100) format Parquet"|gzip > test_sample.parquet.gz

if (ClickHouseVersion.of(connection.getServerVersion()).isOlderThan("20.8")) {
if (ClickHouseVersion.check(connection.getServerVersion(), "(,20.8)")) {
return;
}

Expand Down

0 comments on commit 19f0c9d

Please sign in to comment.