jtuglu1 commented on code in PR #18813: URL: https://github.com/apache/druid/pull/18813#discussion_r2600737163
########## extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java: ########## @@ -0,0 +1,103 @@ +/* + * 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.spectator.histogram; + +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import org.apache.druid.query.aggregation.VectorAggregator; +import org.apache.druid.segment.vector.VectorValueSelector; + +import javax.annotation.Nullable; +import java.nio.ByteBuffer; + +/* + Aggregator used during ingestion time when aggregating against numeric values. +*/ +public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator +{ + private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper(); + private final VectorValueSelector selector; + + public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector) + { + this.selector = selector; + } + + @Override + public void init(ByteBuffer buffer, int position) + { + // Map buf to a new histogram + innerAggregator.init(buffer, position); + } + + @Override + public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow) + { + boolean[] isNull = selector.getNullVector(); + long[] vector = selector.getLongVector(); + boolean hasNulls = isNull != null; + final SpectatorHistogram histogram = innerAggregator.get(buffer, position); + + for (int i = startRow; i < endRow; ++i) { + if (!hasNulls || !isNull[i]) { + innerAggregator.merge(histogram, vector[i]); + } + } + } + + @Override + public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset) + { + boolean[] isNull = selector.getNullVector(); + long[] vector = selector.getLongVector(); + boolean hasNulls = isNull != null; + + final Int2ObjectMap<SpectatorHistogram> histMap = innerAggregator.get(buffer); + for (int i = 0; i < numRows; ++i) { + int rowIndex = rows != null ? rows[i] : i; + if (!hasNulls || !isNull[rowIndex]) { + int position = positions[i] + positionOffset; + innerAggregator.merge(histMap.get(position), vector[rowIndex]); Review Comment: I had thought about this. I think `positions[]` might not be deterministic enough for that to matter and we'd likely spend more time branching on the cached value than not -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
