jnaous commented on a change in pull request #9111: Add HashJoinSegment, a virtual segment for joins. URL: https://github.com/apache/druid/pull/9111#discussion_r365285521
########## File path: processing/src/main/java/org/apache/druid/segment/ColumnProcessors.java ########## @@ -0,0 +1,190 @@ +/* + * 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.druid.segment; + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.math.expr.Expr; +import org.apache.druid.query.dimension.DefaultDimensionSpec; +import org.apache.druid.query.dimension.DimensionSpec; +import org.apache.druid.segment.column.ColumnCapabilities; +import org.apache.druid.segment.column.ValueType; +import org.apache.druid.segment.virtual.ExpressionSelectors; + +import javax.annotation.Nullable; + +/** + * Creates "column processors", which are objects that wrap a single input column and provide some functionality on + * top of it. + * + * @see DimensionHandlerUtils#createColumnSelectorPlus which this may eventually replace + * @see DimensionHandlerUtils#makeVectorProcessor which creates similar, vectorized processors; may eventually be moved + * into this class. + */ +public class ColumnProcessors +{ + /** + * Make a processor for a particular named column. + * + * @param column the column + * @param processorFactory the processor factory + * @param selectorFactory the column selector factory + * @param <T> processor type + */ + public static <T> T makeProcessor( + final String column, + final ColumnProcessorFactory<T> processorFactory, + final ColumnSelectorFactory selectorFactory + ) + { + return makeProcessorInternal( + factory -> getColumnType(factory, column), + factory -> factory.makeDimensionSelector(DefaultDimensionSpec.of(column)), + factory -> factory.makeColumnValueSelector(column), + processorFactory, + selectorFactory + ); + } + + /** + * Make a processor for a particular dimension spec. If the dimension spec includes an extractionFn or if it + * must decorate, then a string selector (DimensionSelector) will always be used. Otherwise, this behaves identically + * to {@link ColumnProcessors#makeProcessor(String, ColumnProcessorFactory, ColumnSelectorFactory)}. + * + * @param dimensionSpec the dimension spec + * @param processorFactory the processor factory + * @param selectorFactory the column selector factory + * @param <T> processor type + */ + public static <T> T makeProcessor( + final DimensionSpec dimensionSpec, + final ColumnProcessorFactory<T> processorFactory, + final ColumnSelectorFactory selectorFactory + ) + { + return makeProcessorInternal( + factory -> { + if (dimensionSpec.getExtractionFn() != null || dimensionSpec.mustDecorate()) { + // Currently, all extractionFns output Strings, so the column will return String values via a + // DimensionSelector if an extractionFn is present. Additionally, DimensionSpec's "decorate" method only + // operates on DimensionSelectors, so if a spec mustDecorate(), we must treat the input as a string + // regardless of its actual type. + return ValueType.STRING; + } else { + return getColumnType(factory, dimensionSpec.getDimension()); + } + }, + factory -> factory.makeDimensionSelector(dimensionSpec), + factory -> { + Preconditions.checkState( + dimensionSpec.getExtractionFn() == null && !dimensionSpec.mustDecorate(), + "Uh oh, was about to try to make a value selector for a dimensionSpec of class[%s] that " + + "requires decoration. Possible bug.", + dimensionSpec.getClass().getName() + ); + + return factory.makeColumnValueSelector(dimensionSpec.getDimension()); + }, + processorFactory, + selectorFactory + ); + } Review comment: This method embeds logic that must not inadvertently be changed during a refactor in the future. Additionally there are many branches. I suggest adding unit tests. ---------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
