2021-01-13 16:46:55 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/IColumn.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionStringToString.h>
|
|
|
|
#include <common/find_symbols.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int TOO_LARGE_STRING_SIZE;
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
2021-01-14 10:44:16 +00:00
|
|
|
extern const int FILE_DOESNT_EXIST;
|
|
|
|
extern const int CANNOT_OPEN_FILE;
|
|
|
|
extern const int CANNOT_CLOSE_FILE;
|
|
|
|
extern const int CANNOT_FSTAT;
|
|
|
|
extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
|
2021-01-14 12:09:13 +00:00
|
|
|
extern const int CANNOT_CLOSE_FILE;
|
2021-01-13 16:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-14 10:44:16 +00:00
|
|
|
/** A function to read file as a string.
|
2021-01-13 16:46:55 +00:00
|
|
|
*/
|
2021-01-14 10:44:16 +00:00
|
|
|
class FunctionFile : public IFunction
|
2021-01-13 16:46:55 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "file";
|
2021-01-14 10:44:16 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionFile>(); }
|
|
|
|
static FunctionPtr create() { return std::make_shared<FunctionFile>(); }
|
2021-01-13 16:46:55 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2021-01-14 10:44:16 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return true; }
|
2021-01-13 16:46:55 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isStringOrFixedString(arguments[0].type))
|
|
|
|
throw Exception(getName() + " is only implemented for types String and FixedString", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-01-14 10:44:16 +00:00
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2021-01-14 05:36:22 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2021-01-13 16:46:55 +00:00
|
|
|
{
|
|
|
|
const auto & column = arguments[0].column;
|
|
|
|
const char * filename = nullptr;
|
2021-01-14 10:44:16 +00:00
|
|
|
|
2021-01-13 16:46:55 +00:00
|
|
|
if (const auto * column_string = checkAndGetColumn<ColumnString>(column.get()))
|
|
|
|
{
|
|
|
|
const auto & filename_chars = column_string->getChars();
|
|
|
|
filename = reinterpret_cast<const char *>(&filename_chars[0]);
|
|
|
|
|
|
|
|
auto fd = open(filename, O_RDONLY);
|
2021-01-14 10:44:16 +00:00
|
|
|
if (-1 == fd)
|
|
|
|
throwFromErrnoWithPath("Cannot open file " + std::string(filename), std::string(filename),
|
|
|
|
errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
|
2021-01-13 16:46:55 +00:00
|
|
|
struct stat file_stat;
|
2021-01-14 10:44:16 +00:00
|
|
|
if (-1 == fstat(fd, &file_stat))
|
|
|
|
throwFromErrnoWithPath("Cannot stat file " + std::string(filename), std::string(filename),
|
|
|
|
ErrorCodes::CANNOT_FSTAT);
|
|
|
|
|
2021-01-13 16:46:55 +00:00
|
|
|
auto file_length = static_cast<uint64_t>(file_stat.st_size);
|
|
|
|
auto res = ColumnString::create();
|
|
|
|
auto & res_chars = res->getChars();
|
|
|
|
auto & res_offsets = res->getOffsets();
|
|
|
|
res_chars.resize_exact(file_length + 1);
|
|
|
|
res_offsets.push_back(file_length + 1);
|
2021-01-14 10:44:16 +00:00
|
|
|
char * res_buf = reinterpret_cast<char *>(&res_chars[0]);
|
2021-01-13 16:46:55 +00:00
|
|
|
|
2021-01-14 10:44:16 +00:00
|
|
|
//To read directly into the String buf, avoiding one redundant copy
|
|
|
|
ssize_t bytes_read = pread(fd, res_buf, file_length, 0);
|
2021-01-14 11:46:19 +00:00
|
|
|
if (-1 == bytes_read)
|
2021-01-14 10:44:16 +00:00
|
|
|
throwFromErrnoWithPath("Read failed for " + std::string(filename), std::string(filename),
|
|
|
|
errno == EBADF ? ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR : ErrorCodes::ILLEGAL_COLUMN);
|
2021-01-13 16:46:55 +00:00
|
|
|
if (static_cast<uint64_t>(bytes_read) != file_length)
|
2021-01-14 10:44:16 +00:00
|
|
|
throwFromErrnoWithPath("Cannot read all bytes from " + std::string(filename), std::string(filename), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
res_buf[file_length] = '\0';
|
2021-01-14 12:09:13 +00:00
|
|
|
if (0 != close(fd))
|
|
|
|
throw Exception("Cannot close file " + std::string(filename), ErrorCodes::CANNOT_CLOSE_FILE);
|
|
|
|
fd = -1;
|
|
|
|
|
2021-01-13 16:46:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception("Bad Function arguments for file() " + std::string(filename), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionFromFile(FunctionFactory & factory)
|
|
|
|
{
|
2021-01-14 10:44:16 +00:00
|
|
|
factory.registerFunction<FunctionFile>();
|
2021-01-13 16:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|