File tree Expand file tree Collapse file tree 2 files changed +17
-5
lines changed
bigframes/core/compile/sqlglot/expressions Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change 1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import math
16+
1517import sqlglot .expressions as sge
1618
1719_ZERO = sge .Cast (this = sge .convert (0 ), to = "INT64" )
2325# FLOAT64 has 11 exponent bits, so max values is about 2**(2**10)
2426# ln(2**(2**10)) == (2**10)*ln(2) ~= 709.78, so EXP(x) for x>709.78 will overflow.
2527_FLOAT64_EXP_BOUND = sge .convert (709.78 )
28+
29+ # The natural logarithm of the maximum value for a signed 64-bit integer.
30+ # This is used to check for potential overflows in power operations involving integers
31+ # by checking if `exponent * log(base)` exceeds this value.
32+ _INT64_LOG_BOUND = math .log (2 ** 63 - 1 )
33+
34+ # Represents the largest integer N where all integers from -N to N can be
35+ # represented exactly as a float64. Float64 types have a 53-bit significand precision,
36+ # so integers beyond this value may lose precision.
37+ _FLOAT64_MAX_INT_PRECISION = 2 ** 53
Original file line number Diff line number Diff line change @@ -223,16 +223,13 @@ def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
223223def _int_pow_op (
224224 left_expr : sge .Expression , right_expr : sge .Expression
225225) -> sge .Expression :
226- import math
227-
228- overflow_value = math .log (2 ** 63 - 1 )
229226 overflow_cond = sge .and_ (
230227 sge .NEQ (this = left_expr , expression = sge .convert (0 )),
231228 sge .GT (
232229 this = sge .Mul (
233230 this = right_expr , expression = sge .Ln (this = sge .Abs (this = left_expr ))
234231 ),
235- expression = sge .convert (overflow_value ),
232+ expression = sge .convert (constants . _INT64_LOG_BOUND ),
236233 ),
237234 )
238235
@@ -271,7 +268,10 @@ def _float_pow_op(
271268 )
272269
273270 # Float64 lose integer precision beyond 2**53, beyond this insufficient precision to get parity
274- exp_too_big = sge .GT (this = sge .Abs (this = right_expr ), expression = sge .convert (2 ** 53 ))
271+ exp_too_big = sge .GT (
272+ this = sge .Abs (this = right_expr ),
273+ expression = sge .convert (constants ._FLOAT64_MAX_INT_PRECISION ),
274+ )
275275 # Treat very large exponents as +=INF
276276 norm_exp = sge .Case (
277277 ifs = [
You can’t perform that action at this time.
0 commit comments