2023-10-04 13:11:15 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2023-10-24 14:10:43 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2023-11-03 22:48:52 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2023-10-04 13:11:15 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Parsers/ParserQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2023-11-03 19:36:18 +00:00
|
|
|
enum class OutputFormatting
|
|
|
|
{
|
|
|
|
SingleLine,
|
|
|
|
MultiLine
|
|
|
|
};
|
|
|
|
|
|
|
|
template <OutputFormatting output_formatting, typename Name>
|
2023-10-04 13:11:15 +00:00
|
|
|
class FunctionFormatQuery : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2023-10-25 08:10:04 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2023-10-04 13:11:15 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
|
|
|
{
|
|
|
|
const auto & settings = context->getSettings();
|
|
|
|
return std::make_shared<FunctionFormatQuery>(settings.max_query_size, settings.max_parser_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionFormatQuery(size_t max_query_size_, size_t max_parser_depth_)
|
2023-11-03 19:36:18 +00:00
|
|
|
: max_query_size(max_query_size_)
|
|
|
|
, max_parser_depth(max_parser_depth_)
|
2023-10-04 13:11:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2023-11-03 19:36:18 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2023-10-04 13:11:15 +00:00
|
|
|
|
2023-10-24 14:10:43 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
2023-10-04 13:11:15 +00:00
|
|
|
{
|
2023-11-03 19:36:18 +00:00
|
|
|
FunctionArgumentDescriptors args{
|
|
|
|
{"query", &isString<IDataType>, nullptr, "String"}
|
|
|
|
};
|
|
|
|
validateFunctionArgumentTypes(*this, arguments, args);
|
|
|
|
|
2023-10-24 14:10:43 +00:00
|
|
|
return arguments[0].type;
|
2023-10-04 13:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
|
|
|
{
|
2023-11-03 19:36:18 +00:00
|
|
|
const ColumnPtr col_query = arguments[0].column;
|
|
|
|
if (const ColumnString * col_query_string = checkAndGetColumn<ColumnString>(col_query.get()))
|
2023-10-04 13:11:15 +00:00
|
|
|
{
|
|
|
|
auto col_res = ColumnString::create();
|
2023-11-03 19:36:18 +00:00
|
|
|
formatVector(col_query_string->getChars(), col_query_string->getOffsets(), col_res->getChars(), col_res->getOffsets());
|
2023-10-04 13:11:15 +00:00
|
|
|
return col_res;
|
|
|
|
}
|
|
|
|
else
|
2023-11-03 19:36:18 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}", col_query->getName(), getName());
|
2023-10-04 13:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void formatVector(
|
|
|
|
const ColumnString::Chars & data,
|
|
|
|
const ColumnString::Offsets & offsets,
|
|
|
|
ColumnString::Chars & res_data,
|
|
|
|
ColumnString::Offsets & res_offsets) const
|
|
|
|
{
|
|
|
|
const size_t size = offsets.size();
|
|
|
|
res_offsets.resize(size);
|
2023-11-03 22:48:52 +00:00
|
|
|
res_data.resize(data.size());
|
2023-10-04 13:11:15 +00:00
|
|
|
|
2023-11-03 19:36:18 +00:00
|
|
|
size_t prev_offset = 0;
|
2023-11-03 22:48:52 +00:00
|
|
|
size_t res_data_size = 0;
|
|
|
|
|
2023-10-04 13:11:15 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2023-11-03 19:36:18 +00:00
|
|
|
const char * begin = reinterpret_cast<const char *>(&data[prev_offset]);
|
2023-11-03 22:48:52 +00:00
|
|
|
const char * end = begin + offsets[i] - prev_offset - 1;
|
2023-11-03 19:36:18 +00:00
|
|
|
|
|
|
|
ParserQuery parser(end);
|
|
|
|
auto ast = parseQuery(parser, begin, end, /*query_description*/ {}, max_query_size, max_parser_depth);
|
2023-11-03 22:48:52 +00:00
|
|
|
|
|
|
|
WriteBufferFromOwnString buf;
|
2023-11-03 19:36:18 +00:00
|
|
|
formatAST(*ast, buf, /*hilite*/ false, /*single_line*/ output_formatting == OutputFormatting::SingleLine);
|
2023-11-03 22:48:52 +00:00
|
|
|
auto formatted = buf.stringView();
|
|
|
|
|
|
|
|
const size_t res_data_new_size = res_data_size + formatted.size() + 1;
|
|
|
|
if (res_data_new_size > res_data.size())
|
|
|
|
res_data.resize(2 * res_data_new_size);
|
2023-11-03 19:36:18 +00:00
|
|
|
|
2023-11-03 22:48:52 +00:00
|
|
|
memcpy(&res_data[res_data_size], formatted.begin(), formatted.size());
|
|
|
|
res_data_size += formatted.size();
|
|
|
|
|
|
|
|
res_data[res_data_size] = '\0';
|
|
|
|
res_data_size += 1;
|
|
|
|
|
|
|
|
res_offsets[i] = res_data_size;
|
2023-11-03 19:36:18 +00:00
|
|
|
prev_offset = offsets[i];
|
2023-10-04 13:11:15 +00:00
|
|
|
}
|
2023-11-03 22:48:52 +00:00
|
|
|
|
|
|
|
res_data.resize(res_data_size);
|
2023-10-04 13:11:15 +00:00
|
|
|
}
|
2023-11-03 19:36:18 +00:00
|
|
|
|
|
|
|
const size_t max_query_size;
|
|
|
|
const size_t max_parser_depth;
|
2023-10-04 13:11:15 +00:00
|
|
|
};
|
|
|
|
|
2023-10-25 08:10:04 +00:00
|
|
|
struct NameFormatQuery
|
|
|
|
{
|
|
|
|
static constexpr auto name = "formatQuery";
|
|
|
|
};
|
|
|
|
|
2023-10-26 11:09:20 +00:00
|
|
|
struct NameFormatQuerySingleLine
|
2023-10-25 08:10:04 +00:00
|
|
|
{
|
2023-10-26 11:09:20 +00:00
|
|
|
static constexpr auto name = "formatQuerySingleLine";
|
2023-10-25 08:10:04 +00:00
|
|
|
};
|
2023-10-04 13:11:15 +00:00
|
|
|
|
|
|
|
REGISTER_FUNCTION(formatQuery)
|
|
|
|
{
|
2023-11-03 19:36:18 +00:00
|
|
|
factory.registerFunction<FunctionFormatQuery<OutputFormatting::MultiLine, NameFormatQuery>>(FunctionDocumentation{
|
2023-10-26 11:08:11 +00:00
|
|
|
.description = "Returns a formatted, possibly multi-line, version of the given SQL query.\n[example:multiline]",
|
2023-10-04 13:11:15 +00:00
|
|
|
.syntax = "formatQuery(query)",
|
|
|
|
.arguments = {{"query", "The SQL query to be formatted. [String](../../sql-reference/data-types/string.md)"}},
|
|
|
|
.returned_value = "The formatted query. [String](../../sql-reference/data-types/string.md).",
|
2023-10-25 08:10:04 +00:00
|
|
|
.examples{
|
|
|
|
{"multiline",
|
|
|
|
"SELECT formatQuery('select a, b FRom tab WHERE a > 3 and b < 3');",
|
|
|
|
"SELECT\n"
|
|
|
|
" a,\n"
|
|
|
|
" b\n"
|
|
|
|
"FROM tab\n"
|
|
|
|
"WHERE (a > 3) AND (b < 3)"}},
|
|
|
|
.categories{"Other"}});
|
|
|
|
}
|
|
|
|
|
2023-10-26 11:09:20 +00:00
|
|
|
REGISTER_FUNCTION(formatQuerySingleLine)
|
2023-10-25 08:10:04 +00:00
|
|
|
{
|
2023-11-03 19:36:18 +00:00
|
|
|
factory.registerFunction<FunctionFormatQuery<OutputFormatting::SingleLine, NameFormatQuerySingleLine>>(FunctionDocumentation{
|
2023-10-26 11:08:11 +00:00
|
|
|
.description = "Like formatQuery() but the returned formatted string contains no line breaks.\n[example:multiline]",
|
2023-10-26 11:09:20 +00:00
|
|
|
.syntax = "formatQuerySingleLine(query)",
|
2023-10-25 08:10:04 +00:00
|
|
|
.arguments = {{"query", "The SQL query to be formatted. [String](../../sql-reference/data-types/string.md)"}},
|
|
|
|
.returned_value = "The formatted query. [String](../../sql-reference/data-types/string.md).",
|
|
|
|
.examples{
|
|
|
|
{"multiline",
|
2023-10-26 11:09:20 +00:00
|
|
|
"SELECT formatQuerySingleLine('select a, b FRom tab WHERE a > 3 and b < 3');",
|
2023-10-25 08:10:04 +00:00
|
|
|
"SELECT a, b FROM tab WHERE (a > 3) AND (b < 3)"}},
|
2023-10-24 14:13:15 +00:00
|
|
|
.categories{"Other"}});
|
2023-10-04 13:11:15 +00:00
|
|
|
}
|
|
|
|
}
|