{"record":{"id":"1124c1ce21b63654","repo":"prestodb/presto","slug":"distinctvaluescount-must-be-greater-than-or-equal","errorCode":null,"errorMessage":"distinctValuesCount must be greater than or equal to 0: %s","messagePattern":"distinctValuesCount must be greater than or equal to 0: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-spi/src/main/java/com/facebook/presto/spi/statistics/ColumnStatistics.java","lineNumber":78,"sourceCode":"    }\n\n    public ColumnStatistics(\n            Estimate nullsFraction,\n            Estimate distinctValuesCount,\n            Estimate dataSize,\n            Optional<DoubleRange> range,\n            Optional<StringRange> stringRange,\n            Optional<ConnectorHistogram> histogram)\n    {\n        this.nullsFraction = requireNonNull(nullsFraction, \"nullsFraction is null\");\n        if (!nullsFraction.isUnknown()) {\n            if (nullsFraction.getValue() < 0 || nullsFraction.getValue() > 1) {\n                throw new IllegalArgumentException(format(\"nullsFraction must be between 0 and 1: %s\", nullsFraction.getValue()));\n            }\n        }\n        this.distinctValuesCount = requireNonNull(distinctValuesCount, \"distinctValuesCount is null\");\n        if (!distinctValuesCount.isUnknown() && distinctValuesCount.getValue() < 0) {\n            throw new IllegalArgumentException(format(\"distinctValuesCount must be greater than or equal to 0: %s\", distinctValuesCount.getValue()));\n        }\n        this.dataSize = requireNonNull(dataSize, \"dataSize is null\");\n        if (!dataSize.isUnknown() && dataSize.getValue() < 0) {\n            throw new IllegalArgumentException(format(\"dataSize must be greater than or equal to 0: %s\", dataSize.getValue()));\n        }\n        this.range = requireNonNull(range, \"range is null\");\n        this.stringRange = requireNonNull(stringRange, \"string range is null\");\n        this.histogram = requireNonNull(histogram, \"histogram is null\");\n    }\n\n    @JsonProperty\n    public Estimate getNullsFraction()\n    {\n        return nullsFraction;\n    }\n\n    @JsonProperty\n    public Estimate getDistinctValuesCount()","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-spi/src/main/java/com/facebook/presto/spi/statistics/ColumnStatistics.java#L60-L96","documentation":"Constructor invariant check in ColumnStatistics: the provided distinctValuesCount Estimate is known but negative, which is impossible for a cardinality statistic. This IllegalArgumentException enforces valid optimizer statistics produced by connectors.","triggerScenarios":"Constructing ColumnStatistics with a distinctValuesCount Estimate whose value is negative, often from a subtraction like (rowCount - nullCount) that went below zero.","commonSituations":"Connectors computing NDV estimates via subtraction under rounding, or misparsed external stats (e.g. negative NDV from Hive metastore).","solutions":["Fix the connector's statistics computation so distinct values counts are never negative","Clamp or mark the estimate as unknown when a valid value cannot be computed"],"exampleFix":"// before\nEstimate.of(rowCount - nullCount)\n// after\nEstimate.of(Math.max(0, rowCount - nullCount))","handlingStrategy":"validation","validationCode":"if (ndv != null && !ndv.isUnknown() && ndv.getValue() < 0) { throw new IllegalArgumentException(\"distinctValuesCount negative: \" + ndv.getValue()); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp NDV estimates with Math.max(0, value)","Avoid deriving NDV by subtraction of imprecise counts","Use unknown estimates rather than unreliable computed values"],"tags":["spi","statistics","validation","optimizer"],"backgroundTag":"statistics-range-validation","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}