This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.1 by this push:
new 4b3dd6c10a6 branch-2.1: [feat](func) any function supports json #50311
(#50484)
4b3dd6c10a6 is described below
commit 4b3dd6c10a6f8f3136a0928a9ef0af65bbe615b5
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Apr 29 19:11:25 2025 +0800
branch-2.1: [feat](func) any function supports json #50311 (#50484)
Cherry-picked from #50311
Co-authored-by: lw112 <[email protected]>
---
.../aggregate_function_min_max.cpp | 2 +-
.../vec/aggregate_functions/agg_min_max_test.cpp | 29 ++++++
.../data/jsonb_p0/test_jsonb_any_aggregate.out | Bin 0 -> 539 bytes
.../jsonb_p0/test_jsonb_any_aggregate.groovy | 109 +++++++++++++++++++++
4 files changed, 139 insertions(+), 1 deletion(-)
diff --git a/be/src/vec/aggregate_functions/aggregate_function_min_max.cpp
b/be/src/vec/aggregate_functions/aggregate_function_min_max.cpp
index c1a72fd52bd..59a11984c38 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_min_max.cpp
+++ b/be/src/vec/aggregate_functions/aggregate_function_min_max.cpp
@@ -49,7 +49,7 @@ AggregateFunctionPtr
create_aggregate_function_single_value(const String& name,
const DataTypePtr& argument_type = remove_nullable(argument_types[0]);
WhichDataType which(argument_type);
- if (which.idx == TypeIndex::String) {
+ if (which.idx == TypeIndex::String || which.idx == TypeIndex::JSONB) {
return creator_without_type::create<
AggregateFunctionsSingleValue<Data<SingleValueDataString>>>(argument_types,
result_is_nullable);
diff --git a/be/test/vec/aggregate_functions/agg_min_max_test.cpp
b/be/test/vec/aggregate_functions/agg_min_max_test.cpp
index fef91af4d1f..901f11ec39f 100644
--- a/be/test/vec/aggregate_functions/agg_min_max_test.cpp
+++ b/be/test/vec/aggregate_functions/agg_min_max_test.cpp
@@ -35,6 +35,7 @@
#include "vec/core/field.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type_decimal.h"
+#include "vec/data_types/data_type_jsonb.h"
#include "vec/data_types/data_type_number.h"
#include "vec/data_types/data_type_string.h"
@@ -155,6 +156,34 @@ TEST_P(AggMinMaxTest, min_max_string_test) {
agg_function->destroy(place);
}
+TEST_P(AggMinMaxTest, any_json_test) {
+ // Prepare test data with JSON
+ auto column_vector_json = ColumnString::create();
+ std::string json_data = "{}";
+ column_vector_json->insert_data(json_data.c_str(), json_data.length());
+
+ // Set up the any function with JSONB type
+ AggregateFunctionSimpleFactory factory;
+ register_aggregate_function_minmax(factory);
+ DataTypes data_types = {std::make_shared<DataTypeJsonb>()};
+ auto agg_function = factory.get("any", data_types, false, -1);
+
+ // Create and initialize place for aggregation
+ std::unique_ptr<char[]> memory(new char[agg_function->size_of_data()]);
+ AggregateDataPtr place = memory.get();
+ agg_function->create(place);
+
+ // Do aggregation
+ const IColumn* column[1] = {column_vector_json.get()};
+ agg_function->add(place, column, 0, nullptr);
+
+ // Verify result
+ ColumnString ans;
+ agg_function->insert_result_into(place, ans);
+ EXPECT_EQ(StringRef(json_data), ans.get_data_at(0));
+ agg_function->destroy(place);
+}
+
INSTANTIATE_TEST_SUITE_P(Params, AggMinMaxTest,
::testing::ValuesIn(std::vector<std::string> {"min",
"max"}));
diff --git a/regression-test/data/jsonb_p0/test_jsonb_any_aggregate.out
b/regression-test/data/jsonb_p0/test_jsonb_any_aggregate.out
new file mode 100644
index 00000000000..d6ffa674337
Binary files /dev/null and
b/regression-test/data/jsonb_p0/test_jsonb_any_aggregate.out differ
diff --git a/regression-test/suites/jsonb_p0/test_jsonb_any_aggregate.groovy
b/regression-test/suites/jsonb_p0/test_jsonb_any_aggregate.groovy
new file mode 100644
index 00000000000..4468b17ad01
--- /dev/null
+++ b/regression-test/suites/jsonb_p0/test_jsonb_any_aggregate.groovy
@@ -0,0 +1,109 @@
+// 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.
+
+suite("test_jsonb_any_aggregate", "p0") {
+ def tableName = "test_any_json"
+
+ sql "DROP TABLE IF EXISTS ${tableName}"
+
+ sql """
+ CREATE TABLE IF NOT EXISTS ${tableName} (
+ id INT,
+ group_id INT,
+ json_data JSON
+ )
+ DISTRIBUTED BY HASH(id) BUCKETS 3
+ PROPERTIES (
+ "replication_num" = "1"
+ )
+ """
+
+ sql """
+ INSERT INTO ${tableName} VALUES
+ (1, 1, '{"name": "Alice", "age": 25}'),
+ (2, 2, '{"name": "Bob", "age": 30}'),
+ (3, 3, '{"name": "Charlie", "age": 35}')
+ """
+
+ qt_sql """
+ SELECT group_id, ANY(json_data) as any_data
+ FROM ${tableName}
+ GROUP BY group_id
+ ORDER BY group_id
+ """
+
+ qt_sql """
+ SELECT group_id, ANY_VALUE(json_data) as any_value_data
+ FROM ${tableName}
+ GROUP BY group_id
+ ORDER BY group_id
+ """
+
+ qt_sql """
+ WITH t0 AS (
+ SELECT CAST(1 AS BIGINT) AS id, CAST('{}' AS JSON) AS attr
+ )
+ SELECT id, ANY(attr) FROM t0 GROUP BY id
+ """
+
+ qt_sql """
+ SELECT ANY(CAST('{}' AS JSON))
+ """
+
+ qt_sql """
+ SELECT ANY_VALUE(CAST('{}' AS JSON))
+ """
+
+ sql "TRUNCATE TABLE ${tableName}"
+
+ sql """
+ INSERT INTO ${tableName} VALUES
+ (1, 1, '{"info": {"address": {"city": "Beijing", "zipcode": 100000},
"contacts": [{"type": "phone", "value": "123456"}, {"type": "email", "value":
"[email protected]"}]}}')
+ """
+
+ qt_sql """
+ SELECT ANY(json_data) FROM ${tableName} WHERE id = 1
+ """
+
+ sql "TRUNCATE TABLE ${tableName}"
+ sql """
+ INSERT INTO ${tableName} VALUES
+ (1, 1, '{"value": 100}'),
+ (2, 1, '{"value": 100}'),
+ (3, 1, '{"value": 100}')
+ """
+
+ qt_sql """
+ SELECT group_id, ANY(json_data) as any_data
+ FROM ${tableName}
+ GROUP BY group_id
+ """
+
+ // testing for null values
+ sql "TRUNCATE TABLE ${tableName}"
+ sql """
+ INSERT INTO ${tableName}(id, group_id) VALUES
+ (1, 1),
+ (2, 1)
+ """
+
+ qt_sql """
+ SELECT group_id, ANY(json_data) as any_data
+ FROM ${tableName}
+ GROUP BY group_id
+ """
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]