aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp
diff options
context:
space:
mode:
authorvitalyisaev <vitalyisaev@ydb.tech>2023-11-14 09:58:56 +0300
committervitalyisaev <vitalyisaev@ydb.tech>2023-11-14 10:20:20 +0300
commitc2b2dfd9827a400a8495e172a56343462e3ceb82 (patch)
treecd4e4f597d01bede4c82dffeb2d780d0a9046bd0 /contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp
parentd4ae8f119e67808cb0cf776ba6e0cf95296f2df7 (diff)
downloadydb-c2b2dfd9827a400a8495e172a56343462e3ceb82.tar.gz
YQ Connector: move tests from yql to ydb (OSS)
Перенос папки с тестами на Коннектор из папки yql в папку ydb (синхронизируется с github).
Diffstat (limited to 'contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp')
-rw-r--r--contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp b/contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp
new file mode 100644
index 0000000000..bbbca00937
--- /dev/null
+++ b/contrib/clickhouse/src/Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.cpp
@@ -0,0 +1,87 @@
+#include <memory>
+#include <string>
+#include <DataTypes/DataTypeArray.h>
+#include <DataTypes/DataTypeNullable.h>
+#include <DataTypes/DataTypeString.h>
+#include <DataTypes/DataTypesNumber.h>
+#include <DataTypes/Serializations/ISerialization.h>
+#include <IO/ReadHelpers.h>
+#include <Storages/MeiliSearch/MeiliSearchColumnDescriptionFetcher.h>
+#include <base/JSON.h>
+#include <base/types.h>
+
+namespace DB
+{
+namespace ErrorCodes
+{
+ extern const int CANNOT_READ_ALL_DATA;
+ extern const int MEILISEARCH_EXCEPTION;
+}
+
+MeiliSearchColumnDescriptionFetcher::MeiliSearchColumnDescriptionFetcher(const MeiliSearchConfiguration & config) : connection(config)
+{
+}
+
+void MeiliSearchColumnDescriptionFetcher::addParam(const String & key, const String & val)
+{
+ query_params[key] = val;
+}
+
+bool checkIfInteger(const String & s)
+{
+ return s.find('.') == String::npos;
+}
+
+DataTypePtr parseTypeOfField(JSON ptr)
+{
+ if (ptr.isString())
+ {
+ return std::make_shared<DataTypeString>();
+ }
+ if (ptr.isArray())
+ {
+ auto nested_type = parseTypeOfField(ptr.begin());
+ return std::make_shared<DataTypeArray>(nested_type);
+ }
+ if (ptr.isBool())
+ {
+ return std::make_shared<DataTypeUInt8>();
+ }
+ if (ptr.isNull())
+ {
+ DataTypePtr res = std::make_shared<DataTypeNullable>(res);
+ return res;
+ }
+ if (ptr.isNumber())
+ {
+ if (checkIfInteger(ptr.toString()))
+ {
+ return std::make_shared<DataTypeInt64>();
+ }
+ return std::make_shared<DataTypeFloat64>();
+ }
+ return std::make_shared<DataTypeString>();
+}
+
+ColumnsDescription MeiliSearchColumnDescriptionFetcher::fetchColumnsDescription() const
+{
+ auto response = connection.searchQuery(query_params);
+ JSON jres = JSON(response).begin();
+
+ if (jres.getName() == "message")
+ throw Exception::createRuntime(ErrorCodes::MEILISEARCH_EXCEPTION, jres.getValue().toString());
+
+ NamesAndTypesList list;
+
+ for (const JSON kv_pair : jres.getValue().begin())
+ {
+ if (!kv_pair.isNameValuePair())
+ throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Bad response data");
+
+ list.emplace_back(kv_pair.getName(), parseTypeOfField(kv_pair.getValue()));
+ }
+
+ return ColumnsDescription(list);
+}
+
+}