ihuzenko commented on a change in pull request #1899: DRILL-7445: Create batch 
copier based on result set framework
URL: https://github.com/apache/drill/pull/1899#discussion_r347362163
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/physical/resultSet/impl/ResultSetCopierImpl.java
 ##########
 @@ -0,0 +1,321 @@
+/*
+ * 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.drill.exec.physical.resultSet.impl;
+
+import org.apache.drill.exec.memory.BufferAllocator;
+import org.apache.drill.exec.physical.impl.protocol.BatchAccessor;
+import org.apache.drill.exec.physical.resultSet.ResultSetCopier;
+import org.apache.drill.exec.physical.resultSet.ResultSetLoader;
+import org.apache.drill.exec.physical.resultSet.ResultSetReader;
+import org.apache.drill.exec.physical.resultSet.RowSetLoader;
+import org.apache.drill.exec.physical.rowSet.RowSetReader;
+import org.apache.drill.exec.record.VectorContainer;
+import org.apache.drill.exec.record.metadata.MetadataUtils;
+import org.apache.drill.exec.record.metadata.TupleMetadata;
+import org.apache.drill.exec.vector.accessor.ColumnReader;
+import org.apache.drill.exec.vector.accessor.ColumnWriter;
+import org.apache.drill.shaded.guava.com.google.common.base.Preconditions;
+
+public class ResultSetCopierImpl implements ResultSetCopier {
+
+  private enum State {
+    START,
+    NO_SCHEMA,
+    BETWEEN_BATCHES,
+    BATCH_ACTIVE,
+    NEW_SCHEMA,
+    SCHEMA_PENDING,
+    CLOSED
+  }
+
+  private interface BlockCopy {
+    void copy();
+    boolean hasMore();
+  }
+
+  private class CopyAll implements BlockCopy {
+
+    @Override
+    public void copy() {
+      while (!rowWriter.isFull() && rowReader.next()) {
+        project();
+      }
+    }
+
+    @Override
+    public boolean hasMore() {
+      return rowReader.hasNext();
+    }
+  }
+
+  private static class CopyPair {
+    protected final ColumnWriter writer;
+    protected final ColumnReader reader;
+
+    protected CopyPair(ColumnWriter writer, ColumnReader reader) {
+      this.writer = writer;
+      this.reader = reader;
+    }
+  }
+
+  // Input state
+
+  private int currentSchemaVersion = -1;
+  private final ResultSetReader resultSetReader;
+  protected RowSetReader rowReader;
+
+  // Output state
+
+  private final BufferAllocator allocator;
+  private final OptionBuilder writerOptions;
+  private ResultSetLoader resultSetWriter;
+  private RowSetLoader rowWriter;
+
+  // Copy state
+
+  private State state;
+  private CopyPair[] projection;
+  private BlockCopy activeCopy;
+
+  public ResultSetCopierImpl(BufferAllocator allocator, BatchAccessor 
inputBatch) {
+    this(allocator, inputBatch, new OptionBuilder());
+  }
+
+  public ResultSetCopierImpl(BufferAllocator allocator, BatchAccessor 
inputBatch,
+      OptionBuilder outputOptions) {
+    this.allocator = allocator;
+    resultSetReader = new ResultSetReaderImpl(inputBatch);
+    writerOptions = outputOptions;
+    writerOptions.setVectorCache(new ResultVectorCacheImpl(allocator));
+    state = State.START;
+  }
+
+  @Override
+  public void startBatch() {
+    if (state == State.START) {
+
+      // No schema yet. Defer real batch start until we see an input
+      // batch.
+
+      state = State.NO_SCHEMA;
+      return;
+    }
+    Preconditions.checkState(state == State.BETWEEN_BATCHES || state == 
State.SCHEMA_PENDING);
+    if (state == State.SCHEMA_PENDING) {
+
+      // We have a pending new schema. Create new writers to match.
+
+      createMapping();
+    }
+    resultSetWriter.startBatch();
+    state = State.BATCH_ACTIVE;
+    if (isCopyPending()) {
+
+      // Resume copying if a copy is active.
+
+      copyBlock();
+    }
+  }
+
+  @Override
+  public void startInput() {
+    Preconditions.checkState(state == State.NO_SCHEMA || state == 
State.NEW_SCHEMA ||
+                             state == State.BATCH_ACTIVE,
+        "Can only start input while in an output batch");
+    Preconditions.checkState(!isCopyPending(),
+        "Finish the pending copy before changing input");
+
+    bindInput();
+
+    if (state == State.BATCH_ACTIVE) {
+
+      // If no schema change, we are ready to copy.
+
+      if (currentSchemaVersion == 
resultSetReader.inputBatch().schemaVersion()) {
+        return;
+      }
+
+      // The schema has changed. Handle it now or later.
+
+      if (hasRows()) {
+
+        // Output batch has rows. Can't switch and bind inputs
+        // until current batch is sent downstream.
+
+        state = State.NEW_SCHEMA;
+        return;
+      }
+    }
+
+    // The schema changed: first schema, or a change while a bath
+    // is active, but is empty.
+
+    if (state == State.NO_SCHEMA) {
+      state = State.BATCH_ACTIVE;
+    } else {
+
+      // Discard the unused empty batch
+
+      harvest().zeroVectors();
+    }
+    createMapping();
+    resultSetWriter.startBatch();
+
+    // Stay in the current state.
+  }
+
+  protected void bindInput() {
+    resultSetReader.start();
+    rowReader = resultSetReader.reader();
+  }
+
+  @Override
+  public void freeInput() {
+    Preconditions.checkState(state != State.CLOSED);
+    resultSetReader.release();
+  }
+
+  private void createMapping() {
 
 Review comment:
   ```suggestion
     private void createProjection() {
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to