Skip to content

added support for backslash escape in selector #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jsonUdf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ int main(int argc, char** argv) {
}

{
const char * json = "{\"store\":{\"fruit\":[{\"weight\":8,\"type\":\"apple\"},{\"weight\":9,\"type\":\"pear\"}],\"bicycle\":{\"price\":19.95,\"color\":\"red\"}},\"email\":\"amy@only_for_json_udf_test.net\",\"owner\":\"amy\"}";
const char * json = "{\"store\":{\"fruit\":[{\"weight\":8,\"type\":\"apple\"},{\"weight\":9,\"type\":\"pear\"}],\"bicycle\":{\"price\":19.95,\"color\":\"red\"}},\"email\":\"amy@only_for_json_udf_test.net\",\"owner\":\"amy\",\"lang.iso 639-1\":\"en\"}";
passed &= UdfTestHarness::ValidateUdf<StringVal, StringVal, StringVal>(
JsonGetObject, StringVal(json), StringVal("$.owner"), StringVal("amy"));
passed &= UdfTestHarness::ValidateUdf<StringVal, StringVal, StringVal>(
JsonGetObject, StringVal(json), StringVal("$.store.fruit[0]"), StringVal("{\"weight\":8,\"type\":\"apple\"}"));
passed &= UdfTestHarness::ValidateUdf<StringVal, StringVal, StringVal>(
JsonGetObject, StringVal(json), StringVal("$.non_exist_key"), StringVal::null());
passed &= UdfTestHarness::ValidateUdf<StringVal, StringVal, StringVal>(
JsonGetObject, StringVal(json), StringVal("$.lang\\.iso 639-1"), StringVal("en"));
}

cout << "Tests " << (passed ? "Passed." : "Failed.") << endl;
Expand Down
9 changes: 9 additions & 0 deletions jsonUdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// - output is always a String
// - llvm udf works fine. .so udf wasn't tested
// - selector is the same with hive get_json_object, except that * operator is not supported
// - selector supports backslash escape for names with special chars (characters that should be escaped currently: '.$[]\')


// we have to define this macro before <inttypes.h> is included
Expand Down Expand Up @@ -129,6 +130,14 @@ StringVal JsonGetObject(FunctionContext *context, const StringVal & jsonVal, con
}
inBracket = false;
break;
case '\\':
i++;
if (i >= selector.size()) {
context->SetError("encountered backslash at end of selector");
return StringVal::null();
}
token += pSel[i];
break;
default:
token += pSel[i];
break;
Expand Down