Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](ip)fix default value for ip #45194

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ 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);
if (i > 0) {
sb.append(".");
}
}
sb.append("\"");
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public int compareLiteral(LiteralExpr expr) {

@Override
public String getStringValue() {
return this.value;
return "\"" + this.value + "\"";
}

@Override
Expand Down
31 changes: 31 additions & 0 deletions regression-test/data/datatype_p0/ip/test_ip_basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -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

26 changes: 26 additions & 0 deletions regression-test/suites/datatype_p0/ip/test_ip_basic.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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") """
amorynan marked this conversation as resolved.
Show resolved Hide resolved
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 """
}
Loading