-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement](func)Introduce non_nullable extraction function. #16621
Introduced a new function non_nullable to BE, which can extract concrete data column from a nullable column. If the input argument is already not a nullable column, raise an error.
- Loading branch information
Showing
7 changed files
with
192 additions
and
3 deletions.
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
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,76 @@ | ||
// 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. | ||
// This file is copied from | ||
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/Ifnull.h | ||
// and modified by Doris | ||
|
||
#include "common/logging.h" | ||
#include "common/status.h" | ||
#include "vec/columns/column.h" | ||
#include "vec/columns/column_nullable.h" | ||
#include "vec/data_types/data_type.h" | ||
#include "vec/data_types/data_type_array.h" | ||
#include "vec/data_types/data_type_nullable.h" | ||
#include "vec/data_types/get_least_supertype.h" | ||
#include "vec/functions/function_helpers.h" | ||
#include "vec/functions/function_string.h" | ||
#include "vec/functions/simple_function_factory.h" | ||
#include "vec/utils/util.hpp" | ||
|
||
namespace doris::vectorized { | ||
class FunctionNonNullable : public IFunction { | ||
public: | ||
static constexpr auto name = "non_nullable"; | ||
|
||
static FunctionPtr create() { return std::make_shared<FunctionNonNullable>(); } | ||
|
||
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 { | ||
return remove_nullable(arguments[0]); | ||
} | ||
|
||
bool use_default_implementation_for_constants() const override { return true; } | ||
bool use_default_implementation_for_nulls() const override { return false; } | ||
|
||
// trans nullable column to non-nullable column. If argument is already non-nullable, raise error. | ||
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
size_t result, size_t input_rows_count) override { | ||
auto& data = block.get_by_position(arguments[0]); | ||
const ColumnNullable* column = check_and_get_column<ColumnNullable>(data.column); | ||
|
||
if (column == nullptr) // raise error if input is not nullable. | ||
{ | ||
return Status::RuntimeError( | ||
"Try to use originally non-nullable column {} in nullable's non-nullable \ | ||
convertion.", | ||
data.column->get_name(), get_name()); | ||
} else { // column is ColumnNullable | ||
const ColumnPtr& type_ptr = column->get_nested_column_ptr(); | ||
block.replace_by_position(result, type_ptr->clone_resized(type_ptr->size())); | ||
} | ||
return Status::OK(); | ||
} | ||
}; | ||
|
||
void register_function_non_nullable(SimpleFunctionFactory& factory) { | ||
factory.register_function<FunctionNonNullable>(); | ||
} | ||
|
||
} // 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
22 changes: 22 additions & 0 deletions
22
regression-test/data/query_p0/sql_functions/cast_function/test_non_nullable_function.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,22 @@ | ||
-- This file is automatically generated. You should know what you did if you want to edit this | ||
-- !nullable -- | ||
\N 0 | ||
1 1 | ||
2 2 | ||
3 3 | ||
4 4 | ||
|
||
-- !nullable -- | ||
\N [] | ||
1 [1, 2, 3] | ||
2 [] | ||
3 [1, 2, 3] | ||
4 [] | ||
|
||
-- !nullable -- | ||
\N [] | ||
1 ['a', 'b', 'c'] | ||
2 [] | ||
3 [] | ||
4 ['a', 'b', 'c'] | ||
|
50 changes: 50 additions & 0 deletions
50
...ession-test/suites/query_p0/sql_functions/cast_function/test_non_nullable_function.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,50 @@ | ||
// 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_non_nullable_function", "query") { | ||
def tableName = "tbl_test_non_nullable_function" | ||
|
||
sql """DROP TABLE IF EXISTS ${tableName}""" | ||
sql """ | ||
CREATE TABLE IF NOT EXISTS ${tableName} ( | ||
`k1` int(11) NULL COMMENT "", | ||
`k2` ARRAY<int(11)> NULL COMMENT "", | ||
`k3` ARRAY<VARCHAR(11)> NULL COMMENT "", | ||
`k4` ARRAY<decimal(27,9)> NOT 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 ${tableName} VALUES(1, [1, 2, 3], ["a", "b", "c"], [1.3, 2.14]) """ | ||
sql """ INSERT INTO ${tableName} VALUES(2, [], [], [1.3, 2.14]) """ | ||
sql """ INSERT INTO ${tableName} VALUES(3, [1, 2, 3], [], [1.3, 2.14]) """ | ||
sql """ INSERT INTO ${tableName} VALUES(4, [], ["a", "b", "c"], [1.3, 2.14]) """ | ||
sql """ INSERT INTO ${tableName} VALUES(null, null, null, [1.1,2.2,3.3]) """ | ||
|
||
qt_nullable "SELECT k1, non_nullable(k1) FROM ${tableName} ORDER BY k1" | ||
qt_nullable "SELECT k1, non_nullable(k2) FROM ${tableName} ORDER BY k1" | ||
qt_nullable "SELECT k1, non_nullable(k3) FROM ${tableName} ORDER BY k1" | ||
try { | ||
def result = "SELECT k1, non_nullable(k4) FROM ${tableName} ORDER BY k1" | ||
} catch (Exception e) { | ||
assertTrue(e.getMessage().contains("Try to use originally non-nullable column")) | ||
} | ||
} |
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