{"record":{"id":"f0c7eaf4c67a4757","repo":"prestodb/presto","slug":"malformed-query-file","errorCode":"MALFORMED_QUERY_FILE","errorMessage":"sql file size %s is different from expected sqlFileSizeInBytes %s","messagePattern":"sql file size (.+?) is different from expected sqlFileSizeInBytes (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-spark-base/src/main/java/com/facebook/presto/spark/PrestoSparkQueryExecutionFactory.java","lineNumber":578,"sourceCode":"            PrestoSparkTaskExecutorFactoryProvider executorFactoryProvider,\n            Optional<String> queryStatusInfoOutputLocation,\n            Optional<String> queryDataOutputLocation,\n            List<ExecutionStrategy> executionStrategies,\n            Optional<CollectionAccumulator<Map<String, Long>>> bootstrapMetricsCollector)\n    {\n        PrestoSparkConfInitializer.checkInitialized(sparkContext);\n\n        String sql;\n        if (sqlText.isPresent()) {\n            checkArgument(!sqlLocation.isPresent(), \"sqlText and sqlLocation should not be set at the same time\");\n            sql = sqlText.get();\n        }\n        else {\n            checkArgument(sqlLocation.isPresent(), \"sqlText or sqlLocation must be present\");\n            byte[] sqlFileBytes = metadataStorage.read(sqlLocation.get());\n            if (sqlFileSizeInBytes.isPresent()) {\n                if (Integer.valueOf(sqlFileSizeInBytes.get()) != sqlFileBytes.length) {\n                    throw new PrestoException(\n                            MALFORMED_QUERY_FILE,\n                            format(\"sql file size %s is different from expected sqlFileSizeInBytes %s\", sqlFileBytes.length, sqlFileSizeInBytes.get()));\n                }\n            }\n            if (sqlFileHexHash.isPresent()) {\n                try {\n                    MessageDigest md = MessageDigest.getInstance(\"SHA-512\");\n                    String actualHexHashCode = BaseEncoding.base16().lowerCase().encode(md.digest(sqlFileBytes));\n                    if (!sqlFileHexHash.get().equals(actualHexHashCode)) {\n                        throw new PrestoException(\n                                MALFORMED_QUERY_FILE,\n                                format(\"actual hash code %s is different from expected sqlFileHexHash %s\", actualHexHashCode, sqlFileHexHash.get()));\n                    }\n                }\n                catch (NoSuchAlgorithmException e) {\n                    throw new PrestoException(GENERIC_INTERNAL_ERROR, \"unsupported hash algorithm\", e);\n                }\n            }","sourceCodeStart":560,"sourceCodeEnd":596,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-spark-base/src/main/java/com/facebook/presto/spark/PrestoSparkQueryExecutionFactory.java#L560-L596","documentation":"When a Spark query is submitted with sqlLocation (SQL read from metadata storage) plus an expected sqlFileSizeInBytes, PrestoSparkQueryExecutionFactory verifies the downloaded file length matches the declared size. A mismatch means the SQL file was corrupted, truncated, or changed after hashing; it throws MALFORMED_QUERY_FILE. This guards against executing a different query than the submitter intended.","triggerScenarios":"Submitting a query with both sqlLocation and sqlFileSizeInBytes where the byte length of metadataStorage.read(sqlLocation) differs from sqlFileSizeInBytes (e.g. the file was edited after submission, or the size was computed on different content).","commonSituations":"Overwriting the SQL file in shared storage between job submission and execution; manual edits to the uploaded SQL file; client computing the size from a different file than uploaded; storage truncation.","solutions":["Re-upload the SQL file and resubmit, ensuring sqlFileSizeInBytes is recomputed from the final bytes of the same file","Verify the file in metadata storage was not modified between submission and launch (check timestamps/checksums)","If integrity checking is not needed, omit sqlFileSizeInBytes and rely on the hex hash instead (or neither)"],"exampleFix":"// before: size computed before final write\nlong size = oldBytes.length;\nstorage.write(path, newBytes);\nnew PrestoSparkQueryExecution(..., sqlLocation, Optional.of((int) size), ...);\n// after\nstorage.write(path, newBytes);\nnew PrestoSparkQueryExecution(..., sqlLocation, Optional.of(newBytes.length), ...);","handlingStrategy":"validation","validationCode":"byte[] bytes = storage.read(sqlLocation);\nif (sqlFileSizeInBytes != null && bytes.length != sqlFileSizeInBytes) {\n    throw new IllegalStateException(\"SQL file changed after submission; re-upload and resubmit\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    QueryExecution qe = factory.create(queryExecutionConfig);\n} catch (PrestoException e) {\n    if (\"MALFORMED_QUERY_FILE\".equals(e.getErrorCode().getName())) {\n        // re-upload SQL file and resubmit the query\n    } else throw e;\n}","preventionTips":["Compute sqlFileSizeInBytes from the exact uploaded bytes, after the final write","Write SQL files immutably (unique names) to shared storage","Avoid editing SQL files between submission and execution","Prefer hex-hash verification over size-only checking"],"tags":["presto-spark","query-submission","integrity-check"],"backgroundTag":"file-size-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"}