From 9d1d66dabfa62c858714dfcb855d0a6e06e7a3a5 Mon Sep 17 00:00:00 2001 From: amorynan Date: Mon, 9 Dec 2024 16:15:18 +0800 Subject: [PATCH 1/4] fix default value for ip --- .../src/main/java/org/apache/doris/analysis/ColumnDef.java | 6 ++++++ .../main/java/org/apache/doris/analysis/IPv4Literal.java | 2 ++ .../main/java/org/apache/doris/analysis/IPv6Literal.java | 2 +- regression-test/data/datatype_p0/ip/test_ip_basic.out | 4 ++++ regression-test/suites/datatype_p0/ip/test_ip_basic.groovy | 7 +++++++ 5 files changed, 20 insertions(+), 1 deletion(-) 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 93bcb92f58062a..5cf4a3c6940c6a 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 @@ -651,6 +651,12 @@ public static void validateDefaultValue(Type type, String defaultValue, DefaultV 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 6fa84d18b25f41..871062b04b296c 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 @@ -89,6 +89,7 @@ private static long parseIPv4toLong(String ipv4) { private static String parseLongToIPv4(long ipv4) { StringBuilder sb = new StringBuilder(); + sb.append("\""); for (int i = 3; i >= 0; i--) { short octet = (short) ((ipv4 >> (i * 8)) & 0xFF); sb.append(octet); @@ -96,6 +97,7 @@ private static String parseLongToIPv4(long ipv4) { sb.append("."); } } + sb.append("\""); return sb.toString(); } 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 a3ec7a3a349824..d8257d6175047f 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 @@ -101,7 +101,7 @@ public int compareLiteral(LiteralExpr expr) { @Override public String getStringValue() { - return this.value; + return "\"" + this.value + "\""; } @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 b69f9708a1b1fc..9f70d29305f41e 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,7 @@ 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 + 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 468b6f6f146e14..72d10467560d4a 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,11 @@ 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"""; } From 3bdfc39554235b13018f60f201777bee0c4ceeb3 Mon Sep 17 00:00:00 2001 From: amorynan Date: Wed, 11 Dec 2024 19:05:01 +0800 Subject: [PATCH 2/4] add more cases --- .../data/datatype_p0/ip/test_ip_basic.out | 30 +++++++++++++++++++ .../datatype_p0/ip/test_ip_basic.groovy | 18 ++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) 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 9f70d29305f41e..785bfc36a74129 100644 --- a/regression-test/data/datatype_p0/ip/test_ip_basic.out +++ b/regression-test/data/datatype_p0/ip/test_ip_basic.out @@ -377,3 +377,33 @@ ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 4 1 5be8:dde9:7f0b:d5a7:bd01:b3be:9c69:573b 0.0.0.1 2 :: 127.0.0.1 +-- !sql -- +table_ip_default CREATE TABLE `table_ip_default` (\n `col0` bigint NOT NULL,\n `col4` ipv6 NULL DEFAULT "::",\n `col24` ipv4 NULL DEFAULT "127.0.0.1"\n) ENGINE=OLAP\nUNIQUE KEY(`col0`)\nDISTRIBUTED BY HASH(`col0`) BUCKETS 4\nPROPERTIES (\n"replication_allocation" = "tag.location.default: 1",\n"min_load_replica_num" = "-1",\n"is_being_synced" = "false",\n"storage_medium" = "hdd",\n"storage_format" = "V2",\n"inverted_index_storage_format" = "V2",\n"enable_unique_key_merge_on_write" = "true",\n"light_schema_change" = "true",\n"disable_auto_compaction" = "false",\n"binlog.enable" = "false",\n"binlog.ttl_seconds" = "86400",\n"binlog.max_bytes" = "9223372036854775807",\n"binlog.max_history_nums" = "9223372036854775807",\n"enable_single_replica_compaction" = "false",\n"group_commit_interval_ms" = "10000",\n"group_commit_data_bytes" = "134217728",\n"enable_mow_light_delete" = "false"\n); + +-- !sql -- +col0 bigint No true \N +col4 ipv6 Yes false :: NONE +col24 ipv4 Yes false 127.0.0.1 NONE + +-- !sql -- +col0 bigint No true \N +col4 ipv6 Yes false :: NONE +col24 ipv4 Yes false 127.0.0.1 NONE + +-- !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 72d10467560d4a..d6ab3724d697e4 100644 --- a/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy +++ b/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy @@ -166,5 +166,21 @@ suite("test_ip_basic") { 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"""; + 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 + qt_sql """ show create table table_ip_default """ + qt_sql """ desc table_ip_default """ + 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""" + 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 """ } From 370c175bbe3f6dbfec82f1f869409d51454cd3cf Mon Sep 17 00:00:00 2001 From: amorynan Date: Thu, 12 Dec 2024 11:00:32 +0800 Subject: [PATCH 3/4] fix case --- .../data/datatype_p0/ip/test_ip_basic.out | 15 ++++++--------- .../suites/datatype_p0/ip/test_ip_basic.groovy | 9 ++++++--- 2 files changed, 12 insertions(+), 12 deletions(-) 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 785bfc36a74129..e1b85abe00c41b 100644 --- a/regression-test/data/datatype_p0/ip/test_ip_basic.out +++ b/regression-test/data/datatype_p0/ip/test_ip_basic.out @@ -378,17 +378,14 @@ ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 4 2 :: 127.0.0.1 -- !sql -- -table_ip_default CREATE TABLE `table_ip_default` (\n `col0` bigint NOT NULL,\n `col4` ipv6 NULL DEFAULT "::",\n `col24` ipv4 NULL DEFAULT "127.0.0.1"\n) ENGINE=OLAP\nUNIQUE KEY(`col0`)\nDISTRIBUTED BY HASH(`col0`) BUCKETS 4\nPROPERTIES (\n"replication_allocation" = "tag.location.default: 1",\n"min_load_replica_num" = "-1",\n"is_being_synced" = "false",\n"storage_medium" = "hdd",\n"storage_format" = "V2",\n"inverted_index_storage_format" = "V2",\n"enable_unique_key_merge_on_write" = "true",\n"light_schema_change" = "true",\n"disable_auto_compaction" = "false",\n"binlog.enable" = "false",\n"binlog.ttl_seconds" = "86400",\n"binlog.max_bytes" = "9223372036854775807",\n"binlog.max_history_nums" = "9223372036854775807",\n"enable_single_replica_compaction" = "false",\n"group_commit_interval_ms" = "10000",\n"group_commit_data_bytes" = "134217728",\n"enable_mow_light_delete" = "false"\n); +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 -- -col0 bigint No true \N -col4 ipv6 Yes false :: NONE -col24 ipv4 Yes false 127.0.0.1 NONE - --- !sql -- -col0 bigint No true \N -col4 ipv6 Yes false :: NONE -col24 ipv4 Yes false 127.0.0.1 NONE +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 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 d6ab3724d697e4..5a4ab2ca94aca9 100644 --- a/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy +++ b/regression-test/suites/datatype_p0/ip/test_ip_basic.groovy @@ -173,11 +173,14 @@ suite("test_ip_basic") { // create table like // insert into table // alter new ip column with default value - qt_sql """ show create table table_ip_default """ - qt_sql """ desc table_ip_default """ + 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""" + 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 "::" """ From 0c83421e3735e8099f619a4c05a7677cd9c095e0 Mon Sep 17 00:00:00 2001 From: amorynan Date: Thu, 12 Dec 2024 18:27:29 +0800 Subject: [PATCH 4/4] fixed --- .../src/main/java/org/apache/doris/analysis/IPv4Literal.java | 4 +--- .../src/main/java/org/apache/doris/analysis/IPv6Literal.java | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) 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 871062b04b296c..92063e6b9b30c3 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 @@ -89,7 +89,6 @@ private static long parseIPv4toLong(String ipv4) { private static String parseLongToIPv4(long ipv4) { StringBuilder sb = new StringBuilder(); - sb.append("\""); for (int i = 3; i >= 0; i--) { short octet = (short) ((ipv4 >> (i * 8)) & 0xFF); sb.append(octet); @@ -97,7 +96,6 @@ private static String parseLongToIPv4(long ipv4) { sb.append("."); } } - sb.append("\""); return sb.toString(); } @@ -117,7 +115,7 @@ public Expr clone() { @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 d8257d6175047f..bb986e0ffe7a2c 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 @@ private void checkValueValid(String ipv6) throws AnalysisException { @Override protected String toSqlImpl() { - return getStringValue(); + return "\"" + getStringValue() + "\""; } @Override @@ -101,7 +101,7 @@ public int compareLiteral(LiteralExpr expr) { @Override public String getStringValue() { - return "\"" + this.value + "\""; + return this.value; } @Override