Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions be/src/vec/common/string_utils/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ inline bool is_whitespace_ascii(char c) {
inline bool is_not_whitespace_ascii(char c) {
return !is_whitespace_ascii(c);
}

inline bool is_hex_ascii(char c) {
return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || is_numeric_ascii(c);
}
67 changes: 67 additions & 0 deletions be/src/vec/functions/uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
#include "common/status.h"
#include "vec/aggregate_functions/aggregate_function.h"
#include "vec/columns/column_string.h"
#include "vec/common/string_utils/string_utils.h"
#include "vec/core/block.h"
#include "vec/core/column_numbers.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type_string.h"
#include "vec/functions/function.h"
#include "vec/functions/function_totype.h"
#include "vec/functions/simple_function_factory.h"

namespace doris {
Expand Down Expand Up @@ -76,8 +78,73 @@ class Uuid : public IFunction {
}
};

struct NameIsUuid {
static constexpr auto name = "is_uuid";
};

struct IsUuidImpl {
using ReturnType = DataTypeBool;
using ReturnColumnType = ColumnUInt8;
static constexpr auto PrimitiveTypeImpl = PrimitiveType::TYPE_STRING;
static constexpr size_t uuid_without_dash_length = 32;
static constexpr size_t uuid_with_dash_length = 36;
static constexpr size_t uuid_with_braces_and_dash_length = 38;
static constexpr size_t dash_positions[4] = {8, 13, 18, 23};

static bool is_uuid_with_dash(const char* src, const char* end) {
size_t str_size = end - src;
for (int i = 0; i < str_size; ++i) {
if (!is_hex_ascii(src[i])) {
if (i == dash_positions[0] || i == dash_positions[1] || i == dash_positions[2] ||
i == dash_positions[3]) {
if (src[i] != '-') {
return false;
}
} else {
return false;
}
}
}
return true;
}

static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets,
PaddedPODArray<UInt8>& res) {
size_t rows_count = offsets.size();
res.resize(rows_count);
for (size_t i = 0; i < rows_count; ++i) {
const char* source = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
int str_size = offsets[i] - offsets[i - 1];
if (str_size == uuid_without_dash_length) {
bool is_valid = true;
for (int j = 0; j < str_size; ++j) {
if (!is_hex_ascii(source[j])) {
is_valid = false;
break;
}
}
res[i] = is_valid;
} else if (str_size == uuid_with_dash_length) {
res[i] = is_uuid_with_dash(source, source + str_size);
} else if (str_size == uuid_with_braces_and_dash_length) {
if (source[0] != '{' || source[str_size - 1] != '}') {
res[i] = 0;
continue;
}
res[i] = is_uuid_with_dash(source + 1, source + str_size - 1);
} else {
res[i] = 0;
}
}
return Status::OK();
}
};

using FunctionIsUuid = FunctionUnaryToType<IsUuidImpl, NameIsUuid>;

void register_function_uuid(SimpleFunctionFactory& factory) {
factory.register_function<Uuid>();
factory.register_function<FunctionIsUuid>();
}

} // namespace doris::vectorized
49 changes: 49 additions & 0 deletions be/test/vec/function/function_uuid_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

#include "function_test_util.h"
#include "vec/functions/uuid.cpp"

