Skip to content
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

Fix NPE when initializing connection #685

Merged
merged 2 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ private void initConnection(ClickHouseProperties properties) throws SQLException
}

serverTimeZone = TimeZone.getTimeZone("UTC"); // just for next query
timezone = serverTimeZone;
serverVersion = "";

try (Statement s = createStatement(); ResultSet rs = s.executeQuery("select timezone(), version()")) {
if (rs.next()) {
serverTimeZone = TimeZone.getTimeZone(rs.getString(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
public final class ClickHouseVersionNumberUtil {

private static final Pattern VERSION_NUMBER_PATTERN =
Pattern.compile("^\\s*(\\d+)\\.(\\d+).*");
private static final Pattern VERSION_NUMBER_PATTERN = Pattern.compile("^\\s*(\\d+)\\.(\\d+).*");
private static final Pattern NON_NUMBERIC_PATTERN = Pattern.compile("[^0-9.]");

public static int getMajorVersion(String versionNumber) {
Matcher m = VERSION_NUMBER_PATTERN.matcher(versionNumber);
Expand All @@ -22,5 +22,36 @@ public static int getMinorVersion(String versionNumber) {
return m.matches() ? Integer.parseInt(m.group(2)) : 0;
}

private ClickHouseVersionNumberUtil() { /* do not instantiate util */ }
public static int compare(String currentVersion, String targetVersion) {
if (currentVersion == null || targetVersion == null || currentVersion.isEmpty() || targetVersion.isEmpty()) {
throw new IllegalArgumentException("Both version cannot be null or empty");
}

currentVersion = NON_NUMBERIC_PATTERN.matcher(currentVersion).replaceAll("");
targetVersion = NON_NUMBERIC_PATTERN.matcher(targetVersion).replaceAll("");
if (currentVersion.equals(targetVersion)) {
return 0;
}

String[] v1 = currentVersion.split("\\.");
String[] v2 = targetVersion.split("\\.");

int result = 0;
for (int i = 0, len = Math.min(v1.length, v2.length); i < len; i++) {
int n1 = Integer.parseInt(v1[i]);
int n2 = Integer.parseInt(v2[i]);

if (n1 == n2) {
continue;
} else {
result = n1 > n2 ? 1 : -1;
break;
}
}

return result;
}

private ClickHouseVersionNumberUtil() {
/* do not instantiate util */ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import ru.yandex.clickhouse.except.ClickHouseException;
import ru.yandex.clickhouse.settings.ClickHouseProperties;
import ru.yandex.clickhouse.settings.ClickHouseQueryParam;
import ru.yandex.clickhouse.util.ClickHouseVersionNumberUtil;

public class ClickHouseLargeNumberTest {
private Connection conn;
private ClickHouseConnection conn;

@BeforeTest
public void setUp() throws Exception {
Expand Down Expand Up @@ -57,10 +58,10 @@ public void tearDown() throws Exception {

@Test
public void testBigIntSupport() throws SQLException {
if (conn == null) {
if (conn == null || ClickHouseVersionNumberUtil.compare(conn.getServerVersion(), "21.7") >= 0) {
return;
}

String testSql = "create table if not exists system.test_bigint_support(i Int256) engine=Memory;"
+ "drop table if exists system.test_bigint_support;";
try (Connection conn = ClickHouseContainerForTest.newDataSource().getConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ public void testMajorNull() {
try {
ClickHouseVersionNumberUtil.getMajorVersion(null);
Assert.fail();
} catch (NullPointerException npe) { /* expected */ }
} catch (NullPointerException npe) {
/* expected */ }
}

@Test
public void testMinorNull() {
try {
ClickHouseVersionNumberUtil.getMinorVersion(null);
Assert.fail();
} catch (NullPointerException npe) { /* expected */ }
} catch (NullPointerException npe) {
/* expected */ }
}

@Test
Expand Down Expand Up @@ -62,4 +64,15 @@ public void testMinorSimple() {
Assert.assertEquals(ClickHouseVersionNumberUtil.getMinorVersion("1.1-SNAPSHOT"), 1);
}

@Test
public void testCompare() {
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("1", "1"), 0);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("21.3", "21.12"), -1);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("21 .3", "21. 12"), -1);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("21 .3", "21. 12-test"), -1);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("21.3", "21.1"), 1);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("21. 3", "21 .1"), 1);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("21. 3-test", "21 .1"), 1);
Assert.assertEquals(ClickHouseVersionNumberUtil.compare("18.16", "18.16.0"), 0);
}
}