Skip to content

Commit 1057a9e

Browse files
committed
Add unit tests for ACID tables
1 parent 79ca350 commit 1057a9e

File tree

8 files changed

+156
-6
lines changed

8 files changed

+156
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2025 Qiheng He
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.linghengqian.hive.server2.jdbc.driver.thin;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
import org.testcontainers.utility.DockerImageName;
24+
25+
import java.sql.*;
26+
import java.time.Duration;
27+
import java.time.temporal.ChronoUnit;
28+
29+
import static org.awaitility.Awaitility.await;
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.hamcrest.Matchers.is;
32+
33+
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
34+
@Testcontainers
35+
public class AcidTableTest {
36+
@Container
37+
public static final GenericContainer<?> CONTAINER = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.1"))
38+
.withEnv("SERVICE_NAME", "hiveserver2")
39+
.withExposedPorts(10000);
40+
41+
@Test
42+
void test() throws SQLException {
43+
String jdbcUrlPrefix = "jdbc:hive2://" + CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(10000);
44+
await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).ignoreExceptions().until(() -> {
45+
DriverManager.getConnection(jdbcUrlPrefix).close();
46+
return true;
47+
});
48+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix);
49+
Statement statement = connection.createStatement()) {
50+
statement.execute("CREATE DATABASE demo_ds_0");
51+
}
52+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/demo_ds_0");
53+
Statement statement = connection.createStatement()) {
54+
statement.execute("set metastore.compactor.initiator.on=true");
55+
statement.execute("set metastore.compactor.cleaner.on=true");
56+
statement.execute("set metastore.compactor.worker.threads=1");
57+
statement.execute("set hive.support.concurrency=true");
58+
statement.execute("set hive.exec.dynamic.partition.mode=nonstrict");
59+
statement.execute("set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager");
60+
statement.execute("create table IF NOT EXISTS t_order (\n" +
61+
" order_id BIGINT NOT NULL,\n" +
62+
" order_type INT,\n" +
63+
" user_id INT NOT NULL,\n" +
64+
" address_id BIGINT NOT NULL,\n" +
65+
" status VARCHAR(50),\n" +
66+
" PRIMARY KEY (order_id) disable novalidate\n" +
67+
") CLUSTERED BY (order_id) INTO 2 BUCKETS STORED AS ORC TBLPROPERTIES ('transactional' = 'true')");
68+
statement.execute("TRUNCATE TABLE t_order");
69+
statement.executeUpdate("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')");
70+
ResultSet firstResultSet = statement.executeQuery("select * from t_order");
71+
assertThat(firstResultSet.next(), is(true));
72+
statement.executeUpdate("DELETE FROM t_order WHERE order_id=1");
73+
ResultSet secondResultSet = statement.executeQuery("select * from t_order");
74+
assertThat(secondResultSet.next(), is(false));
75+
statement.execute("DROP TABLE IF EXISTS t_order");
76+
}
77+
}
78+
}

hive-server2-jdbc-driver-thin/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/thin/iceberg/AvroTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,5 @@ void test() throws SQLException {
6868
assertThat(secondResultSet.next(), is(false));
6969
statement.execute("DROP TABLE IF EXISTS t_order");
7070
}
71-
7271
}
7372
}

hive-server2-jdbc-driver-thin/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/thin/iceberg/OrcTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,5 @@ void test() throws SQLException {
6868
assertThat(secondResultSet.next(), is(false));
6969
statement.execute("DROP TABLE IF EXISTS t_order");
7070
}
71-
7271
}
7372
}

