hello-stephen commented on code in PR #71438:
URL: https://github.com/apache/doris/pull/71438#discussion_r4800010631
##########
fe/fe-core/src/test/java/org/apache/doris/catalog/AIResourceTest.java:
##########
@@ -426,6 +427,19 @@ public void
testAcceptMultimodalEmbedOnlyResourceForJsonEmbed() throws DdlExcept
});
}
+ @Test
+ public void testAcceptMultimodalEmbedOnlyResourceForArrayJsonEmbed()
throws DdlException {
+ AIResource aiResource = createMultimodalEmbedOnlyResource();
+ withAIResource(aiResource, () -> {
+ Embed function = new Embed(new
StringLiteral("multimodal-embed-only-resource"),
+ new ArrayLiteral(ImmutableList.of(
+ new
JsonLiteral("{"content_type":"text","text":"hello"}"),
Review Comment:
[P1] Escape the quotes in both new JSON fixtures. The quote before
`content_type` terminates the Java string, so `AIResourceTest` fails to
compile. The preceding scalar JSON test shows the escaping needed here.
##########
be/src/exprs/function/ai/embed.h:
##########
@@ -187,16 +204,34 @@ class FunctionEmbed : public AIFunction<FunctionEmbed> {
return Status::OK();
}
+ Status _parse_multimodal_item(const IColumn& json_column, size_t row_idx,
int64_t ttl_seconds,
+ MultimodalInputItem& item) const {
+ rapidjson::Document file_input;
+ RETURN_IF_ERROR(_parse_file_input(json_column, row_idx, file_input));
+
+ std::string content_type;
+ MultimodalType media_type;
+ RETURN_IF_ERROR(_infer_media_type(file_input, content_type,
media_type));
+
+ std::string media_input;
+ if (media_type == MultimodalType::TEXT) {
+ RETURN_IF_ERROR(_get_required_string_field(file_input, "text",
media_input));
Review Comment:
[P2] Preserve embedded NULs in multimodal text. JSONB preserves an escaped
`\u0000`, but `_get_required_string_field` assigns from `GetString()` without
its length, silently dropping the suffix. The new text serializers in the Qwen,
VoyageAI, Jina, and Gemini adapters also infer length from a C string. Pass
explicit lengths at both boundaries so the provider receives the original text.
##########
be/src/exprs/function/ai/embed.h:
##########
@@ -210,36 +245,65 @@ class FunctionEmbed : public AIFunction<FunctionEmbed> {
const int32_t max_batch_size = _get_embed_max_batch_size(context);
+ const ColumnArray* col_array = nullptr;
+ const ColumnArray::Offsets64* offsets = nullptr;
+ const ColumnNullable* nested_nullable = nullptr;
+ const IColumn* fused_json_column = nullptr;
+ bool is_array_const = false;
+ if (is_fused) {
+ const auto& [unpacked_col, is_const] =
unpack_if_const(input_column);
+ is_array_const = is_const;
+ col_array = &assert_cast<const ColumnArray&>(*unpacked_col);
+ offsets = &col_array->get_offsets();
+ const IColumn& nested_col = col_array->get_data();
+ nested_nullable = check_and_get_column<ColumnNullable>(nested_col);
+ fused_json_column =
+ nested_nullable ? &nested_nullable->get_nested_column() :
&nested_col;
+ }
+
for (size_t i = 0; i < input_rows_count; ++i) {
if (null_map && (*null_map)[i]) {
continue;
}
- rapidjson::Document file_input;
- RETURN_IF_ERROR(_parse_file_input(*input_column, i, file_input));
-
- std::string content_type;
- MultimodalType media_type;
- RETURN_IF_ERROR(_infer_media_type(file_input, content_type,
media_type));
-
- std::string media_url;
- RETURN_IF_ERROR(_resolve_media_url(file_input, ttl_seconds,
media_url));
+ std::vector<MultimodalInputItem> row_items;
+ if (!is_fused) {
+ MultimodalInputItem item;
+ RETURN_IF_ERROR(_parse_multimodal_item(*input_column, i,
ttl_seconds, item));
+ row_items.emplace_back(std::move(item));
+ } else {
+ const size_t array_idx = index_check_const(i, is_array_const);
+ const size_t start = array_idx == 0 ? 0 : (*offsets)[array_idx
- 1];
+ const size_t end = (*offsets)[array_idx];
+ if (start == end) {
+ return Status::InvalidArgument(
+ "Function EMBED fused multimodal array can not be
empty");
+ }
+ row_items.reserve(end - start);
+ for (size_t j = start; j < end; ++j) {
+ if (nested_nullable && nested_nullable->is_null_at(j)) {
+ return Status::InvalidArgument(
+ "Function EMBED fused multimodal array can not
contain NULL "
+ "element");
+ }
+ MultimodalInputItem item;
+ RETURN_IF_ERROR(
+ _parse_multimodal_item(*fused_json_column, j,
ttl_seconds, item));
+ row_items.emplace_back(std::move(item));
+ }
+ }
- if (!batch_media_urls.empty() &&
- batch_media_urls.size() >=
static_cast<size_t>(max_batch_size)) {
+ if (!batch_inputs.empty() &&
+ batch_inputs.size() >= static_cast<size_t>(max_batch_size)) {
Review Comment:
[P2] Honor `ai_context_window_size` when batching multimodal text. With a
10-byte window and a five-row cap, two six-byte scalar JSON text inputs are
sent in one request. The existing string path splits the equivalent inputs.
Account for accumulated text bytes before adding another row, while keeping all
parts of a fused row together.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/ai/Embed.java:
##########
@@ -52,7 +53,11 @@ public class Embed extends AIFunction {
FunctionSignature.ret(ArrayType.of(FloatType.INSTANCE))
.args(StringType.INSTANCE, JsonType.INSTANCE),
FunctionSignature.ret(ArrayType.of(FloatType.INSTANCE))
- .args(VarcharType.SYSTEM_DEFAULT, JsonType.INSTANCE)
+ .args(VarcharType.SYSTEM_DEFAULT, JsonType.INSTANCE),
+ FunctionSignature.ret(ArrayType.of(FloatType.INSTANCE))
+ .args(StringType.INSTANCE,
ArrayType.of(JsonType.INSTANCE)),
Review Comment:
[P2] Account for existing callers that embed typed arrays as text.
Previously, signature search could coerce an `ARRAY<JSON>` argument to STRING.
With an existing text-only OpenAI resource, the exact array signature now wins,
resource validation succeeds, and BE rejects the request as unsupported fused
embedding. Preserve text execution for this resource profile or document the
required explicit STRING cast and cover that migration in a test.
##########
be/src/exprs/function/ai/ai_adapter.h:
##########
@@ -1357,55 +1379,25 @@ class QwenAdapter : public OpenAIAdapter {
const std::vector<std::string>& /*media_content_types*/,
std::string& request_body) const override {
RETURN_IF_ERROR(validate_multimodal_embedding_inputs(
- "QWEN", media_types, media_urls, {MultimodalType::IMAGE,
MultimodalType::VIDEO}));
-
- rapidjson::Document doc;
- doc.SetObject();
- auto& allocator = doc.GetAllocator();
-
- /*{
- "model": "tongyi-embedding-vision-plus",
- "input": {
- "contents": [
- {"image": "<url>"},
- {"video": "<url>"}
- ]
- }
- "parameters": {
- "dimension": 512
- }
- }*/
- doc.AddMember("model", rapidjson::Value(_config.model_name.c_str(),
allocator), allocator);
- rapidjson::Value input(rapidjson::kObjectType);
- rapidjson::Value contents(rapidjson::kArrayType);
+ "QWEN", media_types, media_urls,
+ {MultimodalType::IMAGE, MultimodalType::VIDEO,
MultimodalType::TEXT}));
+ std::vector<std::vector<MultimodalInputItem>> rows;
+ rows.reserve(media_urls.size());
for (size_t i = 0; i < media_urls.size(); ++i) {
- rapidjson::Value media_item(rapidjson::kObjectType);
- if (media_types[i] == MultimodalType::IMAGE) {
- media_item.AddMember("image",
rapidjson::Value(media_urls[i].c_str(), allocator),
- allocator);
- } else {
- media_item.AddMember("video",
rapidjson::Value(media_urls[i].c_str(), allocator),
- allocator);
- }
- contents.PushBack(media_item, allocator);
+ rows.push_back({{media_types[i], media_urls[i], {}}});
}
+ return _build_qwen_multimodal_payload(rows, false /* reject_duplicate
*/, request_body);
+ }
- input.AddMember("contents", contents, allocator);
- doc.AddMember("input", input, allocator);
- if (_config.dimensions != -1 &&
supports_dimension_param(_config.model_name)) {
- rapidjson::Value parameters(rapidjson::kObjectType);
- std::string param_name = get_dimension_param_name();
- rapidjson::Value dimension_name(param_name.c_str(), allocator);
- parameters.AddMember(dimension_name, _config.dimensions,
allocator);
- doc.AddMember("parameters", parameters, allocator);
+ Status build_fused_multimodal_embedding_request(
+ const std::vector<std::vector<MultimodalInputItem>>& fused_inputs,
+ std::string& request_body) const override {
+ if (fused_inputs.empty()) {
+ return Status::InvalidArgument("QWEN fused multimodal embed inputs
can not be empty");
}
-
- rapidjson::StringBuffer buffer;
- rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
- doc.Accept(writer);
- request_body = buffer.GetString();
- return Status::OK();
+ return _build_qwen_multimodal_payload(fused_inputs, true /*
reject_duplicate */,
Review Comment:
[P1] Send a DashScope fusion request for each SQL row. This path adds one
JSON object with several modality keys per row and never sets
`parameters.enable_fusion`. DashScope expects a separate object for each
content item, including repeated image items, and enabling fusion produces one
vector for the entire request. Adding the flag to the current multi-row batch
would still fail the BE result-count check. Send each row's ordered parts in
its own fused request and exercise the result-to-row mapping with distinct
rows. [DashScope HTTP request
contract](https://docs.modelstudio.console.alibabacloud.com/en/model-studio/multimodal-embedding-api-reference)
##########
be/src/exprs/function/ai/embed.h:
##########
@@ -210,36 +245,65 @@ class FunctionEmbed : public AIFunction<FunctionEmbed> {
const int32_t max_batch_size = _get_embed_max_batch_size(context);
+ const ColumnArray* col_array = nullptr;
+ const ColumnArray::Offsets64* offsets = nullptr;
+ const ColumnNullable* nested_nullable = nullptr;
+ const IColumn* fused_json_column = nullptr;
+ bool is_array_const = false;
+ if (is_fused) {
+ const auto& [unpacked_col, is_const] =
unpack_if_const(input_column);
+ is_array_const = is_const;
+ col_array = &assert_cast<const ColumnArray&>(*unpacked_col);
+ offsets = &col_array->get_offsets();
+ const IColumn& nested_col = col_array->get_data();
+ nested_nullable = check_and_get_column<ColumnNullable>(nested_col);
+ fused_json_column =
+ nested_nullable ? &nested_nullable->get_nested_column() :
&nested_col;
+ }
+
for (size_t i = 0; i < input_rows_count; ++i) {
if (null_map && (*null_map)[i]) {
continue;
}
- rapidjson::Document file_input;
- RETURN_IF_ERROR(_parse_file_input(*input_column, i, file_input));
-
- std::string content_type;
- MultimodalType media_type;
- RETURN_IF_ERROR(_infer_media_type(file_input, content_type,
media_type));
-
- std::string media_url;
- RETURN_IF_ERROR(_resolve_media_url(file_input, ttl_seconds,
media_url));
+ std::vector<MultimodalInputItem> row_items;
+ if (!is_fused) {
+ MultimodalInputItem item;
+ RETURN_IF_ERROR(_parse_multimodal_item(*input_column, i,
ttl_seconds, item));
+ row_items.emplace_back(std::move(item));
+ } else {
+ const size_t array_idx = index_check_const(i, is_array_const);
+ const size_t start = array_idx == 0 ? 0 : (*offsets)[array_idx
- 1];
+ const size_t end = (*offsets)[array_idx];
+ if (start == end) {
+ return Status::InvalidArgument(
+ "Function EMBED fused multimodal array can not be
empty");
+ }
+ row_items.reserve(end - start);
+ for (size_t j = start; j < end; ++j) {
+ if (nested_nullable && nested_nullable->is_null_at(j)) {
+ return Status::InvalidArgument(
+ "Function EMBED fused multimodal array can not
contain NULL "
+ "element");
+ }
+ MultimodalInputItem item;
+ RETURN_IF_ERROR(
+ _parse_multimodal_item(*fused_json_column, j,
ttl_seconds, item));
Review Comment:
[P2] Reject non-object JSON elements with an argument error. A JSON number
or JSON null is valid in an `ARRAY<JSON>` column and passes the SQL NULL check
above. It reaches `_parse_file_input`'s `DORIS_CHECK(IsObject())`, which aborts
debug builds and surfaces an internal FatalError in release builds. Validate
the object shape and cover it alongside the SQL NULL test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]