Skip to content

Commit

Permalink
Support sparksql json_array_length
Browse files Browse the repository at this point in the history
  • Loading branch information
WangGuangxin committed Dec 24, 2024
1 parent 8ebd3a8 commit a971121
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
53 changes: 53 additions & 0 deletions velox/functions/sparksql/JsonArrayLength.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.
*/

#pragma once

#include "velox/functions/Macros.h"
#include "velox/functions/prestosql/types/JsonType.h"

namespace facebook::velox::functions::sparksql {

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

FOLLY_ALWAYS_INLINE bool call(int32_t& len, const arg_type<Json>& json) {
simdjson::ondemand::document jsonDoc;

simdjson::padded_string paddedJson(json.data(), json.size());
if (simdjsonParse(paddedJson).get(jsonDoc)) {
return false;
}
if (jsonDoc.type().error()) {
return false;
}

if (jsonDoc.type() != simdjson::ondemand::json_type::array) {
return false;
}

size_t numElements;
if (jsonDoc.count_elements().get(numElements)) {
return false;
}

len = numElements;
return true;
}
};
} // namespace facebook::velox::functions::sparksql

4 changes: 4 additions & 0 deletions velox/functions/sparksql/registration/RegisterJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/
#include "velox/functions/lib/RegistrationHelpers.h"

#include "velox/functions/sparksql/GetJsonObject.h"
#include "velox/functions/sparksql/JsonArrayLength.h"
#include "velox/functions/sparksql/JsonObjectKeys.h"

namespace facebook::velox::functions::sparksql {
Expand All @@ -24,6 +26,8 @@ void registerJsonFunctions(const std::string& prefix) {
{prefix + "get_json_object"});
registerFunction<JsonObjectKeysFunction, Array<Varchar>, Varchar>(
{prefix + "json_object_keys"});
registerFunction<JsonArrayLengthFunction, int32_t, Varchar>(
{prefix + "json_array_length"});
}

} // namespace facebook::velox::functions::sparksql

0 comments on commit a971121

Please sign in to comment.