kgyrtkirk commented on code in PR #17038: URL: https://github.com/apache/druid/pull/17038#discussion_r1756618670
########## 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 Review Comment: I guess there are a lot of parts copied from `NaivePartitioningOperator` - can we have a common parent like `PartitioningOperator` ; or something like that... actually where some `instanceof` calls were made; weren't they checking for if its ` PartitioningOperator` and not for the concreate implementation? note: I feel like `Naive` and `Glueing` is a runtime specialized implementation of a generic `PartitioningOperator` ########## sql/src/main/java/org/apache/druid/sql/calcite/rel/Windowing.java: ########## @@ -248,7 +256,8 @@ public static Windowing fromCalciteStuff( private static List<OperatorFactory> computeWindowOperations( final PartialDruidQuery partialQuery, final RowSignature sourceRowSignature, - List<WindowComputationProcessor> windowGroupProcessors + List<WindowComputationProcessor> windowGroupProcessors, + SqlEngine sqlEngine Review Comment: please don't expose the execution engine for the planner; if you need something that should be configurable differently... ########## 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: I find the current contracts quite odd; * 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 * 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 * also the `previousRac` should be cleared right away if `next` returns; as if the iterator's `next` is consumed it will be sent forward * it seems to me that if the `receiver` returns `!GO` the old value will be retained which could cause duplicate results to appear and cause incorrect results * cover this with a testcase? ########## sql/src/main/java/org/apache/druid/sql/calcite/rel/Windowing.java: ########## @@ -280,12 +289,21 @@ private static List<OperatorFactory> computeWindowOperations( if (!sortMatches(priorSortColumns, sortColumns)) { // Sort order needs to change. Resort and repartition. ops.add(new NaiveSortOperatorFactory(new ArrayList<>(sortColumns))); - ops.add(new NaivePartitioningOperatorFactory(group.getPartitionColumns())); + if (sqlEngine instanceof NativeSqlEngine) { + ops.add(new NaivePartitioningOperatorFactory(group.getPartitionColumns())); + } else { + ops.add(new GlueingPartitioningOperatorFactory(group.getPartitionColumns())); + } Review Comment: this is possibly needed due to the fact how it executes these things I think this should be part of the MSQ translation layer part -- 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]
