Skip to content

Commit

Permalink
[feature](function) support ip functions named to_ipv4[or_default, or…
Browse files Browse the repository at this point in the history
…_null](string) and to_ipv6[or_default, or_null](string) (apache#29838)
  • Loading branch information
sjyango authored Jan 22, 2024
1 parent 01c227e commit acc0645
Show file tree
Hide file tree
Showing 13 changed files with 720 additions and 55 deletions.
26 changes: 14 additions & 12 deletions be/src/vec/functions/function_ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ namespace doris::vectorized {
void register_function_ip(SimpleFunctionFactory& factory) {
factory.register_function<FunctionIPv4NumToString>();
factory.register_alias(FunctionIPv4NumToString::name, "inet_ntoa");
factory.register_function<FunctionIPv4StringToNum<IPStringToNumExceptionMode::Throw>>();
factory.register_function<FunctionIPv4StringToNum<IPStringToNumExceptionMode::Default>>();
factory.register_function<FunctionIPv4StringToNum<IPStringToNumExceptionMode::Null>>();
factory.register_alias(FunctionIPv4StringToNum<IPStringToNumExceptionMode::Null>::name,
"inet_aton");

factory.register_function<FunctionIPv4StringToNum<IPExceptionMode::Throw>>();
factory.register_function<FunctionIPv4StringToNum<IPExceptionMode::Default>>();
factory.register_function<FunctionIPv4StringToNum<IPExceptionMode::Null>>();
factory.register_alias(FunctionIPv4StringToNum<IPExceptionMode::Null>::name, "inet_aton");
factory.register_function<FunctionIPv6NumToString>();
factory.register_alias(FunctionIPv6NumToString::name, "inet6_ntoa");
factory.register_function<FunctionIPv6StringToNum<IPStringToNumExceptionMode::Throw>>();
factory.register_function<FunctionIPv6StringToNum<IPStringToNumExceptionMode::Default>>();
factory.register_function<FunctionIPv6StringToNum<IPStringToNumExceptionMode::Null>>();
factory.register_alias(FunctionIPv6StringToNum<IPStringToNumExceptionMode::Null>::name,
"inet6_aton");

factory.register_function<FunctionIPv6StringToNum<IPExceptionMode::Throw>>();
factory.register_function<FunctionIPv6StringToNum<IPExceptionMode::Default>>();
factory.register_function<FunctionIPv6StringToNum<IPExceptionMode::Null>>();
factory.register_alias(FunctionIPv6StringToNum<IPExceptionMode::Null>::name, "inet6_aton");
factory.register_function<FunctionIsIPv4Compat>();
factory.register_function<FunctionIsIPv4Mapped>();
factory.register_function<FunctionIsIPString<IPv4>>();
factory.register_function<FunctionIsIPString<IPv6>>();
factory.register_function<FunctionIsIPAddressInRange>();
factory.register_function<FunctionIPv6CIDRToRange>();
factory.register_function<FunctionToIP<IPExceptionMode::Throw, IPv4>>();
factory.register_function<FunctionToIP<IPExceptionMode::Default, IPv4>>();
factory.register_function<FunctionToIP<IPExceptionMode::Null, IPv4>>();
factory.register_function<FunctionToIP<IPExceptionMode::Throw, IPv6>>();
factory.register_function<FunctionToIP<IPExceptionMode::Default, IPv6>>();
factory.register_function<FunctionToIP<IPExceptionMode::Null, IPv6>>();
}
} // namespace doris::vectorized
223 changes: 180 additions & 43 deletions be/src/vec/functions/function_ip.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToDate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToDateV2;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToDays;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToIpv4;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToIpv4OrDefault;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToIpv4OrNull;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToIpv6;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToIpv6OrDefault;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToIpv6OrNull;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToMonday;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToQuantileState;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Tokenize;
Expand Down Expand Up @@ -623,6 +629,12 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(IsIpv6String.class, "is_ipv6_string"),
scalar(IsIpAddressInRange.class, "is_ip_address_in_range"),
scalar(Ipv6CIDRToRange.class, "ipv6_cidr_to_range"),
scalar(ToIpv4.class, "to_ipv4"),
scalar(ToIpv4OrDefault.class, "to_ipv4_or_default"),
scalar(ToIpv4OrNull.class, "to_ipv4_or_null"),
scalar(ToIpv6.class, "to_ipv6"),
scalar(ToIpv6OrDefault.class, "to_ipv6_or_default"),
scalar(ToIpv6OrNull.class, "to_ipv6_or_null"),
scalar(JsonArray.class, "json_array"),
scalar(JsonObject.class, "json_object"),
scalar(JsonQuote.class, "json_quote"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IPv4Type;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* scalar function to_ipv4
*/
public class ToIpv4 extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNotNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IPv4Type.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(IPv4Type.INSTANCE).args(StringType.INSTANCE));

public ToIpv4(Expression arg0) {
super("to_ipv4", arg0);
}

@Override
public ToIpv4 withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1,
"to_ipv4 accept 1 args, but got %s (%s)",
children.size(),
children);
return new ToIpv4(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitToIpv4(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IPv4Type;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* scalar function to_ipv4_or_default
*/
public class ToIpv4OrDefault extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNotNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IPv4Type.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(IPv4Type.INSTANCE).args(StringType.INSTANCE));

public ToIpv4OrDefault(Expression arg0) {
super("to_ipv4_or_default", arg0);
}

@Override
public ToIpv4OrDefault withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1,
"to_ipv4_or_default accept 1 args, but got %s (%s)",
children.size(),
children);
return new ToIpv4OrDefault(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitToIpv4OrDefault(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IPv4Type;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* scalar function to_ipv4_or_null
*/
public class ToIpv4OrNull extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IPv4Type.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(IPv4Type.INSTANCE).args(StringType.INSTANCE));

public ToIpv4OrNull(Expression arg0) {
super("to_ipv4_or_null", arg0);
}

@Override
public ToIpv4OrNull withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1,
"to_ipv4_or_null accept 1 args, but got %s (%s)",
children.size(),
children);
return new ToIpv4OrNull(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitToIpv4OrNull(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IPv6Type;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* scalar function to_ipv6
*/
public class ToIpv6 extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNotNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IPv6Type.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(IPv6Type.INSTANCE).args(StringType.INSTANCE));

public ToIpv6(Expression arg0) {
super("to_ipv6", arg0);
}

@Override
public ToIpv6 withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1,
"to_ipv6 accept 1 args, but got %s (%s)",
children.size(),
children);
return new ToIpv6(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitToIpv6(this, context);
}
}
Loading

0 comments on commit acc0645

Please sign in to comment.