{"record":{"id":"ce2fe428d5a7a815","repo":"prestodb/presto","slug":"elasticsearch-type-mismatch-ce2fe4","errorCode":"ELASTICSEARCH_TYPE_MISMATCH","errorMessage":"Expected a string or numeric value for field '%s' of type VARCHAR: %s [%s]","messagePattern":"Expected a string or numeric value for field '(.+?)' of type VARCHAR: (.+?) \\[(.+?)\\]","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-elasticsearch/src/main/java/com/facebook/presto/elasticsearch/decoders/VarcharDecoder.java","lineNumber":49,"sourceCode":"    private final String path;\n\n    public VarcharDecoder(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 String || value instanceof Number) {\n            VARCHAR.writeSlice(output, Slices.utf8Slice(value.toString()));\n        }\n        else {\n            throw new PrestoException(ELASTICSEARCH_TYPE_MISMATCH, format(\"Expected a string or numeric value for field '%s' of type VARCHAR: %s [%s]\", path, value, value.getClass().getSimpleName()));\n        }\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":53,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-elasticsearch/src/main/java/com/facebook/presto/elasticsearch/decoders/VarcharDecoder.java#L31-L53","documentation":"VarcharDecoder.decode converts Elasticsearch document values into Presto VARCHAR. It accepts String and Number values (via toString), but any other type (Boolean, Map, List, date object, etc.) triggers ELASTICSEARCH_TYPE_MISMATCH because the value cannot be rendered as a string.","triggerScenarios":"A VARCHAR-mapped Elasticsearch field holds a boolean, array, or object in some documents; decode throws when reading such a document.","commonSituations":"Dynamic mapping produced mixed types (booleans in a string field); nested objects indexed into a field the Presto mapping declares as VARCHAR; aggregation/scripted fields returning complex values.","solutions":["Fix or reindex the documents so the field is consistently a string or number","Remap the column and normalize in SQL, e.g. CAST(field AS VARCHAR) if the underlying type is coercible","Add explicit index mappings so Elasticsearch coerces or rejects wrong-typed values at write time"],"exampleFix":"// before (mapping allows any type)\n\"status\": true\n// after (normalize producer)\n\"status\": String.valueOf(true)","handlingStrategy":"validation","validationCode":"// Verify field type homogeneity before mapping as VARCHAR-backed column\n// Use a scripted query or _mapping check that all values are string/number","typeGuard":"boolean isVarcharDecodable(Object v) {\n  return v == null || v instanceof String || v instanceof Number;\n}","tryCatchPattern":"try { pages = elasticsearchPageSource.read(); }\ncatch (PrestoException e) {\n  if (\"ELASTICSEARCH_TYPE_MISMATCH\".equals(e.getErrorCode().getName())) {\n    // reindex offending documents as strings, or widen/adjust the mapping\n  } else throw e;\n}","preventionTips":["Use explicit mappings so fields are coerced to string at write time","Reject boolean/object/array writes to string fields via ingest pipelines","Regularly audit dynamic-mapped indices for type drift"],"tags":["elasticsearch","type-mismatch","varchar","decoder"],"backgroundTag":"schema-type-mismatch","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"}