2018-04-02 11:43:37 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFile.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2018-04-06 12:10:22 +00:00
|
|
|
#include <Storages/StorageFile.h>
|
2018-04-02 13:15:25 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2018-04-02 11:43:37 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2018-04-02 13:56:54 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2018-04-02 11:43:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2018-04-06 12:10:22 +00:00
|
|
|
extern const int DATABASE_ACCESS_DENIED;
|
2018-04-02 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr TableFunctionFile::executeImpl(const ASTPtr & ast_function, const Context & context) const
|
|
|
|
{
|
2018-04-06 12:10:22 +00:00
|
|
|
// Parse args
|
2018-04-02 11:43:37 +00:00
|
|
|
ASTs & args_func = typeid_cast<ASTFunction &>(*ast_function).children;
|
|
|
|
|
2018-04-02 14:23:53 +00:00
|
|
|
if (args_func.size() != 1)
|
2018-04-19 04:46:29 +00:00
|
|
|
throw Exception("Table function '" + getName() + "' must have arguments.", ErrorCodes::LOGICAL_ERROR);
|
2018-04-02 11:43:37 +00:00
|
|
|
|
|
|
|
ASTs & args = typeid_cast<ASTExpressionList &>(*args_func.at(0)).children;
|
|
|
|
|
2018-04-10 07:26:33 +00:00
|
|
|
if (args.size() != 3)
|
2018-04-19 04:46:29 +00:00
|
|
|
throw Exception("Table function '" + getName() + "' requires exactly 3 arguments: path, format and structure.",
|
2018-04-02 11:43:37 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 3; ++i)
|
|
|
|
args[i] = evaluateConstantExpressionOrIdentifierAsLiteral(args[i], context);
|
|
|
|
|
2018-04-02 13:15:25 +00:00
|
|
|
std::string path = static_cast<const ASTLiteral &>(*args[0]).value.safeGet<String>();
|
|
|
|
std::string format = static_cast<const ASTLiteral &>(*args[1]).value.safeGet<String>();
|
|
|
|
std::string structure = static_cast<const ASTLiteral &>(*args[2]).value.safeGet<String>();
|
2018-04-02 15:38:43 +00:00
|
|
|
|
2018-04-02 13:15:25 +00:00
|
|
|
// Create sample block
|
2018-04-02 13:56:54 +00:00
|
|
|
std::vector<std::string> structure_vals;
|
|
|
|
boost::split(structure_vals, structure, boost::algorithm::is_any_of(" ,"), boost::algorithm::token_compress_on);
|
2018-04-02 13:15:25 +00:00
|
|
|
|
2018-04-19 04:46:29 +00:00
|
|
|
if (structure_vals.size() % 2 != 0)
|
|
|
|
throw Exception("Odd number of elements in section structure: must be a list of name type pairs", ErrorCodes::LOGICAL_ERROR);
|
2018-04-02 13:15:25 +00:00
|
|
|
|
2018-04-19 04:46:29 +00:00
|
|
|
Block sample_block;
|
2018-04-02 13:15:25 +00:00
|
|
|
const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
|
|
|
|
|
2018-04-19 04:46:29 +00:00
|
|
|
for (size_t i = 0, size = structure_vals.size(); i < size; i += 2)
|
2018-04-02 13:15:25 +00:00
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
column.name = structure_vals[i];
|
|
|
|
column.type = data_type_factory.get(structure_vals[i + 1]);
|
|
|
|
column.column = column.type->createColumn();
|
|
|
|
sample_block.insert(std::move(column));
|
|
|
|
}
|
|
|
|
|
2018-04-06 12:10:22 +00:00
|
|
|
// Create table
|
2018-04-10 07:28:57 +00:00
|
|
|
StoragePtr storage = StorageFile::create(
|
2018-04-19 05:32:09 +00:00
|
|
|
path, -1, context.getUserFilesPath(), getName(), format,
|
2018-04-10 07:28:57 +00:00
|
|
|
ColumnsDescription{sample_block.getNamesAndTypesList()}, const_cast<Context &>(context));
|
|
|
|
|
2018-04-10 07:26:33 +00:00
|
|
|
storage->startup();
|
2018-04-02 11:43:37 +00:00
|
|
|
|
2018-04-02 13:15:25 +00:00
|
|
|
return storage;
|
2018-04-02 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerTableFunctionFile(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionFile>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|