You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by li...@apache.org on 2023/04/26 11:52:39 UTC
[flink] branch master updated: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver
This is an automated email from the ASF dual-hosted git repository.
libenchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/master by this push:
new 63166226f65 [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver
63166226f65 is described below
commit 63166226f65cc4da80cc7661d76eb90032ec2222
Author: Shammon FY <zj...@gmail.com>
AuthorDate: Tue Apr 18 19:13:24 2023 +0800
[FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver
Close apache/flink#22417
---
flink-table/flink-sql-jdbc-driver/pom.xml | 12 ++
.../org/apache/flink/table/jdbc/BaseStatement.java | 238 +++++++++++++++++++++
.../apache/flink/table/jdbc/FlinkConnection.java | 2 +-
.../apache/flink/table/jdbc/FlinkResultSet.java | 5 +-
.../flink/table/jdbc/FlinkResultSetMetaData.java | 19 +-
.../apache/flink/table/jdbc/FlinkStatement.java | 157 ++++++++++++++
.../flink/table/jdbc/FlinkConnectionTest.java | 48 +----
.../flink/table/jdbc/FlinkJdbcDriverTestBase.java | 66 ++++++
.../table/jdbc/FlinkResultSetMetaDataTest.java | 7 +-
.../flink/table/jdbc/FlinkStatementTest.java | 123 +++++++++++
10 files changed, 619 insertions(+), 58 deletions(-)
diff --git a/flink-table/flink-sql-jdbc-driver/pom.xml b/flink-table/flink-sql-jdbc-driver/pom.xml
index 3d6968fa8e4..fc6b7b285c8 100644
--- a/flink-table/flink-sql-jdbc-driver/pom.xml
+++ b/flink-table/flink-sql-jdbc-driver/pom.xml
@@ -72,6 +72,18 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.flink</groupId>
+ <artifactId>flink-connector-files</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.flink</groupId>
+ <artifactId>flink-csv</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/BaseStatement.java b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/BaseStatement.java
new file mode 100644
index 00000000000..08bf3107f28
--- /dev/null
+++ b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/BaseStatement.java
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.jdbc;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.SQLWarning;
+import java.sql.Statement;
+
+/** Base statement in flink driver. */
+public abstract class BaseStatement implements Statement {
+ @Override
+ public int executeUpdate(String sql) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#executeUpdate is not supported yet.");
+ }
+
+ @Override
+ public int getMaxFieldSize() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getMaxFieldSize is not supported yet.");
+ }
+
+ @Override
+ public void setMaxFieldSize(int max) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setMaxFieldSize is not supported yet.");
+ }
+
+ @Override
+ public int getMaxRows() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getMaxRows is not supported yet.");
+ }
+
+ @Override
+ public void setMaxRows(int max) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setMaxRows is not supported yet.");
+ }
+
+ @Override
+ public void setEscapeProcessing(boolean enable) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setEscapeProcessing is not supported yet.");
+ }
+
+ @Override
+ public int getQueryTimeout() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getQueryTimeout is not supported yet.");
+ }
+
+ @Override
+ public void setQueryTimeout(int seconds) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setQueryTimeout is not supported yet.");
+ }
+
+ @Override
+ public SQLWarning getWarnings() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getWarnings is not supported yet.");
+ }
+
+ @Override
+ public void clearWarnings() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#clearWarnings is not supported yet.");
+ }
+
+ @Override
+ public void setCursorName(String name) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setCursorName is not supported yet.");
+ }
+
+ @Override
+ public void setFetchDirection(int direction) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setFetchDirection is not supported yet.");
+ }
+
+ @Override
+ public int getFetchDirection() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getFetchDirection is not supported yet.");
+ }
+
+ @Override
+ public void setFetchSize(int rows) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setFetchSize is not supported yet.");
+ }
+
+ @Override
+ public int getFetchSize() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getFetchSize is not supported yet.");
+ }
+
+ @Override
+ public int getResultSetConcurrency() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getResultSetConcurrency is not supported yet.");
+ }
+
+ @Override
+ public int getResultSetType() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getResultSetType is not supported yet.");
+ }
+
+ @Override
+ public void addBatch(String sql) throws SQLException {
+ throw new SQLFeatureNotSupportedException("FlinkStatement#addBatch is not supported yet.");
+ }
+
+ @Override
+ public void clearBatch() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#clearBatch is not supported yet.");
+ }
+
+ @Override
+ public int[] executeBatch() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#executeBatch is not supported yet.");
+ }
+
+ @Override
+ public boolean getMoreResults(int current) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getMoreResults is not supported yet.");
+ }
+
+ @Override
+ public ResultSet getGeneratedKeys() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getGeneratedKeys is not supported yet.");
+ }
+
+ @Override
+ public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#executeUpdate is not supported yet.");
+ }
+
+ @Override
+ public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#executeUpdate is not supported yet.");
+ }
+
+ @Override
+ public int executeUpdate(String sql, String[] columnNames) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#executeUpdate is not supported yet.");
+ }
+
+ @Override
+ public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
+ throw new SQLFeatureNotSupportedException("FlinkStatement#execute is not supported yet.");
+ }
+
+ @Override
+ public boolean execute(String sql, int[] columnIndexes) throws SQLException {
+ throw new SQLFeatureNotSupportedException("FlinkStatement#execute is not supported yet.");
+ }
+
+ @Override
+ public boolean execute(String sql, String[] columnNames) throws SQLException {
+ throw new SQLFeatureNotSupportedException("FlinkStatement#execute is not supported yet.");
+ }
+
+ @Override
+ public int getResultSetHoldability() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#getResultSetHoldability is not supported yet.");
+ }
+
+ @Override
+ public void setPoolable(boolean poolable) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#setPoolable is not supported yet.");
+ }
+
+ @Override
+ public boolean isPoolable() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#isPoolable is not supported yet.");
+ }
+
+ @Override
+ public <T> T unwrap(Class<T> iface) throws SQLException {
+ throw new SQLFeatureNotSupportedException("FlinkStatement#unwrap is not supported yet.");
+ }
+
+ @Override
+ public boolean isWrapperFor(Class<?> iface) throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#isWrapperFor is not supported yet.");
+ }
+
+ @Override
+ public int getUpdateCount() throws SQLException {
+ throw new SQLFeatureNotSupportedException("FlinkStatement#getUpdateCount is not supported");
+ }
+
+ @Override
+ public void closeOnCompletion() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#closeOnCompletion is not supported");
+ }
+
+ @Override
+ public boolean isCloseOnCompletion() throws SQLException {
+ throw new SQLFeatureNotSupportedException(
+ "FlinkStatement#isCloseOnCompletion is not supported");
+ }
+}
diff --git a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkConnection.java b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkConnection.java
index 9af04bcd7ba..7efe7de419f 100644
--- a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkConnection.java
+++ b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkConnection.java
@@ -56,7 +56,7 @@ public class FlinkConnection extends BaseConnection {
@Override
public Statement createStatement() throws SQLException {
- throw new SQLFeatureNotSupportedException();
+ return new FlinkStatement(this);
}
@VisibleForTesting
diff --git a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSet.java b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSet.java
index 6c8072b60ba..42bdcee30a4 100644
--- a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSet.java
+++ b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSet.java
@@ -51,6 +51,7 @@ public class FlinkResultSet extends BaseResultSet {
private final Statement statement;
private final StatementResult result;
private final DataConverter dataConverter;
+ private final FlinkResultSetMetaData resultSetMetaData;
private RowData currentRow;
private boolean wasNull;
@@ -67,6 +68,7 @@ public class FlinkResultSet extends BaseResultSet {
final ResolvedSchema schema = result.getResultSchema();
this.dataTypeList = schema.getColumnDataTypes();
this.columnNameList = schema.getColumnNames();
+ this.resultSetMetaData = new FlinkResultSetMetaData(columnNameList, dataTypeList);
}
@Override
@@ -338,8 +340,7 @@ public class FlinkResultSet extends BaseResultSet {
@Override
public ResultSetMetaData getMetaData() throws SQLException {
- // TODO support result set meta data
- throw new SQLFeatureNotSupportedException("FlinkResultSet#getMetaData is not supported");
+ return resultSetMetaData;
}
@Override
diff --git a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSetMetaData.java b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSetMetaData.java
index 172afcad191..0398d1c6806 100644
--- a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSetMetaData.java
+++ b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkResultSetMetaData.java
@@ -18,8 +18,8 @@
package org.apache.flink.table.jdbc;
-import org.apache.flink.table.catalog.ResolvedSchema;
import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.DataType;
import java.math.BigDecimal;
import java.sql.Array;
@@ -31,22 +31,21 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.OffsetDateTime;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import java.util.stream.Collectors;
/** Implementation of {@link ResultSetMetaData} for flink jdbc driver. */
public class FlinkResultSetMetaData implements ResultSetMetaData {
private final List<ColumnInfo> columnList;
- public FlinkResultSetMetaData(ResolvedSchema schema) {
- this.columnList =
- schema.getColumns().stream()
- .map(
- c ->
- ColumnInfo.fromLogicalType(
- c.getName(), c.getDataType().getLogicalType()))
- .collect(Collectors.toList());
+ public FlinkResultSetMetaData(List<String> columnNameList, List<DataType> columnTypeList) {
+ this.columnList = new ArrayList<>(columnNameList.size());
+ for (int i = 0; i < columnNameList.size(); i++) {
+ this.columnList.add(
+ ColumnInfo.fromLogicalType(
+ columnNameList.get(i), columnTypeList.get(i).getLogicalType()));
+ }
}
@Override
diff --git a/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java
new file mode 100644
index 00000000000..f0b27d17409
--- /dev/null
+++ b/flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.jdbc;
+
+import org.apache.flink.table.api.ResultKind;
+import org.apache.flink.table.client.gateway.Executor;
+import org.apache.flink.table.client.gateway.StatementResult;
+import org.apache.flink.table.jdbc.utils.StringDataConverter;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+
+/** Statement for flink jdbc driver. */
+@NotThreadSafe
+public class FlinkStatement extends BaseStatement {
+ private final Connection connection;
+ private final Executor executor;
+ private FlinkResultSet currentResults;
+ private boolean closed;
+
+ public FlinkStatement(FlinkConnection connection) {
+ this.connection = connection;
+ this.executor = connection.getExecutor();
+ this.currentResults = null;
+ }
+
+ /**
+ * Execute a SELECT query.
+ *
+ * @param sql an SQL statement to be sent to the database, typically a static SQL <code>SELECT
+ * </code> statement
+ * @return the select query result set.
+ * @throws SQLException the thrown exception
+ */
+ @Override
+ public ResultSet executeQuery(String sql) throws SQLException {
+ StatementResult result = executeInternal(sql);
+ if (!result.isQueryResult()) {
+ throw new SQLException(String.format("Statement[%s] is not a query.", sql));
+ }
+ currentResults = new FlinkResultSet(this, result, StringDataConverter.CONVERTER);
+
+ return currentResults;
+ }
+
+ private void clearCurrentResults() throws SQLException {
+ if (currentResults == null) {
+ return;
+ }
+ currentResults.close();
+ currentResults = null;
+ }
+
+ @Override
+ public void close() throws SQLException {
+ if (closed) {
+ return;
+ }
+
+ cancel();
+ closed = true;
+ }
+
+ @Override
+ public void cancel() throws SQLException {
+ checkClosed();
+ clearCurrentResults();
+ }
+
+ private void checkClosed() throws SQLException {
+ if (closed) {
+ throw new SQLException("This result set is already closed");
+ }
+ }
+
+ /**
+ * Execute a sql statement. Notice that the <code>INSERT</code> statement in Flink would return
+ * job id as result set.
+ *
+ * @param sql any SQL statement
+ * @return True if there is result set for the statement.
+ * @throws SQLException the thrown exception.
+ */
+ @Override
+ public boolean execute(String sql) throws SQLException {
+ StatementResult result = executeInternal(sql);
+ if (result.isQueryResult() || result.getResultKind() == ResultKind.SUCCESS_WITH_CONTENT) {
+ currentResults = new FlinkResultSet(this, result, StringDataConverter.CONVERTER);
+ return true;
+ }
+
+ return false;
+ }
+
+ private StatementResult executeInternal(String sql) throws SQLException {
+ checkClosed();
+ clearCurrentResults();
+
+ return executor.executeStatement(sql);
+ }
+
+ @Override
+ public ResultSet getResultSet() throws SQLException {
+ checkClosed();
+
+ if (currentResults == null) {
+ throw new SQLException("No result set in the current statement.");
+ }
+ if (currentResults.isClosed()) {
+ throw new SQLException("Result set has been closed");
+ }
+
+ return currentResults;
+ }
+
+ @Override
+ public boolean getMoreResults() throws SQLException {
+ checkClosed();
+
+ if (currentResults != null) {
+ cancel();
+ return false;
+ }
+
+ throw new SQLFeatureNotSupportedException("Multiple open results not supported");
+ }
+
+ @Override
+ public Connection getConnection() throws SQLException {
+ return connection;
+ }
+
+ @Override
+ public boolean isClosed() throws SQLException {
+ return closed;
+ }
+}
diff --git a/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkConnectionTest.java b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkConnectionTest.java
index 4b543f923ce..9311175d17d 100644
--- a/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkConnectionTest.java
+++ b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkConnectionTest.java
@@ -18,17 +18,10 @@
package org.apache.flink.table.jdbc;
-import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
import org.apache.flink.table.client.gateway.Executor;
-import org.apache.flink.table.client.gateway.SingleSessionManager;
import org.apache.flink.table.client.gateway.StatementResult;
-import org.apache.flink.table.gateway.rest.util.SqlGatewayRestEndpointExtension;
-import org.apache.flink.table.gateway.service.utils.SqlGatewayServiceExtension;
-import org.apache.flink.test.junit5.MiniClusterExtension;
-import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
import java.sql.SQLException;
import java.util.Properties;
@@ -38,37 +31,11 @@ import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** Tests for {@link FlinkConnection}. */
-public class FlinkConnectionTest {
- @RegisterExtension
- @Order(1)
- private static final MiniClusterExtension MINI_CLUSTER_RESOURCE =
- new MiniClusterExtension(
- new MiniClusterResourceConfiguration.Builder()
- .setNumberTaskManagers(1)
- .setNumberSlotsPerTaskManager(4)
- .build());
-
- @RegisterExtension
- @Order(2)
- public static final SqlGatewayServiceExtension SQL_GATEWAY_SERVICE_EXTENSION =
- new SqlGatewayServiceExtension(
- MINI_CLUSTER_RESOURCE::getClientConfiguration, SingleSessionManager::new);
-
- @RegisterExtension
- @Order(3)
- private static final SqlGatewayRestEndpointExtension SQL_GATEWAY_REST_ENDPOINT_EXTENSION =
- new SqlGatewayRestEndpointExtension(SQL_GATEWAY_SERVICE_EXTENSION::getService);
+public class FlinkConnectionTest extends FlinkJdbcDriverTestBase {
@Test
public void testCatalogSchema() throws Exception {
- DriverUri driverUri =
- DriverUri.create(
- String.format(
- "jdbc:flink://%s:%s",
- SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetAddress(),
- SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetPort()),
- new Properties());
- try (FlinkConnection connection = new FlinkConnection(driverUri)) {
+ try (FlinkConnection connection = new FlinkConnection(getDriverUri())) {
assertEquals("default_catalog", connection.getCatalog());
assertEquals("default_database", connection.getSchema());
@@ -106,14 +73,9 @@ public class FlinkConnectionTest {
public void testClientInfo() throws Exception {
Properties properties = new Properties();
properties.setProperty("key3", "val3");
- DriverUri driverUri =
- DriverUri.create(
- String.format(
- "jdbc:flink://%s:%s?key1=val1&key2=val2",
- SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetAddress(),
- SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetPort()),
- properties);
- try (FlinkConnection connection = new FlinkConnection(driverUri)) {
+ try (FlinkConnection connection =
+ new FlinkConnection(
+ getDriverUri("jdbc:flink://%s:%s?key1=val1&key2=val2", properties))) {
assertEquals("val1", connection.getClientInfo("key1"));
assertEquals("val2", connection.getClientInfo("key2"));
assertEquals("val3", connection.getClientInfo("key3"));
diff --git a/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkJdbcDriverTestBase.java b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkJdbcDriverTestBase.java
new file mode 100644
index 00000000000..2e25d898c16
--- /dev/null
+++ b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkJdbcDriverTestBase.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.jdbc;
+
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.table.client.gateway.SingleSessionManager;
+import org.apache.flink.table.gateway.rest.util.SqlGatewayRestEndpointExtension;
+import org.apache.flink.table.gateway.service.utils.SqlGatewayServiceExtension;
+import org.apache.flink.test.junit5.MiniClusterExtension;
+
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.util.Properties;
+
+/** Base test for flink jdbc driver which will launch flink cluster and sql gatewa. */
+public class FlinkJdbcDriverTestBase {
+ @RegisterExtension
+ @Order(1)
+ private static final MiniClusterExtension MINI_CLUSTER_RESOURCE =
+ new MiniClusterExtension(
+ new MiniClusterResourceConfiguration.Builder()
+ .setNumberTaskManagers(2)
+ .setNumberSlotsPerTaskManager(4)
+ .build());
+
+ @RegisterExtension
+ @Order(2)
+ private static final SqlGatewayServiceExtension SQL_GATEWAY_SERVICE_EXTENSION =
+ new SqlGatewayServiceExtension(
+ MINI_CLUSTER_RESOURCE::getClientConfiguration, SingleSessionManager::new);
+
+ @RegisterExtension
+ @Order(3)
+ private static final SqlGatewayRestEndpointExtension SQL_GATEWAY_REST_ENDPOINT_EXTENSION =
+ new SqlGatewayRestEndpointExtension(SQL_GATEWAY_SERVICE_EXTENSION::getService);
+
+ protected DriverUri getDriverUri() throws Exception {
+ return getDriverUri("jdbc:flink://%s:%s", new Properties());
+ }
+
+ protected DriverUri getDriverUri(String url, Properties properties) throws Exception {
+ return DriverUri.create(
+ String.format(
+ url,
+ SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetAddress(),
+ SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetPort()),
+ properties);
+ }
+}
diff --git a/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkResultSetMetaDataTest.java b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkResultSetMetaDataTest.java
index f589bf064af..4c31e5ed140 100644
--- a/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkResultSetMetaDataTest.java
+++ b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkResultSetMetaDataTest.java
@@ -64,7 +64,8 @@ public class FlinkResultSetMetaDataTest {
Column.physical("c17", DataTypes.ROW()),
Column.physical(
"c18", DataTypes.MAP(DataTypes.STRING(), DataTypes.STRING())));
- FlinkResultSetMetaData metaData = new FlinkResultSetMetaData(schema);
+ FlinkResultSetMetaData metaData =
+ new FlinkResultSetMetaData(schema.getColumnNames(), schema.getColumnDataTypes());
assertEquals(18, metaData.getColumnCount());
for (int i = 1; i <= 18; i++) {
@@ -118,7 +119,9 @@ public class FlinkResultSetMetaDataTest {
ResolvedSchema.of(Column.physical("c1", DataTypes.MULTISET(DataTypes.STRING())));
assertThrowsExactly(
RuntimeException.class,
- () -> new FlinkResultSetMetaData(schema),
+ () ->
+ new FlinkResultSetMetaData(
+ schema.getColumnNames(), schema.getColumnDataTypes()),
"Not supported type[MULTISET<STRING>]");
}
}
diff --git a/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkStatementTest.java b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkStatementTest.java
new file mode 100644
index 00000000000..0892f8fba41
--- /dev/null
+++ b/flink-table/flink-sql-jdbc-driver/src/test/java/org/apache/flink/table/jdbc/FlinkStatementTest.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.jdbc;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** Tests for flink statement. */
+public class FlinkStatementTest extends FlinkJdbcDriverTestBase {
+ @TempDir private Path tempDir;
+
+ @Test
+ @Timeout(value = 60)
+ public void testExecuteQuery() throws Exception {
+ try (FlinkConnection connection = new FlinkConnection(getDriverUri())) {
+ try (Statement statement = connection.createStatement()) {
+ // CREATE TABLE is not a query and has no results
+ assertFalse(
+ statement.execute(
+ String.format(
+ "CREATE TABLE test_table(id bigint, val int, str string) "
+ + "with ("
+ + "'connector'='filesystem',\n"
+ + "'format'='csv',\n"
+ + "'path'='%s')",
+ tempDir)));
+
+ // INSERT TABLE returns job id
+ assertTrue(
+ statement.execute(
+ "INSERT INTO test_table VALUES "
+ + "(1, 11, '111'), "
+ + "(3, 33, '333'), "
+ + "(2, 22, '222'), "
+ + "(4, 44, '444')"));
+ String jobId;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ assertTrue(resultSet.next());
+ assertEquals(1, resultSet.getMetaData().getColumnCount());
+ jobId = resultSet.getString("job id");
+ assertEquals(jobId, resultSet.getString(1));
+ assertFalse(resultSet.next());
+ }
+ assertNotNull(jobId);
+ // Wait job finished
+ boolean jobFinished = false;
+ while (!jobFinished) {
+ assertTrue(statement.execute("SHOW JOBS"));
+ try (ResultSet resultSet = statement.getResultSet()) {
+ while (resultSet.next()) {
+ if (resultSet.getString(1).equals(jobId)) {
+ if (resultSet.getString(3).equals("FINISHED")) {
+ jobFinished = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ // SELECT all data from test_table
+ try (ResultSet resultSet = statement.executeQuery("SELECT * FROM test_table")) {
+ assertEquals(3, resultSet.getMetaData().getColumnCount());
+ List<String> resultList = new ArrayList<>();
+ while (resultSet.next()) {
+ assertEquals(resultSet.getLong("id"), resultSet.getLong(1));
+ assertEquals(resultSet.getInt("val"), resultSet.getInt(2));
+ assertEquals(resultSet.getString("str"), resultSet.getString(3));
+ resultList.add(
+ String.format(
+ "%s,%s,%s",
+ resultSet.getLong("id"),
+ resultSet.getInt("val"),
+ resultSet.getString("str")));
+ }
+ assertThat(resultList)
+ .containsExactlyInAnyOrder(
+ "1,11,111", "2,22,222", "3,33,333", "4,44,444");
+ }
+
+ assertTrue(statement.execute("SHOW JOBS"));
+ try (ResultSet resultSet = statement.getResultSet()) {
+ // Check there are two finished jobs.
+ int count = 0;
+ while (resultSet.next()) {
+ assertEquals("FINISHED", resultSet.getString(3));
+ count++;
+ }
+ assertEquals(2, count);
+ }
+ }
+ }
+ }
+}