Skip to content

Commit

Permalink
Support convert ip type to double for column stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jibing-Li committed Mar 7, 2025
1 parent ca850fa commit 6494bf6
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public Long getValue() {
return value;
}

@Override
public double getDouble() {
return (double) value;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitIPv4Literal(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public IPv6Address getValue() {
return value;
}

@Override
public double getDouble() {
return value.toBigInteger().doubleValue();
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitIPv6Literal(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import org.apache.doris.datasource.hive.HMSExternalTable;
import org.apache.doris.datasource.hive.HMSExternalTable.DLAType;
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.IPv4Literal;
import org.apache.doris.nereids.trees.expressions.literal.IPv6Literal;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.qe.AuditLogHelper;
import org.apache.doris.qe.AutoCloseConnectContext;
Expand Down Expand Up @@ -292,6 +294,10 @@ public static LiteralExpr readableValue(Type type, String columnValue) throws An
case VARCHAR:
case STRING:
return new StringLiteral(columnValue);
case IPV4:
return new org.apache.doris.analysis.IPv4Literal(columnValue);
case IPV6:
return new org.apache.doris.analysis.IPv6Literal(columnValue);
case HLL:
case BITMAP:
case ARRAY:
Expand Down Expand Up @@ -342,6 +348,12 @@ public static double convertToDouble(Type type, String columnValue) throws Analy
case STRING:
VarcharLiteral varchar = new VarcharLiteral(columnValue);
return varchar.getDouble();
case IPV4:
IPv4Literal ipv4 = new IPv4Literal(columnValue);
return ipv4.getDouble();
case IPV6:
IPv6Literal ipv6 = new IPv6Literal(columnValue);
return ipv6.getDouble();
case HLL:
case BITMAP:
case ARRAY:
Expand Down
97 changes: 97 additions & 0 deletions regression-test/suites/statistics/test_analyze_ip_type.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

suite("test_analyze_ip_type") {

sql """drop database if exists test_analyze_ip_type"""
sql """create database test_analyze_ip_type"""
sql """use test_analyze_ip_type"""
sql """set global force_sample_analyze=false"""
sql """set global enable_auto_analyze=false"""
def tableName = "iptest"
sql """drop table if exists ${tableName}"""
sql """
CREATE TABLE ${tableName} (
`id` bigint,
`ip_v4` ipv4,
`ip_v6` ipv6
) ENGINE=OLAP
DISTRIBUTED BY HASH(`id`) BUCKETS 4
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
"""
sql """insert into ${tableName} values(-1, NULL, NULL)"""
sql """insert into ${tableName} values(0, 0, '::')"""
sql """insert into ${tableName} values(1, 1, '::1')"""
sql """insert into ${tableName} values(2130706433, 2130706433, '2001:1b70:a1:610::b102:2')"""
sql """insert into ${tableName} values(4294967295, 4294967295, 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')"""
sql """analyze table ${tableName} with sync"""

def result = sql """show column stats ${tableName}"""
assertEquals(3, result.size())
result = sql """show column cached stats ${tableName}"""
assertEquals(3, result.size())

result = sql """show column stats ${tableName} (ip_v6);"""
assertEquals("ip_v6", result[0][0])
assertEquals("iptest", result[0][1])
assertEquals("5.0", result[0][2])
assertEquals("4.0", result[0][3])
assertEquals("1.0", result[0][4])
assertEquals("80.0", result[0][5])
assertEquals("16.0", result[0][6])
assertEquals("\"::\"", result[0][7])
assertEquals("\"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"", result[0][8])

result = sql """show column cached stats ${tableName} (ip_v6);"""
assertEquals("ip_v6", result[0][0])
assertEquals("iptest", result[0][1])
assertEquals("5.0", result[0][2])
assertEquals("4.0", result[0][3])
assertEquals("1.0", result[0][4])
assertEquals("80.0", result[0][5])
assertEquals("16.0", result[0][6])
assertEquals("\"::\"", result[0][7])
assertEquals("\"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"", result[0][8])

result = sql """show column stats ${tableName} (ip_v4);"""
assertEquals("ip_v4", result[0][0])
assertEquals("iptest", result[0][1])
assertEquals("5.0", result[0][2])
assertEquals("4.0", result[0][3])
assertEquals("1.0", result[0][4])
assertEquals("20.0", result[0][5])
assertEquals("4.0", result[0][6])
assertEquals("\"0.0.0.0\"", result[0][7])
assertEquals("\"255.255.255.255\"", result[0][8])

result = sql """show column cached stats ${tableName} (ip_v4);"""
assertEquals("ip_v4", result[0][0])
assertEquals("iptest", result[0][1])
assertEquals("5.0", result[0][2])
assertEquals("4.0", result[0][3])
assertEquals("1.0", result[0][4])
assertEquals("20.0", result[0][5])
assertEquals("4.0", result[0][6])
assertEquals("\"0.0.0.0\"", result[0][7])
assertEquals("\"255.255.255.255\"", result[0][8])

sql """drop database if exists test_analyze_ip_type"""
}


0 comments on commit 6494bf6

Please sign in to comment.