Skip to content

Commit

Permalink
Merge pull request #685 from zhicwu/fix-npe
Browse files Browse the repository at this point in the history
Fix NPE when initializing connection
  • Loading branch information
zhicwu authored Jul 11, 2021
2 parents 989f9f7 + 3dea256 commit 0f20121
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
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);
}
}

0 comments on commit 0f20121

Please sign in to comment.