Skip to content

Commit 324fd59

Browse files
authored
[opt](nereids) print slotReference in explain verbose (#57167)
### What problem does this PR solve? In the result of explain verbose, most of columns of SlotDescriptors are null. It is not easy to trace column in TupleDescriptor table. In this pr, we add caption for SlotDescriptor. if column is null, the caption is its corresponding slotreference.toString(). for example : "select sum(a) as mysum from t;" we have "SlotDescriptor{id=5, col=mysum#3, ..."
1 parent 8e919ba commit 324fd59

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

fe/fe-core/src/main/java/org/apache/doris/analysis/SlotDescriptor.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class SlotDescriptor {
4242
private Type type;
4343
private Column column; // underlying column, if there is one
4444

45+
// used in explain verbose, caption is column name or alias name
46+
private String caption;
47+
4548
// for SlotRef.toSql() in the absence of a path
4649
private String label;
4750
// for variant column's sub column lables
@@ -148,6 +151,7 @@ public Column getColumn() {
148151
public void setColumn(Column column) {
149152
this.column = column;
150153
this.type = column.getType();
154+
this.caption = column.getName();
151155
}
152156

153157
public void setSrcColumn(Column column) {
@@ -338,11 +342,42 @@ public TSlotDescriptor toThrift() {
338342
return tSlotDescriptor;
339343
}
340344

345+
private String normalizeCaption(String caption) {
346+
int maxLength = 15;
347+
if (caption == null || caption.length() <= maxLength) {
348+
return caption;
349+
}
350+
351+
String normalized = caption.replaceAll("\\s+", " ");
352+
353+
if (normalized.length() <= maxLength) {
354+
return normalized;
355+
}
356+
357+
int lastHashIndex = normalized.lastIndexOf('#');
358+
359+
if (lastHashIndex == -1) {
360+
return normalized.substring(0, maxLength);
361+
}
362+
363+
String suffixWithHash = normalized.substring(lastHashIndex);
364+
int prefixLength = maxLength - suffixWithHash.length();
365+
366+
if (prefixLength <= 0) {
367+
return suffixWithHash;
368+
}
369+
370+
return normalized.substring(0, prefixLength) + suffixWithHash;
371+
}
372+
373+
public void setCaptionAndNormalize(String caption) {
374+
this.caption = normalizeCaption(caption);
375+
}
376+
341377
public String debugString() {
342-
String colStr = (column == null ? "null" : column.getName());
343378
String typeStr = (type == null ? "null" : type.toString());
344379
String parentTupleId = (parent == null) ? "null" : parent.getId().toString();
345-
return MoreObjects.toStringHelper(this).add("id", id.asInt()).add("parent", parentTupleId).add("col", colStr)
380+
return MoreObjects.toStringHelper(this).add("id", id.asInt()).add("parent", parentTupleId).add("col", caption)
346381
.add("type", typeStr).add("materialized", isMaterialized).add("byteSize", byteSize)
347382
.add("byteOffset", byteOffset).add("slotIdx", slotIdx).add("nullable", getIsNullable())
348383
.add("isAutoIncrement", isAutoInc).add("subColPath", subColPath)
@@ -358,7 +393,7 @@ public String getExplainString(String prefix) {
358393
return new StringBuilder()
359394
.append(prefix).append("SlotDescriptor{")
360395
.append("id=").append(id)
361-
.append(", col=").append(column == null ? "null" : column.getName())
396+
.append(", col=").append(caption)
362397
.append(", colUniqueId=").append(column == null ? "null" : column.getUniqueId())
363398
.append(", type=").append(type == null ? "null" : type.toSql())
364399
.append(", nullable=").append(isNullable)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ public SlotDescriptor createSlotDesc(TupleDescriptor tupleDesc, SlotReference sl
298298
SlotDescriptor slotDescriptor = this.addSlotDesc(tupleDesc);
299299
// Only the SlotDesc that in the tuple generated for scan node would have corresponding column.
300300
Optional<Column> column = slotReference.getOriginalColumn();
301-
column.ifPresent(slotDescriptor::setColumn);
301+
if (column.isPresent()) {
302+
slotDescriptor.setColumn(column.get());
303+
} else {
304+
slotDescriptor.setCaptionAndNormalize(slotReference.toString());
305+
}
302306
slotDescriptor.setType(slotReference.getDataType().toCatalogDataType());
303307
slotDescriptor.setIsMaterialized(true);
304308
SlotRef slotRef;

regression-test/suites/nereids_p0/datatype/test_date_implicit_cast.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ suite("test_date_implicit_cast") {
3838
if (value.contains("col=k1, colUniqueId=0, type=datetimev2(4)")) {
3939
contain0 = true;
4040
}
41-
if (value.contains("col=null, colUniqueId=null, type=text")) {
41+
if (value.contains("col=if(k1='2020-1#1, colUniqueId=null, type=text")) {
4242
contain1 = true;
4343
}
4444
}
@@ -50,7 +50,7 @@ suite("test_date_implicit_cast") {
5050
if (value.contains("col=k1, colUniqueId=0, type=datetimev2(4)")) {
5151
contain0 = true;
5252
}
53-
if (value.contains("col=null, colUniqueId=null, type=datetimev2(4)")) {
53+
if (value.contains("col=if(k1='2020-1#1, colUniqueId=null, type=datetimev2(4)")) {
5454
contain1 = true;
5555
}
5656
}
@@ -61,7 +61,7 @@ suite("test_date_implicit_cast") {
6161
if (value.contains("col=k1, colUniqueId=0, type=datetimev2(4)")) {
6262
contain0 = true;
6363
}
64-
if (value.contains("col=null, colUniqueId=null, type=text")) {
64+
if (value.contains("col=if(k1='2020-1#1, colUniqueId=null, type=text")) {
6565
contain1 = true;
6666
}
6767
}
@@ -80,7 +80,7 @@ suite("test_date_implicit_cast") {
8080
if (value.contains("col=k1, colUniqueId=0, type=datetimev2(6)")) {
8181
contain0 = true;
8282
}
83-
if (value.contains("col=null, colUniqueId=null, type=text")) {
83+
if (value.contains("col=if(k1='2020-1#1, colUniqueId=null, type=text")) {
8484
contain1 = true;
8585
}
8686
}

regression-test/suites/nereids_syntax_p0/explain.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ suite("explain") {
6262
when 1>1 then cast(1 as float)
6363
else 0.0 end;
6464
"""
65-
contains "SlotDescriptor{id=0, col=null, colUniqueId=null, type=double, nullable=false, isAutoIncrement=false, subColPath=null, virtualColumn=null}"
65+
contains "SlotDescriptor{id=0, col=case when 1=1#1, colUniqueId=null, type=double, nullable=false, isAutoIncrement=false, subColPath=null, virtualColumn=null}"
6666
}
6767

6868
def explainStr = sql("select sum(if(lo_tax=1,lo_tax,0)) from lineorder where false").toString()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("explain_alias") {
19+
sql """
20+
create table t (
21+
a int,
22+
b int,
23+
c int
24+
) distributed by hash(a)
25+
properties (
26+
'replication_num' = '1'
27+
);
28+
29+
insert into t values (1, 10, 100), (2, 20, 200), (3, 30, 300);
30+
31+
"""
32+
33+
explain {
34+
sql """
35+
verbose
36+
select sum(a) as mysum from t;
37+
"""
38+
contains "col=mysum"
39+
}
40+
/**
41+
even SlotDescriptor.column is null, the col in explain is not null
42+
| Tuples: |
43+
| TupleDescriptor{id=0, tbl=t} |
44+
| SlotDescriptor{id=0, col=a, colUniqueId=0, type=int, nullable=true, isAutoIncrement=false, subColPath=null, virtualColumn=null} |
45+
| |
46+
| TupleDescriptor{id=1, tbl=t} |
47+
| SlotDescriptor{id=3, col=a, colUniqueId=0, type=int, nullable=true, isAutoIncrement=false, subColPath=null, virtualColumn=null} |
48+
| |
49+
| TupleDescriptor{id=2, tbl=null} |
50+
| SlotDescriptor{id=4, col=partial_sum(a)#4, colUniqueId=null, type=varchar(65533), nullable=true, isAutoIncrement=false, subColPath=null, virtualColumn=null} |
51+
| |
52+
| TupleDescriptor{id=3, tbl=null} |
53+
| SlotDescriptor{id=5, col=mysum#3, colUniqueId=null, type=bigint, nullable=true, isAutoIncrement=false, subColPath=null, virtualColumn=null}
54+
*/
55+
56+
}

0 commit comments

Comments
 (0)