capistrant commented on code in PR #20805: URL: https://github.com/apache/druid/pull/20805#discussion_r4474782999
########## multi-stage-query/src/main/java/org/apache/druid/msq/querykit/scan/RowCountScanLimitTracker.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.msq.querykit.scan; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.druid.query.OrderBy; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.joda.time.Interval; + +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Tracker for a limited scan with no ordering. Any {@code rowsNeeded} rows satisfy the limit. + */ +public class RowCountScanLimitTracker extends ScanLimitTracker +{ + private final long rowsNeeded; + private final AtomicLong rowsWritten; + + RowCountScanLimitTracker(final long rowsNeeded) + { + this.rowsNeeded = rowsNeeded; + this.rowsWritten = new AtomicLong(); + } + + @VisibleForTesting + long getRowsNeeded() + { + return rowsNeeded; + } + + @Override + public boolean isSatisfied() + { + return rowsWritten.get() >= rowsNeeded; + } + + @Override + public boolean canSkip(final Interval interval) + { + // Once the limit is met, no input can contribute regardless of its interval. + return isSatisfied(); + } + + @Override + public RowGate forCursor(final ColumnSelectorFactory columnSelectorFactory, final List<OrderBy> cursorOrdering) + { + // Any rows satisfy the limit, so the gate does not need to consider a row's values or the cursor's ordering. + return new RowGate() + { + private long bufferedRows = 0; + + @Override + public Decision check() + { + return rowsWritten.get() + bufferedRows >= rowsNeeded ? Decision.STOP : Decision.WRITE; + } + + @Override + public void onRowWritten() + { + bufferedRows++; + } + + @Override + public void onFrameFlushed() + { + rowsWritten.addAndGet(bufferedRows); Review Comment: Fair observation. I did some benchmarking and per row update on the atomic does appear to be noticeably slower as the limit and processing threads grow. How about a compromise with a batched update to rows written, PR currently uses 64 rows per batch. It does introduce the requirement that the owner has to remember to flush on premature stop (would be same for tracking every row write) -- 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]
