Skip to content

Commit 374af1d

Browse files
authored
[opt](nereids) use one instance per be to execute simple query (apache#53854)
The simple query not need to use multiple instances to compute, we should use one instance to save resources. the simple query support these cases, `select ... from tbl [where yyy=k] [limit n]` for example ```sql select xxx from tbl; select xxx, yyy from tbl where id=1; select xxx from tbl limit 1; select xxx from tbl where id=1 limit 1; ```
1 parent 45df4f0 commit 374af1d

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
155155
import org.apache.doris.nereids.trees.plans.physical.PhysicalProject;
156156
import org.apache.doris.nereids.trees.plans.physical.PhysicalQuickSort;
157+
import org.apache.doris.nereids.trees.plans.physical.PhysicalRelation;
157158
import org.apache.doris.nereids.trees.plans.physical.PhysicalRepeat;
158159
import org.apache.doris.nereids.trees.plans.physical.PhysicalResultSink;
159160
import org.apache.doris.nereids.trees.plans.physical.PhysicalSchemaScan;
@@ -307,6 +308,14 @@ public PlanFragment translatePlan(PhysicalPlan physicalPlan) {
307308
throw new RuntimeException(e.getMessage(), e);
308309
}
309310
}
311+
312+
if (isSimpleQuery(physicalPlan)) {
313+
// the simple query maybe has two fragments or one fragment
314+
rootFragment.setForceSingleInstance();
315+
for (PlanFragment child : rootFragment.getChildren()) {
316+
child.setForceSingleInstance();
317+
}
318+
}
310319
return rootFragment;
311320
}
312321

@@ -3147,4 +3156,30 @@ private List<Expr> getDistributeExpr(List<ExprId> childOutputIds, DistributionSp
31473156
}
31483157
return Lists.newArrayList();
31493158
}
3159+
3160+
// matching the simple query:
3161+
// 1. select xxx from tbl
3162+
// 2. select xxx from tbl where xxx=yyy
3163+
private boolean isSimpleQuery(PhysicalPlan root) {
3164+
if (!(root instanceof PhysicalResultSink)) {
3165+
return false;
3166+
}
3167+
Plan child = root.child(0);
3168+
if (child instanceof PhysicalLimit) {
3169+
child = child.child(0);
3170+
}
3171+
if (child instanceof PhysicalDistribute) {
3172+
child = child.child(0);
3173+
}
3174+
if (child instanceof PhysicalLimit) {
3175+
child = child.child(0);
3176+
}
3177+
if (child instanceof PhysicalProject) {
3178+
child = child.child(0);
3179+
}
3180+
if (child instanceof PhysicalFilter) {
3181+
child = child.child(0);
3182+
}
3183+
return child instanceof PhysicalRelation;
3184+
}
31503185
}

‎fe/fe-core/src/main/java/org/apache/doris/planner/PlanFragment.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public class PlanFragment extends TreeNode<PlanFragment> {
163163

164164
public TQueryCacheParam queryCacheParam;
165165
private int numBackends = 0;
166+
private boolean forceSingleInstance = false;
166167

167168
/**
168169
* C'tor for fragment with specific partition; the output is by default broadcast.
@@ -312,6 +313,9 @@ public int getNumNodes() {
312313
}
313314

314315
public int getParallelExecNum() {
316+
if (forceSingleInstance) {
317+
return 1;
318+
}
315319
return parallelExecNum;
316320
}
317321

@@ -530,4 +534,8 @@ public boolean useSerialSource(ConnectContext context) {
530534
public boolean hasSerialScanNode() {
531535
return planRoot.hasSerialScanChildren();
532536
}
537+
538+
public void setForceSingleInstance() {
539+
this.forceSingleInstance = true;
540+
}
533541
}

‎fe/fe-core/src/test/java/org/apache/doris/qe/NereidsCoordinatorTest.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.doris.common.FeConstants;
2222
import org.apache.doris.nereids.NereidsPlanner;
2323
import org.apache.doris.nereids.util.PlanChecker;
24+
import org.apache.doris.planner.PlanFragment;
2425
import org.apache.doris.thrift.TUniqueId;
2526
import org.apache.doris.utframe.TestWithFeService;
2627

@@ -60,8 +61,31 @@ public void testNereidsCoordinatorScanRangeNum2() throws IOException {
6061
Assertions.assertEquals(0, scanRangeNum);
6162
}
6263

63-
private NereidsPlanner plan(String sql) throws IOException {
64+
@Test
65+
public void testSimpleQueryUseOneInstance() throws IOException {
6466
ConnectContext connectContext = createDefaultCtx();
67+
connectContext.getSessionVariable().parallelPipelineTaskNum = 10;
68+
NereidsPlanner planner = plan("select * from test.tbl", connectContext);
69+
for (PlanFragment fragment : planner.getFragments()) {
70+
Assertions.assertEquals(1, fragment.getParallelExecNum());
71+
}
72+
73+
planner = plan("select * from test.tbl where id=1", connectContext);
74+
for (PlanFragment fragment : planner.getFragments()) {
75+
Assertions.assertEquals(1, fragment.getParallelExecNum());
76+
}
77+
78+
planner = plan("select id, id + 1 from test.tbl where id = 2 limit 1", connectContext);
79+
for (PlanFragment fragment : planner.getFragments()) {
80+
Assertions.assertEquals(1, fragment.getParallelExecNum());
81+
}
82+
}
83+
84+
private NereidsPlanner plan(String sql) throws IOException {
85+
return plan(sql, createDefaultCtx());
86+
}
87+
88+
private NereidsPlanner plan(String sql, ConnectContext connectContext) throws IOException {
6589
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION,OLAP_SCAN_TABLET_PRUNE");
6690
connectContext.setThreadLocalInfo();
6791

0 commit comments

Comments
 (0)