Coverage Report

Created: 2026-03-14 18:33

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
10.6k
                                              std::shared_ptr<arrow::Schema>* schema) {
35
10.6k
    assert(!types.empty());
36
10.6k
    arrow::SchemaBuilder builder;
37
24.4k
    for (size_t i = 0; i < types.size(); ++i) {
38
13.7k
        std::shared_ptr<arrow::DataType> arrow_type;
39
13.7k
        RETURN_IF_ERROR(convert_to_arrow_type(types[i], &arrow_type, timezone));
40
13.7k
        std::shared_ptr<arrow::Field> field = std::make_shared<arrow::Field>(
41
13.7k
                "arg" + std::to_string(i), arrow_type, types[i]->is_nullable());
42
13.7k
        RETURN_DORIS_STATUS_IF_ERROR(builder.AddField(field));
43
13.7k
    }
44
10.6k
    RETURN_DORIS_STATUS_IF_RESULT_ERROR(schema, builder.Finish());
45
10.6k
    return Status::OK();
46
10.6k
}
47
48
Status PythonUDFMeta::serialize_arrow_schema(const std::shared_ptr<arrow::Schema>& schema,
49
10.6k
                                             std::shared_ptr<arrow::Buffer>* out) {
50
10.6k
    RETURN_DORIS_STATUS_IF_RESULT_ERROR(
51
10.6k
            out, arrow::ipc::SerializeSchema(*schema, arrow::default_memory_pool()));
52
10.6k
    return Status::OK();
53
10.6k
}
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
5.32k
Status PythonUDFMeta::serialize_to_json(std::string* json_str) const {
71
5.32k
    rapidjson::Document doc;
72
5.32k
    doc.SetObject();
73
5.32k
    auto& allocator = doc.GetAllocator();
74
5.32k
    doc.AddMember("name", rapidjson::Value().SetString(name.c_str(), allocator), allocator);
75
5.32k
    doc.AddMember("symbol", rapidjson::Value().SetString(symbol.c_str(), allocator), allocator);
76
5.32k
    doc.AddMember("location", rapidjson::Value().SetString(location.c_str(), allocator), allocator);
77
5.32k
    doc.AddMember("udf_load_type", rapidjson::Value().SetInt(static_cast<int>(type)), allocator);
78
5.32k
    doc.AddMember("client_type", rapidjson::Value().SetInt(static_cast<int>(client_type)),
79
5.32k
                  allocator);
80
5.32k
    doc.AddMember("runtime_version",
81
5.32k
                  rapidjson::Value().SetString(runtime_version.c_str(), allocator), allocator);
82
5.32k
    doc.AddMember("always_nullable", rapidjson::Value().SetBool(always_nullable), allocator);
83
84
5.32k
    {
85
        // Serialize base64 inline code to json
86
5.32k
        std::string base64_str = arrow::util::base64_encode(inline_code);
87
5.32k
        doc.AddMember("inline_code", rapidjson::Value().SetString(base64_str.c_str(), allocator),
88
5.32k
                      allocator);
89
5.32k
    }
90
5.32k
    {
91
        // Serialize base64 input types to json
92
5.32k
        std::shared_ptr<arrow::Schema> input_schema;
93
5.32k
        RETURN_IF_ERROR(convert_types_to_schema(input_types, TimezoneUtils::default_time_zone,
94
5.32k
                                                &input_schema));
95
5.32k
        std::shared_ptr<arrow::Buffer> input_schema_buffer;
96
5.32k
        RETURN_IF_ERROR(serialize_arrow_schema(input_schema, &input_schema_buffer));
97
5.32k
        std::string base64_str =
98
5.32k
                arrow::util::base64_encode({input_schema_buffer->data_as<char>(),
99
5.32k
                                            static_cast<size_t>(input_schema_buffer->size())});
100
5.32k
        doc.AddMember("input_types", rapidjson::Value().SetString(base64_str.c_str(), allocator),
101
5.32k
                      allocator);
102
5.32k
    }
103
0
    {
104
        // Serialize base64 return type to json
105
5.32k
        std::shared_ptr<arrow::Schema> return_schema;
106
5.32k
        RETURN_IF_ERROR(convert_types_to_schema({return_type}, TimezoneUtils::default_time_zone,
107
5.32k
                                                &return_schema));
108
5.32k
        std::shared_ptr<arrow::Buffer> return_schema_buffer;
109
5.32k
        RETURN_IF_ERROR(serialize_arrow_schema(return_schema, &return_schema_buffer));
110
5.32k
        std::string base64_str =
111
5.32k
                arrow::util::base64_encode({return_schema_buffer->data_as<char>(),
112
5.32k
                                            static_cast<size_t>(return_schema_buffer->size())});
113
5.32k
        doc.AddMember("return_type", rapidjson::Value().SetString(base64_str.c_str(), allocator),
114
5.32k
                      allocator);
115
5.32k
    }
116
117
    // Convert document to json string
118
0
    rapidjson::StringBuffer buffer;
119
5.32k
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
120
5.32k
    doc.Accept(writer);
121
5.32k
    *json_str = std::string(buffer.GetString(), buffer.GetSize());
122
5.32k
    return Status::OK();
123
5.32k
}
124
125
2.05k
std::string PythonUDFMeta::to_string() const {
126
2.05k
    std::stringstream input_types_ss;
127
2.05k
    input_types_ss << "<";
128
5.38k
    for (size_t i = 0; i < input_types.size(); ++i) {
129
3.32k
        input_types_ss << input_types[i]->get_name();
130
3.32k
        if (i != input_types.size() - 1) {
131
1.27k
            input_types_ss << ", ";
132
1.27k
        }
133
3.32k
    }
134
2.05k
    input_types_ss << ">";
135
2.05k
    return fmt::format(
136
2.05k
            "[name: {}, symbol: {}, location: {}, runtime_version: {}, always_nullable: {}, "
137
2.05k
            "inline_code: {}][input_types: {}][return_type: {}]",
138
2.05k
            name, symbol, location, runtime_version, always_nullable, inline_code,
139
2.05k
            input_types_ss.str(), return_type->get_name());
140
2.05k
}
141
142
3.36k
Status PythonUDFMeta::check() const {
143
3.36k
    if (trim(name).empty()) {
144
2
        return Status::InvalidArgument("Python UDF name is empty");
145
2
    }
146
147
3.36k
    if (trim(symbol).empty()) {
148
1
        return Status::InvalidArgument("Python UDF symbol is empty");
149
1
    }
150
151
3.35k
    if (trim(runtime_version).empty()) {
152
1
        return Status::InvalidArgument("Python UDF runtime version is empty");
153
1
    }
154
155
3.35k
    if (input_types.empty()) {
156
1
        return Status::InvalidArgument("Python UDF input types is empty");
157
1
    }
158
159
3.35k
    if (!return_type) {
160
1
        return Status::InvalidArgument("Python UDF return type is empty");
161
1
    }
162
163
3.35k
    if (type == PythonUDFLoadType::UNKNOWN) {
164
1
        return Status::InvalidArgument(
165
1
                "Python UDF load type is invalid, please check inline code or file path");
166
1
    }
167
168
3.35k
    if (type == PythonUDFLoadType::MODULE) {
169
1.64k
        if (trim(location).empty()) {
170
1
            return Status::InvalidArgument("Non-inline Python UDF location is empty");
171
1
        }
172
1.64k
        if (trim(checksum).empty()) {
173
1
            return Status::InvalidArgument("Non-inline Python UDF checksum is empty");
174
1
        }
175
1.64k
    }
176
177
3.35k
    return Status::OK();
178
3.35k
}
179
180
} // namespace doris