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

eldenmoon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 6cead2fff51 [fix](ip)fix default value for ip (#45194)
6cead2fff51 is described below

commit 6cead2fff51583bc0b567c93e813f33a3185976b
Author: amory <wangqian...@selectdb.com>
AuthorDate: Fri Dec 13 11:48:01 2024 +0800

    [fix](ip)fix default value for ip (#45194)
    
    before this pr: we do not support create table for IP type with default
    value
    like this:
    ```
    mysql> CREATE TABLE table2
        -> (
        ->     col0 BIGINT  NOT NULL DEFAULT '1' ,
        ->     col24 IPV4  NULL DEFAULT '127.0.0.1'
        -> )
        -> DUPLICATE KEY(col0)
        -> DISTRIBUTED BY HASH(col0) BUCKETS 4
        -> PROPERTIES (
        ->     "replication_num" = "1"
        -> );
    ERROR 1105 (HY000): errCode = 2, detailMessage = Unsupported type: ipv4
    ```
    after this pr we deal with this problem
---
 .../java/org/apache/doris/analysis/ColumnDef.java  |  6 +++++
 .../org/apache/doris/analysis/IPv4Literal.java     |  2 +-
 .../org/apache/doris/analysis/IPv6Literal.java     |  2 +-
 .../data/datatype_p0/ip/test_ip_basic.out          | 31 ++++++++++++++++++++++
 .../suites/datatype_p0/ip/test_ip_basic.groovy     | 26 ++++++++++++++++++
 5 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
index c2c55c4ce7c..02e123397c6 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
@@ -652,6 +652,12 @@ public class ColumnDef {
             case BOOLEAN:
                 new BoolLiteral(defaultValue);
                 break;
+            case IPV4:
+                new IPv4Literal(defaultValue);
+                break;
+            case IPV6:
+                new IPv6Literal(defaultValue);
+                break;
             default:
                 throw new AnalysisException("Unsupported type: " + type);
         }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv4Literal.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv4Literal.java
index 6fa84d18b25..92063e6b9b3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv4Literal.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv4Literal.java
@@ -115,7 +115,7 @@ public class IPv4Literal extends LiteralExpr {
 
     @Override
     protected String toSqlImpl() {
-        return getStringValue();
+        return "\"" + getStringValue() + "\"";
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv6Literal.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv6Literal.java
index a3ec7a3a349..bb986e0ffe7 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv6Literal.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/IPv6Literal.java
@@ -75,7 +75,7 @@ public class IPv6Literal extends LiteralExpr {
 
     @Override
     protected String toSqlImpl() {
-        return getStringValue();
+        return "\"" + getStringValue() + "\"";
     }
 
     @Override
diff --git a/regression-test/data/datatype_p0/ip/test_ip_basic.out 
b/regression-test/data/datatype_p0/ip/test_ip_basic.out
index b69f9708a1b..e1b85abe00c 100644
--- a/regression-test/data/datatype_p0/ip/test_ip_basic.out
+++ b/regression-test/data/datatype_p0/ip/test_ip_basic.out
@@ -373,3 +373,34 @@ ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff    4
 -- !sql --
 1      false   127.0.0.1       ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
 
+-- !sql --
+1      5be8:dde9:7f0b:d5a7:bd01:b3be:9c69:573b 0.0.0.1
+2      ::      127.0.0.1
+
+-- !sql --
+table_ip_default       UNIQUE_KEYS     col0    bigint  bigint  No      true    
\N              true            
+               col4    ipv6    ipv6    Yes     false   ::      NONE    true    
        
+               col24   ipv4    ipv4    Yes     false   127.0.0.1       NONE    
true            
+
+-- !sql --
+table_ip_default_like  UNIQUE_KEYS     col0    bigint  bigint  No      true    
\N              true            
+               col4    ipv6    ipv6    Yes     false   ::      NONE    true    
        
+               col24   ipv4    ipv4    Yes     false   127.0.0.1       NONE    
true            
+
+-- !sql --
+2
+
+-- !sql --
+1      5be8:dde9:7f0b:d5a7:bd01:b3be:9c69:573b 0.0.0.1
+2      ::      127.0.0.1
+
+-- !sql --
+0
+
+-- !sql --
+0
+
+-- !sql --
+1      5be8:dde9:7f0b:d5a7:bd01:b3be:9c69:573b 0.0.0.1 ::      127.0.0.1
+2      ::      127.0.0.1       ::      127.0.0.1
+
diff --git a/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy 
b/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy
index 468b6f6f146..5a4ab2ca94a 100644
--- a/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy
+++ b/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy
@@ -160,4 +160,30 @@ suite("test_ip_basic") {
     qt_sql """ select * from table_ip where col0 = 1"""
     sql """ Update table_ip set col25 = 
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' where col0 = 1 """
     qt_sql """ select * from table_ip where col0 = 1"""
+
+    // test ip with default value
+    sql """ DROP TABLE IF EXISTS table_ip_default """
+    sql """ CREATE TABLE IF NOT EXISTS `table_ip_default` (`col0` bigint NOT 
NULL, `col4` ipv6 NULL DEFAULT "::",   `col24` ipv4 NULL DEFAULT "127.0.0.1") 
ENGINE=OLAP UNIQUE KEY(`col0`) DISTRIBUTED BY HASH(`col0`) BUCKETS 4 PROPERTIES 
("replication_allocation" = "tag.location.default: 1") """
+    sql """ insert into table_ip_default values (1, 
"5be8:dde9:7f0b:d5a7:bd01:b3be:9c69:573b", "0.0.0.1") """
+    sql """ insert into table_ip_default(col0) values (2); """
+    qt_sql """ select * from table_ip_default order by col0"""
+    // add cases for default value to make sure in all cases, the default 
value is not lost.
+    // show create table
+    // desc table
+    // create table like
+    // insert into table
+    // alter new ip column with default value
+    def result = sql """ show create table table_ip_default """
+    log.info("show result : ${result}")
+    assertTrue(result.toString().containsIgnoreCase("`col4` ipv6 NULL DEFAULT 
\"::\""))
+    assertTrue(result.toString().containsIgnoreCase("`col24` ipv4 NULL DEFAULT 
\"127.0.0.1\""))
+    qt_sql """ desc table_ip_default all"""
+    sql """ DROP TABLE IF EXISTS table_ip_default_like """
+    sql """ create table table_ip_default_like like table_ip_default """
+    qt_sql """ desc table_ip_default_like all"""
+    qt_sql """ insert into table_ip_default_like select * from 
table_ip_default """
+    qt_sql """ select * from table_ip_default_like order by col0 """
+    qt_sql """ alter table table_ip_default_like add column col25 ipv6 NULL 
DEFAULT "::" """
+    qt_sql """ alter table table_ip_default_like add column col26 ipv4 NULL 
DEFAULT "127.0.0.1" """
+    qt_sql """ select * from table_ip_default_like order by col0 """
 }


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

Reply via email to