summaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/toJSONString.cpp
diff options
context:
space:
mode:
authorvitalyisaev <[email protected]>2023-11-14 09:58:56 +0300
committervitalyisaev <[email protected]>2023-11-14 10:20:20 +0300
commitc2b2dfd9827a400a8495e172a56343462e3ceb82 (patch)
treecd4e4f597d01bede4c82dffeb2d780d0a9046bd0 /contrib/clickhouse/src/Functions/toJSONString.cpp
parentd4ae8f119e67808cb0cf776ba6e0cf95296f2df7 (diff)
YQ Connector: move tests from yql to ydb (OSS)
Перенос папки с тестами на Коннектор из папки yql в папку ydb (синхронизируется с github).
Diffstat (limited to 'contrib/clickhouse/src/Functions/toJSONString.cpp')
-rw-r--r--contrib/clickhouse/src/Functions/toJSONString.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/contrib/clickhouse/src/Functions/toJSONString.cpp b/contrib/clickhouse/src/Functions/toJSONString.cpp
new file mode 100644
index 00000000000..de82d7cb50a
--- /dev/null
+++ b/contrib/clickhouse/src/Functions/toJSONString.cpp
@@ -0,0 +1,63 @@
+#include <Columns/ColumnString.h>
+#include <DataTypes/DataTypeString.h>
+#include <Functions/FunctionFactory.h>
+#include <Functions/IFunction.h>
+#include <Formats/FormatFactory.h>
+#include <IO/WriteBufferFromVector.h>
+#include <IO/WriteHelpers.h>
+
+namespace DB
+{
+namespace
+{
+ class FunctionToJSONString : public IFunction
+ {
+ public:
+ static constexpr auto name = "toJSONString";
+ static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionToJSONString>(context); }
+
+ explicit FunctionToJSONString(ContextPtr context) : format_settings(getFormatSettings(context)) {}
+
+ String getName() const override { return name; }
+
+ size_t getNumberOfArguments() const override { return 1; }
+
+ DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName &) const override { return std::make_shared<DataTypeString>(); }
+
+ bool useDefaultImplementationForConstants() const override { return true; }
+
+ bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
+
+ ColumnPtr
+ executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
+ {
+ auto res = ColumnString::create();
+ ColumnString::Chars & data_to = res->getChars();
+ ColumnString::Offsets & offsets_to = res->getOffsets();
+ offsets_to.resize(input_rows_count);
+
+ auto serializer = arguments[0].type->getDefaultSerialization();
+ WriteBufferFromVector<ColumnString::Chars> json(data_to);
+ for (size_t i = 0; i < input_rows_count; ++i)
+ {
+ serializer->serializeTextJSON(*arguments[0].column, i, json, format_settings);
+ writeChar(0, json);
+ offsets_to[i] = json.count();
+ }
+
+ json.finalize();
+ return res;
+ }
+
+ private:
+ /// Affects only subset of part of settings related to json.
+ const FormatSettings format_settings;
+ };
+}
+
+REGISTER_FUNCTION(ToJSONString)
+{
+ factory.registerFunction<FunctionToJSONString>();
+}
+
+}