Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7841,7 +7841,7 @@ public LogicalPlan visitShowTabletsFromTable(DorisParser.ShowTabletsFromTableCon
if (ctx.sortClause() != null) {
orderKeys = visit(ctx.sortClause().sortItem(), OrderKey.class);
}
long limit = 0;
long limit = -1;
long offset = 0;
if (ctx.limitClause() != null) {
limit = ctx.limitClause().limit != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.apache.doris.catalog.Partition;
import org.apache.doris.catalog.Replica;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.info.PartitionNamesInfo;
import org.apache.doris.catalog.info.TableNameInfo;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
Expand All @@ -37,6 +35,8 @@
import org.apache.doris.common.util.ListComparator;
import org.apache.doris.common.util.OrderByPair;
import org.apache.doris.common.util.Util;
import org.apache.doris.info.PartitionNamesInfo;
import org.apache.doris.info.TableNameInfo;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.analyzer.UnboundSlot;
import org.apache.doris.nereids.properties.OrderKey;
Expand All @@ -48,6 +48,7 @@
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.nereids.util.ExpressionUtils;
import org.apache.doris.nereids.util.RecordPickerUtils;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSet;
import org.apache.doris.qe.ShowResultSetMetaData;
Expand All @@ -57,9 +58,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

/**
* ShowTabletsFromTableCommand
Expand All @@ -69,7 +70,7 @@ public class ShowTabletsFromTableCommand extends ShowCommand {
private PartitionNamesInfo partitionNames;
private Expression whereClause;
private List<OrderKey> orderKeys;
private long limit = 0;
private long limit = -1;
private long offset = 0;

private long version;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void validate(ConnectContext ctx) throws UserException {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "SHOW TABLETS");
}

dbTableName.analyze(ctx.getNameSpaceContext());
dbTableName.analyze(ctx);
Util.prohibitExternalCatalog(dbTableName.getCtl(), this.getClass().getSimpleName());

if (partitionNames != null) {
Expand Down Expand Up @@ -201,13 +202,12 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc
OlapTable olapTable = db.getOlapTableOrAnalysisException(dbTableName.getTbl());
olapTable.readLock();
try {
long sizeLimit = -1;
Optional<Integer> sizeLimit = Optional.empty();
if (offset > 0 && limit > 0) {
sizeLimit = offset + limit;
sizeLimit = Optional.of((int) (offset + limit));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset and limit should be long. check overflow by org.apache.doris.nereids.util.Utils#addOverflows

@SudharsanK2308 SudharsanK2308 Jul 28, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morrySnow Since it is already typecast in the original version, I preserved the same. Also,

List<List<Comparable>> tabletInfos

's size always returns int.

} else if (limit > 0) {
sizeLimit = limit;
sizeLimit = Optional.of((int) limit);
}
boolean stop = false;
Collection<Partition> partitions = new ArrayList<Partition>();
if (partitionNames != null) {
List<String> paNames = partitionNames.getPartitionNames();
Expand All @@ -225,44 +225,30 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc
}
List<List<Comparable>> tabletInfos = new ArrayList<>();
for (Partition partition : partitions) {
if (stop) {
break;
}
for (MaterializedIndex index : partition.getMaterializedIndices(IndexExtState.ALL)) {
TabletsProcDir procDir = new TabletsProcDir(olapTable, index);
tabletInfos.addAll(procDir.fetchComparableResult(
version, backendId, replicaState));
if (sizeLimit > -1 && tabletInfos.size() >= sizeLimit) {
stop = true;
break;
}
}
}
if (offset >= tabletInfos.size()) {
tabletInfos.clear();
// order by
ListComparator<List<Comparable>> comparator;
if (orderByPairs != null) {
OrderByPair[] orderByPairArr = new OrderByPair[orderByPairs.size()];
comparator = new ListComparator<>(orderByPairs.toArray(orderByPairArr));
} else {
// order by
ListComparator<List<Comparable>> comparator = null;
if (orderByPairs != null) {
OrderByPair[] orderByPairArr = new OrderByPair[orderByPairs.size()];
comparator = new ListComparator<>(orderByPairs.toArray(orderByPairArr));
} else {
// order by tabletId, replicaId
comparator = new ListComparator<>(0, 1);
}
Collections.sort(tabletInfos, comparator);
if (sizeLimit > -1) {
tabletInfos = tabletInfos.subList((int) offset,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Narrowing Type Casting in the existing version

Math.min((int) sizeLimit, tabletInfos.size()));
}

for (List<Comparable> tabletInfo : tabletInfos) {
List<String> oneTablet = new ArrayList<String>(tabletInfo.size());
for (Comparable column : tabletInfo) {
oneTablet.add(column.toString());
}
rows.add(oneTablet);
// order by tabletId, replicaId
comparator = new ListComparator<>(0, 1);
}
List<List<Comparable>> orderedTableInfos =
RecordPickerUtils.getQualifiedRecords(tabletInfos, comparator, sizeLimit);
int resultOffset = (int) Math.min(offset, orderedTableInfos.size());
for (List<Comparable> tabletInfo : orderedTableInfos.subList(resultOffset, orderedTableInfos.size())) {
List<String> oneTablet = new ArrayList<String>(tabletInfo.size());
for (Comparable column : tabletInfo) {
oneTablet.add(column.toString());
}
rows.add(oneTablet);
}
} finally {
olapTable.readUnlock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.doris.nereids.util;

import org.apache.doris.common.util.ListComparator;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Record Picker Util.
*/
public class RecordPickerUtils {
/**
* Qualifies the record(s) that are available to be picked in the Query / Command's result.
*/
public static List<List<Comparable>> getQualifiedRecords(
List<List<Comparable>> comparables, ListComparator<List<Comparable>> comparator, Optional<Integer> k) {
int limit = k.orElse(comparables.size());
if (comparables.isEmpty()) {
return new ArrayList<>();
}

//Collections.sort(comparables, comparator);
comparables.sort(comparator);
return new ArrayList<>(comparables.subList(0, Math.min(limit, comparables.size())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.doris.mysql.privilege.AccessControllerManager;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.analyzer.UnboundSlot;
import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.properties.OrderKey;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
Expand All @@ -50,6 +51,19 @@ public class ShowTabletsFromTableCommandTest extends TestWithFeService {
private Env env;
private AccessControllerManager accessControllerManager;

@Test
void testLimitClauseParsing() {
ShowTabletsFromTableCommand withoutLimit = (ShowTabletsFromTableCommand) new NereidsParser()
.parseSingle("SHOW TABLETS FROM test_table ORDER BY LocalDataSize DESC, TabletId ASC");
ShowTabletsFromTableCommand withZeroLimit = (ShowTabletsFromTableCommand) new NereidsParser()
.parseSingle("SHOW TABLETS FROM test_table ORDER BY LocalDataSize DESC, TabletId ASC LIMIT 0");
long withoutLimitValue = Deencapsulation.getField(withoutLimit, "limit");
long zeroLimitValue = Deencapsulation.getField(withZeroLimit, "limit");

Assertions.assertEquals(-1L, withoutLimitValue);
Assertions.assertEquals(0L, zeroLimitValue);
}

private void runBefore() throws IOException {
connectContext = createDefaultCtx();
env = Env.getCurrentEnv();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.doris.nereids.util;

import org.apache.doris.common.util.ListComparator;
import org.apache.doris.common.util.OrderByPair;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

class RecordPickerUtilsTest {

@Test
void testZeroLimitReturnsNoRecords() {
List<List<Comparable>> records = Arrays.asList(
createTabletRecord(2L, 100L),
createTabletRecord(1L, 100L));
ListComparator<List<Comparable>> comparator = new ListComparator<>(
new OrderByPair(8, true),
new OrderByPair(0, false));

List<List<Comparable>> actual = RecordPickerUtils.getQualifiedRecords(
records, comparator, Optional.of(0));

Assertions.assertTrue(actual.isEmpty());
}

@Test
void testMultipleOrderByColumnsWithDifferentDirections() {
List<List<Comparable>> records = Arrays.asList(
createTabletRecord(3L, 200L),
createTabletRecord(2L, 100L),
createTabletRecord(1L, 200L),
createTabletRecord(3L, 100L),
createTabletRecord(2L, 200L),
createTabletRecord(1L, 100L));
ListComparator<List<Comparable>> comparator = new ListComparator<>(
new OrderByPair(8, true),
new OrderByPair(0, false));

List<List<Comparable>> actual = RecordPickerUtils.getQualifiedRecords(
records, comparator, Optional.of(5));

Assertions.assertEquals(Arrays.asList(
createTabletRecord(1L, 200L),
createTabletRecord(2L, 200L),
createTabletRecord(3L, 200L),
createTabletRecord(1L, 100L),
createTabletRecord(2L, 100L)), actual);
}

private List<Comparable> createTabletRecord(long tabletId, long localDataSize) {
return Arrays.asList(tabletId, 0L, 0L, 0L, 0L, 0L, 0L, 0L, localDataSize);
}
}
Loading