This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new ce680bba998 branch-4.1: [fix](fe) Quote unsafe view comments in 
exported DDL #67708 (#68076)
ce680bba998 is described below

commit ce680bba998ac2c667e594ad7601b5ff6c85ae10
Author: morrySnow <[email protected]>
AuthorDate: Sat Sep 19 15:32:41 2026 +0800

    branch-4.1: [fix](fe) Quote unsafe view comments in exported DDL #67708 
(#68076)
    
    ### What problem does this PR solve?
    
    Related PR: #67708
    
    Problem Summary:
    
    Backport the fix for invalid `SHOW CREATE VIEW` output when a view
    comment contains an apostrophe or backslash. Previously the comment was
    inserted directly into a single-quoted literal, so a value such as
    `O'Reilly` produced DDL that could not be replayed.
    
    Both view DDL construction paths now use a shared renderer. Unsafe
    comments are quoted with branch-4.1's existing
    `SqlLiteralUtils.quoteStringLiteral`, which preserves the active
    SQL-mode behavior, while safe comments retain the historical
    single-quoted output.
    
    ### Release note
    
    Fix exported view DDL for comments containing apostrophes or
    backslashes.
    
    ### Check List (For Author)
    
    - Test
        - [x] Unit Test
        - [x] Regression test coverage included
    - Behavior changed:
    - [x] Yes. Unsafe view comments are emitted as replayable SQL string
    literals.
    - Does this need documentation?
        - [x] No.
    
    Test details:
    
    - `CreateViewTest`: 8 tests passed.
    - Full FE Maven reactor: `BUILD SUCCESS`.
    
    ### Check List (For Reviewer who merge this PR)
    
    - [ ] Confirm the release note
    - [ ] Confirm test cases
    - [ ] Confirm document
    - [ ] Add branch pick label
---
 .../main/java/org/apache/doris/catalog/Env.java    | 22 ++++++++++++++++------
 .../org/apache/doris/catalog/CreateViewTest.java   | 21 +++++++++++++++++++++
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
index 43969050b50..265fe780bd7 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
@@ -202,6 +202,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.info.CreateTableInfo;
 import org.apache.doris.nereids.trees.plans.commands.info.CreateTableLikeInfo;
 import org.apache.doris.nereids.trees.plans.commands.info.CreateViewInfo;
 import org.apache.doris.nereids.trees.plans.commands.info.DropMTMVInfo;
+import org.apache.doris.nereids.util.SqlLiteralUtils;
 import org.apache.doris.persist.AlterMTMV;
 import org.apache.doris.persist.AutoIncrementIdUpdateLog;
 import org.apache.doris.persist.BackendReplicasInfo;
@@ -4092,9 +4093,7 @@ public class Env {
             View view = (View) table;
 
             sb.append("CREATE VIEW `").append(table.getName()).append("`");
-            if (StringUtils.isNotBlank(table.getComment())) {
-                sb.append(" COMMENT '").append(table.getComment()).append("'");
-            }
+            addViewComment(table, sb);
             sb.append(" AS ").append(view.getInlineViewDef());
             createTableStmt.add(sb + ";");
             return;
@@ -4513,9 +4512,7 @@ public class Env {
             sb.append("CREATE VIEW `").append(table.getName()).append("`");
             addColNameAndComment(view, sb);
             sb.append("\n");
-            if (StringUtils.isNotBlank(table.getComment())) {
-                sb.append(" COMMENT '").append(table.getComment()).append("'");
-            }
+            addViewComment(table, sb);
             sb.append(" AS ").append(view.getInlineViewDef());
             createTableStmt.add(sb + ";");
             return;
@@ -7488,6 +7485,19 @@ public class Env {
         }
     }
 
+    private static void addViewComment(TableIf table, StringBuilder sb) {
+        if (StringUtils.isNotBlank(table.getComment())) {
+            String comment = table.getComment();
+            sb.append(" COMMENT ");
+            // Keep the historical output unchanged when the comment is 
already safe in single quotes.
+            if (comment.indexOf('\'') >= 0 || comment.indexOf('\\') >= 0) {
+                sb.append(SqlLiteralUtils.quoteStringLiteral(comment));
+            } else {
+                sb.append('\'').append(comment).append('\'');
+            }
+        }
+    }
+
     public int getFollowerCount() {
         int count = 0;
         for (Frontend fe : frontends.values()) {
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java
index 450e824de10..b5b0ba2a9eb 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java
@@ -35,7 +35,9 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.UUID;
 
 public class CreateViewTest {
@@ -173,6 +175,25 @@ public class CreateViewTest {
         Assert.assertNotNull(view8.getColumn("c_array"));
     }
 
+    @Test
+    public void testViewCommentDdlRoundTrip() throws Exception {
+        createView("create view test.view_comment_round_trip comment 
\"O'Reilly\" as select 1 as c");
+
+        Database db = 
Env.getCurrentInternalCatalog().getDbOrDdlException("test");
+        View originalView = (View) 
db.getTableOrDdlException("view_comment_round_trip");
+        List<String> createViewStmts = new ArrayList<>();
+        Env.getDdlStmt(originalView, createViewStmts, null, null, false, true, 
-1L);
+
+        String exportedDdl = createViewStmts.get(0);
+        Assert.assertTrue(exportedDdl.contains(" COMMENT \"O'Reilly\""));
+        String copiedDdl = exportedDdl.replace("CREATE VIEW 
`view_comment_round_trip`",
+                "CREATE VIEW test.`view_comment_round_trip_copy`");
+        createView(copiedDdl);
+
+        View copiedView = (View) 
db.getTableOrDdlException("view_comment_round_trip_copy");
+        Assert.assertEquals(originalView.getComment(), 
copiedView.getComment());
+    }
+
     @Test
     public void testNestedViews() throws Exception {
         ExceptionChecker.expectThrowsNoException(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to