You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "FangYongs (via GitHub)" <gi...@apache.org> on 2023/04/18 11:16:35 UTC

[GitHub] [flink] FangYongs opened a new pull request, #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

FangYongs opened a new pull request, #22417:
URL: https://github.com/apache/flink/pull/22417

   ## What is the purpose of the change
   
   This PR aims to introduce statement for flink jdbc driver.
   
   ## Brief change log
   
     - Add FlinkStatement for flink jdbc driver
     - Create flink result set in flink result set
     - Create flink statement in flink connection
   
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - Added FlinkStatementTest to execute ddl/insert/sql queries
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (yes / no) no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / no) no
     - The serializers: (yes / no / don't know) no
     - The runtime per-record code paths (performance sensitive): (yes / no / don't know) no
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (yes / no / don't know) no
     - The S3 file system connector: (yes / no / don't know) no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes / no) no
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] FangYongs commented on a diff in pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

Posted by "FangYongs (via GitHub)" <gi...@apache.org>.
FangYongs commented on code in PR #22417:
URL: https://github.com/apache/flink/pull/22417#discussion_r1175014855


##########
flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Statement for flink jdbc driver. */
+public class FlinkStatement extends BaseStatement {
+    private final Connection connection;
+    private final Executor executor;
+    private final AtomicReference<FlinkResultSet> currentResults;

Review Comment:
   Good point, I will add `@NotThreadSafe` for `FlinkStatement`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] flinkbot commented on pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #22417:
URL: https://github.com/apache/flink/pull/22417#issuecomment-1512903147

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "401630b8cce4a358c25cb1a839fefe9d2a051f0a",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "401630b8cce4a358c25cb1a839fefe9d2a051f0a",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 401630b8cce4a358c25cb1a839fefe9d2a051f0a UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] FangYongs commented on pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

Posted by "FangYongs (via GitHub)" <gi...@apache.org>.
FangYongs commented on PR #22417:
URL: https://github.com/apache/flink/pull/22417#issuecomment-1519857974

   Thanks @libenchao , I have updated this PR. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] libenchao closed pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

Posted by "libenchao (via GitHub)" <gi...@apache.org>.
libenchao closed pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver
URL: https://github.com/apache/flink/pull/22417


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] FangYongs commented on a diff in pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

Posted by "FangYongs (via GitHub)" <gi...@apache.org>.
FangYongs commented on code in PR #22417:
URL: https://github.com/apache/flink/pull/22417#discussion_r1175092974


##########
flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Statement for flink jdbc driver. */
+public class FlinkStatement extends BaseStatement {
+    private final Connection connection;
+    private final Executor executor;
+    private final AtomicReference<FlinkResultSet> currentResults;
+    private volatile boolean closed;
+
+    public FlinkStatement(FlinkConnection connection) {
+        this.connection = connection;
+        this.executor = connection.getExecutor();
+        this.currentResults = new AtomicReference<>();
+    }
+
+    @Override
+    public ResultSet executeQuery(String sql) throws SQLException {
+        if (!execute(sql)) {
+            throw new SQLException(String.format("Statement[%s] is not a query.", sql));
+        }
+
+        return currentResults.get();
+    }
+
+    private void clearCurrentResults() throws SQLException {
+        FlinkResultSet resultSet = currentResults.get();
+        if (resultSet == null) {
+            return;
+        }
+        resultSet.close();
+        currentResults.set(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");
+        }
+    }
+
+    @Override
+    public boolean execute(String sql) throws SQLException {
+        checkClosed();
+        clearCurrentResults();
+
+        StatementResult result = executor.executeStatement(sql);
+        FlinkResultSet resultSet = new FlinkResultSet(this, result, StringDataConverter.CONVERTER);
+        currentResults.set(resultSet);
+
+        return resultSet.isQuery() || result.getResultKind() == ResultKind.SUCCESS_WITH_CONTENT;

Review Comment:
   I have removed the `isQuery()` method in ResultSet. 
   1. According to the `ResultSet`, `executeQuery` will only execute `SELECT` statement and the `StatementResult.isQueryResult` is `True`. 
   2. For other statement such as `SHOW JOBS`, `SHOW TABLES`, `INSERT` and other statements, Flink will return `StatementResult` with `isQueryResult` is `False` and `resultKind` is `SUCCESS_WITH_CONTENT`. I return `true` in `FlinkStatement.execute` method for them and users can get result set from `FlinkStatement`
   3. For regular jdbc, `INSERT` will not return a result set, but Flink will. I have added doc for `FlinkStatement.execute` to clarify this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] libenchao commented on a diff in pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

Posted by "libenchao (via GitHub)" <gi...@apache.org>.
libenchao commented on code in PR #22417:
URL: https://github.com/apache/flink/pull/22417#discussion_r1174521573


##########
flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Statement for flink jdbc driver. */
+public class FlinkStatement extends BaseStatement {
+    private final Connection connection;
+    private final Executor executor;
+    private final AtomicReference<FlinkResultSet> currentResults;
+    private volatile boolean closed;
+
+    public FlinkStatement(FlinkConnection connection) {
+        this.connection = connection;
+        this.executor = connection.getExecutor();
+        this.currentResults = new AtomicReference<>();
+    }
+
+    @Override
+    public ResultSet executeQuery(String sql) throws SQLException {
+        if (!execute(sql)) {
+            throw new SQLException(String.format("Statement[%s] is not a query.", sql));
+        }
+
+        return currentResults.get();
+    }
+
+    private void clearCurrentResults() throws SQLException {
+        FlinkResultSet resultSet = currentResults.get();
+        if (resultSet == null) {
+            return;
+        }
+        resultSet.close();
+        currentResults.set(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");
+        }
+    }
+
+    @Override
+    public boolean execute(String sql) throws SQLException {
+        checkClosed();
+        clearCurrentResults();
+
+        StatementResult result = executor.executeStatement(sql);
+        FlinkResultSet resultSet = new FlinkResultSet(this, result, StringDataConverter.CONVERTER);
+        currentResults.set(resultSet);
+
+        return resultSet.isQuery() || result.getResultKind() == ResultKind.SUCCESS_WITH_CONTENT;

Review Comment:
   `resultSet.isQuery()` already contains `result.getResultKind() == ResultKind.SUCCESS_WITH_CONTENT`? We may need to make `FlinkResultSet#isQuery` more clear about what it really means.



##########
flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Statement for flink jdbc driver. */
+public class FlinkStatement extends BaseStatement {
+    private final Connection connection;
+    private final Executor executor;
+    private final AtomicReference<FlinkResultSet> currentResults;

Review Comment:
   Do we need to use `AtomicReference` here? I'm wondering that whether we need to guarantee 'thread-safe' for flink jdbc, if not, then I guess it's not necessary to use `AtomicReference`; if yes, we may need to rethink all APIs for `Connection`, `Statement`, `ResultSet`. What do you think about this?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org