Skip to content

Commit 7d0afaf

Browse files
kevinwilfongfacebook-github-bot
authored andcommitted
fix
1 parent 404c4dd commit 7d0afaf

File tree

5 files changed

+46
-74
lines changed

5 files changed

+46
-74
lines changed

velox/functions/lib/JsonArrayLength.h

-71
This file was deleted.

velox/functions/prestosql/JsonFunctions.h

+45
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,49 @@ struct JsonSizeFunction {
379379
}
380380
};
381381

382+
/// json_array_length(jsonString) -> length
383+
///
384+
/// Returns the number of elements in the outermost JSON array from jsonString.
385+
/// If jsonString is not a valid JSON array or NULL, the function returns null.
386+
/// Presto:
387+
/// https://prestodb.io/docs/current/functions/json.html#json_array_length-json-bigint
388+
/// SparkSQL:
389+
/// https://spark.apache.org/docs/latest/api/sql/index.html#json_array_length
390+
template <typename T>
391+
struct JsonArrayLengthFunction {
392+
VELOX_DEFINE_FUNCTION_TYPES(T);
393+
394+
template <typename TOutput>
395+
FOLLY_ALWAYS_INLINE bool call(TOutput& len, const arg_type<Json>& json) {
396+
simdjson::ondemand::document jsonDoc;
397+
398+
simdjson::padded_string paddedJson(json.data(), json.size());
399+
if (simdjsonParse(paddedJson).get(jsonDoc)) {
400+
return false;
401+
}
402+
if (jsonDoc.type().error()) {
403+
return false;
404+
}
405+
406+
if (jsonDoc.type() != simdjson::ondemand::json_type::array) {
407+
return false;
408+
}
409+
410+
size_t numElements;
411+
if (jsonDoc.count_elements().get(numElements)) {
412+
return false;
413+
}
414+
415+
VELOX_USER_CHECK_LE(
416+
numElements,
417+
std::numeric_limits<TOutput>::max(),
418+
"The json array length {} is bigger than the max value of output type {}.",
419+
numElements,
420+
std::numeric_limits<TOutput>::max());
421+
422+
len = numElements;
423+
return true;
424+
}
425+
};
426+
382427
} // namespace facebook::velox::functions

velox/functions/prestosql/benchmarks/JsonExprBenchmark.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <folly/Benchmark.h>
2121
#include <folly/init/Init.h>
2222
#include "velox/functions/Registerer.h"
23-
#include "velox/functions/lib/JsonArrayLength.h"
2423
#include "velox/functions/lib/benchmarks/FunctionBenchmarkBase.h"
2524
#include "velox/functions/prestosql/JsonFunctions.h"
2625
#include "velox/functions/prestosql/json/JsonExtractor.h"

velox/functions/prestosql/registration/JsonFunctionsRegistration.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
#include "velox/functions/Registerer.h"
18-
#include "velox/functions/lib/JsonArrayLength.h"
1918
#include "velox/functions/prestosql/JsonFunctions.h"
2019
#include "velox/functions/prestosql/types/JsonRegistration.h"
2120

velox/functions/sparksql/registration/RegisterJson.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "velox/functions/lib/JsonArrayLength.h"
1817
#include "velox/functions/lib/RegistrationHelpers.h"
18+
#include "velox/functions/prestosql/JsonFunctions.h"
1919
#include "velox/functions/sparksql/GetJsonObject.h"
2020
#include "velox/functions/sparksql/JsonObjectKeys.h"
2121

0 commit comments

Comments
 (0)