This repository has been archived by the owner on Dec 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Optimize DAG executor building logic with column pruning #203
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9074a5
TblScan executor column pruning
Novemser a3a560c
Fix aggfunc offset not set issue
Novemser 86c81a4
Merge branch 'master' into optimize_table_scan_executor
Novemser 3931855
Merge branch 'master' into optimize_table_scan_executor
Novemser 47fccff
Merge branch 'master' into optimize_table_scan_executor
zhexuany bd6375f
Remove empty line
Novemser 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 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 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 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import com.pingcap.tikv.expression.TiByItem; | ||
import com.pingcap.tikv.expression.TiColumnRef; | ||
import com.pingcap.tikv.expression.TiExpr; | ||
import com.pingcap.tikv.expression.TiFunctionExpression; | ||
import com.pingcap.tikv.kvproto.Coprocessor; | ||
import com.pingcap.tikv.types.DataType; | ||
import com.pingcap.tikv.util.KeyRangeUtils; | ||
|
@@ -168,7 +169,7 @@ private DAGRequest buildIndexScan() { | |
dagRequestBuilder.addExecutors(executorBuilder.setIdxScan(indexScanBuilder).build()); | ||
int colCount = indexScanBuilder.getColumnsCount(); | ||
dagRequestBuilder.addOutputOffsets( | ||
colCount != 0 ? colCount - 1 : 0 | ||
colCount != 0 ? colCount - 1 : 0 | ||
); | ||
return dagRequestBuilder | ||
.setFlags(flags) | ||
|
@@ -178,7 +179,7 @@ private DAGRequest buildIndexScan() { | |
} | ||
|
||
/** | ||
* @return | ||
* @return DAGRequest built | ||
*/ | ||
private DAGRequest buildTableScan() { | ||
checkArgument(startTs != 0, "timestamp is 0"); | ||
|
@@ -187,7 +188,11 @@ private DAGRequest buildTableScan() { | |
TableScan.Builder tblScanBuilder = TableScan.newBuilder(); | ||
|
||
// Step1. Add columns to first executor | ||
tableInfo.getColumns().forEach(tiColumnInfo -> tblScanBuilder.addColumns(tiColumnInfo.toProto(tableInfo))); | ||
getFields().forEach(tiColumnInfo -> | ||
tblScanBuilder.addColumns( | ||
tiColumnInfo.getColumnInfo().toProto(tableInfo) | ||
) | ||
); | ||
executorBuilder.setTp(ExecType.TypeTableScan); | ||
tblScanBuilder.setTableId(tableInfo.getId()); | ||
// Currently, according to TiKV's implementation, if handle | ||
|
@@ -210,7 +215,10 @@ private DAGRequest buildTableScan() { | |
// DO NOT EDIT EXPRESSION CONSTRUCTION ORDER | ||
// Or make sure the construction order is below: | ||
// TableScan/IndexScan > Selection > Aggregation > TopN/Limit | ||
TiExpr whereExpr = mergeCNFExpressions(getWhere()); | ||
TiExpr whereExpr = mergeCNFExpressions( | ||
getWhere().stream().peek(this::setColumnOffsets).collect(Collectors.toList()) | ||
); | ||
|
||
if (whereExpr != null) { | ||
executorBuilder.setTp(ExecType.TypeSelection); | ||
dagRequestBuilder.addExecutors( | ||
|
@@ -223,7 +231,9 @@ private DAGRequest buildTableScan() { | |
|
||
if (!getGroupByItems().isEmpty() || !getAggregates().isEmpty()) { | ||
Aggregation.Builder aggregationBuilder = Aggregation.newBuilder(); | ||
getGroupByItems().stream().map(TiByItem::getExpr).forEach(this::setColumnOffsets); | ||
getGroupByItems().forEach(tiByItem -> aggregationBuilder.addGroupBy(tiByItem.getExpr().toProto())); | ||
getAggregates().forEach(this::setColumnOffsets); | ||
getAggregates().forEach(tiExpr -> aggregationBuilder.addAggFunc(tiExpr.toProto())); | ||
executorBuilder.setTp(ExecType.TypeAggregation); | ||
dagRequestBuilder.addExecutors( | ||
|
@@ -234,6 +244,7 @@ private DAGRequest buildTableScan() { | |
|
||
if (!getOrderByItems().isEmpty()) { | ||
TopN.Builder topNBuilder = TopN.newBuilder(); | ||
getOrderByItems().stream().map(TiByItem::getExpr).forEach(this::setColumnOffsets); | ||
getOrderByItems().forEach(tiByItem -> topNBuilder.addOrderBy(tiByItem.toProto())); | ||
executorBuilder.setTp(ExecType.TypeTopN); | ||
topNBuilder.setLimit(getLimit()); | ||
|
@@ -247,7 +258,10 @@ private DAGRequest buildTableScan() { | |
executorBuilder.clear(); | ||
} | ||
|
||
getFields().forEach(tiColumnInfo -> dagRequestBuilder.addOutputOffsets(tiColumnInfo.getColumnInfo().getOffset())); | ||
// column offset should be in accordance with the | ||
for (int i = 0; i < getFields().size(); i++) { | ||
dagRequestBuilder.addOutputOffsets(i); | ||
} | ||
// if handle is needed, we should append one output offset | ||
if (isHandleNeeded()) { | ||
dagRequestBuilder.addOutputOffsets(tableInfo.getColumns().size()); | ||
|
@@ -264,13 +278,38 @@ private DAGRequest buildTableScan() { | |
return request; | ||
} | ||
|
||
private void setColumnOffsets(TiExpr expr) { | ||
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. by seting column offset, tikv can take advantage of it. But my concern is where is the logic in tidb side, I just want to make sure this logic can do the trick. 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. This may lie in TiDB's |
||
if (expr instanceof TiFunctionExpression) { | ||
((TiFunctionExpression) expr).getArgs().forEach( | ||
this::setColumnOffsets | ||
); | ||
} else if (expr instanceof TiColumnRef) { | ||
TiColumnRef columnRef = (TiColumnRef) expr; | ||
long targetId = columnRef.getColumnInfo().getId(); | ||
int pos = 0; | ||
// Set offset of each Column according to the ordering | ||
// of fields. | ||
for (TiColumnRef col : getFields()) { | ||
if (col.getColumnInfo().getId() == targetId) { | ||
break; | ||
} | ||
pos++; | ||
} | ||
|
||
if (getFields().size() == pos) { | ||
throw new DAGRequestException("No column match id:" + targetId); | ||
} | ||
columnRef.setOffset(pos); | ||
} | ||
} | ||
|
||
public boolean isIndexScan() { | ||
return indexInfo != null; | ||
} | ||
|
||
/** | ||
* Check if a DAG request is valid. | ||
* | ||
* <p> | ||
* Note: | ||
* When constructing a DAG request, a executor with an ExecType of higher priority | ||
* should always be placed before those lower ones. | ||
|
@@ -565,6 +604,7 @@ public void setHandleNeeded(boolean handleNeeded) { | |
|
||
/** | ||
* Whether we use streaming processing to retrieve data | ||
* | ||
* @return push down type. | ||
*/ | ||
public PushDownType getPushDownType() { | ||
|
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.
remove empty line
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.
Done