[feature](Nereids) support digital_masking function (#15252)

This commit is contained in:
jakevin
2022-12-23 18:59:08 +08:00
committed by GitHub
parent 2f089be37e
commit bfaaa2bd7c
7 changed files with 131 additions and 4 deletions

View File

@ -81,6 +81,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.Dceil;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Degrees;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dexp;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dfloor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DigitalMasking;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dlog1;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dlog10;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dpow;
@ -267,9 +268,9 @@ import java.util.List;
/**
* Builtin scalar functions.
*
* <p>
* Note: Please ensure that this class only has some lists and no procedural code.
* It helps to be clear and concise.
* It helps to be clear and concise.
*/
public class BuiltinScalarFunctions implements FunctionHelper {
public final List<ScalarFunc> scalarFunctions = ImmutableList.of(
@ -337,6 +338,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(Degrees.class, "degrees"),
scalar(Dexp.class, "dexp"),
scalar(Dfloor.class, "dfloor"),
scalar(DigitalMasking.class, "digital_masking"),
scalar(Dlog1.class, "dlog1"),
scalar(Dlog10.class, "dlog10"),
scalar(Dpow.class, "dpow"),
@ -521,5 +523,6 @@ public class BuiltinScalarFunctions implements FunctionHelper {
public static final BuiltinScalarFunctions INSTANCE = new BuiltinScalarFunctions();
// Note: Do not add any code here!
private BuiltinScalarFunctions() {}
private BuiltinScalarFunctions() {
}
}

View File

@ -19,6 +19,7 @@ package org.apache.doris.nereids.rules.expression.rewrite;
import org.apache.doris.nereids.rules.expression.rewrite.rules.BetweenToCompoundRule;
import org.apache.doris.nereids.rules.expression.rewrite.rules.CharacterLiteralTypeCoercion;
import org.apache.doris.nereids.rules.expression.rewrite.rules.DigitalMaskingConvert;
import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
import org.apache.doris.nereids.rules.expression.rewrite.rules.InPredicateToEqualToRule;
import org.apache.doris.nereids.rules.expression.rewrite.rules.NormalizeBinaryPredicatesRule;
@ -44,7 +45,8 @@ public class ExpressionNormalization extends ExpressionRewrite {
CharacterLiteralTypeCoercion.INSTANCE,
TypeCoercion.INSTANCE,
FoldConstantRule.INSTANCE,
SimplifyCastRule.INSTANCE
SimplifyCastRule.INSTANCE,
DigitalMaskingConvert.INSTANCE
);
public ExpressionNormalization(ConnectContext context) {

View File

@ -0,0 +1,41 @@
// 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.rules.expression.rewrite.rules;
import org.apache.doris.nereids.rules.expression.rewrite.AbstractExpressionRewriteRule;
import org.apache.doris.nereids.rules.expression.rewrite.ExpressionRewriteContext;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Concat;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DigitalMasking;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Left;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Right;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
/**
* Convert DigitalMasking to Concat
*/
public class DigitalMaskingConvert extends AbstractExpressionRewriteRule {
public static DigitalMaskingConvert INSTANCE = new DigitalMaskingConvert();
@Override
public Expression visitDigitalMasking(DigitalMasking digitalMasking, ExpressionRewriteContext context) {
return new Concat(new Left(digitalMasking.child(), Literal.of(3)), Literal.of("****"),
new Right(digitalMasking.child(), Literal.of(4)));
}
}

View File

@ -0,0 +1,69 @@
// 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.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
import org.apache.doris.nereids.types.VarcharType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ScalarFunction 'digital_masking'.
*/
public class DigitalMasking extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT).args(BigIntType.INSTANCE)
);
/**
* constructor with 1 argument.
*/
public DigitalMasking(Expression arg) {
super("digital_masking", arg);
}
/**
* withChildren.
*/
@Override
public DigitalMasking withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new DigitalMasking(children.get(0));
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitDigitalMasking(this, context);
}
}

View File

@ -83,6 +83,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.Dceil;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Degrees;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dexp;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dfloor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DigitalMasking;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dlog1;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dlog10;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Dpow;
@ -525,6 +526,10 @@ public interface ScalarFunctionVisitor<R, C> {
return visitScalarFunction(daysSub, context);
}
default R visitDigitalMasking(DigitalMasking digitalMasking, C context) {
return visitScalarFunction(digitalMasking, context);
}
default R visitYearsSub(YearsSub yearsSub, C context) {
return visitScalarFunction(yearsSub, context);
}

View File

@ -55,3 +55,6 @@ Meimei Han Xxxxxx Xxx Meimei Xxx Xxxxxx Xxx
19943216789 nnnnnnnnnnn 19943216nnn nnnnnnnnnnn
13556780000 nnnnnnnnnnn 13556780nnn nnnnnnnnnnn
-- !select_digital_masking --
138****5678

View File

@ -71,4 +71,8 @@ suite("test_mask_function") {
qt_select_mask_last_n_nullable """
select phone, mask_last_n(phone), mask_last_n(phone, 3), mask_last_n(phone, 100) from table_mask_test order by id;
"""
qt_select_digital_masking """
select digital_masking(13812345678);
"""
}