Skip to content
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

Add is_json_scalar Presto Json function #2291

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions velox/docs/functions/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ JSON Functions

.. _JSONPath: http://goessner.net/articles/JsonPath/

.. function:: is_json_scalar(json) -> boolean

Determine if ``json`` is a scalar (i.e. a JSON number, a JSON string,
``true``, ``false`` or ``null``)::

SELECT is_json_scalar('1'); *-- true*
SELECT is_json_scalar('[1, 2, 3]'); *-- false*

============
JSON Vectors
============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

namespace facebook::velox::functions {

template <typename T>
struct IsJsonScalarFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(
bool& result,
const arg_type<Varchar>& json) {
auto parsedJson = folly::parseJson(json);
result = parsedJson.isNumber() || parsedJson.isString() || parsedJson.isBool() || parsedJson.isNull();
}
};

// jsonExtractScalar(json, json_path) -> varchar
// Current implementation support UTF-8 in json, but not in json_path.
// Like jsonExtract(), but returns the result value as a string (as opposed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/

#include "velox/functions/Registerer.h"
#include "velox/functions/prestosql/JsonExtractScalar.h"
#include "velox/functions/prestosql/JsonFunctions.h"

namespace facebook::velox::functions {
void registerJsonFunctions() {
registerFunction<IsJsonScalarFunction, bool, Varchar>({"is_json_scalar"});
registerFunction<JsonExtractScalarFunction, Varchar, Varchar, Varchar>(
{"json_extract_scalar"});
}
Expand Down
1 change: 1 addition & 0 deletions velox/functions/prestosql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_executable(
InPredicateTest.cpp
JsonCastTest.cpp
JsonExtractScalarTest.cpp
JsonFunctionsTest.cpp
MapConcatTest.cpp
MapEntriesTest.cpp
MapFilterTest.cpp
Expand Down
50 changes: 50 additions & 0 deletions velox/functions/prestosql/tests/JsonFunctionsTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "velox/functions/prestosql/tests/FunctionBaseTest.h"
#include "velox/functions/prestosql/types/JsonType.h"

namespace facebook::velox::functions::prestosql {

namespace {

class JsonFunctionsTest : public functions::test::FunctionBaseTest {
public:
std::optional<bool> is_json_scalar(std::optional<std::string> json) {
return evaluateOnce<bool>("is_json_scalar(c0)", json);
}
};

TEST_F(JsonFunctionsTest, isJsonScalar) {
// Scalars.
EXPECT_EQ(is_json_scalar(R"(1)"), true);
EXPECT_EQ(is_json_scalar(R"(123456)"), true);
EXPECT_EQ(is_json_scalar(R"("hello")"), true);
EXPECT_EQ(is_json_scalar(R"("thefoxjumpedoverthefence")"), true);
EXPECT_EQ(is_json_scalar(R"(1.1)"), true);
EXPECT_EQ(is_json_scalar(R"("")"), true);
EXPECT_EQ(is_json_scalar(R"(true)"), true);

// Lists and maps
EXPECT_EQ(is_json_scalar(R"([1,2])"), false);
EXPECT_EQ(is_json_scalar(R"({"k1":"v1"})"), false);
EXPECT_EQ(is_json_scalar(R"({"k1":[0,1,2]})"), false);
EXPECT_EQ(is_json_scalar(R"({"k1":""})"), false);
}

} // namespace

} // namespace facebook::velox::functions::prestosql
2 changes: 1 addition & 1 deletion velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "velox/functions/lib/Re2Functions.h"
#include "velox/functions/lib/RegistrationHelpers.h"
#include "velox/functions/prestosql/DateTimeFunctions.h"
#include "velox/functions/prestosql/JsonExtractScalar.h"
#include "velox/functions/prestosql/JsonFunctions.h"
#include "velox/functions/prestosql/Rand.h"
#include "velox/functions/prestosql/StringFunctions.h"
#include "velox/functions/sparksql/ArraySort.h"
Expand Down