924060929 commented on code in PR #67238: URL: https://github.com/apache/doris/pull/67238#discussion_r4674605900
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/SearchSignatureForDateDiff.java: ########## @@ -0,0 +1,118 @@ +// 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. + +package org.apache.doris.nereids.trees.expressions.functions; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.DateTimeV2Type; +import org.apache.doris.nereids.types.TimeStampTzType; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.TypeCoercionUtils; + +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * Signature search for the {@code *_diff} functions, which list {@code (TIMESTAMPTZ, TIMESTAMPTZ)} first. + * + * <p>{@link SearchSignature} scores a string literal against the TIMESTAMPTZ overload by whether it + * carries a time zone, and breaks a remaining tie between the temporal overloads by list order. A + * string-typed argument that is not a literal, such as a VARCHAR column or a subquery or UNION + * output slot, gives it nothing to score, so the call bound TIMESTAMPTZ by list order and the value + * was cast through the session time zone (apache/doris#66120). + * + * <p>For that case only, when the call has a string-typed argument and no string literal to inspect, + * the TIMESTAMPTZ overload is tried after the others, so the call binds DATETIMEV2 as it does for + * zone-less literals. A call with an inspectable string literal is searched in list order with the + * usual score, so results for zoned, zone-less and mixed literals do not change. + * + * <p>A string column can hold zoned and zone-less rows together, so the overload alone does not suit + * every row: the DATETIME string parser rejects zone offsets that the TIMESTAMPTZ one accepts, such as + * {@code -14:21}. A call whose overload moves away from TIMESTAMPTZ therefore converts each string + * argument through TIMESTAMPTZ, the cast it got before the move, and then to DATETIMEV2, which reads + * the instant back in the session time zone. The rows TIMESTAMPTZ parses are still parsed, and a + * zone-less row keeps its wall-clock time. + */ +public interface SearchSignatureForDateDiff extends ExplicitlyCastableSignature { + + @Override + default FunctionSignature searchSignature(List<FunctionSignature> signatures) { + if (searchesTimeStampTzLast(getArguments())) { + return ExplicitlyCastableSignature.super.searchSignature(withTimeStampTzLast(signatures)); + } + return ExplicitlyCastableSignature.super.searchSignature(signatures); + } + + @Override + default Expression prepareBeforeTypeCoercion() { + if (!searchesTimeStampTzLast(getArguments())) { + return (Expression) this; + } + // convert only when trying TIMESTAMPTZ last is what moves the overload: beside a typed temporal + // argument the number of casts decides, the call binds as it did, and so does its coercion + List<FunctionSignature> signatures = getSignatures(); + if (!hasTimeStampTzArgument(ExplicitlyCastableSignature.super.searchSignature(signatures)) + || hasTimeStampTzArgument( + ExplicitlyCastableSignature.super.searchSignature(withTimeStampTzLast(signatures)))) { + return (Expression) this; + } + // the converted arguments are no longer strings, so preparing the result again changes nothing + return withChildren(argument -> argument.getDataType().isStringLikeType() + ? TypeCoercionUtils.castIfNotSameType( Review Comment: [P2] Preserve valid UTC inputs when the new outer cast exceeds the local datetime range The inner TIMESTAMPTZ cast preserves parsing, but the new outer DATETIMEV2 cast adds another representability constraint. With `time_zone='+08:00'`, consider VARCHAR values: ```text a = '9999-12-31 23:00:00+00:00' b = '9999-12-31 22:00:00+00:00' ``` Both UTC instants are valid. On the base, the VARCHAR call binds TIMESTAMPTZ and `hours_diff(a,b)` computes 1 without materializing session-local datetimes. This rewrite converts them to `10000-01-01 07:00:00` and `10000-01-01 06:00:00`, outside DATETIMEV2's supported years. This is distinct from the disclosed zone-less input whose initial UTC conversion overflows: here the original UTC values and inner casts are valid; only the newly inserted outer casts fail. I built head `d6bc63c5`, verified its runtime version, and checked the subquery plan: it contains both nested casts. Calling the production FE conversion code on these values gives an original UTC diff of 1, then `AnalysisException: datetime out of range+10000-01-01T07:00` / `...T06:00` for the outer casts. For stored rows, `TimestampTzValue::to_datetime` rejects the local year; `cast_to_date.h` lines 493-498 return an error in strict mode, while lines 533-537 set NULL in non-strict mode. Those BE outcomes are source-confirmed; I have not executed this stored-row case on BE in this recheck. Please preserve the previously computable input domain in the conversion strategy and add stored-VARCHAR boundary regressions in both strict-cast modes, including the corresponding lower-year/negative-session-offset case. Parsing through TIMESTAMPTZ alone does not guarantee the subsequent local conversion is representable. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
