Akshat-Jain commented on code in PR #17038: URL: https://github.com/apache/druid/pull/17038#discussion_r1760978609
########## processing/src/main/java/org/apache/druid/query/operator/GlueingPartitioningOperator.java: ########## @@ -0,0 +1,261 @@ +/* + * 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.query.operator; + +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.query.rowsandcols.ConcatRowsAndColumns; +import org.apache.druid.query.rowsandcols.LimitedRowsAndColumns; +import org.apache.druid.query.rowsandcols.RowsAndColumns; +import org.apache.druid.query.rowsandcols.column.Column; +import org.apache.druid.query.rowsandcols.column.ColumnAccessor; +import org.apache.druid.query.rowsandcols.semantic.ClusteredGroupPartitioner; +import org.apache.druid.query.rowsandcols.semantic.DefaultClusteredGroupPartitioner; + +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +/** + * todo: write detailed javadoc for the class, all the methods, etc. + */ +public class GlueingPartitioningOperator implements Operator +{ + private final List<String> partitionColumns; + private final Operator child; + private RowsAndColumns previousRac; + + public GlueingPartitioningOperator( + List<String> partitionColumns, + Operator child + ) + { + this.partitionColumns = partitionColumns; + this.child = child; + } + + @Override + public Closeable goOrContinue(Closeable continuation, Receiver receiver) + { + if (continuation != null) { + Continuation cont = (Continuation) continuation; + + if (cont.iter != null) { + while (cont.iter.hasNext()) { + RowsAndColumns next = cont.iter.next(); + + if (!cont.iter.hasNext()) { + // We are at the last RAC. Process it only if subContinuation is null, otherwise save it in previousRac. + if (cont.subContinuation == null) { + receiver.push(next); + receiver.completed(); + return null; + } else { + previousRac = next; + break; + } + } + + final Signal signal = receiver.push(next); + switch (signal) { + case GO: + break; + case PAUSE: + if (cont.iter.hasNext()) { + return cont; + } + + if (cont.subContinuation == null) { + // We were finished anyway + receiver.completed(); + return null; + } + + return new Continuation(null, cont.subContinuation); + case STOP: + receiver.completed(); + try { + cont.close(); + } + catch (IOException e) { + throw new RE(e, "Unable to close continuation"); + } + return null; + default: + throw new RE("Unknown signal[%s]", signal); + } + } + + if (cont.subContinuation == null) { + receiver.completed(); + return null; + } + } + + continuation = cont.subContinuation; + } + + AtomicReference<Iterator<RowsAndColumns>> iterHolder = new AtomicReference<>(); + + final Closeable retVal = child.goOrContinue( + continuation, + new Receiver() + { + @Override + public Signal push(RowsAndColumns rac) + { + if (rac == null) { + throw DruidException.defensive("Should never get a null rac here."); + } + + ClusteredGroupPartitioner groupPartitioner = rac.as(ClusteredGroupPartitioner.class); + if (groupPartitioner == null) { + groupPartitioner = new DefaultClusteredGroupPartitioner(rac); + } + + int[] computedBoundaries = groupPartitioner.computeBoundaries(partitionColumns); + + final ArrayList<RowsAndColumns> gluedRACs = getGluedRACs(rac, computedBoundaries); + Iterator<RowsAndColumns> partitionsIter = gluedRACs.iterator(); + + Signal keepItGoing = Signal.GO; + while (keepItGoing == Signal.GO && partitionsIter.hasNext()) { + RowsAndColumns rowsAndColumns = partitionsIter.next(); + if (partitionsIter.hasNext()) { + keepItGoing = receiver.push(rowsAndColumns); + } else { + // If it's the last element, save it in previousRac instead of pushing to receiver. + previousRac = rowsAndColumns; Review Comment: Based on offline discussion: > this Reciever should be a static inner class (there's no need to create it on every invocation ; and I guess it will be cleaner). the previousRac should be a field in that class This isn't needed right now, can be done later. Right now we can/should just focus on moving the redundant code to a base partitioning class. > I think the getGluedRACs method should should not exists as it is now; instead it should be an iterator which may do the glueing (or not) on the 1st returned partition if it exists depending on the previousRac value This is done in the latest PR code. > also the previousRac should be cleared right away if next returns; as if the iterator's next is consumed it will be sent forward This is already handled in the PR code. -- 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]
