{"record":{"id":"0f11f40327e4c2b0","repo":"prestodb/presto","slug":"elasticsearch-type-mismatch-0f11f4","errorCode":"ELASTICSEARCH_TYPE_MISMATCH","errorMessage":"Value out of range for field '%s' of type TINYINT: %s","messagePattern":"Value out of range for field '(.+?)' of type TINYINT: (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-elasticsearch/src/main/java/com/facebook/presto/elasticsearch/decoders/TinyintDecoder.java","lineNumber":48,"sourceCode":"    private final String path;\n\n    public TinyintDecoder(String path)\n    {\n        this.path = requireNonNull(path, \"path is null\");\n    }\n\n    @Override\n    public void decode(Hit hit, Supplier<Object> getter, BlockBuilder output)\n    {\n        Object value = getter.get();\n        if (value == null) {\n            output.appendNull();\n        }\n        else if (value instanceof Number) {\n            long decoded = ((Number) value).longValue();\n\n            if (decoded < Byte.MIN_VALUE || decoded > Byte.MAX_VALUE) {\n                throw new PrestoException(ELASTICSEARCH_TYPE_MISMATCH, format(\"Value out of range for field '%s' of type TINYINT: %s\", path, decoded));\n            }\n\n            TINYINT.writeLong(output, decoded);\n        }\n        else {\n            throw new PrestoException(ELASTICSEARCH_TYPE_MISMATCH, format(\"Expected a numeric value for field '%s' of type TINYINT: %s [%s]\", path, value, value.getClass().getSimpleName()));\n        }\n    }\n}\n","sourceCodeStart":30,"sourceCodeEnd":58,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-elasticsearch/src/main/java/com/facebook/presto/elasticsearch/decoders/TinyintDecoder.java#L30-L58","documentation":"TinyintDecoder.decode converts an Elasticsearch document field value into a Presto TINYINT. When the source value is a Number whose long value falls outside the signed 8-bit range (-128..127), the decoder throws ELASTICSEARCH_TYPE_MISMATCH because the value cannot be represented as TINYINT. This guards against silent truncation of out-of-range data.","triggerScenarios":"Querying an Elasticsearch field mapped as Presto TINYINT whose document value is a number outside -128..127 (e.g. 300, -5000); the mismatch is hit lazily during row decoding, not at query planning.","commonSituations":"Elasticsearch index has no strict numeric type enforcement (dynamic mapping stored larger integers); the Presto table column was declared TINYINT based on expected range but documents contain larger values; a reindex or bulk import added out-of-range data after the table was defined.","solutions":["Widen the Presto column type to SMALLINT/INTEGER/BIGINT in the table mapping and re-run the query","Fix the data in Elasticsearch: query for out-of-range documents and clamp or correct them","Use a cast in the query after mapping the field as a wider type, e.g. CAST(field AS TINYINT) with explicit clipping via LEAST/GREATEST"],"exampleFix":"// before (table mapping)\n\"myfield\": {\"type\": \"tinyint\"}\n// after\n\"myfield\": {\"type\": \"smallint\"}\n-- or in query\nSELECT LEAST(GREATEST(myfield, -128), 127) AS myfield_tinyint","handlingStrategy":"validation","validationCode":"// Before querying with TINYINT mapping, verify the ES field range\n// GET index/_search\n// {\"size\":0,\"aggs\":{\"mn\":{\"min\":{\"field\":\"myfield\"}}},\"mx\":{\"max\":{\"field\":\"myfield\"}}}\n// ensure -128 <= mn <= mx <= 127","typeGuard":"boolean isTinyintSafe(Object v) {\n  return v instanceof Number\n      && ((Number) v).longValue() >= Byte.MIN_VALUE\n      && ((Number) v).longValue() <= Byte.MAX_VALUE;\n}","tryCatchPattern":"try { rows = session.execute(\"SELECT tinyfield FROM idx\").fetchAll(); }\ncatch (PrestoException e) {\n  if (\"ELASTICSEARCH_TYPE_MISMATCH\".equals(e.getErrorCode().getName())) {\n    // remap column to SMALLINT/INTEGER and retry\n  } else throw e;\n}","preventionTips":["Map Elasticsearch numeric fields to the widest sensible integer type, not TINYINT, unless range is enforced","Enforce explicit dynamic mappings/templates so out-of-range numbers cannot be indexed","Validate bulk-ingested numeric data ranges before writing to Elasticsearch"],"tags":["elasticsearch","type-mismatch","decoder","data-range"],"backgroundTag":"value-out-of-range","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"}