abstractdog commented on code in PR #6703: URL: https://github.com/apache/hive/pull/6703#discussion_r3841504729
########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorCumeDist.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.hadoop.hive.ql.exec.vector.ptf; + +import java.util.ArrayDeque; +import java.util.Deque; + +import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type; +import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef; + +/** + * This class evaluates cume_dist() for a PTF partition. + * Unlike rank(), cume_dist needs the total partition row count, group row + * count, so it cannot produce a group's + * result while the group is still streaming in. It is therefore a peer group + * aggregated streaming evaluator + * (see {@link VectorPTFEvaluatorBase#isGroupAggregatedStreamingEvaluator()}): a + * first pass over the + * buffered group sizes precomputes each peer group's value via + * {@link #addStreamingGroupResult(int)} + * (after {@link #setPartitionSize(int)} has been called), and the regular + * streaming pass then just + * populates the precomputed values into the output column. + */ +public class VectorPTFEvaluatorCumeDist extends VectorPTFEvaluatorBase { + + /** + * Per peer group cume_dist values computed in the first pass and consumed, in + * order, by the + * streaming pass (one value is popped when a group's last batch is processed). Review Comment: nit: reformat comment, the short second line "order, by the" looks strange at first sight ########## ql/src/test/queries/clientpositive/vector_ptf_cume_dist.q: ########## @@ -0,0 +1,90 @@ +set hive.vectorized.testing.reducer.batch.size=2; + +DROP TABLE IF EXISTS vector_ptf_cume_dist_int; + +CREATE TABLE vector_ptf_cume_dist_int(name string, rowindex int, mynumber int) stored as orc; + +INSERT INTO vector_ptf_cume_dist_int values +-- a partition +('first', 1, 1), +('first', 2, 2), +('first', 3, 2), +('first', 4, NULL), +('first', 5, 3), +('first', 6, 3), +('first', 7, 4), +('first', 8, NULL), +('first', 9, 4), +('first', 10, 4), +('first', 11, 5), +('first', 12, 5), +('first', 13, NULL), +('first', 14, 5), +('first', 15, 5), +('first', 16, 6), +('first', 17, 6), +('first', 18, 6), +('first', 19, NULL), +('first', 20, 6), +('first', 21, 6), +-- another partition +('second', 22, 1), +('second', 23, 2), +('second', 24, 2), +('second', 25, NULL), +('second', 26, 3), +('second', 27, 3), +('second', 28, 4), +('second', 29, NULL), +('second', 30, 4), +('second', 31, 4), +('second', 32, 5), +('second', 33, 5), +('second', 34, NULL), +('second', 35, 5), +('second', 36, 5), +('second', 37, 6), +('second', 38, 6), +('second', 39, 6), +('second', 40, NULL), +('second', 41, 6), +('second', 42, 6), +-- null partition +(NULL, 43, 7), +(NULL, 44, 7); + +-- NON-VECTORIZED +set hive.vectorized.execution.ptf.enabled=false; + +select name, rowindex, mynumber, +cume_dist() over (partition by name order by mynumber) as cud +from vector_ptf_cume_dist_int; + +select name, rowindex, mynumber, +rank() over (partition by name order by mynumber) as r, +dense_rank() over (partition by name order by mynumber) as dr, +cume_dist() over (partition by name order by mynumber) as cud Review Comment: I'm wondering whether these functions work without "partition by"? I think they should, in which case, cume_dist is calculated over the whole table I guess, could you please add coverage for that? ########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorCumeDist.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.hadoop.hive.ql.exec.vector.ptf; + +import java.util.ArrayDeque; +import java.util.Deque; + +import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type; +import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef; + +/** + * This class evaluates cume_dist() for a PTF partition. + * Unlike rank(), cume_dist needs the total partition row count, group row + * count, so it cannot produce a group's + * result while the group is still streaming in. It is therefore a peer group + * aggregated streaming evaluator + * (see {@link VectorPTFEvaluatorBase#isGroupAggregatedStreamingEvaluator()}): a + * first pass over the + * buffered group sizes precomputes each peer group's value via + * {@link #addStreamingGroupResult(int)} + * (after {@link #setPartitionSize(int)} has been called), and the regular + * streaming pass then just + * populates the precomputed values into the output column. + */ +public class VectorPTFEvaluatorCumeDist extends VectorPTFEvaluatorBase { + + /** + * Per peer group cume_dist values computed in the first pass and consumed, in + * order, by the + * streaming pass (one value is popped when a group's last batch is processed). + */ + private final Deque<Double> groupResults = new ArrayDeque<>(); + private int rowPosition; + + public VectorPTFEvaluatorCumeDist(WindowFrameDef windowFrameDef, int outputColumnNum) { + super(windowFrameDef, outputColumnNum); + resetEvaluator(); + } + + @Override + public boolean needPartitionSize() { + return true; + } + + @Override + public boolean isGroupAggregatedStreamingEvaluator() { + return true; + } + + @Override + public void addStreamingGroupResult(int groupRowCount) throws HiveException { + if (partitionSize <= 0) { + throw new HiveException("Partition size must be set before precomputing cume_dist"); + } + rowPosition += groupRowCount; + groupResults.addLast(((double) rowPosition) / partitionSize); + } + + @Override + public void evaluateGroupBatch(VectorizedRowBatch batch) throws HiveException { + Double result = groupResults.peekFirst(); + if (result == null) { + throw new HiveException("cume_dist streaming result is not available for the current group"); + } + DoubleColumnVector outputColVector = (DoubleColumnVector) batch.cols[outputColumnNum]; + outputColVector.isRepeating = true; + outputColVector.noNulls = true; + outputColVector.isNull[0] = false; + outputColVector.vector[0] = result; + } + + @Override + public void doLastBatchWork() { + groupResults.pollFirst(); + } + + @Override + public boolean streamsResult() { + return true; + } Review Comment: the more I look at this code the more I feel this evaluator is not a streaming one :( from the point it needs to have group counts, and makes `getAllEvaluatorsAreStreaming` return false, I believe we have no reason the to treat it like a streaming one, because it doesn't make the code fall into optimized codepaths anyway, like ``` if (allEvaluatorsAreStreaming) { // We can process this batch immediately. groupBatches.evaluateStreamingGroupBatch(batch, isLastGroupBatch); vectorForward(batch); ``` if you agree, please edit the javadoc for the `VectorPTFEvaluatorCumeDist` class too, and similify code introduced by this PR ########## ql/src/test/queries/clientpositive/vector_ptf_cume_dist.q: ########## @@ -0,0 +1,90 @@ +set hive.vectorized.testing.reducer.batch.size=2; + +DROP TABLE IF EXISTS vector_ptf_cume_dist_int; + +CREATE TABLE vector_ptf_cume_dist_int(name string, rowindex int, mynumber int) stored as orc; + +INSERT INTO vector_ptf_cume_dist_int values +-- a partition +('first', 1, 1), +('first', 2, 2), +('first', 3, 2), +('first', 4, NULL), +('first', 5, 3), +('first', 6, 3), +('first', 7, 4), +('first', 8, NULL), +('first', 9, 4), +('first', 10, 4), +('first', 11, 5), +('first', 12, 5), +('first', 13, NULL), +('first', 14, 5), +('first', 15, 5), +('first', 16, 6), +('first', 17, 6), +('first', 18, 6), +('first', 19, NULL), +('first', 20, 6), +('first', 21, 6), +-- another partition +('second', 22, 1), +('second', 23, 2), +('second', 24, 2), +('second', 25, NULL), +('second', 26, 3), +('second', 27, 3), +('second', 28, 4), +('second', 29, NULL), +('second', 30, 4), +('second', 31, 4), +('second', 32, 5), +('second', 33, 5), +('second', 34, NULL), +('second', 35, 5), +('second', 36, 5), +('second', 37, 6), +('second', 38, 6), +('second', 39, 6), +('second', 40, NULL), +('second', 41, 6), +('second', 42, 6), Review Comment: the second partition has the same `cume_dist` characterisctics, would you consider taking a chance to change values in `mynumber` columns to make this qtest much more self-descriptive value-wise? e.g. having values that lead to cume_dist results much easier to understand by humans, like 0.25, 0.333, 0.5, 0.8, 0.9, etc? ########## ql/src/java/org/apache/hadoop/hive/ql/plan/VectorPTFDesc.java: ########## @@ -468,6 +473,17 @@ public static int[] getStreamingEvaluatorNums(VectorPTFEvaluatorBase[] evaluator return ArrayUtils.toPrimitive(streamingEvaluatorNums.toArray(new Integer[0])); } + public static boolean getAllEvaluatorsAreStreaming(VectorPTFEvaluatorBase[] evaluators) { + for (VectorPTFEvaluatorBase evaluator : evaluators) { + if (evaluator.isGroupAggregatedStreamingEvaluator() || + evaluator.needPartitionSize() || Review Comment: currently `needPartitionSize` is only true when `isGroupAggregatedStreamingEvaluator` is true, what about completely removing `needPartitionSize` check and interface method? ########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFOperator.java: ########## @@ -782,7 +775,7 @@ protected void closeOp(boolean abort) throws HiveException { /* * Why would finishPartition be skipped here? * 1. abort: obviously - * 2. allEvaluatorsAreStreaming: if all evaluators are streaming, we already evaluated + * 2. allEvaluatorsPurelyStreaming: if all evaluators are streaming, we already evaluated Review Comment: revert naming here also to allEvaluatorsAreStreaming ########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFGroupBatches.java: ########## @@ -421,6 +441,7 @@ public int size() { public void finishPartition() throws HiveException { long startTime = System.currentTimeMillis(); preFinishPartition(); + precomputeAggregatedStreamingResults(); Review Comment: what about calling this from `preFinishPartition`? this looks more like a "finish partitions" task ########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFGroupBatches.java: ########## @@ -94,6 +94,18 @@ public class VectorPTFGroupBatches extends PTFPartition { List<Integer> inMemoryStartRowIndex; + private boolean isGroupAggregatedStreamingEvaluator; Review Comment: this is rather "hasGroupAggregatedStreamingEvaluator", because the point of this boolean is to store that whether this `VectorPTFGroupBatches` has to handle at least one group aggregated streaming evaluator -> this also has to be javadoc documented here ########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFGroupBatches.java: ########## @@ -839,6 +887,12 @@ private void setRepeatingColumn(Object partitionKey, VectorizedRowBatch targetBa void preFinishPartition() throws HiveException { int rows = size(); + for (VectorPTFEvaluatorBase evaluator : evaluators) { + if (evaluator.needPartitionSize()) { Review Comment: what about removing this check and call `evaluator.setPartitionSize(rows);` directly? ########## ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorCumeDist.java: ########## @@ -0,0 +1,106 @@ +/* + * 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.hadoop.hive.ql.exec.vector.ptf; + +import java.util.ArrayDeque; +import java.util.Deque; + +import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type; +import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef; + +/** + * This class evaluates cume_dist() for a PTF partition. + * Unlike rank(), cume_dist needs the total partition row count, group row count, so it cannot produce a group's + * result while the group is still streaming in. It is therefore a peer group aggregated streaming evaluator + * (see {@link VectorPTFEvaluatorBase#isGroupAggregatedStreamingEvaluator()}): a first pass over the + * buffered group sizes precomputes each peer group's value via {@link #addStreamingGroupResult(int)} + * (after {@link #setPartitionSize(int)} has been called), and the regular streaming pass then just + * populates the precomputed values into the output column. + */ +public class VectorPTFEvaluatorCumeDist extends VectorPTFEvaluatorBase { + + /** + * Per peer group cume_dist values computed in the first pass and consumed, in order, by the + * streaming pass (one value is popped when a group's last batch is processed). + */ + private final Deque<Double> groupResults = new ArrayDeque<>(); + private int rowPosition; + + public VectorPTFEvaluatorCumeDist(WindowFrameDef windowFrameDef, int outputColumnNum) { + super(windowFrameDef, outputColumnNum); + resetEvaluator(); + } + + @Override + public boolean needPartitionSize() { + return true; + } + + @Override + public boolean isGroupAggregatedStreamingEvaluator() { + return true; + } + + @Override + public void addStreamingGroupResult(int groupRowCount) throws HiveException { + if (partitionSize <= 0) { + throw new HiveException("Partition size must be set before precomputing cume_dist"); + } + rowPosition += groupRowCount; + groupResults.addLast(((double) rowPosition) / partitionSize); + } + + @Override + public void evaluateGroupBatch(VectorizedRowBatch batch) throws HiveException { + Double result = groupResults.peekFirst(); + if (result == null) { + throw new HiveException("cume_dist streaming result is not available for the current group"); + } + DoubleColumnVector outputColVector = (DoubleColumnVector) batch.cols[outputColumnNum]; + outputColVector.isRepeating = true; Review Comment: I think this comment by copilot might be accurate, however, in this case, it would make sense to update the same in another evaluators as well, like: https://github.com/apache/hive/blob/5bc5e7f875d5f8879145ecfb07ba838cd52c1911/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorRank.java#L53-L55 can be done in this PR or in a follow-up -- 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]
