-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove old Json UDF (used only in SQL v0)." (#7612)
- Loading branch information
1 parent
48718ab
commit f71909a
Showing
10 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"test.test[Basic]": [ | ||
{ | ||
"uri": "file://test.test_Basic_/results.txt" | ||
} | ||
] | ||
} |
57 changes: 57 additions & 0 deletions
57
ydb/library/yql/udfs/common/json/test/canondata/test.test_Basic_/results.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
]; | ||
[ | ||
"" | ||
]; | ||
[] | ||
] | ||
] | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "///"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ RECURSE( | |
histogram | ||
hyperloglog | ||
ip_base | ||
json | ||
json2 | ||
knn | ||
math | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters