{"record":{"id":"06fd8c109d9ae50f","repo":"hibernate/hibernate-orm","slug":"illegal-operator-for-a-duration","errorCode":null,"errorMessage":"Illegal operator for a duration {}","messagePattern":"Illegal operator for a duration (.+?)","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/sql/spi/BaseSqmToSqlAstConverter.java","lineNumber":7273,"sourceCode":"\t\t\t\t// -x * (d1 - d2) => - x * d1 + x * d2\n\t\t\t\tinferrableTypeAccessStack.push( () -> determineValueMapping( rhs, fromClauseIndex ) );\n\t\t\t\tfinal Expression duration = toSqlExpression( lhs.accept( this ) );\n\t\t\t\tinferrableTypeAccessStack.pop();\n\t\t\t\tfinal Expression scale = adjustmentScale;\n\t\t\t\tfinal boolean negate = negativeAdjustment;\n\t\t\t\tadjustmentScale = applyScale( duration );\n\t\t\t\tnegativeAdjustment = false; //was sucked into the scale\n\t\t\t\ttry {\n\t\t\t\t\tinferrableTypeAccessStack.push( () -> determineValueMapping( lhs, fromClauseIndex ) );\n\t\t\t\t\treturn rhs.accept( this );\n\t\t\t\t}\n\t\t\t\tfinally {\n\t\t\t\t\tinferrableTypeAccessStack.pop();\n\t\t\t\t\tadjustmentScale = scale;\n\t\t\t\t\tnegativeAdjustment = negate;\n\t\t\t\t}\n\t\t\tdefault:\n\t\t\t\tthrow new SemanticException( \"Illegal operator for a duration \" + operator );\n\t\t}\n\t}\n\n\tprivate Object transformDatetimeArithmetic(SqmBinaryArithmetic<?> expression) {\n\t\tfinal var operator = expression.getOperator();\n\n\t\t// the only kind of algebra we know how to\n\t\t// do on dates/timestamps is subtract them,\n\t\t// producing a duration - all other binary\n\t\t// operator expressions with two dates or\n\t\t// timestamps are ill-formed\n\t\tif ( operator != SUBTRACT ) {\n\t\t\tthrow new SemanticException( \"Illegal operator for temporal type: \" + operator );\n\t\t}\n\n\t\t// a difference between two dates or two\n\t\t// timestamps is a leaf duration, so we\n\t\t// must apply the scale, and the 'by unit'","sourceCodeStart":7255,"sourceCodeEnd":7291,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/spi/BaseSqmToSqlAstConverter.java#L7255-L7291","documentation":"transformDurationArithmetic handles only duration algebra with ADD, SUBTRACT and MULTIPLY-with-scalar-on-the-left; any other operator hitting the switch default throws this SemanticException. Effectively: durations can be added/subtracted and scaled by a scalar, but not divided, modulo'd, or multiplied by another duration.","triggerScenarios":"HQL dividing a duration ('(ts1 - ts2) / 2' after normalization in some versions), applying modulo to a duration, or multiplying two duration expressions together; negated duration constructs that fall through to an unhandled operator.","commonSituations":"Computing averages or ratios of time spans in report queries; porting SQL like 'datediff(...) / 7' to HQL duration syntax; refactors that change operand order so the duration lands under an unsupported operator.","solutions":["Express division of a duration as multiplication by a fraction: 'dur * 0.5'","Extract a scalar first (e.g. via 'extract(epoch from ...)' style functions) and do plain numeric math","Move the operation so the duration only ever appears with +, -, or scalar *"],"exampleFix":"// before\nselect (o.closedAt - o.openedAt) / 2 from Ord o\n\n// after\nselect (o.closedAt - o.openedAt) * 0.5 from Ord o","handlingStrategy":"try-catch","validationCode":"if (isDurationExpr(operand) && (operator == '/' || operator == '%' )) {\n    throw new IllegalArgumentException(\"Duration supports only +, - and scalar *; use dur * 0.5\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return session.createQuery(hql).getResultList();\n} catch (org.hibernate.query.SemanticException e) {\n    log.error(\"Illegal duration operator in {}: {}\", hql, e.getMessage());\n    throw e;\n}","preventionTips":["Use multiplication by a fraction instead of dividing durations","Extract scalars with functions for ratio math","Keep durations only in +/-/scalar-* positions in generated expressions"],"tags":["hibernate","hql","duration","arithmetic","operator"],"backgroundTag":"temporal-arithmetic-unsupported","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}