Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Json function - json_array_length
Browse files Browse the repository at this point in the history
pramodsatya committed Aug 15, 2022
1 parent fe48440 commit 7d5a5bd
Showing 6 changed files with 78 additions and 2 deletions.
7 changes: 7 additions & 0 deletions velox/docs/functions/json.rst
Original file line number Diff line number Diff line change
@@ -101,6 +101,13 @@ JSON Functions

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

.. function:: json_array_length(json) → bigint#

Returns the array length of ``json`` (a string containing a JSON
array). Returns NULL if ``json`` is not an array::

SELECT json_array_length('[1, 2, 3]');

============
JSON Vectors
============
Original file line number Diff line number Diff line change
@@ -46,4 +46,20 @@ struct JsonExtractScalarFunction {
}
};

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

FOLLY_ALWAYS_INLINE void call(
int64_t& result,
const arg_type<Varchar>& json) {
auto parsedJson = folly::parseJson(json);
if(!parsedJson.isArray()){
result = NULL;
} else {
result = parsedJson.size();
}
}
};

} // namespace facebook::velox::functions
Original file line number Diff line number Diff line change
@@ -15,12 +15,14 @@
*/

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

namespace facebook::velox::functions {
void registerJsonFunctions() {
registerFunction<JsonExtractScalarFunction, Varchar, Varchar, Varchar>(
{"json_extract_scalar"});
registerFunction<jsonArrayLengthFunction, int64_t, Varchar>(
{"json_array_length"});
}

} // namespace facebook::velox::functions
1 change: 1 addition & 0 deletions velox/functions/prestosql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ add_executable(
InPredicateTest.cpp
JsonCastTest.cpp
JsonExtractScalarTest.cpp
JsonFunctionsTest.cpp
MapConcatTest.cpp
MapEntriesTest.cpp
MapFilterTest.cpp
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<int64_t> json_array_length(std::optional<std::string> json) {
return evaluateOnce<int64_t>("json_array_length(c0)", json);
}
};

TEST_F(JsonFunctionsTest, jsonArrayLength) {
EXPECT_EQ(json_array_length(R"([])"), 0);
EXPECT_EQ(json_array_length(R"([1])"), 1);
EXPECT_EQ(json_array_length(R"([1, 2, 3])"), 3);
EXPECT_EQ(
json_array_length(
R"([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])"),
20);

EXPECT_EQ(json_array_length(R"(1)"), 0);
EXPECT_EQ(json_array_length(R"("hello")"), 0);
EXPECT_EQ(json_array_length(R"("")"), 0);
EXPECT_EQ(json_array_length(R"(true)"), 0);
EXPECT_EQ(json_array_length(R"({"k1":"v1"})"), 0);
EXPECT_EQ(json_array_length(R"({"k1":[0,1,2]})"), 0);
}

} // 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
@@ -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"

0 comments on commit 7d5a5bd

Please sign in to comment.