{"record":{"id":"513e47bea52137ac","repo":"prestodb/presto","slug":"invalid-cast-argument-513e47","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Cannot cast %s to %s","messagePattern":"Cannot cast (.+?) to (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/DistinctTypeCasts.java","lineNumber":105,"sourceCode":"            Type toType = boundVariables.getTypeVariable(\"T2\");\n            checkCanCast(fromType, toType);\n            return new BuiltInScalarFunctionImplementation(\n                    false,\n                    ImmutableList.of(\n                            valueTypeArgumentProperty(RETURN_NULL_ON_NULL),\n                            valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),\n                    identity(fromType.getJavaType()));\n        }\n    }\n\n    private static void checkCanCast(Type fromType, Type toType)\n    {\n        if (fromType instanceof DistinctType && toType instanceof DistinctType) {\n            DistinctType fromDistinctType = (DistinctType) fromType;\n            DistinctType toDistinctType = (DistinctType) toType;\n\n            if (!hasAncestorRelationship(fromDistinctType, toDistinctType)) {\n                throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast %s to %s\", fromDistinctType.getName(), toDistinctType.getName()));\n            }\n        }\n        else if (fromType instanceof DistinctType) {\n            DistinctType fromDistinctType = (DistinctType) fromType;\n            if (!fromDistinctType.getBaseType().equals(toType)) {\n                throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast %s to %s\", fromDistinctType.getName(), toType));\n            }\n        }\n        else if (toType instanceof DistinctType) {\n            DistinctType toDistinctType = (DistinctType) toType;\n            if (!toDistinctType.getBaseType().equals(fromType)) {\n                throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast %s to %s\", fromType, toDistinctType.getName()));\n            }\n        }\n    }\n}\n","sourceCodeStart":87,"sourceCodeEnd":122,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/DistinctTypeCasts.java#L87-L122","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Cast through the base type explicitly: CAST(d AS baseType) first, then to the target","Use the distinct type's base type as the cast target instead of an unrelated type","Redefine one distinct type to derive from the other if a relationship is intended","Rework the schema to avoid needing direct distinct-to-distinct casts"],"exampleFix":"// before\nCAST(user_id AS order_id) -- unrelated distinct types\n// after\nCAST(CAST(user_id AS BIGINT) AS order_id) -- via shared base type","handlingStrategy":"type-guard","validationCode":"boolean canCastDistinct(DistinctType from, DistinctType to) { return hasAncestorRelationship(from, to); }\nboolean canCastToBase(DistinctType from, Type to) { return from.getBaseType().equals(to); }","typeGuard":"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; }","tryCatchPattern":"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; } }","preventionTips":["Always cast distinct types through their base type","Consult type metadata for ancestor relationships before CAST","Avoid defining multiple unrelated distinct types that must interconvert","Document distinct-type cast rules in schema docs"],"tags":["presto","distinct-type","cast","sql"],"backgroundTag":"invalid-cast-distinct-type","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"}