github-actions[bot] commented on code in PR #63528:
URL: https://github.com/apache/doris/pull/63528#discussion_r4227894831


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/ComplexTypeDefaultValue.java:
##########
@@ -0,0 +1,184 @@
+// 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.plans.commands.info;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.parser.NereidsParser;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.literal.ArrayLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.MapLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.StructLiteral;
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.MapType;
+import org.apache.doris.nereids.types.StructField;
+import org.apache.doris.nereids.types.StructType;
+
+import com.google.common.base.Preconditions;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringJoiner;
+
+/**
+ * Validates a non-null ARRAY/MAP/STRUCT column default and rewrites it into a 
canonical literal text.
+ *
+ * <p>The user supplied text is parsed as a SQL literal and every nested value 
is cast to the declared
+ * nested type, so a type mismatch is rejected at DDL time instead of when old 
rows are read. The
+ * canonical text only contains plain nested values (unquoted numbers, double 
quoted strings, nested
+ * brackets and NULL). BE parses the stored text in two places that must 
agree: the complex SerDe
+ * {@code from_fe_string} used by the default value iterator and schema 
change, and the
+ * string-to-complex cast used when INSERT fills an unmentioned column. 
Neither of them decodes
+ * escape sequences, and the DDL parser keeps the text of a default value 
verbatim when SHOW CREATE
+ * TABLE output is replayed, so string values containing quotes or backslashes 
are rejected instead
+ * of stored.
+ */
+public class ComplexTypeDefaultValue {
+    private ComplexTypeDefaultValue() {
+    }
+
+    /**
+     * Validate the default literal of a complex column and return its 
canonical text.
+     */
+    public static String canonicalize(DataType type, String defaultValue) 
throws AnalysisException {
+        Preconditions.checkArgument(type.isArrayType() || type.isMapType() || 
type.isStructType(),
+                "%s is not a complex type", type);
+        Expression expression;
+        try {
+            expression = new NereidsParser().parseExpression(defaultValue);

Review Comment:
   [P1] Reject nested values lost during SQL literal parsing. With the default 
`enable_strict_cast=false`, `ARRAY<DATEV2> DEFAULT '[DATEV2 "2024-01-01", 1]'` 
reaches `parseExpression`, whose array-item coercion infers DATEV2 and converts 
the failed cast of `1` into `NullLiteral`. `render()` then stores 
`["2024-01-01", NULL]`, so CREATE/ALTER succeeds and old rows or omitted-column 
INSERTs get a NULL the user did not specify. MAP keys and values use the same 
parser path. Parse defaults without permissive inferred-item coercion (or 
reject cast-to-NULL) before target rendering, and add a negative mixed-type 
case under the default session setting.



-- 
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]

Reply via email to