prestodb/presto · error · DuplicateMapKeyException

DuplicateMapKeyException

Error message

DuplicateMapKeyException

What it means

DuplicateMapKeyException is thrown by buildHashTableStrict (via closeEntryStrict) when inserting a key into the open-addressing hash table finds the key already present. Presto maps enforce key uniqueness at build time in strict mode, so writing a second entry with an equal key fails. The exception message is the class name and it is special-cased by Presto's error handling for retry semantics.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/MapBlockBuilder.java:591

                Boolean isDuplicateKey;
                try {
                    // assuming maps with indeterminate keys are not supported
                    isDuplicateKey = (Boolean) keyBlockEquals.invokeExact(keyBlock, keyOffset + i, keyBlock, keyOffset + outputHashTable[hashTableOffset + hash]);
                }
                catch (RuntimeException e) {
                    throw e;
                }
                catch (Throwable throwable) {
                    throw new RuntimeException(throwable);
                }

                if (isDuplicateKey == null) {
                    throw new NotSupportedException("map key cannot be null or contain nulls");
                }

                if (isDuplicateKey) {
                    throw new DuplicateMapKeyException(keyBlock, keyOffset + i);
                }

                hash++;
                if (hash == hashTableSize) {
                    hash = 0;
                }
            }
        }
    }

    private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize)
    {
        if (keyBlock.isNull(position)) {
            throw new IllegalArgumentException("map keys cannot be null");
        }

        long hashCode;
        try {

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Deduplicate entries before building the map
  2. Use a function that tolerates duplicates (e.g. keep-last semantics) if available
  3. Report a clear user-facing error that input entries contain duplicate keys

Example fix

// before
builder.key(keyBlock, pos).value(...).closeEntryStrict(); // '1' already added
// after
Set<Integer> seen = new HashSet<>();
checkState(seen.add(keyHash), "duplicate key in map entries");
builder.key(keyBlock, pos).value(...).closeEntryStrict();
Defensive patterns

Strategy: try-catch

Try / catch

try {
    builder.closeEntryStrict();
} catch (DuplicateMapKeyException e) {
    // handle duplicate key: report position e.getPosition() or deduplicate and retry
}

Prevention

When it happens

Trigger: Calling closeEntryStrict twice with equal keys on the same MapBlockBuilder, e.g. map_from_entries with duplicate entries, or a map constructor fed repeated keys.

Common situations: SQL like map_from_entries(ARRAY[(1,'a'),(1,'b')]); upstream data with duplicate join/lookup keys feeding map construction.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/3968142638b82259. Report an issue: GitHub.