Skip to content

Commit f71909a

Browse files
authored
Revert "Remove old Json UDF (used only in SQL v0)." (#7612)
1 parent 48718ab commit f71909a

File tree

10 files changed

+243
-0
lines changed

10 files changed

+243
-0
lines changed

ydb/apps/ydbd/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ PEERDIR(
5353
ydb/library/yql/udfs/common/hyperloglog
5454
ydb/library/yql/udfs/common/ip_base
5555
ydb/library/yql/udfs/common/knn
56+
ydb/library/yql/udfs/common/json
5657
ydb/library/yql/udfs/common/json2
5758
ydb/library/yql/udfs/common/math
5859
ydb/library/yql/udfs/common/pire
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include <ydb/library/yql/public/udf/udf_helpers.h>
2+
3+
#include <library/cpp/json/easy_parse/json_easy_parser.h>
4+
5+
using namespace NKikimr;
6+
using namespace NUdf;
7+
8+
namespace {
9+
class TGetField: public TBoxedValue {
10+
public:
11+
typedef bool TTypeAwareMarker;
12+
13+
public:
14+
static TStringRef Name() {
15+
return TStringRef::Of("GetField");
16+
}
17+
18+
TUnboxedValue Run(
19+
const IValueBuilder* valueBuilder,
20+
const TUnboxedValuePod* args) const override {
21+
if (!args[0]) {
22+
return valueBuilder->NewEmptyList();
23+
}
24+
25+
const TString json(args[0].AsStringRef());
26+
const TString field(args[1].AsStringRef());
27+
28+
if (field.empty()) {
29+
return valueBuilder->NewEmptyList();
30+
}
31+
32+
NJson::TJsonParser parser;
33+
parser.AddField(field, false);
34+
35+
TVector<TString> result;
36+
parser.Parse(json, &result);
37+
38+
TUnboxedValue* items = nullptr;
39+
const auto list = valueBuilder->NewArray(result.size(), items);
40+
for (const TString& item : result) {
41+
*items++ = valueBuilder->NewString(item);
42+
}
43+
44+
return list;
45+
}
46+
47+
static bool DeclareSignature(
48+
const TStringRef& name,
49+
TType* userType,
50+
IFunctionTypeInfoBuilder& builder,
51+
bool typesOnly) {
52+
if (Name() == name) {
53+
bool useString = true;
54+
bool isOptional = true;
55+
if (userType) {
56+
// support of an overload with Json/Json? input type
57+
auto typeHelper = builder.TypeInfoHelper();
58+
auto userTypeInspector = TTupleTypeInspector(*typeHelper, userType);
59+
if (!userTypeInspector || userTypeInspector.GetElementsCount() < 1) {
60+
builder.SetError("Missing or invalid user type.");
61+
return true;
62+
}
63+
64+
auto argsTypeTuple = userTypeInspector.GetElementType(0);
65+
auto argsTypeInspector = TTupleTypeInspector(*typeHelper, argsTypeTuple);
66+
if (!argsTypeInspector) {
67+
builder.SetError("Invalid user type - expected tuple.");
68+
return true;
69+
}
70+
71+
if (argsTypeInspector.GetElementsCount() != 2) {
72+
builder.SetError("Invalid user type - expected two arguments.");
73+
return true;
74+
}
75+
76+
auto inputType = argsTypeInspector.GetElementType(0);
77+
auto optInspector = TOptionalTypeInspector(*typeHelper, inputType);
78+
auto dataType = inputType;
79+
if (optInspector) {
80+
dataType = optInspector.GetItemType();
81+
} else {
82+
isOptional = false;
83+
}
84+
85+
auto dataInspector = TDataTypeInspector(*typeHelper, dataType);
86+
if (dataInspector && dataInspector.GetTypeId() == TDataType<TJson>::Id) {
87+
useString = false;
88+
builder.UserType(userType);
89+
}
90+
}
91+
92+
auto retType = builder.List()->Item<char*>().Build();
93+
if (useString) {
94+
builder.Args()->Add(builder.Optional()->Item<char*>().Build()).Add<char*>().Done().Returns(retType);
95+
} else {
96+
auto type = builder.SimpleType<TJson>();
97+
if (isOptional) {
98+
builder.Args()->Add(builder.Optional()->Item(type).Build()).Add<char*>().Done().Returns(retType);
99+
} else {
100+
builder.Args()->Add(type).Add<char*>().Done().Returns(retType);
101+
}
102+
}
103+
104+
if (!typesOnly) {
105+
builder.Implementation(new TGetField);
106+
}
107+
108+
builder.IsStrict();
109+
return true;
110+
} else {
111+
return false;
112+
}
113+
}
114+
};
115+
}
116+
117+
SIMPLE_MODULE(TJsonModule,
118+
TGetField)
119+
120+
REGISTER_MODULES(TJsonModule)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"test.test[Basic]": [
3+
{
4+
"uri": "file://test.test_Basic_/results.txt"
5+
}
6+
]
7+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[
2+
{
3+
"Write" = [
4+
{
5+
"Type" = [
6+
"ListType";
7+
[
8+
"StructType";
9+
[
10+
[
11+
"column0";
12+
[
13+
"ListType";
14+
[
15+
"DataType";
16+
"String"
17+
]
18+
]
19+
];
20+
[
21+
"column1";
22+
[
23+
"ListType";
24+
[
25+
"DataType";
26+
"String"
27+
]
28+
]
29+
];
30+
[
31+
"column2";
32+
[
33+
"ListType";
34+
[
35+
"DataType";
36+
"String"
37+
]
38+
]
39+
]
40+
]
41+
]
42+
];
43+
"Data" = [
44+
[
45+
[
46+
"11"
47+
];
48+
[
49+
""
50+
];
51+
[]
52+
]
53+
]
54+
}
55+
]
56+
}
57+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* syntax version 0 */
2+
$json1 = @@{
3+
"x": {
4+
"y": ["15", "11", "17"],
5+
"z": 1
6+
}
7+
}@@;
8+
9+
SELECT
10+
Json::GetField($json1, "/x/y/[1]"),
11+
Json::GetField("[]", "/"),
12+
Json::GetField($json1, "///");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
YQL_UDF_YDB_TEST()
2+
3+
DEPENDS(ydb/library/yql/udfs/common/json)
4+
5+
TIMEOUT(300)
6+
7+
SIZE(MEDIUM)
8+
9+
IF (SANITIZER_TYPE == "memory")
10+
TAG(ya:not_autocheck) # YQL-15385
11+
ENDIF()
12+
13+
END()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
IF (YQL_PACKAGED)
2+
PACKAGE()
3+
FROM_SANDBOX(FILE {FILE_RESOURCE_ID} OUT_NOAUTO
4+
libjson_udf.so
5+
)
6+
END()
7+
ELSE ()
8+
YQL_UDF_YDB(json_udf)
9+
10+
YQL_ABI_VERSION(
11+
2
12+
28
13+
0
14+
)
15+
16+
SRCS(
17+
json_udf.cpp
18+
)
19+
20+
PEERDIR(
21+
library/cpp/json/easy_parse
22+
)
23+
24+
END()
25+
ENDIF ()
26+
27+
28+
RECURSE_FOR_TESTS(
29+
test
30+
)

ydb/library/yql/udfs/common/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RECURSE(
88
histogram
99
hyperloglog
1010
ip_base
11+
json
1112
json2
1213
knn
1314
math

ydb/tools/query_replay/common_deps.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ SET(YDB_REPLAY_PEERDIRS
6969
ydb/library/yql/udfs/common/histogram
7070
ydb/library/yql/udfs/common/hyperloglog
7171
ydb/library/yql/udfs/common/hyperscan
72+
ydb/library/yql/udfs/common/json
7273
ydb/library/yql/udfs/common/json2
7374
ydb/library/yql/udfs/common/math
7475
ydb/library/yql/udfs/common/pire

ydb/tools/query_replay_yt/common_deps.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ SET(YDB_REPLAY_PEERDIRS
6868
ydb/library/yql/udfs/common/histogram
6969
ydb/library/yql/udfs/common/hyperloglog
7070
ydb/library/yql/udfs/common/hyperscan
71+
ydb/library/yql/udfs/common/json
7172
ydb/library/yql/udfs/common/json2
7273
ydb/library/yql/udfs/common/math
7374
ydb/library/yql/udfs/common/pire

0 commit comments

Comments
 (0)