jinchengchenghh commented on code in PR #9074: URL: https://github.com/apache/incubator-gluten/pull/9074#discussion_r2064055041
########## backends-velox/src/main/scala/org/apache/gluten/execution/ColumnarCollectTailExec.scala: ########## @@ -0,0 +1,83 @@ +/* + * 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.gluten.execution + +import org.apache.gluten.columnarbatch.ColumnarBatches +import org.apache.gluten.columnarbatch.VeloxColumnarBatches + +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.vectorized.ColumnarBatch + +import scala.collection.mutable + +case class ColumnarCollectTailExec( + limit: Int, + child: SparkPlan +) extends ColumnarCollectTailBaseExec(limit, child) { + + override protected def collectTailRows( + partitionIter: Iterator[ColumnarBatch], + limit: Int + ): Iterator[ColumnarBatch] = { + if (!partitionIter.hasNext || limit <= 0) { + return Iterator.empty + } + + val tailQueue = new mutable.ListBuffer[ColumnarBatch]() + var totalRowsInTail = 0L + + while (partitionIter.hasNext) { + val batch = partitionIter.next() + val batchRows = batch.numRows() + ColumnarBatches.retain(batch) + tailQueue += batch + totalRowsInTail += batchRows + + var canDrop = true + while (tailQueue.nonEmpty && canDrop) { + val front = tailQueue.head + val frontRows = front.numRows().toLong + + if (totalRowsInTail - frontRows >= limit) { + val dropped = tailQueue.remove(0) + dropped.close() + totalRowsInTail -= frontRows + } else { + canDrop = false + } + } + } + + if (tailQueue.nonEmpty) { Review Comment: Remove this check, duplicated with `overflow > 0` ########## backends-velox/src/main/scala/org/apache/gluten/execution/ColumnarCollectTailExec.scala: ########## @@ -0,0 +1,83 @@ +/* + * 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.gluten.execution + +import org.apache.gluten.columnarbatch.ColumnarBatches +import org.apache.gluten.columnarbatch.VeloxColumnarBatches + +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.vectorized.ColumnarBatch + +import scala.collection.mutable + +case class ColumnarCollectTailExec( + limit: Int, + child: SparkPlan +) extends ColumnarCollectTailBaseExec(limit, child) { + + override protected def collectTailRows( + partitionIter: Iterator[ColumnarBatch], + limit: Int + ): Iterator[ColumnarBatch] = { + if (!partitionIter.hasNext || limit <= 0) { + return Iterator.empty + } + + val tailQueue = new mutable.ListBuffer[ColumnarBatch]() + var totalRowsInTail = 0L + + while (partitionIter.hasNext) { + val batch = partitionIter.next() + val batchRows = batch.numRows() + ColumnarBatches.retain(batch) + tailQueue += batch + totalRowsInTail += batchRows + + var canDrop = true + while (tailQueue.nonEmpty && canDrop) { + val front = tailQueue.head + val frontRows = front.numRows().toLong Review Comment: remove toLong, totalRowsInTail is long, which is enough ########## backends-velox/src/main/scala/org/apache/gluten/execution/ColumnarCollectTailExec.scala: ########## @@ -0,0 +1,83 @@ +/* + * 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.gluten.execution + +import org.apache.gluten.columnarbatch.ColumnarBatches +import org.apache.gluten.columnarbatch.VeloxColumnarBatches + +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.vectorized.ColumnarBatch + +import scala.collection.mutable + +case class ColumnarCollectTailExec( + limit: Int, + child: SparkPlan +) extends ColumnarCollectTailBaseExec(limit, child) { + + override protected def collectTailRows( + partitionIter: Iterator[ColumnarBatch], + limit: Int + ): Iterator[ColumnarBatch] = { + if (!partitionIter.hasNext || limit <= 0) { + return Iterator.empty + } + + val tailQueue = new mutable.ListBuffer[ColumnarBatch]() + var totalRowsInTail = 0L + + while (partitionIter.hasNext) { + val batch = partitionIter.next() + val batchRows = batch.numRows() + ColumnarBatches.retain(batch) + tailQueue += batch + totalRowsInTail += batchRows + + var canDrop = true + while (tailQueue.nonEmpty && canDrop) { + val front = tailQueue.head + val frontRows = front.numRows().toLong + + if (totalRowsInTail - frontRows >= limit) { + val dropped = tailQueue.remove(0) + dropped.close() + totalRowsInTail -= frontRows + } else { + canDrop = false + } + } + } + + if (tailQueue.nonEmpty) { + val first = tailQueue.remove(0) Review Comment: Only remove if the overflow occurs. Move to L69 ########## backends-velox/src/main/scala/org/apache/gluten/execution/ColumnarCollectTailExec.scala: ########## @@ -0,0 +1,83 @@ +/* + * 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.gluten.execution + +import org.apache.gluten.columnarbatch.ColumnarBatches +import org.apache.gluten.columnarbatch.VeloxColumnarBatches + +import org.apache.spark.sql.execution.SparkPlan +import org.apache.spark.sql.vectorized.ColumnarBatch + +import scala.collection.mutable + +case class ColumnarCollectTailExec( + limit: Int, + child: SparkPlan +) extends ColumnarCollectTailBaseExec(limit, child) { + + override protected def collectTailRows( + partitionIter: Iterator[ColumnarBatch], + limit: Int + ): Iterator[ColumnarBatch] = { + if (!partitionIter.hasNext || limit <= 0) { + return Iterator.empty + } + + val tailQueue = new mutable.ListBuffer[ColumnarBatch]() + var totalRowsInTail = 0L + + while (partitionIter.hasNext) { + val batch = partitionIter.next() + val batchRows = batch.numRows() + ColumnarBatches.retain(batch) + tailQueue += batch + totalRowsInTail += batchRows + + var canDrop = true + while (tailQueue.nonEmpty && canDrop) { + val front = tailQueue.head + val frontRows = front.numRows().toLong + + if (totalRowsInTail - frontRows >= limit) { + val dropped = tailQueue.remove(0) + dropped.close() + totalRowsInTail -= frontRows + } else { + canDrop = false Review Comment: Please use breakable instead of canDrop flag -- 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]
