Stephen0421 commented on code in PR #10675: URL: https://github.com/apache/paimon/pull/10675#discussion_r4509964832
########## paimon-python/pypaimon/schema/default_value.py: ########## @@ -0,0 +1,337 @@ +# 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. + +"""Validate a scalar column default before it is stored. + +The stored value is still the original string. Scalar checks follow the Java +string casts used by ``DefaultValueUtils`` for booleans, numbers, dates, times +and timestamps. ARRAY, MAP and ROW defaults are accepted by Java's +``StringToArrayCastRule``, ``StringToMapCastRule`` and ``StringToRowCastRule``; +Python does not parse those forms yet and rejects them explicitly. +""" + +import re +from calendar import monthrange +from datetime import time +from typing import Optional + +from pypaimon.schema.data_types import AtomicType + +_TRUE = {"t", "true", "y", "yes", "1"} +_FALSE = {"f", "false", "n", "no", "0"} +_INT_MIN = -2147483648 +_INT_MAX = 2147483647 +_INT_RANGES = { + "TINYINT": (-128, 127), + "SMALLINT": (-32768, 32767), + "INT": (_INT_MIN, _INT_MAX), + "INTEGER": (_INT_MIN, _INT_MAX), + "BIGINT": (-9223372036854775808, 9223372036854775807), +} +_INTEGRAL = set(_INT_RANGES) +_APPROXIMATE = {"FLOAT", "DOUBLE"} +_CHARACTER = {"STRING", "CHAR", "VARCHAR"} +_BINARY = {"BINARY", "VARBINARY", "BYTES"} +_UNSIGNED_INTEGER_RE = re.compile(r"[0-9]+") +# StringUtils.isNumeric guards the epoch fast path of DATE, TIME and TIMESTAMP. +# It allows a leading '-' but not '+', and only ASCII digits after it. +_JAVA_NUMERIC_RE = re.compile(r"-?[0-9]+") +_INTEGRAL_RE = re.compile(r"[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]*)") +# BigDecimal(String) reads digits with Character.digit, so Unicode digits are +# valid here, unlike the other numeric casts. +_DECIMAL_RE = re.compile( + r"[+-]?(?:(\d+)(?:\.(\d*))?|\.(\d+))(?:[eE]([+-]?\d+))?") Review Comment: Thanks for catching this! Fixed in 043e1f8abe. `BigDecimal(String)` reads each UTF-16 char with `Character.digit(char, 10)`. On JDK 8 that is the Unicode 6.2 decimal-digit set, not every digit Python's `\d` and `int()` accept. The validator now allows only those 35 ranges and maps them to ASCII before the exponent and scale checks. Two forms that Python used to accept are rejected before the schema is committed: 1. U+1D7D9 (`\u1D7D9`, mathematical double-struck digit one) is a surrogate pair, so `Character.digit` returns -1 and Java throws `NumberFormatException`. 2. U+0DE7 (`\u0DE7`, Sinhala digit one) was added in Unicode 7.0 and is not a digit on JDK 8. ASCII `1`, Arabic-Indic `\u0661.\u0662`, and fullwidth `\uFF11\uFF12.\uFF15` are still accepted, including when those digits appear in the exponent (`\u0661e\u0661`). `None` still clears the default. A zero value still skips the power-of-ten rescale check, and JDK 8 zero digits, not only ASCII `0`, are stripped from a long exponent first. -- 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]
