Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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);
Expand Down
29 changes: 29 additions & 0 deletions be/test/vec/aggregate_functions/agg_min_max_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"}));

Expand Down
29 changes: 29 additions & 0 deletions regression-test/data/jsonb_p0/test_jsonb_any_aggregate.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
1 {"name":"Alice","age":25}
2 {"name":"Bob","age":30}
3 {"name":"Charlie","age":35}

-- !sql --
1 {"name":"Alice","age":25}
2 {"name":"Bob","age":30}
3 {"name":"Charlie","age":35}

-- !sql --
1 {}

-- !sql --
{}

-- !sql --
{}

-- !sql --
{"info":{"address":{"city":"Beijing","zipcode":100000},"contacts":[{"type":"phone","value":"123456"},{"type":"email","value":"test@example.com"}]}}

-- !sql --
1 {"value":100}

-- !sql --
1 \N

109 changes: 109 additions & 0 deletions regression-test/suites/jsonb_p0/test_jsonb_any_aggregate.groovy
Original file line number Diff line number Diff line change
@@ -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": "test@example.com"}]}}')
"""

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
"""
}
Loading