github-actions[bot] commented on code in PR #61304: URL: https://github.com/apache/doris/pull/61304#discussion_r2944252627
########## fe/fe-core/src/main/java/org/apache/doris/analysis/ExprToStringValueVisitor.java: ########## @@ -0,0 +1,254 @@ +// 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.analysis; + +import org.apache.doris.catalog.ScalarType; +import org.apache.doris.catalog.StructType; +import org.apache.doris.catalog.Type; +import org.apache.doris.common.FeConstants; +import org.apache.doris.common.FractionalFormat; +import org.apache.doris.foundation.format.FormatOptions; +import org.apache.doris.nereids.util.DateUtils; + +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.List; + +/** + * Visitor that generates string values for Expr instances, handling different + * output modes (query, stream load) and complex type nesting. + * + * <p>This visitor extracts the logic previously in {@code getStringValueForQuery}, + * {@code getStringValueInComplexTypeForQuery}, and {@code getStringValueForStreamLoad} + * from Expr subclasses, following the same visitor pattern as {@link ExprToSqlVisitor}. + */ +public class ExprToStringValueVisitor extends ExprVisitor<String, StringValueContext> { + private static final Logger LOG = LogManager.getLogger(ExprToStringValueVisitor.class); + + public static final ExprToStringValueVisitor INSTANCE = new ExprToStringValueVisitor(); + + @Override + public String visit(Expr expr, StringValueContext ctx) { + return expr.getStringValue(); + } + + @Override + public String visitDateLiteral(DateLiteral expr, StringValueContext ctx) { + String value; + if (expr.getType().isTimeStampTz()) { + try { + ZoneId dorisZone = DateUtils.getTimeZone(); + String offset = dorisZone.getRules().getOffset(java.time.Instant.now()).toString(); + DateLiteral dateLiteral = new DateLiteral(expr.getStringValue(), + ScalarType.createDatetimeV2Type(((ScalarType) expr.getType()).getScalarScale())); + value = dateLiteral.getStringValue() + offset; + } catch (Exception e) { + LOG.warn("generate timestamptz({})'s string value for query failed. ", + expr.getStringValue(), e); + value = expr.getStringValue(); + } + } else { + value = expr.getStringValue(); + } + if (ctx.isInComplexType()) { + return wrapWithQuotes(value, ctx); + } + return value; + } + + @Override + public String visitFloatLiteral(FloatLiteral expr, StringValueContext ctx) { + String value; + if (expr.getType() == Type.TIMEV2) { + String timeStr = expr.getStringValue(); + value = timeStr.substring(1, timeStr.length() - 1); + } else { + double dValue = expr.getValue(); + if (expr.getType() == Type.FLOAT) { + Float fValue = (float) dValue; + if (fValue.equals(Float.POSITIVE_INFINITY)) { + dValue = Double.POSITIVE_INFINITY; + } + if (fValue.equals(Float.NEGATIVE_INFINITY)) { + dValue = Double.NEGATIVE_INFINITY; + } + } + value = FractionalFormat.getFormatStringValue(dValue, + expr.getType() == Type.DOUBLE ? 16 : 7, + expr.getType() == Type.DOUBLE ? "%.15E" : "%.6E"); + } + if (ctx.isInComplexType() && expr.getType() == Type.TIMEV2) { + return wrapWithQuotes(value, ctx); + } + return value; + } + + @Override + public String visitBoolLiteral(BoolLiteral expr, StringValueContext ctx) { + FormatOptions options = ctx.getFormatOptions(); + if (options.level > 0) { + return options.isBoolValueNum() ? expr.getStringValue() : (expr.getValue() ? "true" : "false"); + } + return expr.getStringValue(); + } + + @Override + public String visitNullLiteral(NullLiteral expr, StringValueContext ctx) { + if (ctx.isForStreamLoad()) { + return FeConstants.null_string; + } Review Comment: **Behavioral difference:** When `NullLiteral` is a child of a complex type (e.g., `ArrayLiteral`, `MapLiteral`) during stream load, the precedence of checks here differs from the old code. **Old behavior:** 1. `ArrayLiteral.getStringValueForStreamLoad(options)` was NOT overridden. 2. `Expr.getStringValueForStreamLoad` delegated to `getStringValueForQuery(options)`. 3. `ArrayLiteral.getStringValueForQuery` called `child.getStringValueInComplexTypeForQuery(options)` on each child. 4. `NullLiteral.getStringValueInComplexTypeForQuery` returned `options.getNullFormat()` → `"null"`. **New behavior:** 1. `visitArrayLiteral` calls `child.accept(this, ctx.asComplexType())` where ctx has `forStreamLoad=true`. 2. `asComplexType()` creates `{forStreamLoad=true, inComplexType=true}`. 3. `visitNullLiteral` hits the `isForStreamLoad()` check FIRST → returns `FeConstants.null_string` (`"\N"`). So null inside `[1, null]` in stream load goes from `"null"` → `"\N"`. Suggested fix — swap the check order to match old behavior: ```java public String visitNullLiteral(NullLiteral expr, StringValueContext ctx) { if (ctx.isInComplexType()) { return ctx.getFormatOptions().getNullFormat(); } if (ctx.isForStreamLoad()) { return FeConstants.null_string; } return null; } ``` Or add a test confirming the new behavior is intentional. -- 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]
