paul-rogers 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_r348112152
 
 

 ##########
 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() {
+    if (resultSetWriter != null) {
+
+      // Need to build a new writer. Close this one. Doing so
+      // will tear down the whole show. But, the vector cache will
+      // ensure that the new writer reuses any matching vectors from
+      // the prior batch to provide vector persistence as Drill expects.
+
+      resultSetWriter.close();
+    }
+    TupleMetadata schema = 
MetadataUtils.fromFields(resultSetReader.inputBatch().schema());
+    writerOptions.setSchema(schema);
+    resultSetWriter = new ResultSetLoaderImpl(allocator, 
writerOptions.build());
+    rowWriter = resultSetWriter.writer();
+    currentSchemaVersion = resultSetReader.inputBatch().schemaVersion();
+
+    int colCount = schema.size();
+    projection = new CopyPair[colCount];
+    for (int i = 0; i < colCount; i++) {
+      projection[i] = new CopyPair(
+          rowWriter.column(i).writer(),
+          rowReader.column(i).reader());
+    }
+  }
+
+  @Override
+  public boolean hasRows() {
+    switch (state) {
+    case BATCH_ACTIVE:
+    case NEW_SCHEMA:
+      return resultSetWriter.hasRows();
+    default:
+      return false;
+    }
+  }
+
+  @Override
+  public boolean isFull() {
+    switch (state) {
+    case BATCH_ACTIVE:
+      return rowWriter.isFull();
+    case NEW_SCHEMA:
+      return true;
+    default:
+      return false;
+    }
+  }
+
+  protected void verifyWritable() {
+    Preconditions.checkState(state != State.NEW_SCHEMA,
+        "Must harvest current batch to flush for new schema.");
+    Preconditions.checkState(state == State.BATCH_ACTIVE,
+        "Start an output batch before copying");
+    Preconditions.checkState(!isCopyPending(),
+        "Resume the in-flight copy before copying another");
+    Preconditions.checkState(!rowWriter.isFull(),
+        "Output batch is full; harvest before adding more");
+  }
+
+  @Override
+  public boolean copyNext() {
+    verifyWritable();
+    if (!rowReader.next()) {
+      return false;
+    }
+    project();
+    return true;
+  }
+
+  @Override
+  public void copyRecord(int posn) {
+    verifyWritable();
+    rowReader.setPosition(posn);
+    project();
+  }
+
+  private final void project() {
 
 Review comment:
   Renamed to `copyColumns()` (which is what "project" means.) The `final` has 
meaning for a method: it means that there will never be overrides and so the 
JVM can optimize calls, which is helpful given that this is called once per 
rows.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to