3939import org .apache .doris .nereids .types .ArrayType ;
4040import org .apache .doris .nereids .types .DataType ;
4141import org .apache .doris .nereids .types .MapType ;
42+ import org .apache .doris .nereids .types .StringType ;
4243import org .apache .doris .nereids .types .StructField ;
4344import org .apache .doris .nereids .types .StructType ;
4445import org .apache .doris .nereids .types .UuidType ;
@@ -74,6 +75,10 @@ private static Expression convertUuid(Expression input, DataType targetType) {
7475 if (!needsConversion (input .getDataType (), targetType )) {
7576 return input ;
7677 }
78+ // Parse serialized containers with UUID leaves kept as text. Nullable complex SerDes
79+ // otherwise consume malformed UUIDs as NULL before the write validator can see them.
80+ input = TypeCoercionUtils .castIfNotSameType (input ,
81+ uuidIntermediateType (input .getDataType (), targetType , true ));
7782 if (targetType instanceof ArrayType ) {
7883 DataType elementTarget = ((ArrayType ) targetType ).getItemType ();
7984 ArrayItemReference item = new ArrayItemReference ("item" , input );
@@ -102,9 +107,9 @@ private static Expression convertUuid(Expression input, DataType targetType) {
102107 }
103108 return TypeCoercionUtils .processBoundFunction (new CreateMap (children .toArray (new Expression [0 ])));
104109 }
105- // Cast the entire map once: separately extracting keys and values would evaluate
106- // a volatile source twice and could pair keys with values from a different map .
107- return new Cast (input , targetType , false , true );
110+ // Evaluate the map once, but change only UUID text leaves in the strict cast.
111+ // Narrowing non-UUID siblings here would override ordinary sink cast semantics .
112+ return new Cast (input , uuidIntermediateType ( input . getDataType (), targetType , false ) , false , true );
108113 }
109114 if (targetType instanceof StructType ) {
110115 if (!(input instanceof SlotReference ) && !(input instanceof StructLiteral )) {
@@ -136,6 +141,9 @@ private static Expression convertUuid(Expression input, DataType targetType) {
136141 }
137142
138143 private static boolean needsConversion (DataType input , DataType semantic ) {
144+ if (input .isStringLikeType () && containsUuid (semantic )) {
145+ return true ;
146+ }
139147 if (semantic instanceof UuidType ) {
140148 return input .isStringLikeType ();
141149 }
@@ -162,4 +170,59 @@ private static boolean needsConversion(DataType input, DataType semantic) {
162170 return false ;
163171 }
164172
173+ /** Preserve source siblings while parsing containers or validating UUID text leaves. */
174+ private static DataType uuidIntermediateType (DataType input , DataType target , boolean parseContainers ) {
175+ if (input .isStringLikeType () && containsUuid (target )) {
176+ if (target .isUuidType ()) {
177+ return parseContainers ? input : target ;
178+ }
179+ return parseContainers ? uuidTextType (target ) : input ;
180+ }
181+ if (input instanceof ArrayType && target instanceof ArrayType ) {
182+ return ArrayType .of (uuidIntermediateType (((ArrayType ) input ).getItemType (),
183+ ((ArrayType ) target ).getItemType (), parseContainers ));
184+ }
185+ if (input instanceof MapType && target instanceof MapType ) {
186+ return MapType .of (uuidIntermediateType (((MapType ) input ).getKeyType (),
187+ ((MapType ) target ).getKeyType (), parseContainers ),
188+ uuidIntermediateType (((MapType ) input ).getValueType (),
189+ ((MapType ) target ).getValueType (), parseContainers ));
190+ }
191+ if (input instanceof StructType && target instanceof StructType ) {
192+ List <StructField > sourceFields = ((StructType ) input ).getFields ();
193+ List <StructField > targetFields = ((StructType ) target ).getFields ();
194+ if (sourceFields .size () == targetFields .size ()) {
195+ List <StructField > fields = new ArrayList <>();
196+ for (int i = 0 ; i < sourceFields .size (); i ++) {
197+ StructField source = sourceFields .get (i );
198+ fields .add (source .withDataType (uuidIntermediateType (source .getDataType (),
199+ targetFields .get (i ).getDataType (), parseContainers )));
200+ }
201+ return new StructType (fields );
202+ }
203+ }
204+ return input ;
205+ }
206+
207+ private static DataType uuidTextType (DataType type ) {
208+ if (type .isUuidType ()) {
209+ return StringType .INSTANCE ;
210+ }
211+ if (type instanceof ArrayType ) {
212+ return ArrayType .of (uuidTextType (((ArrayType ) type ).getItemType ()));
213+ }
214+ if (type instanceof MapType ) {
215+ return MapType .of (uuidTextType (((MapType ) type ).getKeyType ()),
216+ uuidTextType (((MapType ) type ).getValueType ()));
217+ }
218+ if (type instanceof StructType ) {
219+ List <StructField > fields = new ArrayList <>();
220+ for (StructField field : ((StructType ) type ).getFields ()) {
221+ fields .add (field .withDataType (uuidTextType (field .getDataType ())));
222+ }
223+ return new StructType (fields );
224+ }
225+ return type ;
226+ }
227+
165228}
0 commit comments