These statements produce {@link ResultSet} as results.
476 | */
477 | private class AtomicResultSetStatements implements AtomicStatements NOTE: Please clean up newly added tables and databases after each test,
43 | * set current catalog back to default_catalog and set current database back to default_database.
44 | */
45 | public class FlinkStatementTest {
46 |
47 | private static FlinkJdbcDriverTestingGateway gateway;
48 | private static Connection connection;
49 | private Statement statement;
50 |
51 | @BeforeClass
52 | public static void beforeClass() throws Exception {
53 | gateway = new FlinkJdbcDriverTestingGateway();
54 | gateway.start();
55 |
56 | InetSocketAddress addr = gateway.getServerAddress();
57 | Assert.assertNotNull(addr);
58 | connection = new FlinkConnection("jdbc:flink://localhost:" + addr.getPort() + "?planner=blink");
59 | Assert.assertTrue(connection.isValid(0));
60 | }
61 |
62 | @AfterClass
63 | public static void afterClass() throws Exception {
64 | connection.close();
65 | gateway.stop();
66 | }
67 |
68 | @Before
69 | public void before() throws SQLException {
70 | statement = connection.createStatement();
71 | }
72 |
73 | @After
74 | public void after() throws SQLException {
75 | statement.close();
76 | }
77 |
78 | @Test
79 | public void testExecuteQuery() throws SQLException {
80 | try (ResultSet rs = statement.executeQuery("SELECT * FROM myTable ORDER BY a, b LIMIT 2")) {
81 | Assert.assertTrue(rs.next());
82 | Assert.assertEquals(22, rs.getInt(1));
83 | Assert.assertEquals("BBB Hi", rs.getString(2));
84 |
85 | Assert.assertTrue(rs.next());
86 | Assert.assertEquals(32, rs.getInt(1));
87 | Assert.assertEquals("CCC World", rs.getString(2));
88 |
89 | Assert.assertFalse(rs.next());
90 | }
91 | }
92 |
93 | @Test
94 | public void testExecuteUpdate() throws Exception {
95 | File tmpFile = File.createTempFile("flink-jdbc-driver-test", ".csv");
96 | tmpFile.deleteOnExit();
97 |
98 | int createTableUpdateCount = statement.executeUpdate(
99 | "CREATE TABLE testTable(" +
100 | " fa INT," +
101 | " fb VARCHAR(100)" +
102 | ") WITH (" +
103 | " 'connector.type'='filesystem'," +
104 | " 'connector.path'='file://" + tmpFile.getPath() + "'," +
105 | " 'format.type' = 'csv')");
106 | // CREATE TABLE is a DDL, according to JDBC Java doc it's update count is 0
107 | Assert.assertEquals(0, createTableUpdateCount);
108 |
109 | int insertUpdateCount = statement.executeUpdate(
110 | "INSERT INTO testTable VALUES (1, 'stra'), (2, 'strb')");
111 | // TODO change this when gateway supports real update count
112 | Assert.assertEquals(Statement.SUCCESS_NO_INFO, insertUpdateCount);
113 |
114 | try (ResultSet rs = statement.executeQuery("SELECT * FROM testTable ORDER BY fa")) {
115 | Assert.assertTrue(rs.next());
116 | Assert.assertEquals(1, rs.getInt("fa"));
117 | Assert.assertEquals("stra", rs.getString("fb"));
118 |
119 | Assert.assertTrue(rs.next());
120 | Assert.assertEquals(2, rs.getInt("fa"));
121 | Assert.assertEquals("strb", rs.getString("fb"));
122 |
123 | Assert.assertFalse(rs.next());
124 | }
125 |
126 | int dropTableUpdateCount = statement.executeUpdate("DROP TABLE testTable");
127 | // DROP TABLE is a DDL, according to JDBC Java doc it's update count is 0
128 | Assert.assertEquals(0, dropTableUpdateCount);
129 | }
130 |
131 | @Test
132 | public void testMultipleStatements() throws Exception {
133 | File tmpFile1 = File.createTempFile("flink-jdbc-driver-test", ".csv");
134 | File tmpFile2 = File.createTempFile("flink-jdbc-driver-test", ".csv");
135 | tmpFile1.deleteOnExit();
136 | tmpFile2.deleteOnExit();
137 |
138 | boolean executeIsQuery = statement.execute("CREATE TABLE testTable1(" +
139 | " fa INT," +
140 | " fb VARCHAR(100)" +
141 | ") WITH (" +
142 | " 'connector.type'='filesystem'," +
143 | " 'connector.path'='file://" + tmpFile1.getPath() + "'," +
144 | " 'format.type' = 'csv');" +
145 | "INSERT INTO testTable1 VALUES (1, 'stra'), (2, 'strb');" +
146 | "SELECT * FROM testTable1 ORDER BY fa;" +
147 |
148 | "CREATE TABLE testTable2(" +
149 | " fc INT," +
150 | " fd VARCHAR(100)" +
151 | ") WITH (" +
152 | " 'connector.type'='filesystem'," +
153 | " 'connector.path'='file://" + tmpFile2.getPath() + "'," +
154 | " 'format.type' = 'csv');" +
155 | "INSERT INTO testTable2(fc, fd) SELECT * FROM testTable1;" +
156 | "SELECT * FROM testTable2 ORDER BY fc;" +
157 |
158 | "DROP TABLE testTable1;" +
159 | "DROP TABLE testTable2;");
160 |
161 | Assert.assertFalse(executeIsQuery);
162 | // CREATE TABLE is a DDL, according to JDBC Java doc it's update count is 0
163 | Assert.assertEquals(0, statement.getUpdateCount());
164 |
165 | Assert.assertFalse(statement.getMoreResults());
166 | // TODO change this when gateway supports real update count
167 | Assert.assertEquals(Statement.SUCCESS_NO_INFO, statement.getUpdateCount());
168 |
169 | Assert.assertTrue(statement.getMoreResults());
170 | ResultSet rs1 = statement.getResultSet();
171 | Assert.assertTrue(rs1.next());
172 | Assert.assertEquals(1, rs1.getInt("fa"));
173 | Assert.assertEquals("stra", rs1.getString("fb"));
174 | Assert.assertTrue(rs1.next());
175 | Assert.assertEquals(2, rs1.getInt("fa"));
176 | Assert.assertEquals("strb", rs1.getString("fb"));
177 | Assert.assertFalse(rs1.next());
178 |
179 | Assert.assertFalse(statement.getMoreResults());
180 | // CREATE TABLE is a DDL, according to JDBC Java doc it's update count is 0
181 | Assert.assertEquals(0, statement.getUpdateCount());
182 |
183 | Assert.assertFalse(statement.getMoreResults());
184 | // TODO change this when gateway supports real update count
185 | Assert.assertEquals(Statement.SUCCESS_NO_INFO, statement.getUpdateCount());
186 |
187 | Assert.assertTrue(statement.getMoreResults());
188 | ResultSet rs2 = statement.getResultSet();
189 | Assert.assertTrue(rs2.next());
190 | Assert.assertEquals(1, rs2.getInt("fc"));
191 | Assert.assertEquals("stra", rs2.getString("fd"));
192 | Assert.assertTrue(rs2.next());
193 | Assert.assertEquals(2, rs2.getInt("fc"));
194 | Assert.assertEquals("strb", rs2.getString("fd"));
195 | Assert.assertFalse(rs2.next());
196 |
197 | Assert.assertFalse(statement.getMoreResults());
198 | // DROP TABLE is a DDL, according to JDBC Java doc it's update count is 0
199 | Assert.assertEquals(0, statement.getUpdateCount());
200 |
201 | Assert.assertFalse(statement.getMoreResults());
202 | // DROP TABLE is a DDL, according to JDBC Java doc it's update count is 0
203 | Assert.assertEquals(0, statement.getUpdateCount());
204 |
205 | Assert.assertFalse(statement.getMoreResults());
206 | Assert.assertEquals(-1, statement.getUpdateCount());
207 | }
208 |
209 | @Test
210 | public void testShows() throws Exception {
211 | compareStringResultsWithSorting(
212 | new String[]{"default_catalog", "cat1", "cat2"}, statement.executeQuery("SHOW CATALOGS"));
213 |
214 | statement.execute("USE CATALOG cat1");
215 | statement.execute("CREATE DATABASE db12");
216 | compareStringResultsWithSorting(
217 | new String[]{"db11", "db12"}, statement.executeQuery("SHOW DATABASES"));
218 |
219 | statement.execute("USE db11");
220 | compareStringResultsWithSorting(new String[]{"cat1"}, statement.executeQuery("SHOW CURRENT CATALOG"));
221 | compareStringResultsWithSorting(new String[]{"db11"}, statement.executeQuery("SHOW CURRENT DATABASE"));
222 |
223 | File tmpFile1 = File.createTempFile("flink-jdbc-driver-test", ".csv");
224 | File tmpFile2 = File.createTempFile("flink-jdbc-driver-test", ".csv");
225 | tmpFile1.deleteOnExit();
226 | tmpFile2.deleteOnExit();
227 |
228 | statement.executeUpdate("CREATE TABLE testTable1(" +
229 | " fa INT," +
230 | " fb VARCHAR(100)" +
231 | ") WITH (" +
232 | " 'connector.type'='filesystem'," +
233 | " 'connector.path'='file://" + tmpFile1.getPath() + "'," +
234 | " 'format.type' = 'csv');");
235 | statement.executeUpdate("CREATE TABLE testTable2(" +
236 | " fc INT," +
237 | " fd VARCHAR(100)" +
238 | ") WITH (" +
239 | " 'connector.type'='filesystem'," +
240 | " 'connector.path'='file://" + tmpFile2.getPath() + "'," +
241 | " 'format.type' = 'csv');");
242 | compareStringResultsWithSorting(
243 | new String[]{"testTable1", "testTable2"}, statement.executeQuery("SHOW TABLES"));
244 |
245 | statement.executeUpdate("DROP TABLE testTable1");
246 | statement.executeUpdate("DROP TABLE testTable2");
247 | statement.executeUpdate("DROP DATABASE db12");
248 | statement.executeUpdate("USE CATALOG default_catalog");
249 | }
250 |
251 | @Test
252 | public void testMaxRows() throws SQLException {
253 | // max rows is smaller than actual result count
254 | statement.setMaxRows(2);
255 | try (ResultSet rs = statement.executeQuery("SELECT * FROM myTable ORDER BY a, b")) {
256 | Assert.assertTrue(rs.next());
257 | Assert.assertEquals(22, rs.getInt(1));
258 | Assert.assertEquals("BBB Hi", rs.getString(2));
259 | Assert.assertTrue(rs.next());
260 | Assert.assertEquals(32, rs.getInt(1));
261 | Assert.assertEquals("CCC World", rs.getString(2));
262 | Assert.assertFalse(rs.next());
263 | }
264 |
265 | // max rows is larger than actual result count
266 | statement.setMaxRows(5);
267 | try (ResultSet rs = statement.executeQuery("SELECT * FROM myTable ORDER BY a, b LIMIT 2")) {
268 | Assert.assertTrue(rs.next());
269 | Assert.assertEquals(22, rs.getInt(1));
270 | Assert.assertEquals("BBB Hi", rs.getString(2));
271 | Assert.assertTrue(rs.next());
272 | Assert.assertEquals(32, rs.getInt(1));
273 | Assert.assertEquals("CCC World", rs.getString(2));
274 | Assert.assertFalse(rs.next());
275 | }
276 | }
277 |
278 | private void compareStringResultsWithSorting(String[] expected, ResultSet actualResultSet) throws SQLException {
279 | Arrays.sort(expected);
280 |
281 | List