-
Notifications
You must be signed in to change notification settings - Fork 200
[Coral-Incremental] Cost calculation for RelNode #516
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
yyy1000
wants to merge
18
commits into
linkedin:master
Choose a base branch
from
yyy1000:inc_prev
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.
Open
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8698269
feat: add prev format in table name for Join
yyy1000 83660c7
Java format
yyy1000 2c3efe5
feat: add cost estimate calculator
yyy1000 4c0c6f9
format code and add an example
yyy1000 e38542d
feat: add test json data
yyy1000 1d60f28
test: unify test casts
yyy1000 2cb5338
format code
yyy1000 07e2c73
Update coral-incremental/src/test/resources/statistic.json
yyy1000 65660b4
docs: add java doc and remove unnecessary methods
yyy1000 35d9d6a
fix: delete unused file
yyy1000 3bdb0a4
feat: throw exception when loading statistic data failed
yyy1000 791211e
feat: throe exception when join size less than 1
yyy1000 228db66
feat: remove generator and make tests for single purpose only
yyy1000 0468ea3
feat: make statistic map a uni-structure
yyy1000 4a6e8ba
fix: add new test cases and remove redundent class
yyy1000 4d444f1
feat: no vague item
yyy1000 dd7996b
docs: update doc for execution cost
yyy1000 8a7d8a5
Merge branch 'linkedin:master' into inc_prev
yyy1000 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
237 changes: 237 additions & 0 deletions
237
coral-incremental/src/main/java/com/linkedin/coral/incremental/RelNodeCostEstimator.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,237 @@ | ||
| /** | ||
| * Copyright 2024 LinkedIn Corporation. All rights reserved. | ||
| * Licensed under the BSD-2 Clause license. | ||
| * See LICENSE in the project root for license information. | ||
| */ | ||
| package com.linkedin.coral.incremental; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
|
|
||
| import org.apache.calcite.plan.RelOptTable; | ||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.core.TableScan; | ||
| import org.apache.calcite.rel.logical.LogicalJoin; | ||
| import org.apache.calcite.rel.logical.LogicalProject; | ||
| import org.apache.calcite.rel.logical.LogicalUnion; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeField; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.rex.RexInputRef; | ||
| import org.apache.calcite.rex.RexNode; | ||
|
|
||
| import static java.lang.Math.*; | ||
|
|
||
|
|
||
| public class RelNodeCostEstimator { | ||
|
|
||
| class CostInfo { | ||
| // TODO: we may also need to add TableName field. | ||
| Double cost; | ||
| Double row; | ||
|
|
||
| public CostInfo(Double cost, Double row) { | ||
| this.cost = cost; | ||
| this.row = row; | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| class JoinKey { | ||
| String leftTableName; | ||
| String rightTableName; | ||
| String leftFieldName; | ||
| String rightFieldName; | ||
|
|
||
| public JoinKey(String leftTableName, String rightTableName, String leftFieldName, String rightFieldName) { | ||
| this.leftTableName = leftTableName; | ||
| this.rightTableName = rightTableName; | ||
| this.leftFieldName = leftFieldName; | ||
| this.rightFieldName = rightFieldName; | ||
| } | ||
| } | ||
|
|
||
| private Map<String, Double> stat = new HashMap<>(); | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private Map<String, Double> distinctStat = new HashMap<>(); | ||
|
|
||
| public void setStat(Map<String, Double> stat) { | ||
| this.stat = stat; | ||
| } | ||
|
|
||
| public void setDistinctStat(Map<String, Double> distinctStat) { | ||
| this.distinctStat = distinctStat; | ||
| } | ||
|
|
||
| private Double IOCostParam = 1.0; | ||
|
|
||
| private Double shuffleCostParam = 1.0; | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public void setIOCostParam(Double IOCostParam) { | ||
| this.IOCostParam = IOCostParam; | ||
| } | ||
|
|
||
| public void setShuffleCostParam(Double shuffleCostParam) { | ||
| this.shuffleCostParam = shuffleCostParam; | ||
| } | ||
|
|
||
| public void loadStatistic(String configPath) { | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| String content = new String(Files.readAllBytes(Paths.get(configPath))); | ||
| // Parse JSON string to JsonObject | ||
| JsonObject jsonObject = new JsonParser().parse(content).getAsJsonObject(); | ||
| // Iterate over each table in the JSON object | ||
| for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { | ||
| String tableName = entry.getKey(); | ||
| JsonObject tableObject = entry.getValue().getAsJsonObject(); | ||
|
|
||
| // Extract row count | ||
| Double rowCount = tableObject.get("RowCount").getAsDouble(); | ||
|
|
||
| // Extract distinct counts | ||
| JsonObject distinctCounts = tableObject.getAsJsonObject("DistinctCounts"); | ||
|
|
||
| stat.put(tableName, rowCount); | ||
|
|
||
| // Iterate over distinct counts | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (Map.Entry<String, JsonElement> distinctEntry : distinctCounts.entrySet()) { | ||
| String columnName = distinctEntry.getKey(); | ||
| Double distinctCount = distinctEntry.getValue().getAsDouble(); | ||
|
|
||
| distinctStat.put(tableName + ":" + columnName, distinctCount); | ||
| } | ||
|
|
||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| public Double getCost(RelNode rel) { | ||
| CostInfo executionCostInfo = getExecutionCost(rel); | ||
| Double IOCost = executionCostInfo.row * IOCostParam; | ||
| return executionCostInfo.cost * shuffleCostParam + IOCost; | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public CostInfo getExecutionCost(RelNode rel) { | ||
| if (rel instanceof TableScan) { | ||
| return getExecutionCostTableScan((TableScan) rel); | ||
| } else if (rel instanceof LogicalJoin) { | ||
| return getExecutionCostJoin((LogicalJoin) rel); | ||
| } else if (rel instanceof LogicalUnion) { | ||
| return getExecutionCostUnion((LogicalUnion) rel); | ||
| } else if (rel instanceof LogicalProject) { | ||
| return getExecutionCostProject((LogicalProject) rel); | ||
| } | ||
| return new CostInfo(0.0, 0.0); | ||
| } | ||
|
||
|
|
||
| private CostInfo getExecutionCostTableScan(TableScan scan) { | ||
| RelOptTable originalTable = scan.getTable(); | ||
| String tableName = getTableName(originalTable); | ||
| Double row = stat.getOrDefault(tableName, 5.0); | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return new CostInfo(row, row); | ||
| } | ||
|
|
||
| private String getTableName(RelOptTable table) { | ||
| return String.join(".", table.getQualifiedName()); | ||
| } | ||
|
|
||
| private CostInfo getExecutionCostJoin(LogicalJoin join) { | ||
| RelNode left = join.getLeft(); | ||
| RelNode right = join.getRight(); | ||
| if (!(left instanceof TableScan) || !(right instanceof TableScan)) { | ||
|
||
| return new CostInfo(0.0, 0.0); | ||
| } | ||
| CostInfo leftCost = getExecutionCost(left); | ||
| CostInfo rightCost = getExecutionCost(right); | ||
| Double joinSize = estimateJoinSize(join, leftCost.row, rightCost.row); | ||
| return new CostInfo(max(leftCost.cost, rightCost.cost), joinSize); | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private List<JoinKey> findJoinKeys(LogicalJoin join) { | ||
| List<JoinKey> joinKeys = new ArrayList<>(); | ||
| RexNode condition = join.getCondition(); | ||
| if (condition instanceof RexCall) { | ||
| processRexCall((RexCall) condition, join, joinKeys); | ||
| } | ||
| return joinKeys; | ||
yyy1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private void processRexCall(RexCall call, LogicalJoin join, List<JoinKey> joinKeys) { | ||
yyy1000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (call.getOperator().getName().equalsIgnoreCase("AND")) { | ||
| // Process each operand of the AND separately | ||
| for (RexNode operand : call.getOperands()) { | ||
| if (operand instanceof RexCall) { | ||
| processRexCall((RexCall) operand, join, joinKeys); | ||
| } | ||
| } | ||
| } else { | ||
| // Process the join condition (e.g., EQUALS) | ||
| List<RexNode> operands = call.getOperands(); | ||
| if (operands.size() == 2 && operands.get(0) instanceof RexInputRef && operands.get(1) instanceof RexInputRef) { | ||
| RexInputRef leftRef = (RexInputRef) operands.get(0); | ||
| RexInputRef rightRef = (RexInputRef) operands.get(1); | ||
| RelDataType leftType = join.getLeft().getRowType(); | ||
| RelDataType rightType = join.getRight().getRowType(); | ||
|
|
||
| int leftIndex = leftRef.getIndex(); | ||
| int rightIndex = rightRef.getIndex() - leftType.getFieldCount(); | ||
|
|
||
| RelDataTypeField leftField = leftType.getFieldList().get(leftIndex); | ||
| String leftTableName = getTableName(join.getLeft().getTable()); | ||
| String leftFieldName = leftField.getName(); | ||
| RelDataTypeField rightField = rightType.getFieldList().get(rightIndex); | ||
| String rightTableName = getTableName(join.getRight().getTable()); | ||
| String rightFieldName = rightField.getName(); | ||
|
|
||
| joinKeys.add(new JoinKey(leftTableName, rightTableName, leftFieldName, rightFieldName)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Double estimateJoinSize(LogicalJoin join, Double leftSize, Double rightSize) { | ||
| List<JoinKey> joinKeys = findJoinKeys(join); | ||
| Double selectivity = 1.0; | ||
| for (JoinKey joinKey : joinKeys) { | ||
| String leftTableName = joinKey.leftTableName; | ||
| String rightTableName = joinKey.rightTableName; | ||
| String leftFieldName = joinKey.leftFieldName; | ||
| String rightFieldName = joinKey.rightFieldName; | ||
| Double leftCardinality = stat.getOrDefault(leftTableName, 5.0); | ||
| Double rightCardinality = stat.getOrDefault(rightTableName, 5.0); | ||
| Double leftDistinct = distinctStat.getOrDefault(leftTableName + ":" + leftFieldName, leftCardinality); | ||
| Double rightDistinct = distinctStat.getOrDefault(rightTableName + ":" + rightFieldName, rightCardinality); | ||
| selectivity *= 1 / max(leftDistinct, rightDistinct); | ||
| } | ||
| return leftSize * rightSize * selectivity; | ||
| } | ||
|
|
||
| private CostInfo getExecutionCostUnion(LogicalUnion union) { | ||
| Double unionCost = 0.0; | ||
| Double unionSize = 0.0; | ||
| RelNode input; | ||
| for (Iterator var4 = union.getInputs().iterator(); var4.hasNext();) { | ||
| input = (RelNode) var4.next(); | ||
| CostInfo inputCost = getExecutionCost(input); | ||
| unionSize += inputCost.row; | ||
| unionCost = max(inputCost.cost, unionCost); | ||
| } | ||
| unionCost *= 2; | ||
| return new CostInfo(unionCost, unionSize); | ||
| } | ||
|
|
||
| private CostInfo getExecutionCostProject(LogicalProject project) { | ||
| return getExecutionCost(project.getInput()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.