leaves12138 commented on code in PR #10502: URL: https://github.com/apache/paimon/pull/10502#discussion_r4240949099
########## paimon-python/pypaimon/casting/schema_evolution.py: ########## @@ -0,0 +1,104 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Java cast rules shared by old-file readers.""" + +from decimal import Decimal, localcontext, ROUND_HALF_UP + +import pyarrow as pa + +from pypaimon.table.row.generic_row import _parse_type_precision_scale + + +def _narrow_integer(value, bits): + half = 1 << (bits - 1) + return (value + half) % (half * 2) - half + + +def _numeric_to_integer(array, target_type): + bits = target_type.bit_width + floating = pa.types.is_floating(array.type) + intermediate_bits = 64 if bits == 64 else 32 + lower = -(1 << (intermediate_bits - 1)) + upper = (1 << (intermediate_bits - 1)) - 1 + values = [] + for value in array.to_pylist(): + if value is None: + values.append(None) + continue + # Number.byteValue/shortValue first convert floats to int; Java's + # float-to-int/long conversion saturates before the final narrowing. + if floating: + value = lower if value <= lower else upper if value >= upper else int(value) Review Comment: `float('nan')` reaches this branch because both comparisons are false, and `int(value)` raises `ValueError`. Java `Float/Double.intValue()` returns `0` for NaN, so schema evolution such as `DOUBLE -> TINYINT/INT/BIGINT` must preserve that behavior instead of failing the whole read. Please handle NaN explicitly (and add a regression case); `+/-inf` already follow the saturation path. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
