Skip to content

Commit

Permalink
[BugFix] Do not reuse input null and offset column for array_map (#44226
Browse files Browse the repository at this point in the history
)

Signed-off-by: zihe.liu <[email protected]>
(cherry picked from commit 70db73d)

# Conflicts:
#	be/src/exprs/vectorized/array_map_expr.cpp
  • Loading branch information
ZiheLiu authored and mergify[bot] committed Apr 17, 2024
1 parent 4c9ff36 commit 0057df6
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 4 deletions.
32 changes: 28 additions & 4 deletions be/src/exprs/vectorized/array_map_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ inline bool offsets_equal(const UInt32Column::Ptr& array1, const UInt32Column::P
StatusOr<ColumnPtr> ArrayMapExpr::evaluate_checked(ExprContext* context, Chunk* chunk) {
std::vector<ColumnPtr> inputs;
std::vector<ColumnPtr> input_elements;
<<<<<<< HEAD:be/src/exprs/vectorized/array_map_expr.cpp
NullColumnPtr input_null_map = nullptr;
std::shared_ptr<ArrayColumn> input_array = nullptr;
=======
NullColumnPtr null_column = nullptr;
bool is_single_nullable_child = false;
ArrayColumn* input_array = nullptr;
ColumnPtr input_array_ptr_ref = nullptr; // hold shared_ptr to avoid early deleted.
>>>>>>> 70db73d348 ([BugFix] Do not reuse input null and offset column for array_map (#44226)):be/src/exprs/array_map_expr.cpp
// for many valid arguments:
// if one of them is a null literal, the result is a null literal;
// if one of them is only null, then results are null;
Expand All @@ -61,12 +68,21 @@ StatusOr<ColumnPtr> ArrayMapExpr::evaluate_checked(ExprContext* context, Chunk*
DCHECK(nullable != nullptr);
column = nullable->data_column();
// empty null array with non-zero elements
<<<<<<< HEAD:be/src/exprs/vectorized/array_map_expr.cpp
std::dynamic_pointer_cast<ArrayColumn>(column)->empty_null_array(nullable->null_column());
if (input_null_map) {
input_null_map =
FunctionHelper::union_null_column(nullable->null_column(), input_null_map); // merge null
=======
column->empty_null_in_complex_column(nullable->null_column()->get_data(),
down_cast<const ArrayColumn*>(column.get())->offsets().get_data());
if (null_column) {
is_single_nullable_child = false;
null_column = FunctionHelper::union_null_column(nullable->null_column(), null_column); // merge null
>>>>>>> 70db73d348 ([BugFix] Do not reuse input null and offset column for array_map (#44226)):be/src/exprs/array_map_expr.cpp
} else {
input_null_map = nullable->null_column();
is_single_nullable_child = true;
null_column = nullable->null_column();
}
}
DCHECK(column->is_array());
Expand All @@ -82,6 +98,13 @@ StatusOr<ColumnPtr> ArrayMapExpr::evaluate_checked(ExprContext* context, Chunk*
input_elements.push_back(cur_array->elements_column());
}

if (is_single_nullable_child) {
DCHECK(null_column != nullptr);
// If there are more than one nullable children, the nullable column has been cloned when calling
// union_null_column to merge, so only one nullable child needs to be cloned.
null_column = ColumnHelper::as_column<NullColumn>(null_column->clone_shared());
}

ColumnPtr column = nullptr;
if (input_array->elements_column()->size() == 0) { // arrays may be null or empty
column = ColumnHelper::create_column(type().children[0],
Expand Down Expand Up @@ -136,9 +159,10 @@ StatusOr<ColumnPtr> ArrayMapExpr::evaluate_checked(ExprContext* context, Chunk*
}
}
// attach offsets
auto array_col = std::make_shared<ArrayColumn>(column, input_array->offsets_column());
if (input_null_map != nullptr) {
return NullableColumn::create(std::move(array_col), input_null_map);
auto array_col = std::make_shared<ArrayColumn>(
column, ColumnHelper::as_column<UInt32Column>(input_array->offsets_column()->clone_shared()));
if (null_column != nullptr) {
return NullableColumn::create(std::move(array_col), null_column);
}
return array_col;
}
Expand Down
60 changes: 60 additions & 0 deletions test/sql/test_array/R/test_array_map
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- name: test_array_map_1
CREATE TABLE t1 (
k1 bigint,
c1 array < varchar(65536) >
) ENGINE = OLAP
DUPLICATE KEY(k1) PROPERTIES (
"replication_num" = "1"
);
-- result:
-- !result
CREATE TABLE t2 (
k1 bigint,
c1 bigint
) ENGINE = OLAP
DUPLICATE KEY(k1) PROPERTIES (
"replication_num" = "1"
);
-- result:
-- !result
insert into t1
values
(1, ["1","2"] ),
(2, ["0","2","1"] ),
(3, ["0","2","1"] ),
(4, ["1","2"] ),
(5, ["0","2","1"] ),
(6, ["0","2","1","1"]),
(7, ["0","2","1"] ),
(8, ["1","2"] ),
(9, ["L","2","1"] ),
(10, ["1","2"] );
-- result:
-- !result
insert into t2
values
(1, 1),
(2, 1),
(3, 3),
(4, 5);
-- result:
-- !result
with w1 as (
select
k1, c1, array_map (x -> true, c1) as c2
from
t1
)
select
w1.*
from
w1
join [broadcast] t2 using(k1)
where
array_sum(w1.c1) <= t2.c1
order by
w1.k1;
-- result:
3 ["0","2","1"] [1,1,1]
4 ["1","2"] [1,1]
-- !result
57 changes: 57 additions & 0 deletions test/sql/test_array/T/test_array_map
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-- name: test_array_map_1

-- Prepare Table and Data.
CREATE TABLE t1 (
k1 bigint,
c1 array < varchar(65536) >
) ENGINE = OLAP
DUPLICATE KEY(k1) PROPERTIES (
"replication_num" = "1"
);

CREATE TABLE t2 (
k1 bigint,
c1 bigint
) ENGINE = OLAP
DUPLICATE KEY(k1) PROPERTIES (
"replication_num" = "1"
);

insert into t1
values
(1, ["1","2"] ),
(2, ["0","2","1"] ),
(3, ["0","2","1"] ),
(4, ["1","2"] ),
(5, ["0","2","1"] ),
(6, ["0","2","1","1"]),
(7, ["0","2","1"] ),
(8, ["1","2"] ),
(9, ["L","2","1"] ),
(10, ["1","2"] );


insert into t2
values
(1, 1),
(2, 1),
(3, 3),
(4, 5);

-- Query.
with w1 as (
select
k1, c1, array_map (x -> true, c1) as c2
from
t1
)
select
w1.*
from
w1
join [broadcast] t2 using(k1)
where
array_sum(w1.c1) <= t2.c1
order by
w1.k1;

0 comments on commit 0057df6

Please sign in to comment.