Coverage Report

Created: 2026-03-24 20:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/udf/python/python_udf_meta.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "udf/python/python_udf_meta.h"
19
20
#include <arrow/util/base64.h>
21
#include <fmt/core.h>
22
#include <rapidjson/stringbuffer.h>
23
#include <rapidjson/writer.h>
24
25
#include <sstream>
26
27
#include "common/status.h"
28
#include "format/arrow/arrow_utils.h"
29
#include "util/string_util.h"
30
31
namespace doris {
32
33
Status PythonUDFMeta::convert_types_to_schema(const DataTypes& types, const std::string& timezone,
34
24
                                              std::shared_ptr<arrow::Schema>* schema) {
35
24
    assert(!types.empty());
36
24
    arrow::SchemaBuilder builder;
37
54
    for (size_t i = 0; i < types.size(); ++i) {
38
30
        std::shared_ptr<arrow::DataType> arrow_type;
39
30
        RETURN_IF_ERROR(convert_to_arrow_type(types[i], &arrow_type, timezone));
40
30
        std::shared_ptr<arrow::Field> field = std::make_shared<arrow::Field>(
41
30
                "arg" + std::to_string(i), arrow_type, types[i]->is_nullable());
42
30
        RETURN_DORIS_STATUS_IF_ERROR(builder.AddField(field));
43
30
    }
44
24
    RETURN_DORIS_STATUS_IF_RESULT_ERROR(schema, builder.Finish());
45
24
    return Status::OK();
46
24
}
47
48
Status PythonUDFMeta::serialize_arrow_schema(const std::shared_ptr<arrow::Schema>& schema,
49
22
                                             std::shared_ptr<arrow::Buffer>* out) {
50
22
    RETURN_DORIS_STATUS_IF_RESULT_ERROR(
51
22
            out, arrow::ipc::SerializeSchema(*schema, arrow::default_memory_pool()));
52
22
    return Status::OK();
53
22
}
54
55
/*
56
    json format:
57
    {
58
        "name": "xxx",
59
        "symbol": "xxx",
60
        "location": "xxx",
61
        "udf_load_type": 0 or 1,
62
        "client_type": 0 (UDF) or 1 (UDAF) or 2 (UDTF),
63
        "runtime_version": "x.xx.xx",
64
        "always_nullable": true,
65
        "inline_code": "base64_inline_code",
66
        "input_types": "base64_input_types",
67
        "return_type": "base64_return_type"
68
    }
69
*/
70
10
Status PythonUDFMeta::serialize_to_json(std::string* json_str) const {
71
10
    rapidjson::Document doc;
72
10
    doc.SetObject();
73
10
    auto& allocator = doc.GetAllocator();
74
10
    doc.AddMember("name", rapidjson::Value().SetString(name.c_str(), allocator), allocator);
75
10
    doc.AddMember("symbol", rapidjson::Value().SetString(symbol.c_str(), allocator), allocator);
76
10
    doc.AddMember("location", rapidjson::Value().SetString(location.c_str(), allocator), allocator);
77
10
    doc.AddMember("udf_load_type", rapidjson::Value().SetInt(static_cast<int>(type)), allocator);
78
10
    doc.AddMember("client_type", rapidjson::Value().SetInt(static_cast<int>(client_type)),
79
10
                  allocator);
80
10
    doc.AddMember("runtime_version",
81
10
                  rapidjson::Value().SetString(runtime_version.c_str(), allocator), allocator);
82
10
    doc.AddMember("always_nullable", rapidjson::Value().SetBool(always_nullable), allocator);
83
84
10
    {
85
        // Serialize base64 inline code to json
86
10
        std::string base64_str = arrow::util::base64_encode(inline_code);
87
10
        doc.AddMember("inline_code", rapidjson::Value().SetString(base64_str.c_str(), allocator),
88
10
                      allocator);
89
10
    }
90
10
    {
91
        // Serialize base64 input types to json
92
10
        std::shared_ptr<arrow::Schema> input_schema;
93
10
        RETURN_IF_ERROR(convert_types_to_schema(input_types, TimezoneUtils::default_time_zone,
94
10
                                                &input_schema));
95
10
        std::shared_ptr<arrow::Buffer> input_schema_buffer;
96
10
        RETURN_IF_ERROR(serialize_arrow_schema(input_schema, &input_schema_buffer));
97
10
        std::string base64_str =
98
10
                arrow::util::base64_encode({input_schema_buffer->data_as<char>(),
99
10
                                            static_cast<size_t>(input_schema_buffer->size())});
100
10
        doc.AddMember("input_types", rapidjson::Value().SetString(base64_str.c_str(), allocator),
101
10
                      allocator);
102
10
    }
103
0
    {
104
        // Serialize base64 return type to json
105
10
        std::shared_ptr<arrow::Schema> return_schema;
106
10
        RETURN_IF_ERROR(convert_types_to_schema({return_type}, TimezoneUtils::default_time_zone,
107
10
                                                &return_schema));
108
10
        std::shared_ptr<arrow::Buffer> return_schema_buffer;
109
10
        RETURN_IF_ERROR(serialize_arrow_schema(return_schema, &return_schema_buffer));
110
10
        std::string base64_str =
111
10
                arrow::util::base64_encode({return_schema_buffer->data_as<char>(),
112
10
                                            static_cast<size_t>(return_schema_buffer->size())});
113
10
        doc.AddMember("return_type", rapidjson::Value().SetString(base64_str.c_str(), allocator),
114
10
                      allocator);
115
10
    }
116
117
    // Convert document to json string
118
0
    rapidjson::StringBuffer buffer;
119
10
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
120
10
    doc.Accept(writer);
121
10
    *json_str = std::string(buffer.GetString(), buffer.GetSize());
122
10
    return Status::OK();
123
10
}
124
125
4
std::string PythonUDFMeta::to_string() const {
126
4
    std::stringstream input_types_ss;
127
4
    input_types_ss << "<";
128
14
    for (size_t i = 0; i < input_types.size(); ++i) {
129
10
        input_types_ss << input_types[i]->get_name();
130
10
        if (i != input_types.size() - 1) {
131
6
            input_types_ss << ", ";
132
6
        }
133
10
    }
134
4
    input_types_ss << ">";
135
4
    return fmt::format(
136
4
            "[name: {}, symbol: {}, location: {}, runtime_version: {}, always_nullable: {}, "
137
4
            "inline_code: {}][input_types: {}][return_type: {}]",
138
4
            name, symbol, location, runtime_version, always_nullable, inline_code,
139
4
            input_types_ss.str(), return_type->get_name());
140
4
}
141
142
22
Status PythonUDFMeta::check() const {
143
22
    if (trim(name).empty()) {
144
4
        return Status::InvalidArgument("Python UDF name is empty");
145
4
    }
146
147
18
    if (trim(symbol).empty()) {
148
2
        return Status::InvalidArgument("Python UDF symbol is empty");
149
2
    }
150
151
16
    if (trim(runtime_version).empty()) {
152
2
        return Status::InvalidArgument("Python UDF runtime version is empty");
153
2
    }
154
155
14
    if (input_types.empty()) {
156
2
        return Status::InvalidArgument("Python UDF input types is empty");
157
2
    }
158
159
12
    if (!return_type) {
160
2
        return Status::InvalidArgument("Python UDF return type is empty");
161
2
    }
162
163
10
    if (type == PythonUDFLoadType::UNKNOWN) {
164
2
        return Status::InvalidArgument(
165
2
                "Python UDF load type is invalid, please check inline code or file path");
166
2
    }
167
168
8
    if (type == PythonUDFLoadType::MODULE) {
169
6
        if (trim(location).empty()) {
170
2
            return Status::InvalidArgument("Non-inline Python UDF location is empty");
171
2
        }
172
4
        if (trim(checksum).empty()) {
173
2
            return Status::InvalidArgument("Non-inline Python UDF checksum is empty");
174
2
        }
175
4
    }
176
177
4
    return Status::OK();
178
8
}
179
180
} // namespace doris