Skip to content

Commit

Permalink
Revert "Remove old Json UDF (used only in SQL v0)." (#7612)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony-Romanov authored Aug 9, 2024
1 parent 48718ab commit f71909a
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 0 deletions.
1 change: 1 addition & 0 deletions ydb/apps/ydbd/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
120 changes: 120 additions & 0 deletions ydb/library/yql/udfs/common/json/json_udf.cpp
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions ydb/library/yql/udfs/common/json/test/canondata/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"test.test[Basic]": [
{
"uri": "file://test.test_Basic_/results.txt"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[
{
"Write" = [
{
"Type" = [
"ListType";
[
"StructType";
[
[
"column0";
[
"ListType";
[
"DataType";
"String"
]
]
];
[
"column1";
[
"ListType";
[
"DataType";
"String"
]
]
];
[
"column2";
[
"ListType";
[
"DataType";
"String"
]
]
]
]
]
];
"Data" = [
[
[
"11"
];
[
""
];
[]
]
]
}
]
}
]
12 changes: 12 additions & 0 deletions ydb/library/yql/udfs/common/json/test/cases/Basic.sql
Original file line number Diff line number Diff line change
@@ -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, "///");
13 changes: 13 additions & 0 deletions ydb/library/yql/udfs/common/json/test/ya.make
Original file line number Diff line number Diff line change
@@ -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()
30 changes: 30 additions & 0 deletions ydb/library/yql/udfs/common/json/ya.make
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions ydb/library/yql/udfs/common/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RECURSE(
histogram
hyperloglog
ip_base
json
json2
knn
math
Expand Down
1 change: 1 addition & 0 deletions ydb/tools/query_replay/common_deps.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ydb/tools/query_replay_yt/common_deps.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f71909a

Please sign in to comment.