{"record":{"id":"7903f99357d3fcca","repo":"apache/flink","slug":"error-invalid-number-of-samples-numsamples","errorCode":null,"errorMessage":"Error: Invalid number of samples: ${numSamples}","messagePattern":"Error: Invalid number of samples: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/io/DelimitedInputFormat.java","lineNumber":399,"sourceCode":"            // compute how many samples to take, depending on the defined upper and lower bound\n            final int numSamples;\n            if (this.numLineSamples != NUM_SAMPLES_UNDEFINED) {\n                numSamples = this.numLineSamples;\n            } else {\n                // make the samples small for very small files\n                final int calcSamples = (int) (stats.getTotalInputSize() / 1024);\n                numSamples =\n                        Math.min(\n                                DEFAULT_MAX_NUM_SAMPLES,\n                                Math.max(DEFAULT_MIN_NUM_SAMPLES, calcSamples));\n            }\n\n            // check if sampling is disabled.\n            if (numSamples == 0) {\n                return stats;\n            }\n            if (numSamples < 0) {\n                throw new RuntimeException(\"Error: Invalid number of samples: \" + numSamples);\n            }\n\n            // make sure that the sampling times out after a while if the file system does not\n            // answer in time\n            this.openTimeout = 10000;\n            // set a small read buffer size\n            this.bufferSize = 4 * 1024;\n            // prevent overly large records, for example if we have an incorrectly configured\n            // delimiter\n            this.lineLengthLimit = MAX_SAMPLE_LEN;\n\n            long offset = 0;\n            long totalNumBytes = 0;\n            long stepSize = stats.getTotalInputSize() / numSamples;\n\n            int fileNum = 0;\n            int samplesTaken = 0;\n","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/DelimitedInputFormat.java#L381-L417","documentation":"During getStatistics(), DelimitedInputFormat computes the number of samples from the total input size and clamps it between configured min/max bounds. A negative resulting numSamples indicates an internal arithmetic/config inconsistency (e.g. DEFAULT_MIN_NUM_SAMPLES or DEFAULT_MAX_NUM_SAMPLES misconfigured to negative). The format throws a RuntimeException (not IllegalArgumentException) because this is not a direct user argument but a derived, unexpected value.","triggerScenarios":"Calling format.getStatistics() when the global optimizer config sets DELIMITED_FORMAT_MIN_LINE_SAMPLES or DELIMITED_FORMAT_MAX_LINE_SAMPLES to values that yield a negative derived numSamples; or an environment where the loaded defaults produced a negative result.","commonSituations":"Custom flink-conf.yaml with a typo'd/negative value for optimizer.delimited-format.min-line-samples or max-line-samples; running with a corrupted or hand-edited configuration; a build where loadConfigParameters logged an invalid default but execution continued.","solutions":["Check flink-conf.yaml / OptimizerOptions: ensure optimizer.delimited-format.min-line-samples and max-line-samples are >= 0.","Remove overrides so the documented defaults are used.","If you do not need sampling, set the format's numLineSamples to 0 via setNumLineSamples(0) so getStatistics short-circuits before the negative check.","Inspect the log: loadConfigParameters logs 'Invalid default ... number of line samples' when defaults are bad."],"exampleFix":"// before: bad global config\n// optimizer.delimited-format.min-line-samples: -5\nstats = format.getStatistics(); // throws RuntimeException\n\n// after: fix config (or disable sampling)\n// optimizer.delimited-format.min-line-samples: 2\n// or in code:\nformat.setNumLineSamples(0);\nstats = format.getStatistics();","handlingStrategy":"try-catch","validationCode":"// Before getStatistics, ensure global optimizer sample bounds are non-negative.\nint minS = parameters.get(OptimizerOptions.DELIMITED_FORMAT_MIN_LINE_SAMPLES);\nint maxS = parameters.get(OptimizerOptions.DELIMITED_FORMAT_MAX_LINE_SAMPLES);\nif (minS < 0 || maxS < 0) {\n    // fix config or disable sampling\n    format.setNumLineSamples(0);\n}","typeGuard":null,"tryCatchPattern":"try {\n    BaseStatistics stats = format.getStatistics();\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Invalid number of samples\")) {\n        // global optimizer sample config is bad; fall back to no sampling\n        format.setNumLineSamples(0);\n        stats = format.getStatistics();\n    } else {\n        throw e;\n    }\n}","preventionTips":["Keep optimizer.delimited-format.min-line-samples and max-line-samples >= 0 in flink-conf.yaml.","Watch the log: loadConfigParameters warns on invalid sample defaults.","Disable sampling (setNumLineSamples(0)) when you do not need split-size estimation."],"tags":["input-format","delimited","sampling","statistics","configuration","runtime-exception"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}