2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2019-01-18 15:44:53 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2019-07-31 22:37:41 +00:00
|
|
|
#include <filesystem>
|
2019-01-18 15:44:53 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2019-01-18 15:44:53 +00:00
|
|
|
|
|
|
|
struct FilesystemAvailable
|
|
|
|
{
|
|
|
|
static constexpr auto name = "filesystemAvailable";
|
2020-07-21 13:58:07 +00:00
|
|
|
static std::uintmax_t get(const std::filesystem::space_info & spaceinfo) { return spaceinfo.available; }
|
2019-01-18 15:44:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FilesystemFree
|
|
|
|
{
|
|
|
|
static constexpr auto name = "filesystemFree";
|
2020-07-21 13:58:07 +00:00
|
|
|
static std::uintmax_t get(const std::filesystem::space_info & spaceinfo) { return spaceinfo.free; }
|
2019-01-18 15:44:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FilesystemCapacity
|
|
|
|
{
|
|
|
|
static constexpr auto name = "filesystemCapacity";
|
2020-07-21 13:58:07 +00:00
|
|
|
static std::uintmax_t get(const std::filesystem::space_info & spaceinfo) { return spaceinfo.capacity; }
|
2019-01-18 15:44:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
class FilesystemImpl : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Impl::name;
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2019-01-18 15:44:53 +00:00
|
|
|
{
|
2022-06-25 04:10:50 +00:00
|
|
|
return std::make_shared<FilesystemImpl<Impl>>(std::filesystem::space(context->getPath()));
|
2019-01-18 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override
|
2021-04-29 14:48:26 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:37:41 +00:00
|
|
|
explicit FilesystemImpl(std::filesystem::space_info spaceinfo_) : spaceinfo(spaceinfo_) { }
|
2019-01-18 15:44:53 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
2019-01-18 15:44:53 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
return DataTypeUInt64().createColumnConst(input_rows_count, static_cast<UInt64>(Impl::get(spaceinfo)));
|
2019-01-18 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-31 22:37:41 +00:00
|
|
|
std::filesystem::space_info spaceinfo;
|
2019-01-18 15:44:53 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2019-01-18 15:44:53 +00:00
|
|
|
|
|
|
|
void registerFunctionFilesystem(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FilesystemImpl<FilesystemAvailable>>();
|
|
|
|
factory.registerFunction<FilesystemImpl<FilesystemCapacity>>();
|
|
|
|
factory.registerFunction<FilesystemImpl<FilesystemFree>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|