{"record":{"id":"5eb08405def76d99","repo":"prestodb/presto","slug":"invalid-arguments-5eb084","errorCode":"INVALID_ARGUMENTS","errorMessage":"Type %s does not allow ordering","messagePattern":"Type (.+?) does not allow ordering","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/distinct/DistinctTypeBetweenOperator.java","lineNumber":58,"sourceCode":"        extends SqlOperator\n{\n    public static final DistinctTypeBetweenOperator DISTINCT_TYPE_BETWEEN_OPERATOR = new DistinctTypeBetweenOperator();\n\n    private DistinctTypeBetweenOperator()\n    {\n        super(BETWEEN,\n                ImmutableList.of(withVariadicBound(\"T\", DISTINCT_TYPE)),\n                ImmutableList.of(),\n                parseTypeSignature(BOOLEAN),\n                ImmutableList.of(parseTypeSignature(\"T\"), parseTypeSignature(\"T\"), parseTypeSignature(\"T\")));\n    }\n\n    @Override\n    public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager)\n    {\n        DistinctType type = (DistinctType) boundVariables.getTypeVariable(\"T\");\n        if (!type.isOrderable()) {\n            throw new PrestoException(INVALID_ARGUMENTS, format(\"Type %s does not allow ordering\", type.getDisplayName()));\n        }\n        Type baseType = type.getBaseType();\n        FunctionHandle functionHandle = functionAndTypeManager.resolveOperator(BETWEEN, fromTypes(baseType, baseType, baseType));\n\n        return new BuiltInScalarFunctionImplementation(\n                false,\n                ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL), valueTypeArgumentProperty(RETURN_NULL_ON_NULL), valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),\n                functionAndTypeManager.getJavaScalarFunctionImplementation(functionHandle).getMethodHandle(),\n                Optional.empty());\n    }\n}\n","sourceCodeStart":40,"sourceCodeEnd":70,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/distinct/DistinctTypeBetweenOperator.java#L40-L70","documentation":"The BETWEEN operator specialized for DISTINCT types requires the underlying type to be orderable. During specialize, if the bound type variable T is a DistinctType whose base type does not support ordering, Presto throws INVALID_ARGUMENTS stating the type does not allow ordering. This happens at query planning/specialization time when the operator is resolved against a non-orderable type.","triggerScenarios":"Evaluating x BETWEEN a AND b where x, a, b are of a DISTINCT type whose base type is not orderable (e.g. a distinct type over a non-comparable type such as a map or row with non-orderable fields).","commonSituations":"User-defined DISTINCT types created over complex/non-orderable base types; queries comparing distinct-typed columns after a base-type change; cross-version differences in which types are orderable.","solutions":["Order only on the base type: cast the distinct values to the base type before BETWEEN (e.g. CAST(x AS baseType) BETWEEN ...).","Recreate the DISTINCT type over an orderable base type (e.g. varchar, bigint) instead of a complex one.","Rewrite the predicate with explicit equality/range logic if ordering is genuinely undefined.","Check type.isOrderable() semantics for the base type in your Presto version."],"exampleFix":"// before\nSELECT * FROM t WHERE d_val BETWEEN d_lo AND d_hi; -- d_* non-orderable distinct type\n// after\nSELECT * FROM t WHERE CAST(d_val AS varchar) BETWEEN CAST(d_lo AS varchar) AND CAST(d_hi AS varchar);","handlingStrategy":"type-guard","validationCode":"-- only compare when the distinct type is orderable (app-level metadata check)\n-- if (!distinctType.isOrderable()) use base-type cast comparisons instead","typeGuard":"boolean canUseBetween(DistinctType t) {\n    return t != null && t.isOrderable();\n}","tryCatchPattern":"try { rows = query(\"SELECT * FROM t WHERE d_val BETWEEN d_lo AND d_hi\"); } catch (PrestoException e) { if (e.getErrorCode().getName().equals(\"INVALID_ARGUMENTS\")) { rows = query(\"SELECT * FROM t WHERE CAST(d_val AS \" + baseType + \") BETWEEN CAST(d_lo AS \" + baseType + \") AND CAST(d_hi AS \" + baseType + \")\"); } else { throw e; } }","preventionTips":["Define DISTINCT types only over orderable base types if range predicates are planned.","Cast distinct-typed columns to base types for BETWEEN/comparison predicates.","Verify base-type orderability when migrating schemas across Presto versions.","Prefer equality operators when ordering semantics are not required."],"tags":["presto","sql","distinct-type","ordering","between"],"backgroundTag":"type-not-orderable","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"}