Coverage Report

Created: 2026-05-09 05:26

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