prestodb/presto · error · PrestoException

INVALID_CAST_ARGUMENT

INVALID_CAST_ARGUMENT

Error message

Cannot cast %s to %s

What it means

checkCanCast in DistinctTypeCasts validates CAST between distinct types at bind time. A cast between two distinct types is legal only if they share an ancestor relationship (one derives from the other); otherwise INVALID_CAST_ARGUMENT 'Cannot cast %s to %s' is thrown. The second branch (line 111 area) covers casting a distinct type to a non-distinct type that is not exactly its base type.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/type/DistinctTypeCasts.java:105

            Type toType = boundVariables.getTypeVariable("T2");
            checkCanCast(fromType, toType);
            return new BuiltInScalarFunctionImplementation(
                    false,
                    ImmutableList.of(
                            valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
                            valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
                    identity(fromType.getJavaType()));
        }
    }

    private static void checkCanCast(Type fromType, Type toType)
    {
        if (fromType instanceof DistinctType && toType instanceof DistinctType) {
            DistinctType fromDistinctType = (DistinctType) fromType;
            DistinctType toDistinctType = (DistinctType) toType;

            if (!hasAncestorRelationship(fromDistinctType, toDistinctType)) {
                throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast %s to %s", fromDistinctType.getName(), toDistinctType.getName()));
            }
        }
        else if (fromType instanceof DistinctType) {
            DistinctType fromDistinctType = (DistinctType) fromType;
            if (!fromDistinctType.getBaseType().equals(toType)) {
                throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast %s to %s", fromDistinctType.getName(), toType));
            }
        }
        else if (toType instanceof DistinctType) {
            DistinctType toDistinctType = (DistinctType) toType;
            if (!toDistinctType.getBaseType().equals(fromType)) {
                throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast %s to %s", fromType, toDistinctType.getName()));
            }
        }
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Cast through the base type explicitly: CAST(d AS baseType) first, then to the target
  2. Use the distinct type's base type as the cast target instead of an unrelated type
  3. Redefine one distinct type to derive from the other if a relationship is intended
  4. Rework the schema to avoid needing direct distinct-to-distinct casts

Example fix

// before
CAST(user_id AS order_id) -- unrelated distinct types
// after
CAST(CAST(user_id AS BIGINT) AS order_id) -- via shared base type
Defensive patterns

Strategy: type-guard

Validate before calling

boolean canCastDistinct(DistinctType from, DistinctType to) { return hasAncestorRelationship(from, to); }
boolean canCastToBase(DistinctType from, Type to) { return from.getBaseType().equals(to); }

Type guard

boolean isCastAllowed(Type fromType, Type toType) { if (fromType instanceof DistinctType && toType instanceof DistinctType) { return hasAncestorRelationship((DistinctType) fromType, (DistinctType) toType); } if (fromType instanceof DistinctType) { return ((DistinctType) fromType).getBaseType().equals(toType); } return true; }

Try / catch

try { checkCanCast(fromType, toType); cast(...); } catch (PrestoException e) { if (e.getErrorCode().getCode() == StandardErrorCode.INVALID_CAST_ARGUMENT.toErrorCode().getCode()) { /* route through base type cast */ } else { throw e; } }

Prevention

When it happens

Trigger: `CAST(distinctA AS distinctB)` where neither type is an ancestor of the other, or `CAST(distinctT AS otherType)` where otherType differs from distinctT's base type.

Common situations: Two distinct types created independently over the same base (e.g. two CREATE DISTINCT TYPE ... AS VARCHAR) being interchanged; casting a distinct type to a loosely-related type like VARCHAR hoping for implicit conversion.

Related errors


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