-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[fix](show) apply ORDER BY and LIMIT correctly to SHOW TABLETS (#65871) #66116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SudharsanK2308
wants to merge
6
commits into
apache:master
Choose a base branch
from
SudharsanK2308:fix/issue-65871-SHOW-TABLE-ORDER-LIMIT
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−40
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7fc535
[fix](show) apply ORDER BY to SHOW TABLETS result set
sudharsan-k-3668 0d8be17
[fix](fe) Apply all record sort keys before limiting
sudharsan-k-3668 f0440e4
[fix](fe) Distinguish zero and absent SHOW TABLETS limits
sudharsan-k-3668 b3b289f
[fix](show) apply ORDER BY to SHOW TABLETS result set
sudharsan-k-3668 9febacc
Merge branch 'apache:master' into fix/issue-65871-SHOW-TABLE-ORDER-LIMIT
SudharsanK2308 48242a7
Merge branch 'master' into fix/issue-65871-SHOW-TABLE-ORDER-LIMIT
SudharsanK2308 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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 | ||
|
|
@@ -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; | ||
|
|
@@ -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) { | ||
|
|
@@ -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)); | ||
| } 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(); | ||
|
|
@@ -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, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
|
||
45 changes: 45 additions & 0 deletions
45
fe/fe-core/src/main/java/org/apache/doris/nereids/util/RecordPickerUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()))); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
fe/fe-core/src/test/java/org/apache/doris/nereids/util/RecordPickerUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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#addOverflowsUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.