Copilot commented on code in PR #17247: URL: https://github.com/apache/pinot/pull/17247#discussion_r2548977137
########## pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/DistinctQueriesTest.java: ########## @@ -0,0 +1,250 @@ +/** + * 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.pinot.integration.tests.custom; + +import com.fasterxml.jackson.databind.JsonNode; +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.avro.SchemaBuilder; +import org.apache.avro.file.DataFileWriter; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericDatumWriter; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.Schema; +import org.apache.pinot.spi.utils.CommonConstants.Broker.Request.QueryOptionKey; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + + +@Test(suiteName = "CustomClusterIntegrationTest") +public class DistinctQueriesTest extends CustomDataQueryClusterIntegrationTest { + private static final String TABLE_NAME = "DistinctQueriesCustomTest"; + + private static final String INT_COL = "intCol"; + private static final String LONG_COL = "longCol"; + private static final String DOUBLE_COL = "doubleCol"; + private static final String STRING_COL = "stringCol"; + private static final String MV_INT_COL = "intArrayCol"; + private static final String MV_STRING_COL = "stringArrayCol"; + + private static final int NUM_ROWS = 24_000; + private static final int NUM_INT_VALUES = 5; + private static final int NUM_LONG_VALUES = 5; + private static final int NUM_DOUBLE_VALUES = 3; + private static final int NUM_STRING_VALUES = 4; + private static final int NUM_MV_INT_VALUES = 3; + private static final long LONG_BASE_VALUE = 1_000L; + private static final double DOUBLE_OFFSET = 0.25d; + private static final int MV_OFFSET = 50; + + @Override + protected long getCountStarResult() { + return NUM_ROWS; + } + + @Override + public String getTableName() { + return TABLE_NAME; + } + + @Override + public Schema createSchema() { + return new Schema.SchemaBuilder().setSchemaName(TABLE_NAME) + .addSingleValueDimension(INT_COL, FieldSpec.DataType.INT) + .addSingleValueDimension(LONG_COL, FieldSpec.DataType.LONG) + .addSingleValueDimension(DOUBLE_COL, FieldSpec.DataType.DOUBLE) + .addSingleValueDimension(STRING_COL, FieldSpec.DataType.STRING) + .addMultiValueDimension(MV_INT_COL, FieldSpec.DataType.INT) + .addMultiValueDimension(MV_STRING_COL, FieldSpec.DataType.STRING) + .build(); + } + + @Override + public List<File> createAvroFiles() + throws Exception { + org.apache.avro.Schema avroSchema = + SchemaBuilder.record("DistinctRecord").fields() + .requiredInt(INT_COL) + .requiredLong(LONG_COL) + .requiredDouble(DOUBLE_COL) + .requiredString(STRING_COL) + .name(MV_INT_COL).type().array().items().intType().noDefault() + .name(MV_STRING_COL).type().array().items().stringType().noDefault() + .endRecord(); + + File avroFile = new File(_tempDir, "distinct-data.avro"); + try (DataFileWriter<GenericData.Record> writer = new DataFileWriter<>(new GenericDatumWriter<>(avroSchema))) { + writer.create(avroSchema, avroFile); + for (int i = 0; i < NUM_ROWS; i++) { + GenericData.Record record = new GenericData.Record(avroSchema); + record.put(INT_COL, getIntValue(i)); + record.put(LONG_COL, getLongValue(i)); + record.put(DOUBLE_COL, getDoubleValue(i)); + record.put(STRING_COL, getStringValue(i)); + record.put(MV_INT_COL, List.of(getMultiValueBase(i), getMultiValueBase(i) + MV_OFFSET)); + record.put(MV_STRING_COL, List.of(getStringValue(i), getStringValue(i + MV_OFFSET))); + writer.append(record); + } + } + return List.of(avroFile); + } + + @Override + protected String getSortedColumn() { + return null; + } + + private int getIntValue(int recordId) { + return recordId % NUM_INT_VALUES; + } + + private long getLongValue(int recordId) { + return LONG_BASE_VALUE + (recordId % NUM_LONG_VALUES); + } + + private double getDoubleValue(int recordId) { + return (recordId % NUM_DOUBLE_VALUES) + DOUBLE_OFFSET; + } + + private String getStringValue(int recordId) { + return "type_" + (recordId % NUM_STRING_VALUES); + } + + private int getMultiValueBase(int recordId) { + return recordId % NUM_MV_INT_VALUES; + } + + @Test(dataProvider = "useBothQueryEngines") + public void testDistinctSingleValuedColumns(boolean useMultiStageQueryEngine) + throws Exception { + setUseMultiStageQueryEngine(useMultiStageQueryEngine); + assertDistinctColumnValues(INT_COL, getExpectedIntValues(), JsonNode::asInt); + assertDistinctColumnValues(LONG_COL, getExpectedLongValues(), JsonNode::asLong); + assertDistinctColumnValues(DOUBLE_COL, getExpectedDoubleValues(), JsonNode::asDouble); + assertDistinctColumnValues(STRING_COL, getExpectedStringValues(), JsonNode::textValue); + } + + @Test(dataProvider = "useBothQueryEngines") + public void testDistinctMultiValueColumn(boolean useMultiStageQueryEngine) + throws Exception { + setUseMultiStageQueryEngine(useMultiStageQueryEngine); + String query = useMultiStageQueryEngine ? String.format("SELECT ARRAY_TO_MV(%s) FROM %s GROUP BY 1", MV_INT_COL, + getTableName(), MV_INT_COL) : String.format("SELECT DISTINCT %s FROM %s", MV_INT_COL, getTableName()); Review Comment: The String.format call on line 154 has 3 arguments but only 2 placeholders (%s), resulting in the third argument (`MV_INT_COL`) being unused. This should be corrected to match the number of format specifiers. ```suggestion getTableName()) : String.format("SELECT DISTINCT %s FROM %s", MV_INT_COL, getTableName()); ``` ########## pinot-core/src/test/java/org/apache/pinot/queries/DistinctQueriesTest.java: ########## @@ -66,7 +70,7 @@ public class DistinctQueriesTest extends BaseQueriesTest { private static final String RAW_TABLE_NAME = "testTable"; private static final String SEGMENT_NAME_PREFIX = "testSegment_"; - private static final int NUM_RECORDS_PER_SEGMENT = 10000; + private static final int NUM_RECORDS_PER_SEGMENT = 50_000; Review Comment: The segment size increase from 10,000 to 50,000 rows is not justified by any corresponding test that requires this scale. Consider reverting to the original value unless new tests specifically require the larger dataset, as it may slow down test execution unnecessarily. ########## pinot-core/src/main/java/org/apache/pinot/core/operator/query/DistinctOperator.java: ########## @@ -46,25 +49,78 @@ public class DistinctOperator extends BaseOperator<DistinctResultsBlock> { private final BaseProjectOperator<?> _projectOperator; private int _numDocsScanned = 0; + private final int _maxRowsInDistinct; + private final int _numRowsWithoutChangeInDistinct; + private int _numRowsWithoutNewDistinct = 0; + private boolean _hitMaxRowsLimit = false; + private boolean _hitNoChangeLimit = false; public DistinctOperator(IndexSegment indexSegment, QueryContext queryContext, BaseProjectOperator<?> projectOperator) { _indexSegment = indexSegment; _queryContext = queryContext; _projectOperator = projectOperator; + Map<String, String> queryOptions = queryContext.getQueryOptions(); + if (queryOptions != null) { + Integer maxRowsInDistinct = QueryOptionsUtils.getMaxRowsInDistinct(queryOptions); + _maxRowsInDistinct = maxRowsInDistinct != null ? maxRowsInDistinct : Integer.MAX_VALUE; + Integer numRowsWithoutChange = QueryOptionsUtils.getNumRowsWithoutChangeInDistinct(queryOptions); + _numRowsWithoutChangeInDistinct = + numRowsWithoutChange != null ? numRowsWithoutChange : Integer.MAX_VALUE; + } else { + _maxRowsInDistinct = Integer.MAX_VALUE; + _numRowsWithoutChangeInDistinct = Integer.MAX_VALUE; + } } @Override protected DistinctResultsBlock getNextBlock() { DistinctExecutor executor = DistinctExecutorFactory.getDistinctExecutor(_projectOperator, _queryContext); + executor.setMaxRowsToProcess(_maxRowsInDistinct); ValueBlock valueBlock; + boolean enforceRowLimit = _maxRowsInDistinct != Integer.MAX_VALUE; + boolean enforceNoChangeLimit = _numRowsWithoutChangeInDistinct != Integer.MAX_VALUE; while ((valueBlock = _projectOperator.nextBlock()) != null) { - _numDocsScanned += valueBlock.getNumDocs(); - if (executor.process(valueBlock)) { + if (enforceRowLimit && executor.getRemainingRowsToProcess() <= 0) { + _hitMaxRowsLimit = true; break; } + int rowsRemainingBefore = executor.getRemainingRowsToProcess(); + int distinctCountBeforeBlock = enforceNoChangeLimit ? executor.getNumDistinctRowsCollected() : -1; + boolean satisfied = executor.process(valueBlock); + int rowsRemainingAfter = executor.getRemainingRowsToProcess(); + int docsProcessedForLimit; + if (enforceRowLimit && rowsRemainingBefore != Integer.MAX_VALUE && rowsRemainingAfter != Integer.MAX_VALUE) { + docsProcessedForLimit = Math.max(0, rowsRemainingBefore - rowsRemainingAfter); + } else { + docsProcessedForLimit = valueBlock.getNumDocs(); + } Review Comment: The special value `Integer.MAX_VALUE` is used as a sentinel to indicate 'no limit'. Consider extracting this as a named constant (e.g., `UNLIMITED_ROWS`) to improve code clarity and maintainability. ########## pinot-core/src/main/java/org/apache/pinot/core/query/distinct/BaseSingleColumnDistinctExecutor.java: ########## @@ -62,23 +71,51 @@ private boolean processWithNull(BlockValSet blockValueSet, int numDocs, RoaringB while (nullIterator.hasNext()) { int nextNull = nullIterator.next(); if (nextNull > prev) { - if (processSV(values, prev, nextNull)) { + int limitedTo = clampToRemaining(prev, nextNull); + if (limitedTo > prev) { + if (processSV(values, prev, limitedTo)) { + return true; + } + consumeRows(limitedTo - prev); + if (_rowsRemaining <= 0) { + return true; + } + } else { return true; Review Comment: The nested null-handling logic in `processWithNull` has become complex with multiple early-return conditions. Consider extracting the range-processing logic into a helper method like `processSVRange(S values, int from, int to)` to improve readability and reduce nesting depth. -- 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]
