Skip to content

Commit

Permalink
Reject negative cost components
Browse files Browse the repository at this point in the history
No work has negative cost. Previously such negative costs could go
unnoticed, or noticed only if explain plan is produced.
  • Loading branch information
findepi committed Dec 15, 2023
1 parent 7e8ad5a commit ab27cb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.stream.Stream;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Double.NaN;
import static java.lang.Double.isNaN;

/**
* Represents inherent cost of some plan node, not including cost of its sources.
Expand Down Expand Up @@ -63,6 +65,9 @@ public LocalCostEstimate(
@JsonProperty("maxMemory") double maxMemory,
@JsonProperty("networkCost") double networkCost)
{
checkArgument(isNaN(cpuCost) || cpuCost >= 0, "cpuCost cannot be negative: %s", cpuCost);
checkArgument(isNaN(maxMemory) || maxMemory >= 0, "maxMemory cannot be negative: %s", maxMemory);
checkArgument(isNaN(networkCost) || networkCost >= 0, "networkCost cannot be negative: %s", networkCost);
this.cpuCost = cpuCost;
this.maxMemory = maxMemory;
this.networkCost = networkCost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Double.isNaN;

public class PlanNodeStatsAndCostSummary
{
private final double outputRowCount;
Expand All @@ -32,6 +35,11 @@ public PlanNodeStatsAndCostSummary(
@JsonProperty("memoryCost") double memoryCost,
@JsonProperty("networkCost") double networkCost)
{
checkArgument(isNaN(outputRowCount) || outputRowCount >= 0, "outputRowCount cannot be negative: %s", outputRowCount);
checkArgument(isNaN(outputSizeInBytes) || outputSizeInBytes >= 0, "outputSizeInBytes cannot be negative: %s", outputSizeInBytes);
checkArgument(isNaN(cpuCost) || cpuCost >= 0, "cpuCost cannot be negative: %s", cpuCost);
checkArgument(isNaN(memoryCost) || memoryCost >= 0, "memoryCost cannot be negative: %s", memoryCost);
checkArgument(isNaN(networkCost) || networkCost >= 0, "networkCost cannot be negative: %s", networkCost);
this.outputRowCount = outputRowCount;
this.outputSizeInBytes = outputSizeInBytes;
this.cpuCost = cpuCost;
Expand Down

0 comments on commit ab27cb8

Please sign in to comment.