@@ -2029,6 +2029,8 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
20292029 // Leave the value unchanged only for undefined inputs, then apply the power if
20302030 // the result stays within the max. The exponent is constant, so only the
20312031 // undefined guard its value can actually trigger is emitted.
2032+ $ oddInteger = \floor ($ exponent ) == $ exponent && ((int ) $ exponent ) % 2 !== 0 ;
2033+
20322034 $ whens = [];
20332035 if ($ exponent < 0 ) {
20342036 // 0 to a negative power is undefined (POWER would error / return NULL).
@@ -2038,10 +2040,18 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
20382040 // A negative base to a fractional exponent is not a real number.
20392041 $ whens [] = "WHEN {$ col } < 0 THEN {$ col }" ;
20402042 }
2041- // Compare magnitudes with logarithms so POWER() never runs on a value that would
2042- // overflow (|base|^exp > max <=> exp * LOG(|base|) > LOG(max)). Works for both
2043- // signs; ABS() keeps LOG() defined for negative bases with an integer exponent.
2044- $ whens [] = "WHEN {$ col } <> 0 AND : $ bindKey * LOG(ABS( {$ col })) > LOG(: $ maxKey) THEN {$ col }" ;
2043+ // Cap by magnitude via logarithms so POWER() never runs on a value that would
2044+ // overflow (base^exp > max <=> exp * LOG(base) > LOG(max)).
2045+ if ($ oddInteger ) {
2046+ // An odd exponent keeps a negative base negative, and a negative result is
2047+ // always within a positive max, so only cap positive bases; negative bases
2048+ // fall through to POWER() and their (negative) result is applied.
2049+ $ whens [] = "WHEN {$ col } > 0 AND : $ bindKey * LOG( {$ col }) > LOG(: $ maxKey) THEN {$ col }" ;
2050+ } else {
2051+ // Otherwise the result is non-negative, so its magnitude equals its value —
2052+ // cap either sign. ABS() keeps LOG() defined for a negative even-power base.
2053+ $ whens [] = "WHEN {$ col } <> 0 AND : $ bindKey * LOG(ABS( {$ col })) > LOG(: $ maxKey) THEN {$ col }" ;
2054+ }
20452055
20462056 $ whenSql = \implode (' ' , $ whens );
20472057 return "{$ quotedColumn } = CASE {$ whenSql } ELSE POWER( {$ col }, : $ bindKey) END " ;
0 commit comments