Skip to content

Commit 72aee49

Browse files
committed
Added tests for column tables
1 parent 166f8af commit 72aee49

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package tech.ydb.jdbc;
2+
3+
import java.sql.Connection;
4+
import java.sql.Date;
5+
import java.sql.DriverManager;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import java.time.LocalDate;
11+
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
16+
import tech.ydb.jdbc.impl.helper.ExceptionAssert;
17+
import tech.ydb.jdbc.impl.helper.JdbcUrlHelper;
18+
import tech.ydb.test.junit5.YdbHelperExtension;
19+
20+
/**
21+
*
22+
* @author Aleksandr Gorshenin
23+
*/
24+
public class YdbDriverColumnTablesTest {
25+
@RegisterExtension
26+
private static final YdbHelperExtension ydb = new YdbHelperExtension();
27+
28+
private static final JdbcUrlHelper jdbcURL = new JdbcUrlHelper(ydb);
29+
30+
private final static String ERROR_DATA_MANIPULATION =
31+
"Data manipulation queries do not support column shard tables. (S_ERROR)";
32+
private final static String ERROR_TRANSACTIONS =
33+
"Transactions between column and row tables are disabled at current time. (S_ERROR)";
34+
private final static String ERROR_BULK_UNSUPPORTED =
35+
"BULK mode is available only for prepared statement with one UPSERT";
36+
37+
private final static String CREATE_TABLE = ""
38+
+ "CREATE TABLE columns_sample("
39+
+ " id Int32 NOT NULL,"
40+
+ " value Text,"
41+
+ " date Date,"
42+
+ " PRIMARY KEY (id)"
43+
+ ") WITH (STORE = COLUMN)";
44+
45+
private final static String DROP_TABLE = "DROP TABLE columns_sample";
46+
private final static String UPSERT_ROW = "UPSERT INTO columns_sample (id, value, date) VALUES (?, ?, ?)";
47+
private final static String INSERT_ROW = "INSERT INTO columns_sample (id, value, date) VALUES (?, ?, ?)";
48+
private final static String SELECT_ALL = "SELECT * FROM columns_sample ORDER BY id";
49+
private final static String UPDATE_ROW = "UPDATE columns_sample SET value = ? WHERE id = ?";
50+
private final static String DELETE_ROW = "DELETE FROM columns_sample WHERE id = ?";
51+
52+
@Test
53+
public void defaultModeTest() throws SQLException {
54+
try (Connection connection = DriverManager.getConnection(jdbcURL.build())) {
55+
try {
56+
connection.createStatement().execute(DROP_TABLE);
57+
} catch (SQLException e) {
58+
// ignore
59+
}
60+
61+
connection.createStatement().execute(CREATE_TABLE);
62+
63+
LocalDate ld = LocalDate.of(2017, 12, 3);
64+
String prefix = "text-value-";
65+
int idx = 0;
66+
67+
// single upsert
68+
try (PreparedStatement ps = connection.prepareStatement(UPSERT_ROW)) {
69+
ps.setInt(1, ++idx);
70+
ps.setString(2, prefix + idx);
71+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
72+
ExceptionAssert.ydbException(ERROR_DATA_MANIPULATION, ps::executeUpdate);
73+
}
74+
75+
// single insert
76+
try (PreparedStatement ps = connection.prepareStatement(INSERT_ROW)) {
77+
ps.setInt(1, ++idx);
78+
ps.setString(2, prefix + idx);
79+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
80+
ExceptionAssert.ydbException(ERROR_DATA_MANIPULATION, ps::executeUpdate);
81+
}
82+
83+
// batch upsert
84+
try (PreparedStatement ps = connection.prepareStatement(UPSERT_ROW)) {
85+
for (int j = 0; j < 1000; j++) {
86+
ps.setInt(1, ++idx);
87+
ps.setString(2, prefix + idx);
88+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
89+
ps.addBatch();
90+
}
91+
ExceptionAssert.ydbException(ERROR_TRANSACTIONS, ps::executeBatch);
92+
}
93+
94+
// batch insert
95+
try (PreparedStatement ps = connection.prepareStatement(INSERT_ROW)) {
96+
for (int j = 0; j < 1000; j++) {
97+
ps.setInt(1, ++idx);
98+
ps.setString(2, prefix + idx);
99+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
100+
ps.addBatch();
101+
}
102+
ExceptionAssert.ydbException(ERROR_TRANSACTIONS, ps::executeBatch);
103+
}
104+
105+
// read all
106+
try (Statement st = connection.createStatement()) {
107+
ExceptionAssert.ydbException(ERROR_TRANSACTIONS, () -> st.executeQuery(SELECT_ALL));
108+
}
109+
110+
// single update
111+
try (PreparedStatement ps = connection.prepareStatement(UPDATE_ROW)) {
112+
ps.setString(1, "updated-value");
113+
ps.setInt(2, 1);
114+
ExceptionAssert.ydbException(ERROR_TRANSACTIONS, ps::execute);
115+
}
116+
117+
// single delete
118+
try (PreparedStatement ps = connection.prepareStatement(DELETE_ROW)) {
119+
ps.setInt(1, 2);
120+
ExceptionAssert.ydbException(ERROR_TRANSACTIONS, ps::execute);
121+
}
122+
}
123+
}
124+
125+
@Test
126+
public void prefixesTest() throws SQLException {
127+
try (Connection conn = DriverManager.getConnection(jdbcURL.build())) {
128+
try {
129+
conn.createStatement().execute(DROP_TABLE);
130+
} catch (SQLException e) {
131+
// ignore
132+
}
133+
134+
conn.createStatement().execute(CREATE_TABLE);
135+
136+
LocalDate ld = LocalDate.of(2017, 12, 3);
137+
String prefix = "text-value-";
138+
int idx = 0;
139+
140+
// single bulk upsert
141+
try (PreparedStatement ps = conn.prepareStatement("BULK " + UPSERT_ROW)) {
142+
ps.setInt(1, ++idx);
143+
ps.setString(2, prefix + idx);
144+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
145+
ps.executeUpdate();
146+
}
147+
148+
// single bulk insert
149+
ExceptionAssert.sqlException(ERROR_BULK_UNSUPPORTED, () -> conn.prepareStatement("BULK " + INSERT_ROW));
150+
151+
// scan read
152+
try (Statement st = conn.createStatement()) {
153+
int readed = 0;
154+
try (ResultSet rs = st.executeQuery("SCAN " + SELECT_ALL)) {
155+
while (rs.next()) {
156+
readed++;
157+
Assertions.assertEquals(readed, rs.getInt("id"));
158+
Assertions.assertEquals(prefix + readed, rs.getString("value"));
159+
Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date"));
160+
}
161+
}
162+
Assertions.assertEquals(1, readed);
163+
}
164+
165+
// batch bulk upsert
166+
try (PreparedStatement ps = conn.prepareStatement("BULK " + UPSERT_ROW)) {
167+
for (int j = 0; j < 2000; j++) {
168+
ps.setInt(1, ++idx);
169+
ps.setString(2, prefix + idx);
170+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
171+
ps.addBatch();
172+
}
173+
ps.executeBatch();
174+
175+
for (int j = 0; j < 2000; j++) {
176+
ps.setInt(1, ++idx);
177+
ps.setString(2, prefix + idx);
178+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
179+
ps.addBatch();
180+
}
181+
ps.executeBatch();
182+
183+
// single row insert
184+
ps.setInt(1, ++idx);
185+
ps.setString(2, prefix + idx);
186+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
187+
ps.execute();
188+
}
189+
190+
// read all
191+
try (Statement st = conn.createStatement()) {
192+
int readed = 0;
193+
try (ResultSet rs = st.executeQuery("SCAN " + SELECT_ALL)) {
194+
while (rs.next()) {
195+
readed++;
196+
Assertions.assertEquals(readed, rs.getInt("id"));
197+
Assertions.assertEquals(prefix + readed, rs.getString("value"));
198+
Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date"));
199+
}
200+
}
201+
Assertions.assertEquals(4002, readed);
202+
}
203+
204+
// single update
205+
ExceptionAssert.sqlException(ERROR_BULK_UNSUPPORTED, () -> conn.prepareStatement("BULK " + UPDATE_ROW));
206+
207+
// single delete
208+
ExceptionAssert.sqlException(ERROR_BULK_UNSUPPORTED, () -> conn.prepareStatement("BULK " + DELETE_ROW));
209+
}
210+
}
211+
}

0 commit comments

Comments
 (0)