Skip to content

Commit

Permalink
[fix](map_agg) lost scale information for decimal type (#23776)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhhsg authored Sep 2, 2023
1 parent e5d1248 commit 68aa486
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
11 changes: 6 additions & 5 deletions be/src/vec/aggregate_functions/aggregate_function_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,14 @@ class AggregateFunctionMapAgg final
const size_t num_rows, Arena* arena) const override {
auto& col = assert_cast<ColumnMap&>(*dst);
for (size_t i = 0; i != num_rows; ++i) {
Map map(2);
columns[0]->get(i, map[0]);
if (map[0].is_null()) {
Field key, value;
columns[0]->get(i, key);
if (key.is_null()) {
continue;
}
columns[1]->get(i, map[1]);
col.insert(map);

columns[1]->get(i, value);
col.insert(Map {Array {key}, Array {value}});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,10 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
fn.setReturnType(new ArrayType(getChild(0).type));
}

if (fnName.getFunction().equalsIgnoreCase("map_agg")) {
fn.setReturnType(new MapType(getChild(0).type, getChild(1).type));
}

if (fnName.getFunction().equalsIgnoreCase("group_uniq_array")
|| fnName.getFunction().equalsIgnoreCase("group_array")) {
fn.setReturnType(new ArrayType(getChild(0).type));
Expand Down
9 changes: 7 additions & 2 deletions regression-test/data/query_p0/aggregate/map_agg.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
4 V4_1 V4_2 \N
5 V5_1 V5_2 V5_3

-- !sql2 --
-- !sql3 --
1 V1_1 V1_2 V1_3
2 V2_1 V2_2 V2_3
3 V3_1 V3_2 V3_3
4 V4_1 V4_2 V4_3
5 V5_1 V5_2 V5_3

-- !sql3 --
-- !sql4 --
{"key":["ab", "efg", NULL]}

-- !sql5 --
1 1.2345 2.4567 5.9876
2 2.4567 3.3300 4.5500
3 188.9980 998.9960 1024.1024

58 changes: 52 additions & 6 deletions regression-test/suites/query_p0/aggregate/map_agg.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,39 @@ suite("map_agg") {
(5, 1, "V5_1"),
(5, 9223372036854775807, "V5_2"),
(5, 22000000000, "V5_3");
"""
"""

sql "DROP TABLE IF EXISTS `test_map_agg_decimal`;"
sql """
CREATE TABLE `test_map_agg_decimal` (
`id` int(11) NOT NULL,
`label_name` string NOT NULL,
`value_field` decimal(15,4)
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"storage_format" = "V2",
"light_schema_change" = "true",
"disable_auto_compaction" = "false",
"enable_single_replica_compaction" = "false"
);
"""

sql """
insert into `test_map_agg_decimal` values
(1, "k1", 1.2345),
(1, "k2", 2.4567),
(1, "k3", 5.9876),
(2, "k1", 2.4567),
(2, "k2", 3.33),
(2, "k3", 4.55),
(3, "k1", 188.998),
(3, "k2", 998.996),
(3, "k3", 1024.1024)
"""

qt_sql1 """
WITH `labels` as (
Expand Down Expand Up @@ -155,7 +187,7 @@ suite("map_agg") {
ORDER BY `id`;
"""

qt_sql2 """
qt_sql3 """
WITH `labels` as (
SELECT `id`, map_agg(`label_name`, `value_field`) m FROM test_map_agg_numeric_key GROUP BY `id`
)
Expand All @@ -168,11 +200,25 @@ suite("map_agg") {
ORDER BY `id`;
"""

qt_sql3 """
qt_sql4 """
select map_agg(k, v) from (select 'key' as k, array('ab', 'efg', null) v) a;
"""

sql "DROP TABLE `test_map_agg`"
sql "DROP TABLE `test_map_agg_nullable`"
sql "DROP TABLE `test_map_agg_numeric_key`"
qt_sql5 """
WITH `labels` as (
SELECT `id`, map_agg(`label_name`, `value_field`) m FROM test_map_agg_decimal GROUP BY `id`
)
SELECT
id,
m["k1"] LA,
m["k2"] LB,
m["k3"] LC
FROM `labels`
ORDER BY `id`;
"""

sql "DROP TABLE IF EXISTS `test_map_agg`"
sql "DROP TABLE IF EXISTS `test_map_agg_nullable`"
sql "DROP TABLE IF EXISTS `test_map_agg_numeric_key`"
sql "DROP TABLE IF EXISTS `test_map_agg_decimal`"
}

0 comments on commit 68aa486

Please sign in to comment.