Skip to content

Commit 2b9dbdf

Browse files
committed
prepare for test compatibility
1 parent db3663c commit 2b9dbdf

12 files changed

Lines changed: 185 additions & 88 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ target/
77
/databend/
88
.databend/
99
tests/data
10+
tests/compatibility/*.jar
11+
test-output
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.databend.jdbc;
2+
3+
import com.vdurmont.semver4j.Semver;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
8+
import java.util.Properties;
9+
10+
public class Compatibility {
11+
public static class Capability {
12+
boolean streamingLoad;
13+
14+
public Capability() {
15+
this.streamingLoad = true;
16+
}
17+
public Capability(boolean streamingLoad) {
18+
this.streamingLoad = streamingLoad;
19+
}
20+
21+
public static Capability fromServerVersion(Semver ver) {
22+
boolean streamingLoad = ver.isGreaterThan(new Semver("1.2.781"));
23+
return new Capability(streamingLoad);
24+
}
25+
26+
public static Capability fromDriverVersion(Semver ver) {
27+
boolean streamingLoad = ver.isGreaterThan(new Semver("0.4.1"));
28+
return new Capability(streamingLoad);
29+
}
30+
}
31+
32+
static String port = System.getenv("DATABEND_TEST_CONN_PORT") != null ? System.getenv("DATABEND_TEST_CONN_PORT").trim() : "8000";
33+
public static Semver driverVersion = getDriverVersion();
34+
public static Semver serverVersion = getServerVersion();
35+
public static Capability driverCapability = driverVersion==null? new Capability(): Capability.fromDriverVersion(driverVersion);
36+
public static Capability serverCapability = serverVersion==null? new Capability(): Capability.fromServerVersion(serverVersion);
37+
38+
private static Semver getDriverVersion() {
39+
String env = System.getenv("DATABEND_TEST_DRIVER_VERSION");
40+
if (env == null) {
41+
return null;
42+
}
43+
return new Semver(env);
44+
}
45+
private static Semver getServerVersion() {
46+
String env = System.getenv("DATABEND_TEST_SERVER_VERSION");
47+
if (env == null) {
48+
return null;
49+
}
50+
return new Semver(env);
51+
}
52+
53+
public static boolean skipDriverBug(String version) {
54+
if (driverVersion != null && driverVersion.isLowerThan(new Semver(version))) {
55+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
56+
String callerName = stackTrace[2].getMethodName();
57+
System.out.println("SkipDriverBug version=" + version + ", method=" + callerName);
58+
return true;
59+
}
60+
return false;
61+
}
62+
}

databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public void setUp()
3232
c.createStatement().execute("create table test_basic_driver.table1(i int)");
3333
c.createStatement().execute("insert into test_basic_driver.table1 values(1)");
3434
c.createStatement().execute("create database test_basic_driver_2");
35-
c.createStatement().execute("create table test_basic_driver.table_with_null(a int,b varchar default null, c varchar, d varchar)");
36-
c.createStatement().execute("insert into test_basic_driver.table_with_null(a,b,c,d) values(1,null,'null','NULL')");
3735
}
3836

3937
@Test(groups = {"IT"})
@@ -153,8 +151,8 @@ public void TestMergeInto() throws SQLException {
153151

154152
Assert.assertTrue(r.next());
155153
Assert.assertEquals(3, statement.getUpdateCount());
156-
} catch (SQLException throwables) {
157-
throwables.printStackTrace();
154+
} catch (SQLException throwable) {
155+
throwable.printStackTrace();
158156
}
159157
}
160158

@@ -169,7 +167,7 @@ public void testWriteDouble() throws SQLException {
169167
" City VARCHAR(50),\n" +
170168
" Score DOUBLE\n" +
171169
");");
172-
Double infDouble = Double.POSITIVE_INFINITY;
170+
double infDouble = Double.POSITIVE_INFINITY;
173171

174172
String sql = "INSERT INTO test_basic_driver.table_double (ID, Name, Age, City, Score) values";
175173
PreparedStatement prepareStatement = connection.prepareStatement(sql);
@@ -192,17 +190,24 @@ public void testWriteDouble() throws SQLException {
192190

193191
@Test(groups = {"IT"})
194192
public void testDefaultSelectNullValue() throws SQLException {
195-
try (Connection connection = Utils.createConnection()) {
196-
DatabendStatement statement = (DatabendStatement) connection.createStatement();
197-
statement.executeQuery("SELECT a,b,c,d from test_basic_driver.table_with_null");
193+
try (Connection connection = Utils.createConnection();
194+
Statement statement = connection.createStatement()
195+
) {
196+
statement.execute("create table test_basic_driver.table_with_null(a int,b varchar default null, c varchar, d varchar)");
197+
statement.execute("insert into test_basic_driver.table_with_null(a,b,c,d) values(1,null,'null','NULL')");
198+
statement.execute("SELECT a,b,c,d from test_basic_driver.table_with_null");
198199
ResultSet r = statement.getResultSet();
199200
r.next();
200201
Assert.assertEquals(r.getInt(1), 1);
201202
Assert.assertNull(r.getObject(2));
202203
Assert.assertEquals(r.getObject(3), "null");
203-
Assert.assertEquals(r.getObject(4), "NULL");
204-
} catch (SQLException throwables) {
205-
throwables.printStackTrace();
204+
if (Compatibility.skipDriverBug("0.3.9")) {
205+
Assert.assertNull(r.getObject(4));
206+
} else {
207+
Assert.assertEquals(r.getObject(4), "NULL");
208+
}
209+
} catch (SQLException throwable) {
210+
throwable.printStackTrace();
206211
}
207212
}
208213

@@ -293,7 +298,7 @@ public void testUpdateSession()
293298
public void testResultException() {
294299
try (Connection connection = Utils.createConnection()) {
295300
Statement statement = connection.createStatement();
296-
ResultSet r = statement.executeQuery("SELECT 1e189he 198h");
301+
statement.execute("SELECT 1e189he 198h");
297302
} catch (SQLException e) {
298303
Assert.assertTrue(e.getMessage().contains("Query failed"));
299304
}

databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ public void testVersion() throws SQLException {
7878
DatabaseMetaData metaData = c.getMetaData();
7979
int major = metaData.getDriverMajorVersion();
8080
int minor = metaData.getDriverMinorVersion();
81-
assertEquals(major, 0);
82-
assertEquals(minor, 4);
81+
if (Compatibility.driverVersion != null) {
82+
assertEquals(major, (int) Compatibility.driverVersion.getMajor());
83+
assertEquals(minor, (int) Compatibility.driverVersion.getMinor());
84+
} else {
85+
assertEquals(major, 0);
86+
assertEquals(minor, 4);
87+
}
8388
}
8489
}
8590

@@ -111,7 +116,7 @@ public void testGetDatabaseProductVersion()
111116
String checkVersion = String.format("v%.1f.%d", majorVersion, minorVersion);
112117
Assert.assertTrue(metaData.getDatabaseProductVersion().contains(checkVersion));
113118

114-
if (Utils.serverCapability.streamingLoad && Utils.driverCapability.streamingLoad) {
119+
if (Compatibility.serverCapability.streamingLoad && Compatibility.driverCapability.streamingLoad) {
115120
DatabendConnection conn = connection.unwrap(DatabendConnection.class);
116121
if (conn.getServerVersion() != null) {
117122
String semver = "v" + conn.getServerVersion().toString();

databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,13 @@ public void testLoadStreamToTableWithStreaming() {
191191
}
192192

193193
public void testLoadStreamToTableInner(String method) {
194-
if (!Utils.driverCapability.streamingLoad) {
195-
throw new SkipException("driver version too low");
194+
if (!Compatibility.driverCapability.streamingLoad) {
195+
System.out.println("Skip testLoadStreamToTableInner: driver version too low");
196+
return;
196197
}
197-
if (!Utils.serverCapability.streamingLoad) {
198-
throw new SkipException("server version too low");
198+
if (!Compatibility.serverCapability.streamingLoad) {
199+
System.out.println("Skip testLoadStreamToTableInner: server version too low");
200+
return;
199201
}
200202
System.out.println("testLoadStreamToTableInner " + method);
201203
String filePath = generateRandomCSVComplex(10);

databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public void testAutoDiscoveryUriParsing() throws SQLException {
148148
Assert.assertEquals(uris.size(), 3);
149149
Assert.assertEquals(uris2.size(), 3);
150150
Assert.assertEquals(uris2, uris);
151-
152151
}
153152

154153
private HashMap<Integer, Integer> get_hosts_used(String dsn) throws SQLException {

databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ public void TestBatchDelete() throws SQLException {
162162
deletePs.addBatch();
163163
int[] ansDel = deletePs.executeBatch();
164164
Assert.assertEquals(ansDel.length, 1);
165-
// todo: fix this
166-
Assert.assertEquals(ansDel[0], 0);
165+
// todo: fix this, currently == 0
166+
// Assert.assertEquals(ansDel[0], 1);
167167

168168
System.out.println("execute select");
169169
statement.execute("SELECT * from test_batch_delete");
@@ -611,44 +611,49 @@ public void testExecuteUpdate() throws SQLException {
611611

612612
@Test(groups = "IT")
613613
public void testInsertWithSelect() throws SQLException {
614+
if (Compatibility.skipDriverBug("0.3.9")) {
615+
return;
616+
}
614617
Connection conn = Utils.createConnection();
615-
conn.createStatement().execute("delete from test_prepare_statement");
616-
617-
String insertSql = "insert into test_prepare_statement select a, b from test_prepare_statement where b = ?";
618-
try (PreparedStatement statement = conn.prepareStatement(insertSql)) {
619-
statement.setString(1, "a");
620-
int insertedRows = statement.executeUpdate();
618+
Statement statement = conn.createStatement();
619+
statement.execute("use test_prepare_statement");
620+
statement.execute("create or replace table insert_with_select (a int, b string)");
621+
622+
String insertSql = "insert into insert_with_select select a, b from insert_with_select where b = ?";
623+
try (PreparedStatement ps = conn.prepareStatement(insertSql)) {
624+
ps.setString(1, "a");
625+
int insertedRows = ps.executeUpdate();
621626
Assert.assertEquals(0, insertedRows, "should not insert any rows as the table is empty");
622627
}
623628

624629
// Insert some data
625-
String insertDataSql = "insert into test_prepare_statement values (?,?)";
626-
try (PreparedStatement statement = conn.prepareStatement(insertDataSql)) {
627-
statement.setInt(1, 1);
628-
statement.setString(2, "a");
629-
statement.executeUpdate();
630+
String insertDataSql = "insert into insert_with_select values (?,?)";
631+
try (PreparedStatement ps = conn.prepareStatement(insertDataSql)) {
632+
ps.setInt(1, 1);
633+
ps.setString(2, "a");
634+
int insertedRows = ps.executeUpdate();
635+
Assert.assertEquals(1, insertedRows, "should insert 1 rows");
630636

631-
statement.setInt(1, 2);
632-
statement.setString(2, "b");
633-
statement.executeUpdate();
637+
ps.setInt(1, 2);
638+
ps.setString(2, "b");
639+
insertedRows = ps.executeUpdate();
640+
Assert.assertEquals(1, insertedRows, "should insert 1 rows");
634641
}
635642

636643
// Now try to insert again with select
637-
try (PreparedStatement statement = conn.prepareStatement(insertSql)) {
638-
statement.setString(1, "a");
639-
int insertedRows = statement.executeUpdate();
640-
Assert.assertEquals(1, insertedRows, "should insert two rows from the select");
644+
try (PreparedStatement ps = conn.prepareStatement(insertSql)) {
645+
ps.setString(1, "a");
646+
int insertedRows = ps.executeUpdate();
647+
Assert.assertEquals(1, insertedRows, "should insert 1 row from the select");
641648
}
642649

643-
ResultSet rs = conn.createStatement().executeQuery("select * from test_prepare_statement order by a");
650+
ResultSet rs = conn.createStatement().executeQuery("select * from insert_with_select order by a");
644651
int count = 0;
645652
while (rs.next()) {
646653
count++;
647654
}
648-
Assert.assertEquals(3, count, "should have four rows in the table after insert with select");
649-
650-
// Clean up
651-
conn.createStatement().execute("delete from test_prepare_statement");
655+
Assert.assertEquals(3, count, "should have 3 rows in the table after insert with select");
656+
conn.close();
652657
}
653658

654659
}

databend-jdbc/src/test/java/com/databend/jdbc/TestStageAttachment.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ public void TestStageAttachment() {
1515
StageAttachment attachment = new StageAttachment.Builder().setLocation(stagePath)
1616
.build();
1717
assertEquals("StageAttachment{location=@~/prefix/uuid/test, file_format_options={type=CSV}, copy_options=null}", attachment.toString());
18-
1918
}
2019
}

databend-jdbc/src/test/java/com/databend/jdbc/Utils.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,19 @@
88
import java.util.Properties;
99

1010
public class Utils {
11-
public static class Capability {
12-
boolean streamingLoad;
13-
14-
public Capability() {
15-
this.streamingLoad = true;
16-
}
17-
public Capability(boolean streamingLoad) {
18-
this.streamingLoad = streamingLoad;
19-
}
20-
21-
public static Capability fromServerVersion(Semver ver) {
22-
boolean streamingLoad = ver.isGreaterThan(new Semver("1.2.781"));
23-
return new Capability(streamingLoad);
24-
}
25-
26-
public static Capability fromDriverVersion(Semver ver) {
27-
boolean streamingLoad = ver.isGreaterThan(new Semver("0.4.1"));
28-
return new Capability(streamingLoad);
29-
}
30-
}
31-
3211

3312
static String port = System.getenv("DATABEND_TEST_CONN_PORT") != null ? System.getenv("DATABEND_TEST_CONN_PORT").trim() : "8000";
34-
public static Semver driverVersion = getDriverVersion();
35-
public static Semver serverVersion = getServerVersion();
36-
public static Capability driverCapability = driverVersion==null? new Capability(): Capability.fromDriverVersion(driverVersion);
37-
public static Capability serverCapability = serverVersion==null? new Capability(): Capability.fromServerVersion(serverVersion);
38-
3913
static String username = "databend";
4014
static String password = "databend";
15+
4116
public static String baseURL() {
4217
return "jdbc:databend://localhost:" + port;
4318
}
4419

4520
public static String getUsername() {
4621
return username;
4722
}
23+
4824
public static String getPassword() {
4925
return password;
5026
}
@@ -70,19 +46,5 @@ public static Connection createConnectionWithPresignedUrlDisable() throws SQLExc
7046
String url = baseURL() + "?presigned_url_disabled=true";
7147
return DriverManager.getConnection(url, "databend", "databend");
7248
}
73-
74-
private static Semver getDriverVersion() {
75-
String env = System.getenv("DATABEND_TEST_DRIVER_VERSION");
76-
if (env == null) {
77-
return null;
78-
}
79-
return new Semver(env);
80-
}
81-
private static Semver getServerVersion() {
82-
String env = System.getenv("DATABEND_TEST_SERVER_VERSION");
83-
if (env == null) {
84-
return null;
85-
}
86-
return new Semver(env);
87-
}
8849
}
50+

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<dep.jackson.version>2.19.2</dep.jackson.version>
4949
<dep.guava.version>32.0.1-jre</dep.guava.version>
5050
<dep.slf4j.version>1.7.6</dep.slf4j.version>
51-
<dep.testng.version>7.11.0</dep.testng.version>
5251
<!-- not in airbase -->
5352
<dep.okio.version>3.1.0</dep.okio.version>
5453
<!-- not in airbase -->

0 commit comments

Comments
 (0)