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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/RewriteDefaultExpression.java:
##########
@@ -76,12 +81,23 @@ private static Expression 
rewrite(ExpressionMatchingContext<Default> context) {
         }
 
         Column column = columnOpt.get();
-        DataType targetType = DataType.fromCatalogType(column.getType());
         if (column.isGeneratedColumn()) {
             throw new AnalysisException("DEFAULT cannot be used on generated 
column '"
                     + column.getName() + "'");
         }
 
+        Optional<TableIf> originalTable = slotRef.getOriginalTable();
+        if (originalTable.filter(PluginDrivenExternalTable.class::isInstance)
+                .filter(table -> ((PluginDrivenExternalTable) 
table).hasWriteOnlyDefaults())
+                .isPresent()) {
+            column = 
pinnedWriteColumn(context.cascadesContext.getStatementContext(), 
originalTable.get(), column)
+                    .orElseThrow(() -> new AnalysisException("DEFAULT(" + 
slotRef.toSql()
+                            + ") must reference a column of the table that 
this statement writes"));
+        }
+        // The pinned writer column can be wider than the read column the slot 
was bound with, e.g. after a type
+        // promotion committed outside Doris, and the default is a value of 
the writer column.
+        DataType targetType = DataType.fromCatalogType(column.getType());

Review Comment:
   [P2] Bind enclosing consumers against the writer column type
   
   With a cached read `score INT` and a same-field-ID writer `score BIGINT` 
whose write-default is `4900000001`, consider:
   
   ```sql
   UPDATE t SET score = DEFAULT(score)
   WHERE IF(TRUE, DEFAULT(score), 0) = 4900000001;
   ```
   
   Binding produces `Filter(CAST(IF(TRUE, DEFAULT(score:INT), 0):INT AS BIGINT) 
= 4900000001) -> Scan(score:INT)`. This rewrite correctly replaces DEFAULT with 
a BIGINT literal, but `Rewriter` preserves function signatures, so 
`If.withChildren`/`FunctionParams` retain IF's INT result. 
`FoldConstantRuleOnFE.visitIf` then calls `ensureSameResultType` on the true 
branch and synthesizes `CAST(4900000001 AS INT)`: loose casting yields NULL and 
updates no rows, while strict casting throws overflow. The predicate should be 
true for every row.
   
   The earlier direct DEFAULT result-type issue is fixed here; this narrowing 
cast is created separately by the already-bound IF consumer. Resolve the pinned 
writer type before binding its consumers, or rebind their signatures/coercions 
after substitution, and cover an enclosing IF through both analysis and rewrite.



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