forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature](function) support function array_flatten (apache#47404)
### What problem does this PR solve? ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [X] Regression test - [ ] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [X] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [ ] No. - [X] Yes. <apache/doris-website#1951> ### Check List (For Reviewer who merge this PR) - [X] Confirm the release note - [X] Confirm test cases - [X] Confirm document - [X] Add branch pick label <!-- Add branch pick label that this PR should merge into -->
- Loading branch information
1 parent
3fb37ce
commit 5f46933
Showing
9 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// 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. | ||
|
||
#include "common/status.h" | ||
#include "vec/aggregate_functions/aggregate_function.h" | ||
#include "vec/columns/column.h" | ||
#include "vec/columns/column_array.h" | ||
#include "vec/columns/column_nullable.h" | ||
#include "vec/common/assert_cast.h" | ||
#include "vec/core/block.h" | ||
#include "vec/core/column_numbers.h" | ||
#include "vec/core/column_with_type_and_name.h" | ||
#include "vec/core/types.h" | ||
#include "vec/data_types/data_type.h" | ||
#include "vec/data_types/data_type_array.h" | ||
#include "vec/functions/function.h" | ||
#include "vec/functions/simple_function_factory.h" | ||
|
||
namespace doris::vectorized { | ||
#include "common/compile_check_begin.h" | ||
|
||
class FunctionArrayFlatten : public IFunction { | ||
public: | ||
static constexpr auto name = "array_flatten"; | ||
static FunctionPtr create() { return std::make_shared<FunctionArrayFlatten>(); } | ||
|
||
/// Get function name. | ||
String get_name() const override { return name; } | ||
|
||
size_t get_number_of_arguments() const override { return 1; } | ||
|
||
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | ||
DataTypePtr arg = arguments[0]; | ||
while (is_array(arg)) { | ||
arg = remove_nullable(assert_cast<const DataTypeArray*>(arg.get())->get_nested_type()); | ||
} | ||
return std::make_shared<DataTypeArray>(make_nullable(arg)); | ||
} | ||
|
||
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
uint32_t result, size_t input_rows_count) const override { | ||
auto src_column = | ||
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); | ||
auto* src_column_array_ptr = | ||
assert_cast<ColumnArray*>(remove_nullable(src_column)->assume_mutable().get()); | ||
ColumnArray* nested_src_column_array_ptr = src_column_array_ptr; | ||
|
||
auto result_column_offsets = | ||
assert_cast<ColumnArray::ColumnOffsets&>(src_column_array_ptr->get_offsets_column()) | ||
.clone(); | ||
auto* offsets = assert_cast<ColumnArray::ColumnOffsets*>(result_column_offsets.get()) | ||
->get_data() | ||
.data(); | ||
|
||
while (src_column_array_ptr->get_data_ptr()->is_column_array()) { | ||
nested_src_column_array_ptr = assert_cast<ColumnArray*>( | ||
remove_nullable(src_column_array_ptr->get_data_ptr())->assume_mutable().get()); | ||
|
||
for (size_t i = 0; i < input_rows_count; ++i) { | ||
offsets[i] = nested_src_column_array_ptr->get_offsets()[offsets[i] - 1]; | ||
} | ||
src_column_array_ptr = nested_src_column_array_ptr; | ||
} | ||
|
||
block.replace_by_position( | ||
result, ColumnArray::create(assert_cast<const ColumnNullable&>( | ||
nested_src_column_array_ptr->get_data()) | ||
.clone(), | ||
std::move(result_column_offsets))); | ||
return Status::OK(); | ||
} | ||
}; | ||
|
||
void register_function_array_flatten(SimpleFunctionFactory& factory) { | ||
factory.register_function<FunctionArrayFlatten>(); | ||
} | ||
|
||
} // namespace doris::vectorized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...c/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/ArrayFlatten.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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. | ||
|
||
package org.apache.doris.nereids.trees.expressions.functions.scalar; | ||
|
||
import org.apache.doris.catalog.FunctionSignature; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
import org.apache.doris.nereids.trees.expressions.functions.CustomSignature; | ||
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable; | ||
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; | ||
import org.apache.doris.nereids.types.ArrayType; | ||
import org.apache.doris.nereids.types.DataType; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* ScalarFunction 'array_flatten' | ||
*/ | ||
public class ArrayFlatten extends ScalarFunction | ||
implements CustomSignature, PropagateNullable { | ||
|
||
/** | ||
* constructor with 1 arguments. | ||
*/ | ||
public ArrayFlatten(Expression arg) { | ||
super("array_flatten", arg); | ||
} | ||
|
||
@Override | ||
public FunctionSignature customSignature() { | ||
DataType dataType = getArgument(0).getDataType(); | ||
while (dataType instanceof ArrayType) { | ||
dataType = ((ArrayType) dataType).getItemType(); | ||
} | ||
return FunctionSignature.ret(ArrayType.of(dataType)).args(getArgument(0).getDataType()); | ||
} | ||
|
||
/** | ||
* withChildren. | ||
*/ | ||
@Override | ||
public ArrayFlatten withChildren(List<Expression> children) { | ||
Preconditions.checkArgument(children.size() == 1); | ||
return new ArrayFlatten(children.get(0)); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) { | ||
return visitor.visitArrayFlatten(this, context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
regression-test/data/query_p0/sql_functions/array_functions/array_flatten.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- This file is automatically generated. You should know what you did if you want to edit this | ||
-- !test -- | ||
1 [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] ["a", "b", "c"] | ||
2 \N \N [] \N ["b", null] | ||
3 [1, 2, null] [null] [null, 2] [null, null, 3] [null, "aaaab", "ccc"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
regression-test/suites/query_p0/sql_functions/array_functions/array_flatten.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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("array_flatten") { | ||
sql """DROP TABLE IF EXISTS t_array_flatten""" | ||
sql """ | ||
CREATE TABLE IF NOT EXISTS t_array_flatten ( | ||
`k1` int(11) NULL COMMENT "", | ||
`a1` array<tinyint(4)> NULL COMMENT "", | ||
`aaa1` array<array<array<tinyint(4)>>> NULL COMMENT "", | ||
`aa3` array<array<int(11)>> NOT NULL COMMENT "", | ||
`aa5` array<array<largeint(40)>> NULL COMMENT "", | ||
`aa14` array<array<string>> NULL COMMENT "" | ||
) ENGINE=OLAP | ||
DUPLICATE KEY(`k1`) | ||
DISTRIBUTED BY HASH(`k1`) BUCKETS 1 | ||
PROPERTIES ( | ||
"replication_allocation" = "tag.location.default: 1", | ||
"storage_format" = "V2" | ||
) | ||
""" | ||
sql """ INSERT INTO t_array_flatten VALUES(1, [1, 2, 3],[[[1]],[[2],[3]]],[[1,2],[3]],[[1,2],[3]],[['a'],['b','c']]) """ | ||
sql """ INSERT INTO t_array_flatten VALUES(2, null,null,[],null,[null,['b',null]]) """ | ||
sql """ INSERT INTO t_array_flatten VALUES(3, [1, 2, null],[[[]],[[null],[]]],[[null,2],[]],[[null,null],[3]],[[null],['aaaab','ccc']]) """ | ||
|
||
|
||
|
||
qt_test """ | ||
select k1, array_flatten(a1), array_flatten(aaa1), array_flatten(aa3), array_flatten(aa5), array_flatten(aa14) from t_array_flatten order by k1; | ||
""" | ||
} |