Skip to content
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 @@ -18,7 +18,7 @@ public interface FileTransferAPI {
* @param compressData whether to compress the data
* @throws SQLException failed to upload input stream
*/
public void uploadStream(String stageName, String destPrefix, InputStream inputStream, String destFileName, long fileSize, boolean compressData) throws SQLException;
void uploadStream(String stageName, String destPrefix, InputStream inputStream, String destFileName, long fileSize, boolean compressData) throws SQLException;

/**
* Download a file from the databend internal stage, the data would be downloaded as one file with no split.
Expand All @@ -27,18 +27,18 @@ public interface FileTransferAPI {
* @param sourceFileName the file name in the stage
* @param decompress whether to decompress the data
* @return the input stream of the file
* @throws SQLException
* @throws SQLException failed to download input stream
*/
public InputStream downloadStream(String stageName, String sourceFileName, boolean decompress) throws SQLException;
InputStream downloadStream(String stageName, String sourceFileName, boolean decompress) throws SQLException;

/**
* Copy into the target table from files on the internal stage
* Documentation: https://databend.rs/doc/sql-commands/dml/dml-copy-into-table
* Documentation: <a href="https://databend.rs/doc/sql-commands/dml/dml-copy-into-table">...</a>
*
* @param database the target table's database
* @param tableName the target table name
* @param params copy options and file options
* @throws SQLException
* @throws SQLException fail to copy into table
*/
public void copyIntoTable(String database, String tableName, DatabendCopyParams params) throws SQLException;
void copyIntoTable(String database, String tableName, DatabendCopyParams params) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,38 @@ private static void assertTableMetadata(ResultSet rs)
public void setUp()
throws SQLException {
// create table
Connection c = Utils.createConnection();
c.createStatement().execute("drop table if exists test_column_meta");
c.createStatement().execute("drop table if exists decimal_test");
c.createStatement().execute("drop table if exists decimal_test_1");
c.createStatement().execute("drop table if exists test_comment");
c.createStatement().execute("drop view if exists v_test_comment");
c.createStatement().execute("create table test_column_meta (nu1 uint8 null, u1 uint8, u2 uint16, u3 uint32, u4 uint64, i1 int8, i2 int16, i3 int32, i4 int64, f1 float32, f2 float64, s1 string,d1 date, d2 datetime, v1 variant, a1 array(int64), t1 Tuple(x Int64, y Int64 NULL)) engine = fuse");
c.createStatement().execute("create table decimal_test (a decimal(4,2))");
c.createStatement().execute("create table decimal_test_1 (a decimal(15,12))");
c.createStatement().execute("create table test_comment (a int comment 'test comment')");
c.createStatement().execute("create view v_test_comment as select * from test_comment");
try (Connection c = Utils.createConnection()) {
c.createStatement().execute("drop table if exists test_column_meta");
c.createStatement().execute("drop table if exists decimal_test");
c.createStatement().execute("drop table if exists decimal_test_1");
c.createStatement().execute("drop table if exists test_comment");
c.createStatement().execute("drop view if exists v_test_comment");
c.createStatement().execute("create table test_column_meta (nu1 uint8 null, u1 uint8, u2 uint16, u3 uint32, u4 uint64, i1 int8, i2 int16, i3 int32, i4 int64, f1 float32, f2 float64, s1 string,d1 date, d2 datetime, v1 variant, a1 array(int64), t1 Tuple(x Int64, y Int64 NULL)) engine = fuse");
c.createStatement().execute("create table decimal_test (a decimal(4,2))");
c.createStatement().execute("create table decimal_test_1 (a decimal(15,12))");
c.createStatement().execute("create table test_comment (a int comment 'test comment')");
c.createStatement().execute("create view v_test_comment as select * from test_comment");
}
// json data
}

@Test(groups = {"UNIT"})
public void testVersion() throws SQLException {
try (Connection c = Utils.createConnection()) {
DatabaseMetaData metaData = c.getMetaData();
int major = metaData.getDriverMajorVersion();
int minor = metaData.getDriverMinorVersion();
assertEquals(major, 0);
assertEquals(minor, 4);
}
}

@Test(groups = {"IT"})
public void testGetUrl() throws SQLException {
try (Connection c = Utils.createConnection()) {
DatabaseMetaData metaData = c.getMetaData();
String url = metaData.getURL();
Assert.assertEquals(url, "jdbc:databend://http://localhost:" + Utils.port);;
Assert.assertEquals(url, "jdbc:databend://http://localhost:" + Utils.port);
}
}

Expand Down Expand Up @@ -113,7 +125,6 @@ public void testGetUserName()
@Test(groups = {"IT"})
public void testGetTables() throws Exception {
try (Connection connection = Utils.createConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = connection.getMetaData().getTables(null, null, null, null)) {
assertTableMetadata(rs);
// test for view
Expand All @@ -133,11 +144,11 @@ public void testGetTables() throws Exception {
public void testGetSchemas() throws Exception {
try (Connection connection = Utils.createConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = connection.getMetaData().getSchemas()) {
try (ResultSet rs = metaData.getSchemas()) {
ResultSetMetaData metaData1 = rs.getMetaData();
assertEquals(metaData1.getColumnCount(), 2);
}
try (ResultSet rs = connection.getMetaData().getCatalogs()) {
try (ResultSet rs = metaData.getCatalogs()) {
ResultSetMetaData metaData1 = rs.getMetaData();
assertEquals(metaData1.getColumnCount(), 1);
}
Expand All @@ -147,7 +158,6 @@ public void testGetSchemas() throws Exception {
@Test(groups = {"IT"})
public void testGetColumns() throws Exception {
try (Connection connection = Utils.createConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = connection.getMetaData().getColumns(null, null, null, null)) {
assertEquals(rs.getMetaData().getColumnCount(), 24);
}
Expand All @@ -157,7 +167,6 @@ public void testGetColumns() throws Exception {
@Test(groups = {"IT"})
public void testComment() throws SQLException {
try (Connection connection = Utils.createConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = connection.getMetaData().getColumns("default", "default", "test_comment", null)) {
while (rs.next()) {
String tableSchem = rs.getString("table_schem");
Expand Down Expand Up @@ -281,7 +290,6 @@ public void testGetBigDecimal() throws Exception {
@Test(groups = {"IT"})
public void testGetPrimaryKeys() throws Exception {
try (Connection connection = Utils.createConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = connection.getMetaData().getPrimaryKeys(null, null, null)) {
assertEquals(rs.getMetaData().getColumnCount(), 6);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Vector;

public class TestHeartbeat {
@Test
@Test(groups = {"IT"})
public void testHeartbeat() throws SQLException {
Properties p = new Properties();
p.setProperty("max_rows_in_buffer", "10000");
Expand All @@ -26,7 +26,7 @@ public void testHeartbeat() throws SQLException {
int n = 80000;
int numQuery = 3;

Vector<ResultSet> rss = new Vector();
Vector<ResultSet> rss = new Vector<>();
for (int i = 0; i < numQuery; i++) {
statement = c1.createStatement();
statement.executeQuery("select * from numbers(" + n + ") order by number");
Expand All @@ -39,10 +39,10 @@ public void testHeartbeat() throws SQLException {
for (int i = 0; i < numQuery; i++) {
ResultSet rs = rss.get(i);
for (int j = 0; j < n; j++) {
Assert.assertEquals(true, rs.next());
Assert.assertTrue(rs.next());
Assert.assertEquals(j, rs.getInt(1));
}
Assert.assertEquals(false, rs.next());
Assert.assertFalse(rs.next());
rs.close();
}
statement.close();
Expand Down
154 changes: 92 additions & 62 deletions databend-jdbc/src/test/java/com/databend/jdbc/TestTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,88 +9,118 @@
import java.sql.SQLException;
import java.sql.Statement;

import static org.testng.Assert.*;

public class TestTransaction {

@BeforeTest
public void setUp()
throws SQLException {
// create table
Connection c = Utils.createConnection();
c.createStatement().execute("drop database if exists test_txn");
c.createStatement().execute("create database test_txn");
c.createStatement().execute("create table test_txn.table1(i int)");
// create db
try (Connection c = Utils.createConnection()) {
c.createStatement().execute("create or replace database test_txn");
}
}

@Test
public void testRollback()
throws SQLException {
Connection c = Utils.createConnection();
c.createStatement().execute("delete from test_txn.table1");
try (Statement statement = c.createStatement()) {
statement.execute("begin");
ResultSet r = statement.getResultSet();
}
@Test(groups = {"IT"})
public void testCommit() throws SQLException {
try ( Connection c1 = Utils.createConnection();
Connection c2 = Utils.createConnection();
Connection c3 = Utils.createConnection()
) {

try (Statement statement = c.createStatement()) {
statement.execute("select 11");
// txn_state = Auto_Commit, not correct, should be Active
ResultSet r = statement.getResultSet();
while (r.next()) {
}
}
c1.createStatement().execute("create or replace table test_txn.table1(i int)");

try (Statement statement = c.createStatement()) {
statement.execute("insert into test_txn.table1 values(3)");
ResultSet r = statement.getResultSet();
}
try (Statement statement = c.createStatement()) {
statement.execute("rollback");
ResultSet r = statement.getResultSet();
}
try (Statement statement = c1.createStatement()) {
statement.execute("begin");
statement.execute("insert into test_txn.table1 values(4)");
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
assertTrue(rs.next());
Assert.assertEquals(4, rs.getInt(1));
assertFalse(rs.next());

try (Statement statement = c.createStatement()) {
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
while (rs.next()) {
Assert.assertEquals(0, rs.getInt(1));
}
}
}

@Test
public void testCommit() throws SQLException {
Connection c1 = Utils.createConnection();
Connection c2 = Utils.createConnection();
c1.createStatement().execute("delete from test_txn.table1");
try (Statement statement = c1.createStatement()) {
statement.execute("create or replace table test_txn.table1(i int)");
statement.execute("begin");
statement.execute("insert into test_txn.table1 values(4)");
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
while (rs.next()) {
try (Statement statement = c2.createStatement()) {
statement.execute("begin");
statement.execute("insert into test_txn.table1 values(5)");
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
assertTrue(rs.next());
Assert.assertEquals(5, rs.getInt(1));
assertFalse(rs.next());
}
c1.commit();

try (Statement statement = c3.createStatement()) {
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
assertTrue(rs.next());
Assert.assertEquals(4, rs.getInt(1));
assertFalse(rs.next());
}
}
c2.commit();

try (Statement statement = c2.createStatement()) {
statement.execute("begin");
statement.execute("insert into test_txn.table1 values(5)");
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
while (rs.next()) {
try (Statement statement = c3.createStatement()) {
statement.execute("select * from test_txn.table1 order by i");
ResultSet rs = statement.getResultSet();
assertTrue(rs.next());
Assert.assertEquals(4, rs.getInt(1));
assertTrue(rs.next());
Assert.assertEquals(5, rs.getInt(1));
assertFalse(rs.next());
}
}
c1.commit();
Connection c3 = Utils.createConnection();
try (Statement statement = c3.createStatement()) {
statement.execute("select * from test_txn.table1");
ResultSet rs = statement.getResultSet();
while (rs.next()) {
Assert.assertEquals(4, rs.getInt(1));
}

@Test(groups = {"IT"})
public void testRollback()
throws SQLException {
try (Connection c = Utils.createConnection()) {
try (Statement statement = c.createStatement()) {
c.createStatement().execute("create or replace table test_txn.table2(i int)");
statement.execute("begin");
statement.execute("select 11");
statement.execute("insert into test_txn.table2 values(3)");
statement.execute("rollback");
statement.execute("select * from test_txn.table2");
ResultSet rs = statement.getResultSet();
assertFalse(rs.next());
}
}
}

@Test(groups = {"IT"})
public void testConflict() throws SQLException {
try (Connection c1 = Utils.createConnection(); Connection c2 = Utils.createConnection()) {
Statement statement1 = c1.createStatement();
Statement statement2 = c2.createStatement();

statement1.execute("create or replace table test_txn.table3(i int, j int)");
statement1.execute("insert into test_txn.table3 values (1, 11)");

statement1.execute("begin");
statement1.execute("UPDATE test_txn.table3 set j = 111 where i=1");

statement2.execute("begin");
statement2.execute("UPDATE test_txn.table3 set j = 222 where i=1");
c2.commit();

java.sql.SQLException exception = Assert.expectThrows(
java.sql.SQLException.class,
() -> statement1.execute("commit")
);
// e.g. Unresolvable conflict detected for table 2249
Assert.assertTrue(exception.getMessage().toLowerCase().contains("conflict"));


statement2.execute("select j from test_txn.table3 where i = 1");
ResultSet rs = statement2.getResultSet();
assertTrue(rs.next());
Assert.assertEquals(rs.getInt(1), 222);
assertFalse(rs.next());
}

}
}
Loading