namespace doris::vectorized {

using namespace ut_type;

TEST(function_uuid_test, function_is_uuid_test) {
std::string func_name = "is_uuid";
InputTypeSet input_types = {PrimitiveType::TYPE_VARCHAR};
DataSet data_set = {
{{STRING("6ccd780c-baba-1026-9564-5b8c656024db")}, BOOLEAN(1)},
{{STRING("6ccd780c-baba-1026-9564-5b8c656024dbaaaa")}, BOOLEAN(0)},
{{STRING("6ccd780c-baba-1026-9564-5b8c656024gg")}, BOOLEAN(0)},
{{STRING("6ccd780-cbaba-1026-9564-5b8c656024db")}, BOOLEAN(0)},
{{STRING("6ccd780-cbaba-1026-95645-b8c656024db")}, BOOLEAN(0)},
{{STRING("6ccd780-cbaba-1026-95645-b8c65602")}, BOOLEAN(0)},
{{STRING("{6ccd780c-baba-1026-9564-5b8c656024db}")}, BOOLEAN(1)},
{{STRING("{6ccd780c-baba-1026-95645b8c656024db}")}, BOOLEAN(0)},
{{STRING("{6ccd780c-baba-1026-95645-b8c656024db}")}, BOOLEAN(0)},
{{STRING("6ccd780c-baba-1026-95645-b8c656024db}")}, BOOLEAN(0)},
{{STRING("6ccd780cbaba102695645b8c656024db")}, BOOLEAN(1)},
{{STRING("6ccd780cbaba102695645b8c656024dz")}, BOOLEAN(0)},
{{STRING("6ccd780cbaba102")}, BOOLEAN(0)},
{{STRING("{6ccd780cbaba102}")}, BOOLEAN(0)},
{{Null()}, Null()},
};

check_function_all_arg_comb<DataTypeBool, true>(func_name, input_types, data_set);
}

} // namespace doris::vectorized
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsIpv4String;
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsIpv6String;
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsNan;
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsUuid;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonArray;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonArrayIgnoreNull;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonContains;
Expand Down Expand Up @@ -782,6 +783,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(IsIpv6String.class, "is_ipv6_string", "is_ipv6"),
scalar(IsIpAddressInRange.class, "is_ip_address_in_range"),
scalar(IsNan.class, "isnan"),
scalar(IsUuid.class, "is_uuid"),
scalar(IsInf.class, "isinf"),
scalar(Ipv4CIDRToRange.class, "ipv4_cidr_to_range"),
scalar(Ipv6CIDRToRange.class, "ipv6_cidr_to_range"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,4 +1242,59 @@ private static Expression exportSetImpl(long bit, String on, String off, String

return new VarcharLiteral(result.toString());
}

/**
* Executable arithmetic functions is_uuid
*/
@ExecFunction(name = "is_uuid")
public static Expression isUuid(StringLikeLiteral first) {
String uuid = first.getValue();
return isUuidImpl(uuid);
}

private static boolean isHexChar(char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}

private static Expression isUuidImpl(String uuid) {
final int uuid_without_dash_length = 32;
final int uuid_with_dash_length = 36;
final int uuid_with_braces_and_dash_length = 38;
int len = uuid.length();
int start = 0;
int end = len - 1;
switch (len) {
case uuid_without_dash_length:
for (int i = 0; i < len; i++) {
if (!isHexChar(uuid.charAt(i))) {
return BooleanLiteral.of(false);
}
}
break;
case uuid_with_braces_and_dash_length:
if (uuid.charAt(0) != '{' || uuid.charAt(end) != '}') {
return BooleanLiteral.of(false);
}
start++;
end--;
// fall through
case uuid_with_dash_length:
for (int i = start; i <= end; i++) {
char c = uuid.charAt(i);
if (i == start + 8 || i == start + 13 || i == start + 18 || i == start + 23) {
if (c != '-') {
return BooleanLiteral.of(false);
}
} else {
if (!isHexChar(c)) {
return BooleanLiteral.of(false);
}
}
}
break;
default:
return BooleanLiteral.of(false);
}
return BooleanLiteral.of(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BooleanType;
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;

/**
* ScalarFunction 'is_uuid'.
*/
public class IsUuid extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {

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

/**
* constructor with 1 argument.
*/
public IsUuid(Expression arg) {
super("is_uuid", arg);
}

/**
* withChildren.
*/
@Override
public IsUuid withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new IsUuid(children.get(0));
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsIpv4String;
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsIpv6String;
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsNan;
import org.apache.doris.nereids.trees.expressions.functions.scalar.IsUuid;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonArray;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonArrayIgnoreNull;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonContains;
Expand Down Expand Up @@ -1485,6 +1486,10 @@ default R visitIsNan(IsNan isNan, C context) {
return visitScalarFunction(isNan, context);
}

default R visitIsUuid(IsUuid isUuid, C context) {
return visitScalarFunction(isUuid, context);
}

default R visitIsInf(IsInf isInf, C context) {
return visitScalarFunction(isInf, context);
}
Expand Down
58 changes: 58 additions & 0 deletions regression-test/data/nereids_function_p0/scalar_function/I.out
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,61 @@ Varchar13
1
1

-- !sql_is_uuid_Varchar --
false
false
false
false
false
false
false
false
false
false
false
false
false

-- !sql_is_uuid_Varchar_notnull --
false
false
false
false
false
false
false
false
false
false
false
false

-- !sql_is_uuid_String --
false
false
false
false
false
false
false
false
false
false
false
false
false

-- !sql_is_uuid_String_notnull --
false
false
false
false
false
false
false
false
false
false
false
false

Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ suite("nereids_scalar_fn_I") {
qt_sql_instr_Varchar_Varchar_notnull "select instr(kvchrs1, kvchrs1) from fn_test_not_nullable order by kvchrs1, kvchrs1"
qt_sql_instr_String_String "select instr(kstr, kstr) from fn_test order by kstr, kstr"
qt_sql_instr_String_String_notnull "select instr(kstr, kstr) from fn_test_not_nullable order by kstr, kstr"
qt_sql_is_uuid_Varchar "select is_uuid(kvchrs1) from fn_test order by kvchrs1"
qt_sql_is_uuid_Varchar_notnull "select is_uuid(kvchrs1) from fn_test_not_nullable order by kvchrs1"
qt_sql_is_uuid_String "select is_uuid(kstr) from fn_test order by kstr"
qt_sql_is_uuid_String_notnull "select is_uuid(kstr) from fn_test_not_nullable order by kstr"
}
Loading