hive-server2-jdbc-driver-thin/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/thin/iceberg/ParquetTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,5 @@ void test() throws SQLException {
6868
assertThat(secondResultSet.next(), is(false));
6969
statement.execute("DROP TABLE IF EXISTS t_order");
7070
}
71-
7271
}
7372
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2025 Qiheng He
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.linghengqian.hive.server2.jdbc.driver.uber;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
import org.testcontainers.utility.DockerImageName;
24+
25+
import java.sql.*;
26+
import java.time.Duration;
27+
import java.time.temporal.ChronoUnit;
28+
29+
import static org.awaitility.Awaitility.await;
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.hamcrest.Matchers.is;
32+
33+
@SuppressWarnings({"SqlNoDataSourceInspection", "resource"})
34+
@Testcontainers
35+
public class AcidTableTest {
36+
@Container
37+
public static final GenericContainer<?> CONTAINER = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.1"))
38+
.withEnv("SERVICE_NAME", "hiveserver2")
39+
.withExposedPorts(10000);
40+
41+
@Test
42+
void test() throws SQLException {
43+
String jdbcUrlPrefix = "jdbc:hive2://" + CONTAINER.getHost() + ":" + CONTAINER.getMappedPort(10000);
44+
await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).ignoreExceptions().until(() -> {
45+
DriverManager.getConnection(jdbcUrlPrefix).close();
46+
return true;
47+
});
48+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix);
49+
Statement statement = connection.createStatement()) {
50+
statement.execute("CREATE DATABASE demo_ds_0");
51+
}
52+
try (Connection connection = DriverManager.getConnection(jdbcUrlPrefix + "/demo_ds_0");
53+
Statement statement = connection.createStatement()) {
54+
statement.execute("set metastore.compactor.initiator.on=true");
55+
statement.execute("set metastore.compactor.cleaner.on=true");
56+
statement.execute("set metastore.compactor.worker.threads=1");
57+
statement.execute("set hive.support.concurrency=true");
58+
statement.execute("set hive.exec.dynamic.partition.mode=nonstrict");
59+
statement.execute("set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager");
60+
statement.execute("create table IF NOT EXISTS t_order (\n" +
61+
" order_id BIGINT NOT NULL,\n" +
62+
" order_type INT,\n" +
63+
" user_id INT NOT NULL,\n" +
64+
" address_id BIGINT NOT NULL,\n" +
65+
" status VARCHAR(50),\n" +
66+
" PRIMARY KEY (order_id) disable novalidate\n" +
67+
") CLUSTERED BY (order_id) INTO 2 BUCKETS STORED AS ORC TBLPROPERTIES ('transactional' = 'true')");
68+
statement.execute("TRUNCATE TABLE t_order");
69+
statement.executeUpdate("INSERT INTO t_order (order_id, user_id, order_type, address_id, status) VALUES (1, 1, 1, 1, 'INSERT_TEST')");
70+
ResultSet firstResultSet = statement.executeQuery("select * from t_order");
71+
assertThat(firstResultSet.next(), is(true));
72+
statement.executeUpdate("DELETE FROM t_order WHERE order_id=1");
73+
ResultSet secondResultSet = statement.executeQuery("select * from t_order");
74+
assertThat(secondResultSet.next(), is(false));
75+
statement.execute("DROP TABLE IF EXISTS t_order");
76+
}
77+
}
78+
}

hive-server2-jdbc-driver-uber/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/uber/iceberg/AvroTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,5 @@ void test() throws SQLException {
6868
assertThat(secondResultSet.next(), is(false));
6969
statement.execute("DROP TABLE IF EXISTS t_order");
7070
}
71-
7271
}
7372
}

hive-server2-jdbc-driver-uber/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/uber/iceberg/OrcTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,5 @@ void test() throws SQLException {
6868
assertThat(secondResultSet.next(), is(false));
6969
statement.execute("DROP TABLE IF EXISTS t_order");
7070
}
71-
7271
}
7372
}

hive-server2-jdbc-driver-uber/src/test/java/io/github/linghengqian/hive/server2/jdbc/driver/uber/iceberg/ParquetTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,5 @@ void test() throws SQLException {
6868
assertThat(secondResultSet.next(), is(false));
6969
statement.execute("DROP TABLE IF EXISTS t_order");
7070
}
71-
7271
}
7372
}

0 commit comments

Comments
 (0)