{"record":{"id":"814178e6ee2169a2","repo":"apache/iceberg","slug":"already-exists","errorCode":null,"errorMessage":"Already exists","messagePattern":"Already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/inmemory/InMemoryOutputFile.java","lineNumber":68,"sourceCode":"   * files are populated into the parentFileIO through other means).\n   *\n   * @param location the location returned by location() of this OutputFile, the InputFile obtained\n   *     from calling toInputFile(), and the location for looking up the associated InputFile from a\n   *     parentFileIO, if non-null.\n   * @param parentFileIO if non-null, commits an associated InMemoryInputFile on close() into the\n   *     parentFileIO, and uses the parentFileIO for \"already exists\" checks if creating without\n   *     overwriting.\n   */\n  public InMemoryOutputFile(String location, InMemoryFileIO parentFileIO) {\n    Preconditions.checkNotNull(location, \"location is null\");\n    this.location = location;\n    this.parentFileIO = parentFileIO;\n  }\n\n  @Override\n  public PositionOutputStream create() {\n    if (exists || (parentFileIO != null && parentFileIO.fileExists(location))) {\n      throw new AlreadyExistsException(\"Already exists\");\n    }\n    return createOrOverwrite();\n  }\n\n  @Override\n  public PositionOutputStream createOrOverwrite() {\n    exists = true;\n    contents = new ByteArrayOutputStream();\n    return new InMemoryPositionOutputStream(contents);\n  }\n\n  @Override\n  public String location() {\n    return location;\n  }\n\n  @Override\n  public InputFile toInputFile() {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/inmemory/InMemoryOutputFile.java#L50-L86","documentation":"InMemoryOutputFile.create() enforces create-new semantics: it throws AlreadyExistsException if the file already exists in the parent InMemoryFileIO. Use createOrOverwrite() when replacing an existing file is intended.","triggerScenarios":"Calling InMemoryOutputFile.create() (or outputStream(), which delegates to create()) for a location that already has bytes; writing the same location twice via create(); re-running a task that writes without cleaning up first.","commonSituations":"Retried test setup that re-creates files at fixed locations; assuming create() overwrites like Hadoop's create(overwrite=true); writing manifest/data files to reused in-memory locations across test runs sharing the static map.","solutions":["Use createOrOverwrite() instead of create() when the file may legitimately already exist.","Delete the existing file (io.deleteFile(location)) before create(), or use a unique location per write.","Catch org.apache.iceberg.exceptions.AlreadyExistsException if the duplicate is expected and should be skipped."],"exampleFix":"// before\nOutputFile out = io.newOutputFile(location);\ntry (PositionOutputStream s = out.create()) { ... } // throws if exists\n// after\ntry (PositionOutputStream s = out.createOrOverwrite()) { ... }","handlingStrategy":"validation","validationCode":"if (out instanceof InMemoryOutputFile && ((InMemoryOutputFile) out).exists()) { /* delete or use createOrOverwrite */ }","typeGuard":null,"tryCatchPattern":"try {\n  out.create();\n} catch (AlreadyExistsException e) {\n  out.createOrOverwrite(); // or skip\n}","preventionTips":["Default to createOrOverwrite() unless strict create-new semantics are required.","Use unique locations per write attempt in retried tasks.","Clean up prior outputs before re-running writes to fixed locations."],"tags":["file-io","already-exists","in-memory","conflict"],"backgroundTag":"file-already-exists","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}