Jackie-Jiang commented on a change in pull request #8100: URL: https://github.com/apache/pinot/pull/8100#discussion_r798915206
########## File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/GreatestTransformFunction.java ########## @@ -0,0 +1,117 @@ +/** + * 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.pinot.core.operator.transform.function; + +import org.apache.pinot.common.function.TransformFunctionType; +import org.apache.pinot.core.operator.blocks.ProjectionBlock; + + +public class GreatestTransformFunction extends SelectTupleElementTransformFunction { + + public GreatestTransformFunction() { + super(TransformFunctionType.GREATEST.getName()); + } + + @Override + public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) { + int numDocs = projectionBlock.getNumDocs(); + if (_intValuesSV == null || _intValuesSV.length < numDocs) { + _intValuesSV = new int[numDocs]; + } + int[] values = _arguments.get(0).transformToIntValuesSV(projectionBlock); + System.arraycopy(values, 0, _intValuesSV, 0, numDocs); + for (int i = 1; i < _arguments.size(); i++) { + values = _arguments.get(i).transformToIntValuesSV(projectionBlock); + for (int j = 0; j < numDocs & j < values.length; j++) { Review comment: No need (and should not) to check the boundary of values, same for other places ```suggestion for (int j = 0; j < numDocs; j++) { ``` ########## File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/SelectTupleElementTransformFunction.java ########## @@ -0,0 +1,109 @@ +/** + * 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.pinot.core.operator.transform.function; + +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.List; +import java.util.Map; +import org.apache.pinot.core.operator.transform.TransformResultMetadata; +import org.apache.pinot.segment.spi.datasource.DataSource; +import org.apache.pinot.spi.data.FieldSpec; + + +public abstract class SelectTupleElementTransformFunction extends BaseTransformFunction { + + private static final EnumSet<FieldSpec.DataType> SUPPORTED_DATATYPES = EnumSet.of(FieldSpec.DataType.INT, + FieldSpec.DataType.LONG, FieldSpec.DataType.FLOAT, FieldSpec.DataType.DOUBLE, FieldSpec.DataType.TIMESTAMP, + FieldSpec.DataType.STRING); + + private static final EnumMap<FieldSpec.DataType, EnumSet<FieldSpec.DataType>> ACCEPTABLE_COMBINATIONS = + createAcceptableCombinations(); + + private final String _name; + + protected List<TransformFunction> _arguments; + private TransformResultMetadata _resultMetadata; + + public SelectTupleElementTransformFunction(String name) { + _name = name; + } + + @Override + public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) { + if (arguments.isEmpty()) { + throw new IllegalArgumentException(_name + " takes at least one argument"); + } + FieldSpec.DataType dataType = null; + for (int i = 0; i < arguments.size(); i++) { + TransformFunction argument = arguments.get(i); + TransformResultMetadata metadata = argument.getResultMetadata(); + if (!metadata.isSingleValue()) { + throw new IllegalArgumentException(argument.getName() + " at position " + i + " is not single value"); + } + FieldSpec.DataType argumentType = metadata.getDataType(); + if (!SUPPORTED_DATATYPES.contains(argumentType)) { + throw new IllegalArgumentException(argumentType + " not supported. Required one of " + SUPPORTED_DATATYPES); + } + if (dataType == null) { + dataType = argumentType; + } else if (ACCEPTABLE_COMBINATIONS.get(dataType).contains(argumentType)) { + dataType = getLowestCommonDenominatorType(dataType, argumentType); + } else { + throw new IllegalArgumentException( + "combination " + argumentType + " not supported. Required one of " + ACCEPTABLE_COMBINATIONS.get(dataType)); + } + } + _resultMetadata = new TransformResultMetadata(dataType, true, false); + _arguments = arguments; + } + + @Override + public TransformResultMetadata getResultMetadata() { + return _resultMetadata; + } + + @Override + public String getName() { + return _name; + } + + private static FieldSpec.DataType getLowestCommonDenominatorType(FieldSpec.DataType left, FieldSpec.DataType right) { + if (left == null || left == right) { Review comment: (minor) `left` can never be `null` -- 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. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org