-
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 all 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
286 changes: 286 additions & 0 deletions
286
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,286 @@ | ||
| /** | ||
| * 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.*; | ||
|
|
||
|
|
||
| /** | ||
| * RelNodeCostEstimator is a utility class designed to estimate the cost of executing relational operations | ||
| * in a query plan. It uses statistical information about table row counts and column distinct values | ||
| * to compute costs associated with different types of relational operations like table scans, joins, | ||
| * unions, and projections. | ||
| * | ||
| * <p>This class supports loading statistics from a JSON configuration file. | ||
| * For a relational operations (RelNode), the execution cost and row count are estimated based on | ||
| * these statistics and the input relational expressions. | ||
| * | ||
| * <p>The cost estimation takes into account factors such as I/O costs and data shuffling costs. | ||
| * The cost weight of writing a row to disk is IOCostValue, and the cost weight of execution is executionCostValue. | ||
| * | ||
| * <p>Cost is get from 'getCost' method, which returns the total cost of the query plan, and cost consists of | ||
| * execution cost and I/O cost. | ||
| */ | ||
| public class RelNodeCostEstimator { | ||
|
|
||
| class CostInfo { | ||
| // TODO: we may also need to add TableName field. | ||
| Double executionCost; | ||
| Double outputSize; | ||
|
|
||
| public CostInfo(Double executionCost, Double row) { | ||
| this.executionCost = executionCost; | ||
| this.outputSize = row; | ||
| } | ||
| } | ||
|
|
||
| class TableStatistic { | ||
| // The number of rows in the table | ||
| Double rowCount; | ||
| // The number of distinct values in each column | ||
| // This doesn't work for nested columns and complex types | ||
| Map<String, Double> distinctCountByRow; | ||
|
Contributor
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. Does this work for nested columns? If yes, what is the spec of the key? Does it work for complex types? If the answer is no, we should be explicit about this at least in the Java doc. |
||
|
|
||
| public TableStatistic() { | ||
| this.distinctCountByRow = new HashMap<>(); | ||
| } | ||
| } | ||
|
|
||
| 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, TableStatistic> costStatistic = new HashMap<>(); | ||
|
|
||
| private final Double IOCostValue; | ||
|
|
||
| private final Double executionCostValue; | ||
|
|
||
| public RelNodeCostEstimator(Double IOCostValue, Double executionCostValue) { | ||
| this.IOCostValue = IOCostValue; | ||
| this.executionCostValue = executionCostValue; | ||
| } | ||
|
|
||
| /** | ||
| * Loads statistics from a JSON configuration file and stores them in internal data structures. | ||
| * | ||
| * <p>This method reads a JSON file from the specified path, parses its content, and extracts | ||
| * statistical information. For each table in the JSON object, it retrieves the row count and | ||
| * distinct counts for each column. These values are then stored in the `stat` and `distinctStat` | ||
| * maps, respectively. | ||
| * | ||
| * @param configPath the path to the JSON configuration file | ||
| */ | ||
| public void loadStatistic(String configPath) throws IOException { | ||
| try { | ||
| String content = new String(Files.readAllBytes(Paths.get(configPath))); | ||
| JsonObject jsonObject = new JsonParser().parse(content).getAsJsonObject(); | ||
| for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { | ||
| TableStatistic tableStatistic = new TableStatistic(); | ||
| String tableName = entry.getKey(); | ||
| JsonObject tableObject = entry.getValue().getAsJsonObject(); | ||
|
|
||
| Double rowCount = tableObject.get("RowCount").getAsDouble(); | ||
|
|
||
| JsonObject distinctCounts = tableObject.getAsJsonObject("DistinctCounts"); | ||
|
|
||
| tableStatistic.rowCount = rowCount; | ||
|
|
||
| for (Map.Entry<String, JsonElement> distinctEntry : distinctCounts.entrySet()) { | ||
| String columnName = distinctEntry.getKey(); | ||
| Double distinctCount = distinctEntry.getValue().getAsDouble(); | ||
|
|
||
| tableStatistic.distinctCountByRow.put(columnName, distinctCount); | ||
| } | ||
| costStatistic.put(tableName, tableStatistic); | ||
|
|
||
| } | ||
| } catch (IOException e) { | ||
| throw new IOException("Failed to load statistics from the configuration file: " + configPath, e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Returns the total cost of executing a relational operation. | ||
| * | ||
| * <p>This method computes the cost of executing a relational operation based on the input | ||
| * relational expression. The cost is calculated as the sum of the execution cost and the I/O cost. | ||
| * We assume that I/O only occurs at the root of the query plan (Project) where we write the output to disk. | ||
| * So the cost is the sum of the execution cost of all children RelNodes and IOCostValue * outputSize of the root Project RelNode. | ||
| * | ||
| * @param rel the input relational expression | ||
| * @return the total cost of executing the relational operation | ||
| */ | ||
| public Double getCost(RelNode rel) { | ||
| CostInfo executionCostInfo = getExecutionCost(rel); | ||
| Double writeCost = executionCostInfo.outputSize * IOCostValue; | ||
| return executionCostInfo.executionCost * executionCostValue + writeCost; | ||
| } | ||
|
|
||
| private 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); | ||
| } | ||
| throw new IllegalArgumentException("Unsupported relational operation: " + rel.getClass().getSimpleName()); | ||
| } | ||
|
|
||
| private CostInfo getExecutionCostTableScan(TableScan scan) { | ||
| RelOptTable originalTable = scan.getTable(); | ||
| String tableName = getTableName(originalTable); | ||
| try { | ||
| TableStatistic tableStat = costStatistic.get(tableName); | ||
| Double rowCount = tableStat.rowCount; | ||
| return new CostInfo(rowCount, rowCount); | ||
| } catch (NullPointerException e) { | ||
| throw new IllegalArgumentException("Table statistics not found for table: " + tableName); | ||
yyy1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private String getTableName(RelOptTable table) { | ||
| return String.join(".", table.getQualifiedName()); | ||
| } | ||
|
|
||
| private CostInfo getExecutionCostJoin(LogicalJoin join) { | ||
| RelNode left = join.getLeft(); | ||
| RelNode right = join.getRight(); | ||
| CostInfo leftCost = getExecutionCost(left); | ||
| CostInfo rightCost = getExecutionCost(right); | ||
| Double joinSize = estimateJoinSize(join, leftCost.outputSize, rightCost.outputSize); | ||
| // The execution cost of a join is the maximum execution cost of its children because the execution cost of a single RelNode | ||
| // is mainly determined by the cost of the shuffle operation. | ||
| // And in modern distributed systems, the shuffle cost is dominated by the largest shuffle. | ||
| return new CostInfo(max(leftCost.executionCost, rightCost.executionCost), joinSize); | ||
| } | ||
|
|
||
| private List<JoinKey> getJoinKeys(LogicalJoin join) { | ||
| List<JoinKey> joinKeys = new ArrayList<>(); | ||
| RexNode condition = join.getCondition(); | ||
| if (condition instanceof RexCall) { | ||
| getJoinKeysFromJoinCondition((RexCall) condition, join, joinKeys); | ||
| } | ||
| // Assertion to check if joinKeys.size() is greater than or equal to 1 | ||
| if (joinKeys.size() < 1) { | ||
| throw new IllegalArgumentException("Join keys size is less than 1"); | ||
| } | ||
| return joinKeys; | ||
yyy1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private void getJoinKeysFromJoinCondition(RexCall call, LogicalJoin join, List<JoinKey> joinKeys) { | ||
| if (call.getOperator().getName().equalsIgnoreCase("AND")) { | ||
| // Process each operand of the AND separately | ||
| for (RexNode operand : call.getOperands()) { | ||
| if (operand instanceof RexCall) { | ||
| getJoinKeysFromJoinCondition((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 = getJoinKeys(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; | ||
| try { | ||
| TableStatistic leftTableStat = costStatistic.get(leftTableName); | ||
| TableStatistic rightTableStat = costStatistic.get(rightTableName); | ||
| Double leftCardinality = leftTableStat.rowCount; | ||
| Double rightCardinality = rightTableStat.rowCount; | ||
| Double leftDistinct = leftTableStat.distinctCountByRow.getOrDefault(leftFieldName, leftCardinality); | ||
| Double rightDistinct = rightTableStat.distinctCountByRow.getOrDefault(rightFieldName, rightCardinality); | ||
| selectivity *= 1 / max(leftDistinct, rightDistinct); | ||
| } catch (NullPointerException e) { | ||
| throw new IllegalArgumentException( | ||
| "Table statistics not found for table: " + leftTableName + " or " + rightTableName); | ||
| } | ||
| } | ||
| 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.outputSize; | ||
| unionCost = max(inputCost.executionCost, unionCost); | ||
| } | ||
| unionCost *= 2; | ||
| return new CostInfo(unionCost, unionSize); | ||
| } | ||
|
|
||
| private CostInfo getExecutionCostProject(LogicalProject project) { | ||
| return getExecutionCost(project.getInput()); | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
coral-incremental/src/test/java/com/linkedin/coral/incremental/RelNodeCostEstimatorTest.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,106 @@ | ||
| /** | ||
| * 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.File; | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.hadoop.hive.conf.HiveConf; | ||
| import org.apache.hadoop.hive.metastore.api.MetaException; | ||
| import org.apache.hadoop.hive.ql.metadata.HiveException; | ||
| import org.testng.annotations.AfterTest; | ||
| import org.testng.annotations.BeforeClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static com.linkedin.coral.incremental.TestUtils.*; | ||
| import static org.testng.Assert.*; | ||
|
|
||
|
|
||
| public class RelNodeCostEstimatorTest { | ||
| private HiveConf conf; | ||
|
|
||
| private RelNodeCostEstimator estimator; | ||
|
|
||
| static final String TEST_JSON_FILE_DIR = "src/test/resources/"; | ||
|
|
||
| @BeforeClass | ||
| public void beforeClass() throws HiveException, MetaException, IOException { | ||
| conf = TestUtils.loadResourceHiveConf(); | ||
| estimator = new RelNodeCostEstimator(2.0, 1.0); | ||
| TestUtils.initializeViews(conf); | ||
| } | ||
|
|
||
| @AfterTest | ||
| public void afterClass() throws IOException { | ||
| FileUtils.deleteDirectory(new File(conf.get(CORAL_INCREMENTAL_TEST_DIR))); | ||
| } | ||
|
|
||
| public Map<String, Double> fakeStatData() { | ||
| Map<String, Double> stat = new HashMap<>(); | ||
| stat.put("hive.test.bar1", 80.0); | ||
| stat.put("hive.test.bar2", 20.0); | ||
| stat.put("hive.test.bar1_prev", 40.0); | ||
| stat.put("hive.test.bar2_prev", 10.0); | ||
| stat.put("hive.test.bar1_delta", 60.0); | ||
| stat.put("hive.test.bar2_delta", 10.0); | ||
| return stat; | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleSelectAll() throws IOException { | ||
| String sql = "SELECT * FROM test.bar1"; | ||
| RelNode relNode = hiveToRelConverter.convertSql(sql); | ||
| estimator.loadStatistic(TEST_JSON_FILE_DIR + "statistic.json"); | ||
| assertEquals(estimator.getCost(relNode), 300.0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleJoin() throws IOException { | ||
| String sql = "SELECT * FROM test.bar1 JOIN test.bar2 ON test.bar1.x = test.bar2.x"; | ||
yyy1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| RelNode relNode = hiveToRelConverter.convertSql(sql); | ||
| estimator.loadStatistic(TEST_JSON_FILE_DIR + "statistic.json"); | ||
| assertEquals(estimator.getCost(relNode), 500.0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleUnion() throws IOException { | ||
| String sql = "SELECT *\n" + "FROM test.bar1 AS bar1\n" + "INNER JOIN test.bar2 AS bar2 ON bar1.x = bar2.x\n" | ||
| + "UNION ALL\n" + "SELECT *\n" + "FROM test.bar3 AS bar3\n" + "INNER JOIN test.bar2 AS bar2 ON bar3.x = bar2.x"; | ||
| RelNode relNode = hiveToRelConverter.convertSql(sql); | ||
| estimator.loadStatistic(TEST_JSON_FILE_DIR + "statistic.json"); | ||
| assertEquals(estimator.getCost(relNode), 680.0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnsupportOperator() throws IOException { | ||
| String sql = "SELECT * FROM test.bar1 WHERE x = 1"; | ||
| RelNode relNode = hiveToRelConverter.convertSql(sql); | ||
| estimator.loadStatistic(TEST_JSON_FILE_DIR + "statistic.json"); | ||
| try { | ||
| estimator.getCost(relNode); | ||
| fail("Should throw exception"); | ||
| } catch (RuntimeException e) { | ||
| assertEquals(e.getMessage(), "Unsupported relational operation: " + "LogicalFilter"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoStatistic() throws IOException { | ||
| String sql = "SELECT * FROM test.foo"; | ||
| RelNode relNode = hiveToRelConverter.convertSql(sql); | ||
| estimator.loadStatistic(TEST_JSON_FILE_DIR + "statistic.json"); | ||
| try { | ||
| estimator.getCost(relNode); | ||
| fail("Should throw exception"); | ||
| } catch (RuntimeException e) { | ||
| assertEquals(e.getMessage(), "Table statistics not found for table: " + "hive.test.foo"); | ||
| } | ||
| } | ||
| } | ||
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.