paul-rogers commented on a change in pull request #1633: DRILL-7024: Refactor ColumnWriter to simplify type-conversion shim URL: https://github.com/apache/drill/pull/1633#discussion_r254568782
########## File path: exec/vector/src/main/java/org/apache/drill/exec/vector/accessor/writer/AbstractScalarWriterImpl.java ########## @@ -0,0 +1,186 @@ +/* + * 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.vector.accessor.writer; + +import org.apache.drill.exec.record.metadata.ColumnMetadata; +import org.apache.drill.exec.vector.BaseDataValueVector; +import org.apache.drill.exec.vector.accessor.ColumnConversionFactory; +import org.apache.drill.exec.vector.accessor.ColumnWriter; +import org.apache.drill.exec.vector.accessor.ColumnWriterIndex; +import org.apache.drill.exec.vector.accessor.ObjectType; +import org.apache.drill.exec.vector.accessor.ScalarWriter; +import org.apache.drill.exec.vector.accessor.impl.HierarchicalFormatter; + +/** + * Column writer implementation that acts as the basis for the + * generated, vector-specific implementations. All set methods + * throw an exception; subclasses simply override the supported + * method(s). + */ + +public abstract class AbstractScalarWriterImpl extends AbstractScalarWriter implements WriterEvents { + + /** + * Wraps a scalar writer and its event handler to provide a uniform + * JSON-like interface for all writer types. + * <p> + * The client sees only the scalar writer API. But, internals need + * visibility into a rather complex set of events to orchestrate + * vector events: mostly sent to the writer, but some times sent + * from the writer, such as vector overflow. Separating the two + * concepts makes it easier to add type-conversion shims on top of + * the actual vector writer. + */ + public static class ScalarObjectWriter extends AbstractObjectWriter { + + private final WriterEvents writerEvents; + private ScalarWriter scalarWriter; + + public ScalarObjectWriter(AbstractScalarWriterImpl scalarWriter) { + final ColumnMetadata metadata = scalarWriter.schema(); + final ColumnConversionFactory factory = metadata.typeConverter(); + writerEvents = scalarWriter; + if (factory == null) { + this.scalarWriter = scalarWriter; + } else { + this.scalarWriter = factory.newWriter(metadata, scalarWriter); + } + } + + @Override + public ScalarWriter scalar() { return scalarWriter; } + + @Override + public ColumnWriter writer() { return scalarWriter; } + + @Override + public WriterEvents events() { return writerEvents; } + + @Override + public void dump(HierarchicalFormatter format) { + format + .startObject(this) + .attribute("scalarWriter"); + writerEvents.dump(format); + format.endObject(); + } + } + + /** + * Listener (callback) for vector overflow events. To be optionally + * implemented and bound by the client code of the writer. If no + * listener is bound, and a vector overflows, then an exception is + * thrown. + */ + + public static interface ColumnWriterListener { + + /** + * Alert the listener that a vector has overflowed. Upon return, + * all writers must have a new set of buffers available, ready + * to accept the in-flight value that triggered the overflow. + * + * @param writer the writer that triggered the overflow + */ + + void overflowed(ScalarWriter writer); + + /** + * A writer wants to expand its vector. Allows the listener to + * either allow the growth, or trigger and overflow to limit + * batch size. + * + * @param writer the writer that wishes to grow its vector + * @param delta the amount by which the vector is to grow + * @return true if the vector can be grown, false if the writer + * should instead trigger an overflow by calling + * <tt>overflowed()</tt> + */ + + boolean canExpand(ScalarWriter writer, int delta); + } + + protected ColumnMetadata schema; + + /** + * Indicates the position in the vector to write. Set via an object so that + * all writers (within the same subtree) can agree on the write position. + * For example, all top-level, simple columns see the same row index. + * All columns within a repeated map see the same (inner) index, etc. + */ + + protected ColumnWriterIndex vectorIndex; + + @Override + public ObjectType type() { return ObjectType.SCALAR; } + + public void bindSchema(ColumnMetadata schema) { + this.schema = schema; + } + + @Override + public void bindIndex(ColumnWriterIndex vectorIndex) { + this.vectorIndex = vectorIndex; + } + + @Override + public int rowStartIndex() { + return vectorIndex.rowStartIndex(); + } + + @Override + public int writeIndex() { + return vectorIndex.vectorIndex(); + } + + @Override + public ColumnMetadata schema() { return schema; } + + /** + * Bind a listener to the underlying scalar column, or array of scalar + * columns. Not valid if the underlying writer is a map or array of maps. Review comment: Refactored and reworded to make this a bit more general. The listener here receives vector size events, and so applies only for columns that have vectors. For structured vectors, such as maps and unions, the listener is added by the result set loader as it creates each component vector, so no need to have the container push listeners to its contents. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
