Repository: calcite
Updated Branches:
  refs/heads/master 8c90d8621 -> d27e642c4


[CALCITE-1086] Avoid sending Signature on Execute for updates


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/1a9be678
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/1a9be678
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/1a9be678

Branch: refs/heads/master
Commit: 1a9be678313267963b82888a00878914d372d01c
Parents: 8c90d86
Author: Josh Elser <[email protected]>
Authored: Wed Mar 2 17:43:25 2016 -0500
Committer: Josh Elser <[email protected]>
Committed: Wed Mar 2 19:06:30 2016 -0500

----------------------------------------------------------------------
 .../apache/calcite/avatica/AvaticaConnection.java | 18 ++++++++++++++----
 .../calcite/avatica/AvaticaPreparedStatement.java | 10 ++++++----
 .../apache/calcite/avatica/AvaticaStatement.java  | 10 ++++++----
 .../java/org/apache/calcite/avatica/Meta.java     | 18 ++++++++++++++----
 4 files changed, 40 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/1a9be678/avatica/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
----------------------------------------------------------------------
diff --git 
a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java 
b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
index 4076c4e..2d89f45 100644
--- a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
+++ b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
@@ -115,7 +115,7 @@ public abstract class AvaticaConnection implements 
Connection {
   }
 
   /** Computes the number of retries
-   * {@link AvaticaStatement#executeInternal(Meta.Signature)}
+   * {@link AvaticaStatement#executeInternal(Meta.Signature, boolean)}
    * should retry before failing. */
   long getNumStatementRetries(Properties props) {
     return Long.valueOf(Objects.requireNonNull(props)
@@ -436,11 +436,13 @@ public abstract class AvaticaConnection implements 
Connection {
    * @param signature     Prepared query
    * @param firstFrame    First frame of rows, or null if we need to execute
    * @param state         The state used to create the given result
+   * @param isUpdate      Was the caller context via {@link 
PreparedStatement#executeUpdate()}.
    * @return Result set
    * @throws java.sql.SQLException if a database error occurs
    */
   protected ResultSet executeQueryInternal(AvaticaStatement statement,
-      Meta.Signature signature, Meta.Frame firstFrame, QueryState state) 
throws SQLException {
+      Meta.Signature signature, Meta.Frame firstFrame, QueryState state, 
boolean isUpdate)
+      throws SQLException {
     // Close the previous open result set, if there is one.
     Meta.Frame frame = firstFrame;
     Meta.Signature signature2 = signature;
@@ -460,8 +462,15 @@ public abstract class AvaticaConnection implements 
Connection {
       try {
         if (statement.isWrapperFor(AvaticaPreparedStatement.class)) {
           final AvaticaPreparedStatement pstmt = (AvaticaPreparedStatement) 
statement;
+          Meta.StatementHandle handle = pstmt.handle;
+          if (isUpdate) {
+            // Make a copy of the StatementHandle, nulling out the Signature.
+            // CALCITE-1086 we don't need to send the Signature to the server
+            // when we're only performing an update. Saves on serialization.
+            handle = new Meta.StatementHandle(handle.connectionId, handle.id, 
null);
+          }
           final Meta.ExecuteResult executeResult =
-              meta.execute(pstmt.handle, pstmt.getParameterValues(),
+              meta.execute(handle, pstmt.getParameterValues(),
                   statement.getFetchSize());
           final MetaResultSet metaResultSet = executeResult.resultSets.get(0);
           frame = metaResultSet.firstFrame;
@@ -577,8 +586,9 @@ public abstract class AvaticaConnection implements 
Connection {
     final Meta.StatementHandle h = new Meta.StatementHandle(
         metaResultSet.connectionId, metaResultSet.statementId, null);
     final AvaticaStatement statement = lookupStatement(h);
+    // These are all the metadata operations, no updates
     ResultSet resultSet = executeQueryInternal(statement, 
metaResultSet.signature.sanitize(),
-        metaResultSet.firstFrame, state);
+        metaResultSet.firstFrame, state, false);
     if (metaResultSet.ownStatement) {
       resultSet.getStatement().closeOnCompletion();
     }

http://git-wip-us.apache.org/repos/asf/calcite/blob/1a9be678/avatica/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
----------------------------------------------------------------------
diff --git 
a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
 
b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
index 4f1c726..f3a950a 100644
--- 
a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
+++ 
b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
@@ -109,7 +109,7 @@ public abstract class AvaticaPreparedStatement
     this.updateCount = -1;
     final Signature sig = getSignature();
     return getConnection().executeQueryInternal(this, sig, null,
-        new QueryState(sig.sql));
+        new QueryState(sig.sql), false);
   }
 
   public ParameterMetaData getParameterMetaData() throws SQLException {
@@ -121,8 +121,8 @@ public abstract class AvaticaPreparedStatement
   }
 
   public long executeLargeUpdate() throws SQLException {
-    getConnection().executeQueryInternal(this, getSignature(), null,
-        new QueryState(getSignature().sql));
+    getConnection().executeQueryInternal(this, null, null,
+        new QueryState(getSignature().sql), true);
     return updateCount;
   }
 
@@ -203,8 +203,10 @@ public abstract class AvaticaPreparedStatement
 
   public boolean execute() throws SQLException {
     this.updateCount = -1;
+    // We don't know if this is actually an update or a query, so call it a 
query so we pass the
+    // Signature to the server.
     getConnection().executeQueryInternal(this, getSignature(), null,
-        new QueryState(getSignature().sql));
+        new QueryState(getSignature().sql), false);
     // Result set is null for DML or DDL.
     // Result set is closed if user cancelled the query.
     return openResultSet != null && !openResultSet.isClosed();

http://git-wip-us.apache.org/repos/asf/calcite/blob/1a9be678/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
----------------------------------------------------------------------
diff --git 
a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java 
b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
index ca73f43..cfd1d45 100644
--- a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
+++ b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
@@ -456,13 +456,14 @@ public abstract class AvaticaStatement
    * Executes a prepared statement.
    *
    * @param signature Parsed statement
+   * @param isUpdate if the execute is for an update
    *
    * @return as specified by {@link java.sql.Statement#execute(String)}
    * @throws java.sql.SQLException if a database error occurs
    */
-  protected boolean executeInternal(Meta.Signature signature)
+  protected boolean executeInternal(Meta.Signature signature, boolean isUpdate)
       throws SQLException {
-    ResultSet resultSet = executeQueryInternal(signature);
+    ResultSet resultSet = executeQueryInternal(signature, isUpdate);
     // user may have cancelled the query
     if (resultSet.isClosed()) {
       return false;
@@ -474,12 +475,13 @@ public abstract class AvaticaStatement
    * Executes a prepared query, closing any previously open result set.
    *
    * @param signature Parsed query
+   * @param isUpdate If the execute is for an update
    * @return Result set
    * @throws java.sql.SQLException if a database error occurs
    */
-  protected ResultSet executeQueryInternal(Meta.Signature signature)
+  protected ResultSet executeQueryInternal(Meta.Signature signature, boolean 
isUpdate)
       throws SQLException {
-    return connection.executeQueryInternal(this, signature, null, null);
+    return connection.executeQueryInternal(this, signature, null, null, 
isUpdate);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/calcite/blob/1a9be678/avatica/src/main/java/org/apache/calcite/avatica/Meta.java
----------------------------------------------------------------------
diff --git a/avatica/src/main/java/org/apache/calcite/avatica/Meta.java 
b/avatica/src/main/java/org/apache/calcite/avatica/Meta.java
index 4f1d56a..a83f2b3 100644
--- a/avatica/src/main/java/org/apache/calcite/avatica/Meta.java
+++ b/avatica/src/main/java/org/apache/calcite/avatica/Meta.java
@@ -1129,13 +1129,23 @@ public interface Meta {
     }
 
     public Common.StatementHandle toProto() {
-      return Common.StatementHandle.newBuilder().setConnectionId(connectionId)
-          .setId(id).setSignature(signature.toProto()).build();
+      Common.StatementHandle.Builder builder = 
Common.StatementHandle.newBuilder()
+          .setConnectionId(connectionId).setId(id);
+      if (null != signature) {
+        builder.setSignature(signature.toProto());
+      }
+      return builder.build();
     }
 
     public static StatementHandle fromProto(Common.StatementHandle 
protoHandle) {
-      return new StatementHandle(protoHandle.getConnectionId(), 
protoHandle.getId(),
-          Signature.fromProto(protoHandle.getSignature()));
+      final Descriptor desc = protoHandle.getDescriptorForType();
+      // Signature is optional in the update path for executes.
+      Signature signature = null;
+      if (ProtobufService.hasField(protoHandle, desc,
+          Common.StatementHandle.SIGNATURE_FIELD_NUMBER)) {
+        signature = Signature.fromProto(protoHandle.getSignature());
+      }
+      return new StatementHandle(protoHandle.getConnectionId(), 
protoHandle.getId(), signature);
     }
 
     @Override public int hashCode() {

Reply via email to