2019-07-29 13:50:13 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
|
2019-07-29 16:20:17 +00:00
|
|
|
#include <Core/Block.h>
|
2019-07-29 13:50:13 +00:00
|
|
|
#include <Storages/StorageValues.h>
|
2019-09-16 15:44:12 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2022-01-26 13:29:33 +00:00
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2019-07-29 13:50:13 +00:00
|
|
|
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2022-08-16 09:41:32 +00:00
|
|
|
#include <Interpreters/parseColumnsListForTableFunction.h>
|
2019-07-29 16:20:17 +00:00
|
|
|
|
|
|
|
#include <Interpreters/convertFieldToType.h>
|
2019-07-29 13:50:13 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2020-02-10 15:50:12 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerTableFunctions.h"
|
2019-07-31 15:36:10 +00:00
|
|
|
|
2019-07-29 16:20:17 +00:00
|
|
|
|
2019-07-29 13:50:13 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-06-06 01:23:21 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-07-29 13:50:13 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2022-01-26 13:29:33 +00:00
|
|
|
extern const int CANNOT_EXTRACT_TABLE_STRUCTURE;
|
2019-07-29 13:50:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/* values(structure, values...) - creates a temporary storage filling columns with values
|
|
|
|
* values is case-insensitive table function.
|
|
|
|
*/
|
|
|
|
class TableFunctionValues : public ITableFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "values";
|
|
|
|
std::string getName() const override { return name; }
|
|
|
|
bool hasStaticStructure() const override { return true; }
|
|
|
|
private:
|
|
|
|
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
|
|
|
|
const char * getStorageTypeName() const override { return "Values"; }
|
|
|
|
|
|
|
|
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
|
|
|
|
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
|
|
|
|
|
|
|
|
static DataTypes getTypesFromArgument(const ASTPtr & arg, ContextPtr context);
|
|
|
|
|
|
|
|
ColumnsDescription structure;
|
|
|
|
bool has_structure_in_arguments;
|
|
|
|
};
|
|
|
|
|
|
|
|
void parseAndInsertValues(MutableColumns & res_columns, const ASTs & args, const Block & sample_block, size_t start, ContextPtr context)
|
2019-07-29 16:20:17 +00:00
|
|
|
{
|
|
|
|
if (res_columns.size() == 1) /// Parsing arguments as Fields
|
|
|
|
{
|
2022-01-26 13:29:33 +00:00
|
|
|
for (size_t i = start; i < args.size(); ++i)
|
2019-07-29 16:20:17 +00:00
|
|
|
{
|
|
|
|
const auto & [value_field, value_type_ptr] = evaluateConstantExpression(args[i], context);
|
2019-09-16 15:47:13 +00:00
|
|
|
|
2020-06-24 16:37:04 +00:00
|
|
|
Field value = convertFieldToTypeOrThrow(value_field, *sample_block.getByPosition(0).type, value_type_ptr.get());
|
2019-07-29 16:20:17 +00:00
|
|
|
res_columns[0]->insert(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /// Parsing arguments as Tuples
|
|
|
|
{
|
2022-01-26 13:29:33 +00:00
|
|
|
for (size_t i = start; i < args.size(); ++i)
|
2019-07-29 16:20:17 +00:00
|
|
|
{
|
|
|
|
const auto & [value_field, value_type_ptr] = evaluateConstantExpression(args[i], context);
|
2021-01-21 12:19:00 +00:00
|
|
|
|
|
|
|
const DataTypeTuple * type_tuple = typeid_cast<const DataTypeTuple *>(value_type_ptr.get());
|
|
|
|
if (!type_tuple)
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"Table function VALUES requires all but first argument (rows specification) to be either tuples or single values");
|
|
|
|
|
2019-10-18 15:57:05 +00:00
|
|
|
const Tuple & value_tuple = value_field.safeGet<Tuple>();
|
2019-07-29 16:20:17 +00:00
|
|
|
|
|
|
|
if (value_tuple.size() != sample_block.columns())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Values size should match with number of columns");
|
2019-07-29 16:20:17 +00:00
|
|
|
|
2021-01-21 12:19:00 +00:00
|
|
|
const DataTypes & value_types_tuple = type_tuple->getElements();
|
2019-07-29 16:20:17 +00:00
|
|
|
for (size_t j = 0; j < value_tuple.size(); ++j)
|
|
|
|
{
|
2020-06-24 16:37:04 +00:00
|
|
|
Field value = convertFieldToTypeOrThrow(value_tuple[j], *sample_block.getByPosition(j).type, value_types_tuple[j].get());
|
2019-07-29 16:20:17 +00:00
|
|
|
res_columns[j]->insert(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 13:29:33 +00:00
|
|
|
DataTypes TableFunctionValues::getTypesFromArgument(const ASTPtr & arg, ContextPtr context)
|
|
|
|
{
|
|
|
|
const auto & [value_field, value_type_ptr] = evaluateConstantExpression(arg, context);
|
|
|
|
DataTypes types;
|
|
|
|
if (const DataTypeTuple * type_tuple = typeid_cast<const DataTypeTuple *>(value_type_ptr.get()))
|
|
|
|
return type_tuple->getElements();
|
|
|
|
|
|
|
|
return {value_type_ptr};
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableFunctionValues::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
2019-07-29 13:50:13 +00:00
|
|
|
{
|
|
|
|
ASTs & args_func = ast_function->children;
|
|
|
|
|
|
|
|
if (args_func.size() != 1)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function '{}' must have arguments", getName());
|
2019-07-29 13:50:13 +00:00
|
|
|
|
|
|
|
ASTs & args = args_func.at(0)->children;
|
|
|
|
|
2022-02-07 13:17:01 +00:00
|
|
|
if (args.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires at least 1 argument", getName());
|
2022-02-07 13:17:01 +00:00
|
|
|
|
2022-01-26 13:29:33 +00:00
|
|
|
const auto & literal = args[0]->as<const ASTLiteral>();
|
|
|
|
String value;
|
2023-02-27 15:20:56 +00:00
|
|
|
String error;
|
|
|
|
if (args.size() > 1 && literal && literal->value.tryGet(value) && tryParseColumnsListFromString(value, structure, context, error))
|
2022-01-26 13:29:33 +00:00
|
|
|
{
|
|
|
|
has_structure_in_arguments = true;
|
|
|
|
return;
|
|
|
|
}
|
2019-07-29 13:50:13 +00:00
|
|
|
|
2022-01-26 13:29:33 +00:00
|
|
|
has_structure_in_arguments = false;
|
|
|
|
DataTypes data_types = getTypesFromArgument(args[0], context);
|
|
|
|
for (size_t i = 1; i < args.size(); ++i)
|
2020-06-05 20:45:15 +00:00
|
|
|
{
|
2022-01-26 13:29:33 +00:00
|
|
|
auto arg_types = getTypesFromArgument(args[i], context);
|
|
|
|
if (data_types.size() != arg_types.size())
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::CANNOT_EXTRACT_TABLE_STRUCTURE,
|
|
|
|
"Cannot determine common structure for {} function arguments: the amount of columns is differ for different arguments",
|
|
|
|
getName());
|
|
|
|
for (size_t j = 0; j != arg_types.size(); ++j)
|
2022-02-09 20:47:53 +00:00
|
|
|
data_types[j] = getLeastSupertype(DataTypes{data_types[j], arg_types[j]});
|
2020-06-05 20:45:15 +00:00
|
|
|
}
|
2019-07-29 13:50:13 +00:00
|
|
|
|
2022-01-26 13:29:33 +00:00
|
|
|
NamesAndTypesList names_and_types;
|
|
|
|
for (size_t i = 0; i != data_types.size(); ++i)
|
|
|
|
names_and_types.emplace_back("c" + std::to_string(i + 1), data_types[i]);
|
|
|
|
structure = ColumnsDescription(names_and_types);
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
ColumnsDescription TableFunctionValues::getActualTableStructure(ContextPtr /*context*/, bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2022-01-26 13:29:33 +00:00
|
|
|
return structure;
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
StoragePtr TableFunctionValues::executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool is_insert_query) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2023-07-06 08:56:07 +00:00
|
|
|
auto columns = getActualTableStructure(context, is_insert_query);
|
2019-08-01 01:53:38 +00:00
|
|
|
|
2019-07-31 14:06:22 +00:00
|
|
|
Block sample_block;
|
2020-08-31 17:45:21 +00:00
|
|
|
for (const auto & name_type : columns.getOrdinary())
|
2019-08-01 01:53:38 +00:00
|
|
|
sample_block.insert({ name_type.type->createColumn(), name_type.type, name_type.name });
|
2019-07-29 13:50:13 +00:00
|
|
|
|
|
|
|
MutableColumns res_columns = sample_block.cloneEmptyColumns();
|
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
ASTs & args = ast_function->children.at(0)->children;
|
|
|
|
|
2019-07-31 15:36:10 +00:00
|
|
|
/// Parsing other arguments as values and inserting them into columns
|
2022-01-26 13:29:33 +00:00
|
|
|
parseAndInsertValues(res_columns, args, sample_block, has_structure_in_arguments ? 1 : 0, context);
|
2019-07-29 13:50:13 +00:00
|
|
|
|
|
|
|
Block res_block = sample_block.cloneWithColumns(std::move(res_columns));
|
|
|
|
|
2022-04-19 20:47:29 +00:00
|
|
|
auto res = std::make_shared<StorageValues>(StorageID(getDatabaseName(), table_name), columns, res_block);
|
2019-07-29 13:50:13 +00:00
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 13:50:13 +00:00
|
|
|
void registerTableFunctionValues(TableFunctionFactory & factory)
|
|
|
|
{
|
2022-11-03 11:31:07 +00:00
|
|
|
factory.registerFunction<TableFunctionValues>({.documentation = {}, .allow_readonly = true}, TableFunctionFactory::CaseInsensitive);
|
2019-07-29 13:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|