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

morrysnow pushed a commit to branch branch-1.1-lts
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-1.1-lts by this push:
     new 1fe5b846f6 [Fix](planner)fix cast error when query a view with cast to 
string-like type. (#17131)
1fe5b846f6 is described below

commit 1fe5b846f671b9c8f646b278ef6eccbb38f5afe9
Author: mch_ucchi <41606806+sohardforan...@users.noreply.github.com>
AuthorDate: Mon Mar 6 16:27:30 2023 +0800

    [Fix](planner)fix cast error when query a view with cast to string-like 
type. (#17131)
    
    fix bug:
    
    create table t (
    id int,
    k1 int
    )
    distributed by hash(id) buckets 4
    properties(...)
    
    create view v1 as select cast(k1 as varchar) k1 from t1
    select k1 from v1
    
    which will cause 'Failed analysis when expr subsitution' because 
inlineViewDef always change string-like type to char.
---
 fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java   | 7 ++++++-
 .../src/test/java/org/apache/doris/catalog/CreateFunctionTest.java | 4 ++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
index 261c1cf89a..4cf590eb63 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
@@ -194,7 +194,12 @@ public class CastExpr extends Expr {
         }
         if (isAnalyzed) {
             if (type.isStringType()) {
-                return "CAST(" + getChild(0).toSql() + " AS " + "CHARACTER" + 
")";
+                String typeString = type.toSql();
+                ScalarType scalarType = ((ScalarType) type);
+                if (scalarType.isWildcardChar() || 
scalarType.isWildcardVarchar()) {
+                    typeString = typeString.replace("(-1)", "");
+                }
+                return "CAST(" + getChild(0).toSql() + " AS " + typeString + 
")";
             } else {
                 return "CAST(" + getChild(0).toSql() + " AS " + 
type.toString() + ")";
             }
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateFunctionTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateFunctionTest.java
index 7da7f92b8e..1e7c2fa9b7 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateFunctionTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateFunctionTest.java
@@ -197,7 +197,7 @@ public class CreateFunctionTest {
         Assert.assertTrue(constExprLists.get(0).get(0) instanceof 
StringLiteral);
 
         queryStr = "select db1.varchar(k1, 4) from db1.tbl1;";
-        
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1`
 AS CHARACTER)"));
+        
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1`
 AS varchar(4))"));
 
         // cast any type to char with fixed length
         createFuncStr = "create alias function db1.char(all, int) with 
parameter(text, length) as " +
@@ -224,6 +224,6 @@ public class CreateFunctionTest {
         Assert.assertTrue(constExprLists.get(0).get(0) instanceof 
StringLiteral);
 
         queryStr = "select db1.char(k1, 4) from db1.tbl1;";
-        
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1`
 AS CHARACTER)"));
+        
Assert.assertTrue(dorisAssert.query(queryStr).explainQuery().contains("CAST(`k1`
 AS char(4))"));
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to