Files
doris/be/src/vec/functions/function_fake.h
camby c5718928df [feature-wip](array-type) support explode and explode_outer table function (#8766)
explode(ArrayColumn) desc:
> Create a row for each element in the array column. 

explode_outer(ArrayColumn) desc:
> Create a row for each element in the array column. Unlike explode, if the array is null or empty, it returns null.

Usage example:
1. create a table with array column, and insert some data;
2. open enable_lateral_view and enable_vectorized_engine;
```
set enable_lateral_view = true;
set enable_vectorized_engine=true;
```
3. use explode_outer
```
> select * from array_test;
+------+------+--------+
| k1   | k2   | k3     |
+------+------+--------+
|    3 | NULL | NULL   |
|    1 |    2 | [1, 2] |
|    2 |    3 | NULL   |
|    4 | NULL | []     |
+------+------+--------+

> select k1,explode_column from array_test LATERAL VIEW explode_outer(k3) TempExplodeView as explode_column;
+------+----------------+
| k1   | explode_column |
+------+----------------+
|    1 |              1 |
|    1 |              2 |
|    2 |           NULL |
|    4 |           NULL |
|    3 |           NULL |
+------+----------------+
```
4. explode usage example. explode return empty rows while the ARRAY is null or empty
```
> select k1,explode_column from array_test LATERAL VIEW explode(k3) TempExplodeView as explode_column;
+------+----------------+
| k1   | explode_column |
+------+----------------+
|    1 |              1 |
|    1 |              2 |
+------+----------------+
```
2022-04-08 12:11:04 +08:00

125 lines
4.4 KiB
C++

// 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.
#pragma once
#include "common/status.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type_array.h"
#include "vec/data_types/data_type_number.h"
#include "vec/data_types/data_type_string.h"
#include "vec/functions/function_helpers.h"
#include "vec/functions/simple_function_factory.h"
#include "vec/utils/util.hpp"
namespace doris::vectorized {
struct FunctionEsqueryImpl {
static constexpr auto name = "esquery";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeUInt8>();
}
};
struct FunctionExplodeSplitImpl {
static constexpr auto name = "explode_split";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeString>();
}
};
struct FunctionExplodeNumbersImpl {
static constexpr auto name = "explode_numbers";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeInt32>();
}
};
struct FunctionExplodeJsonArrayIntImpl {
static constexpr auto name = "explode_json_array_int";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeInt64>();
}
};
struct FunctionExplodeJsonArrayStringImpl {
static constexpr auto name = "explode_json_array_string";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeString>();
}
};
struct FunctionExplodeJsonArrayDoubleImpl {
static constexpr auto name = "explode_json_array_double";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeFloat64>();
}
};
struct FunctionExplodeBitmapImpl {
static constexpr auto name = "explode_bitmap";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeInt64>();
}
};
struct FunctionExplodeImpl {
static constexpr auto name = "explode";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DCHECK(is_array(arguments[0])) << arguments[0]->get_name() << " not supported";
return make_nullable(
check_and_get_data_type<DataTypeArray>(arguments[0].get())->get_nested_type());
}
};
struct FunctionExplodeOuterImpl {
static constexpr auto name = "explode_outer";
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DCHECK(is_array(arguments[0])) << arguments[0]->get_name() << " not supported";
return make_nullable(
check_and_get_data_type<DataTypeArray>(arguments[0].get())->get_nested_type());
}
};
//FunctionFake is use for some function call expr only work at prepare/open phase, do not support execute().
template <typename Impl>
class FunctionFake : public IFunction {
public:
static constexpr auto name = Impl::name;
static FunctionPtr create() { return std::make_shared<FunctionFake>(); }
String get_name() const override { return name; }
size_t get_number_of_arguments() const override { return 0; }
bool is_variadic() const override { return true; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return Impl::get_return_type_impl(arguments);
}
bool use_default_implementation_for_nulls() const override { return true; }
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) override {
return Status::NotSupported(fmt::format("Fake function {} do not support execute", name));
}
};
} // namespace doris::vectorized