forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ede83fc
commit f666530
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "json_executors.hpp" | ||
|
||
namespace duckdb { | ||
|
||
static void ArrayTailFunction(DataChunk &args, ExpressionState &state, Vector &result) { | ||
JSONExecutors::UnaryMutExecute(args, state, result, | ||
[](yyjson_mut_val *arr, yyjson_mut_doc *doc, yyjson_alc *alc, Vector &result) { | ||
if (!yyjson_mut_is_arr(arr)) { | ||
throw InvalidInputException("JSON input not an JSON Array"); | ||
} | ||
|
||
if (yyjson_mut_arr_size(arr) == 0) { | ||
return yyjson_mut_arr(doc); | ||
} | ||
|
||
yyjson_mut_arr_remove_first(arr); | ||
return arr; | ||
}); | ||
} | ||
|
||
static void GetArrayTailFunctionInternal(ScalarFunctionSet &set, const LogicalType &input_type) { | ||
set.AddFunction(ScalarFunction("json_array_tail", {input_type}, JSONCommon::JSONType(), ArrayTailFunction, nullptr, | ||
nullptr, nullptr, JSONFunctionLocalState::Init)); | ||
} | ||
|
||
ScalarFunctionSet JSONFunctions::GetArrayTailFunction() { | ||
ScalarFunctionSet set("json_array_tail"); | ||
GetArrayTailFunctionInternal(set, LogicalType::VARCHAR); | ||
GetArrayTailFunctionInternal(set, JSONCommon::JSONType()); | ||
return set; | ||
} | ||
|
||
} // namespace duckdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# name: test/sql/json/scalar/test_json_array_tail.test | ||
# description: Checks whether a given JSON Array's tail is correct | ||
# group: [scalar] | ||
|
||
require json | ||
|
||
statement ok | ||
PRAGMA enable_verification | ||
|
||
# list with varchar to json | ||
statement ok | ||
create table test (j json) | ||
|
||
statement ok | ||
insert into test values('[]'), ('[42]'), ('["a", 42, true]') | ||
|
||
query I | ||
SELECT json_array_tail(j) FROM test | ||
---- | ||
[] | ||
[] | ||
[42,true] | ||
|
||
statement error | ||
SELECT json_array_tail('') | ||
--- | ||
Error: Invalid Input Error: Malformed JSON at byte 0 of input: input length is 0. Input: | ||
|
||
statement error | ||
SELECT json_array_tail('{}') | ||
--- | ||
Error: Invalid Input Error: JSON input not a JSON Array | ||
|
||
statement error | ||
SELECT json_array_tail('true') | ||
--- | ||
Error: Invalid Input Error: JSON input not a JSON Array | ||
|
||
statement error | ||
SELECT json_array_tail('"Hello"') | ||
--- | ||
Error: Invalid Input Error: JSON input not a JSON Array | ||
|
||
statement error | ||
SELECT json_array_tail('42') | ||
--- | ||
Error: Invalid Input Error: JSON input not a JSON Array |