diff options
author | Tony-Romanov <150126326+Tony-Romanov@users.noreply.github.com> | 2024-08-09 14:39:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-09 15:39:23 +0300 |
commit | f71909ab5367f38e7c6c55fc2b4a2beaa3891218 (patch) | |
tree | 3befa067e04416a9401777c21a1b87bff7eaa409 | |
parent | 48718ab5201672889c3430b1a0c85910af085329 (diff) | |
download | ydb-f71909ab5367f38e7c6c55fc2b4a2beaa3891218.tar.gz |
Revert "Remove old Json UDF (used only in SQL v0)." (#7612)
-rw-r--r-- | ydb/apps/ydbd/ya.make | 1 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/json/json_udf.cpp | 120 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/json/test/canondata/result.json | 7 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/json/test/canondata/test.test_Basic_/results.txt | 57 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/json/test/cases/Basic.sql | 12 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/json/test/ya.make | 13 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/json/ya.make | 30 | ||||
-rw-r--r-- | ydb/library/yql/udfs/common/ya.make | 1 | ||||
-rw-r--r-- | ydb/tools/query_replay/common_deps.inc | 1 | ||||
-rw-r--r-- | ydb/tools/query_replay_yt/common_deps.inc | 1 |
10 files changed, 243 insertions, 0 deletions
diff --git a/ydb/apps/ydbd/ya.make b/ydb/apps/ydbd/ya.make index 5d7bbcdd74..12d685e849 100644 --- a/ydb/apps/ydbd/ya.make +++ b/ydb/apps/ydbd/ya.make @@ -53,6 +53,7 @@ PEERDIR( ydb/library/yql/udfs/common/hyperloglog ydb/library/yql/udfs/common/ip_base ydb/library/yql/udfs/common/knn + ydb/library/yql/udfs/common/json ydb/library/yql/udfs/common/json2 ydb/library/yql/udfs/common/math ydb/library/yql/udfs/common/pire diff --git a/ydb/library/yql/udfs/common/json/json_udf.cpp b/ydb/library/yql/udfs/common/json/json_udf.cpp new file mode 100644 index 0000000000..49ffec496c --- /dev/null +++ b/ydb/library/yql/udfs/common/json/json_udf.cpp @@ -0,0 +1,120 @@ +#include <ydb/library/yql/public/udf/udf_helpers.h> + +#include <library/cpp/json/easy_parse/json_easy_parser.h> + +using namespace NKikimr; +using namespace NUdf; + +namespace { + class TGetField: public TBoxedValue { + public: + typedef bool TTypeAwareMarker; + + public: + static TStringRef Name() { + return TStringRef::Of("GetField"); + } + + TUnboxedValue Run( + const IValueBuilder* valueBuilder, + const TUnboxedValuePod* args) const override { + if (!args[0]) { + return valueBuilder->NewEmptyList(); + } + + const TString json(args[0].AsStringRef()); + const TString field(args[1].AsStringRef()); + + if (field.empty()) { + return valueBuilder->NewEmptyList(); + } + + NJson::TJsonParser parser; + parser.AddField(field, false); + + TVector<TString> result; + parser.Parse(json, &result); + + TUnboxedValue* items = nullptr; + const auto list = valueBuilder->NewArray(result.size(), items); + for (const TString& item : result) { + *items++ = valueBuilder->NewString(item); + } + + return list; + } + + static bool DeclareSignature( + const TStringRef& name, + TType* userType, + IFunctionTypeInfoBuilder& builder, + bool typesOnly) { + if (Name() == name) { + bool useString = true; + bool isOptional = true; + if (userType) { + // support of an overload with Json/Json? input type + auto typeHelper = builder.TypeInfoHelper(); + auto userTypeInspector = TTupleTypeInspector(*typeHelper, userType); + if (!userTypeInspector || userTypeInspector.GetElementsCount() < 1) { + builder.SetError("Missing or invalid user type."); + return true; + } + + auto argsTypeTuple = userTypeInspector.GetElementType(0); + auto argsTypeInspector = TTupleTypeInspector(*typeHelper, argsTypeTuple); + if (!argsTypeInspector) { + builder.SetError("Invalid user type - expected tuple."); + return true; + } + + if (argsTypeInspector.GetElementsCount() != 2) { + builder.SetError("Invalid user type - expected two arguments."); + return true; + } + + auto inputType = argsTypeInspector.GetElementType(0); + auto optInspector = TOptionalTypeInspector(*typeHelper, inputType); + auto dataType = inputType; + if (optInspector) { + dataType = optInspector.GetItemType(); + } else { + isOptional = false; + } + + auto dataInspector = TDataTypeInspector(*typeHelper, dataType); + if (dataInspector && dataInspector.GetTypeId() == TDataType<TJson>::Id) { + useString = false; + builder.UserType(userType); + } + } + + auto retType = builder.List()->Item<char*>().Build(); + if (useString) { + builder.Args()->Add(builder.Optional()->Item<char*>().Build()).Add<char*>().Done().Returns(retType); + } else { + auto type = builder.SimpleType<TJson>(); + if (isOptional) { + builder.Args()->Add(builder.Optional()->Item(type).Build()).Add<char*>().Done().Returns(retType); + } else { + builder.Args()->Add(type).Add<char*>().Done().Returns(retType); + } + } + + if (!typesOnly) { + builder.Implementation(new TGetField); + } + + builder.IsStrict(); + return true; + } else { + return false; + } + } + }; +} + +SIMPLE_MODULE(TJsonModule, + TGetField) + +REGISTER_MODULES(TJsonModule) diff --git a/ydb/library/yql/udfs/common/json/test/canondata/result.json b/ydb/library/yql/udfs/common/json/test/canondata/result.json new file mode 100644 index 0000000000..fb6112fc5b --- /dev/null +++ b/ydb/library/yql/udfs/common/json/test/canondata/result.json @@ -0,0 +1,7 @@ +{ + "test.test[Basic]": [ + { + "uri": "file://test.test_Basic_/results.txt" + } + ] +} diff --git a/ydb/library/yql/udfs/common/json/test/canondata/test.test_Basic_/results.txt b/ydb/library/yql/udfs/common/json/test/canondata/test.test_Basic_/results.txt new file mode 100644 index 0000000000..8cd3200dab --- /dev/null +++ b/ydb/library/yql/udfs/common/json/test/canondata/test.test_Basic_/results.txt @@ -0,0 +1,57 @@ +[ + { + "Write" = [ + { + "Type" = [ + "ListType"; + [ + "StructType"; + [ + [ + "column0"; + [ + "ListType"; + [ + "DataType"; + "String" + ] + ] + ]; + [ + "column1"; + [ + "ListType"; + [ + "DataType"; + "String" + ] + ] + ]; + [ + "column2"; + [ + "ListType"; + [ + "DataType"; + "String" + ] + ] + ] + ] + ] + ]; + "Data" = [ + [ + [ + "11" + ]; + [ + "" + ]; + [] + ] + ] + } + ] + } +]
\ No newline at end of file diff --git a/ydb/library/yql/udfs/common/json/test/cases/Basic.sql b/ydb/library/yql/udfs/common/json/test/cases/Basic.sql new file mode 100644 index 0000000000..512246d766 --- /dev/null +++ b/ydb/library/yql/udfs/common/json/test/cases/Basic.sql @@ -0,0 +1,12 @@ +/* syntax version 0 */ +$json1 = @@{ + "x": { + "y": ["15", "11", "17"], + "z": 1 + } +}@@; + +SELECT + Json::GetField($json1, "/x/y/[1]"), + Json::GetField("[]", "/"), + Json::GetField($json1, "///"); diff --git a/ydb/library/yql/udfs/common/json/test/ya.make b/ydb/library/yql/udfs/common/json/test/ya.make new file mode 100644 index 0000000000..c20b6f8b7b --- /dev/null +++ b/ydb/library/yql/udfs/common/json/test/ya.make @@ -0,0 +1,13 @@ +YQL_UDF_YDB_TEST() + +DEPENDS(ydb/library/yql/udfs/common/json) + +TIMEOUT(300) + +SIZE(MEDIUM) + +IF (SANITIZER_TYPE == "memory") + TAG(ya:not_autocheck) # YQL-15385 +ENDIF() + +END() diff --git a/ydb/library/yql/udfs/common/json/ya.make b/ydb/library/yql/udfs/common/json/ya.make new file mode 100644 index 0000000000..30acf1b9c7 --- /dev/null +++ b/ydb/library/yql/udfs/common/json/ya.make @@ -0,0 +1,30 @@ +IF (YQL_PACKAGED) + PACKAGE() + FROM_SANDBOX(FILE {FILE_RESOURCE_ID} OUT_NOAUTO + libjson_udf.so + ) + END() +ELSE () + YQL_UDF_YDB(json_udf) + + YQL_ABI_VERSION( + 2 + 28 + 0 + ) + + SRCS( + json_udf.cpp + ) + + PEERDIR( + library/cpp/json/easy_parse + ) + + END() +ENDIF () + + +RECURSE_FOR_TESTS( + test +) diff --git a/ydb/library/yql/udfs/common/ya.make b/ydb/library/yql/udfs/common/ya.make index 9c3071ee93..6223475ca4 100644 --- a/ydb/library/yql/udfs/common/ya.make +++ b/ydb/library/yql/udfs/common/ya.make @@ -8,6 +8,7 @@ RECURSE( histogram hyperloglog ip_base + json json2 knn math diff --git a/ydb/tools/query_replay/common_deps.inc b/ydb/tools/query_replay/common_deps.inc index 9850b5f1ca..4ad1de2c79 100644 --- a/ydb/tools/query_replay/common_deps.inc +++ b/ydb/tools/query_replay/common_deps.inc @@ -69,6 +69,7 @@ SET(YDB_REPLAY_PEERDIRS ydb/library/yql/udfs/common/histogram ydb/library/yql/udfs/common/hyperloglog ydb/library/yql/udfs/common/hyperscan + ydb/library/yql/udfs/common/json ydb/library/yql/udfs/common/json2 ydb/library/yql/udfs/common/math ydb/library/yql/udfs/common/pire diff --git a/ydb/tools/query_replay_yt/common_deps.inc b/ydb/tools/query_replay_yt/common_deps.inc index 3ce148e1c5..13d4d7b97b 100644 --- a/ydb/tools/query_replay_yt/common_deps.inc +++ b/ydb/tools/query_replay_yt/common_deps.inc @@ -68,6 +68,7 @@ SET(YDB_REPLAY_PEERDIRS ydb/library/yql/udfs/common/histogram ydb/library/yql/udfs/common/hyperloglog ydb/library/yql/udfs/common/hyperscan + ydb/library/yql/udfs/common/json ydb/library/yql/udfs/common/json2 ydb/library/yql/udfs/common/math ydb/library/yql/udfs/common/pire |