Skip to content

Commit

Permalink
Update vendored DuckDB sources to 51d53f8
Browse files Browse the repository at this point in the history
  • Loading branch information
duckdblabs-bot committed Aug 24, 2024
1 parent 51d53f8 commit 4440802
Show file tree
Hide file tree
Showing 108 changed files with 4,305 additions and 3,005 deletions.
43 changes: 20 additions & 23 deletions src/duckdb/extension/json/include/json_executors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@

namespace duckdb {

template <class T>
using json_function_t = std::function<T(yyjson_val *, yyjson_alc *, Vector &, ValidityMask &, idx_t)>;

struct JSONExecutors {
public:
//! Single-argument JSON read function, i.e. json_type('[1, 2, 3]')
template <class T>
static void UnaryExecute(DataChunk &args, ExpressionState &state, Vector &result,
std::function<T(yyjson_val *, yyjson_alc *, Vector &)> fun) {
static void UnaryExecute(DataChunk &args, ExpressionState &state, Vector &result, const json_function_t<T> fun) {
auto &lstate = JSONFunctionLocalState::ResetAndGet(state);
auto alc = lstate.json_allocator.GetYYAlc();

auto &inputs = args.data[0];
UnaryExecutor::Execute<string_t, T>(inputs, result, args.size(), [&](string_t input) {
auto doc = JSONCommon::ReadDocument(input, JSONCommon::READ_FLAG, alc);
return fun(doc->root, alc, result);
});
UnaryExecutor::ExecuteWithNulls<string_t, T>(
inputs, result, args.size(), [&](string_t input, ValidityMask &mask, idx_t idx) {
auto doc = JSONCommon::ReadDocument(input, JSONCommon::READ_FLAG, alc);
return fun(doc->root, alc, result, mask, idx);
});
}

//! Two-argument JSON read function (with path query), i.e. json_type('[1, 2, 3]', '$[0]')
template <class T, bool NULL_IF_NULL = true>
static void BinaryExecute(DataChunk &args, ExpressionState &state, Vector &result,
std::function<T(yyjson_val *, yyjson_alc *, Vector &)> fun) {
template <class T, bool SET_NULL_IF_NOT_FOUND = true>
static void BinaryExecute(DataChunk &args, ExpressionState &state, Vector &result, const json_function_t<T> fun) {
auto &func_expr = state.expr.Cast<BoundFunctionExpression>();
const auto &info = func_expr.bind_info->Cast<JSONReadFunctionData>();
auto &lstate = JSONFunctionLocalState::ResetAndGet(state);
Expand All @@ -48,11 +50,11 @@ struct JSONExecutors {
auto doc =
JSONCommon::ReadDocument(input, JSONCommon::READ_FLAG, lstate.json_allocator.GetYYAlc());
auto val = JSONCommon::GetUnsafe(doc->root, ptr, len);
if (!val || (NULL_IF_NULL && unsafe_yyjson_is_null(val))) {
if (SET_NULL_IF_NOT_FOUND && !val) {
mask.SetInvalid(idx);
return T {};
} else {
return fun(val, alc, result);
return fun(val, alc, result, mask, idx);
}
});
} else {
Expand All @@ -76,11 +78,7 @@ struct JSONExecutors {
for (idx_t i = 0; i < vals.size(); i++) {
auto &val = vals[i];
D_ASSERT(val != nullptr); // Wildcard extract shouldn't give back nullptrs
if (NULL_IF_NULL && unsafe_yyjson_is_null(val)) {
child_validity.SetInvalid(current_size + i);
} else {
child_vals[current_size + i] = fun(val, alc, result);
}
child_vals[current_size + i] = fun(val, alc, result, child_validity, current_size + i);
}

ListVector::SetListSize(result, new_size);
Expand All @@ -95,11 +93,11 @@ struct JSONExecutors {
inputs, paths, result, args.size(), [&](string_t input, string_t path, ValidityMask &mask, idx_t idx) {
auto doc = JSONCommon::ReadDocument(input, JSONCommon::READ_FLAG, lstate.json_allocator.GetYYAlc());
auto val = JSONCommon::Get(doc->root, path);
if (!val || unsafe_yyjson_is_null(val)) {
if (SET_NULL_IF_NOT_FOUND && !val) {
mask.SetInvalid(idx);
return T {};
} else {
return fun(val, alc, result);
return fun(val, alc, result, mask, idx);
}
});
}
Expand All @@ -109,9 +107,8 @@ struct JSONExecutors {
}

//! JSON read function with list of path queries, i.e. json_type('[1, 2, 3]', ['$[0]', '$[1]'])
template <class T, bool NULL_IF_NULL = true>
static void ExecuteMany(DataChunk &args, ExpressionState &state, Vector &result,
std::function<T(yyjson_val *, yyjson_alc *, Vector &)> fun) {
template <class T, bool SET_NULL_IF_NOT_FOUND = true>
static void ExecuteMany(DataChunk &args, ExpressionState &state, Vector &result, const json_function_t<T> fun) {
auto &func_expr = state.expr.Cast<BoundFunctionExpression>();
const auto &info = func_expr.bind_info->Cast<JSONReadManyFunctionData>();
auto &lstate = JSONFunctionLocalState::ResetAndGet(state);
Expand Down Expand Up @@ -148,10 +145,10 @@ struct JSONExecutors {
for (idx_t path_i = 0; path_i < num_paths; path_i++) {
auto child_idx = offset + path_i;
val = JSONCommon::GetUnsafe(doc->root, info.ptrs[path_i], info.lens[path_i]);
if (!val || (NULL_IF_NULL && unsafe_yyjson_is_null(val))) {
if (SET_NULL_IF_NOT_FOUND && !val) {
child_validity.SetInvalid(child_idx);
} else {
child_data[child_idx] = fun(val, alc, child);
child_data[child_idx] = fun(val, alc, child, child_validity, child_idx);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/extension/json/include/json_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ class JSONFunctions {

static ScalarFunctionSet GetArrayLengthFunction();
static ScalarFunctionSet GetContainsFunction();
static ScalarFunctionSet GetExistsFunction();
static ScalarFunctionSet GetKeysFunction();
static ScalarFunctionSet GetTypeFunction();
static ScalarFunctionSet GetValidFunction();
static ScalarFunctionSet GetValueFunction();
static ScalarFunctionSet GetSerializeSqlFunction();
static ScalarFunctionSet GetDeserializeSqlFunction();
static ScalarFunctionSet GetSerializePlanFunction();
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/extension/json/json_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ vector<ScalarFunctionSet> JSONFunctions::GetScalarFunctions() {
// Other
functions.push_back(GetArrayLengthFunction());
functions.push_back(GetContainsFunction());
functions.push_back(GetExistsFunction());
functions.push_back(GetKeysFunction());
functions.push_back(GetTypeFunction());
functions.push_back(GetValidFunction());
functions.push_back(GetValueFunction());
functions.push_back(GetSerializePlanFunction());
functions.push_back(GetSerializeSqlFunction());
functions.push_back(GetDeserializeSqlFunction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace duckdb {

static inline uint64_t GetArrayLength(yyjson_val *val, yyjson_alc *alc, Vector &result) {
static inline uint64_t GetArrayLength(yyjson_val *val, yyjson_alc *, Vector &, ValidityMask &, idx_t) {
return yyjson_arr_size(val);
}

Expand Down
32 changes: 32 additions & 0 deletions src/duckdb/extension/json/json_functions/json_exists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "json_executors.hpp"

namespace duckdb {

static inline bool JSONExists(yyjson_val *val, yyjson_alc *, Vector &, ValidityMask &, idx_t) {
return val;
}

static void BinaryExistsFunction(DataChunk &args, ExpressionState &state, Vector &result) {
JSONExecutors::BinaryExecute<bool, false>(args, state, result, JSONExists);
}

static void ManyExistsFunction(DataChunk &args, ExpressionState &state, Vector &result) {
JSONExecutors::ExecuteMany<bool, false>(args, state, result, JSONExists);
}

static void GetExistsFunctionsInternal(ScalarFunctionSet &set, const LogicalType &input_type) {
set.AddFunction(ScalarFunction({input_type, LogicalType::VARCHAR}, LogicalType::BOOLEAN, BinaryExistsFunction,
JSONReadFunctionData::Bind, nullptr, nullptr, JSONFunctionLocalState::Init));
set.AddFunction(ScalarFunction({input_type, LogicalType::LIST(LogicalType::VARCHAR)},
LogicalType::LIST(LogicalType::BOOLEAN), ManyExistsFunction,
JSONReadManyFunctionData::Bind, nullptr, nullptr, JSONFunctionLocalState::Init));
}

ScalarFunctionSet JSONFunctions::GetExistsFunction() {
ScalarFunctionSet set("json_exists");
GetExistsFunctionsInternal(set, LogicalType::VARCHAR);
GetExistsFunctionsInternal(set, LogicalType::JSON());
return set;
}

} // namespace duckdb
4 changes: 2 additions & 2 deletions src/duckdb/extension/json/json_functions/json_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace duckdb {

static inline string_t ExtractFromVal(yyjson_val *val, yyjson_alc *alc, Vector &) {
static inline string_t ExtractFromVal(yyjson_val *val, yyjson_alc *alc, Vector &, ValidityMask &, idx_t) {
return JSONCommon::WriteVal<yyjson_val>(val, alc);
}

static inline string_t ExtractStringFromVal(yyjson_val *val, yyjson_alc *alc, Vector &) {
static inline string_t ExtractStringFromVal(yyjson_val *val, yyjson_alc *alc, Vector &, ValidityMask &, idx_t) {
return yyjson_is_str(val) ? string_t(unsafe_yyjson_get_str(val), unsafe_yyjson_get_len(val))
: JSONCommon::WriteVal<yyjson_val>(val, alc);
}
Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/extension/json/json_functions/json_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace duckdb {

static inline list_entry_t GetJSONKeys(yyjson_val *val, yyjson_alc *alc, Vector &result) {
static inline list_entry_t GetJSONKeys(yyjson_val *val, yyjson_alc *, Vector &result, ValidityMask &, idx_t) {
auto num_keys = yyjson_obj_size(val);
auto current_size = ListVector::GetListSize(result);
auto new_size = current_size + num_keys;
Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/extension/json/json_functions/json_pretty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace duckdb {

//! Pretty Print a given JSON Document
string_t PrettyPrint(yyjson_val *val, yyjson_alc *alc, Vector &result) {
string_t PrettyPrint(yyjson_val *val, yyjson_alc *alc, Vector &, ValidityMask &, idx_t) {
D_ASSERT(alc);
idx_t len;
auto data =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ static yyjson_mut_val *ConvertStructure(const JSONStructureNode &node, yyjson_mu
}
}

static string_t JSONStructureFunction(yyjson_val *val, yyjson_alc *alc, Vector &) {
static string_t JSONStructureFunction(yyjson_val *val, yyjson_alc *alc, Vector &, ValidityMask &, idx_t) {
return JSONCommon::WriteVal<yyjson_mut_val>(
ConvertStructure(ExtractStructureInternal(val, true), yyjson_mut_doc_new(alc)), alc);
}
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/extension/json/json_functions/json_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace duckdb {

static inline string_t GetType(yyjson_val *val, yyjson_alc *alc, Vector &result) {
static inline string_t GetType(yyjson_val *val, yyjson_alc *, Vector &, ValidityMask &mask, idx_t idx) {
return JSONCommon::ValTypeToStringT(val);
}

Expand All @@ -11,11 +11,11 @@ static void UnaryTypeFunction(DataChunk &args, ExpressionState &state, Vector &r
}

static void BinaryTypeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
JSONExecutors::BinaryExecute<string_t, false>(args, state, result, GetType);
JSONExecutors::BinaryExecute<string_t>(args, state, result, GetType);
}

static void ManyTypeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
JSONExecutors::ExecuteMany<string_t, false>(args, state, result, GetType);
JSONExecutors::ExecuteMany<string_t>(args, state, result, GetType);
}

static void GetTypeFunctionsInternal(ScalarFunctionSet &set, const LogicalType &input_type) {
Expand Down
42 changes: 42 additions & 0 deletions src/duckdb/extension/json/json_functions/json_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "json_executors.hpp"

namespace duckdb {

static inline string_t ValueFromVal(yyjson_val *val, yyjson_alc *alc, Vector &, ValidityMask &mask, idx_t idx) {
switch (yyjson_get_tag(val)) {
case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE:
case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE:
mask.SetInvalid(idx);
return string_t {};
default:
return JSONCommon::WriteVal<yyjson_val>(val, alc);
}
}

static void ValueFunction(DataChunk &args, ExpressionState &state, Vector &result) {
JSONExecutors::BinaryExecute<string_t>(args, state, result, ValueFromVal);
}

static void ValueManyFunction(DataChunk &args, ExpressionState &state, Vector &result) {
JSONExecutors::ExecuteMany<string_t>(args, state, result, ValueFromVal);
}

static void GetValueFunctionsInternal(ScalarFunctionSet &set, const LogicalType &input_type) {
set.AddFunction(ScalarFunction({input_type, LogicalType::BIGINT}, LogicalType::JSON(), ValueFunction,
JSONReadFunctionData::Bind, nullptr, nullptr, JSONFunctionLocalState::Init));
set.AddFunction(ScalarFunction({input_type, LogicalType::VARCHAR}, LogicalType::JSON(), ValueFunction,
JSONReadFunctionData::Bind, nullptr, nullptr, JSONFunctionLocalState::Init));
set.AddFunction(ScalarFunction({input_type, LogicalType::LIST(LogicalType::VARCHAR)},
LogicalType::LIST(LogicalType::JSON()), ValueManyFunction,
JSONReadManyFunctionData::Bind, nullptr, nullptr, JSONFunctionLocalState::Init));
}

ScalarFunctionSet JSONFunctions::GetValueFunction() {
// The value function is just like the extract function but returns NULL if the JSON is not a scalar value
ScalarFunctionSet set("json_value");
GetValueFunctionsInternal(set, LogicalType::VARCHAR);
GetValueFunctionsInternal(set, LogicalType::JSON());
return set;
}

} // namespace duckdb
34 changes: 18 additions & 16 deletions src/duckdb/src/catalog/catalog_entry/duck_schema_entry.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
#include "duckdb/catalog/catalog_entry/duck_schema_entry.hpp"
#include "duckdb/catalog/default/default_functions.hpp"
#include "duckdb/catalog/default/default_table_functions.hpp"
#include "duckdb/catalog/default/default_types.hpp"
#include "duckdb/catalog/default/default_views.hpp"

#include "duckdb/catalog/catalog_entry/aggregate_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/collate_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/copy_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/duck_index_entry.hpp"
#include "duckdb/catalog/catalog_entry/duck_table_entry.hpp"
#include "duckdb/catalog/catalog_entry/pragma_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/scalar_macro_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/sequence_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/table_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/table_macro_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/type_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/view_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/aggregate_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/scalar_macro_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/table_macro_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/duck_table_entry.hpp"
#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
#include "duckdb/catalog/default/default_functions.hpp"
#include "duckdb/catalog/default/default_table_functions.hpp"
#include "duckdb/catalog/default/default_types.hpp"
#include "duckdb/catalog/default/default_views.hpp"
#include "duckdb/catalog/dependency_list.hpp"
#include "duckdb/planner/constraints/bound_foreign_key_constraint.hpp"
#include "duckdb/main/attached_database.hpp"
#include "duckdb/main/database.hpp"
#include "duckdb/parser/constraints/foreign_key_constraint.hpp"
#include "duckdb/parser/parsed_data/alter_table_info.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/storage/data_table.hpp"
#include "duckdb/planner/parsed_data/bound_create_table_info.hpp"
#include "duckdb/parser/parsed_data/create_collation_info.hpp"
#include "duckdb/parser/parsed_data/create_copy_function_info.hpp"
#include "duckdb/parser/parsed_data/create_index_info.hpp"
#include "duckdb/parser/parsed_data/create_pragma_function_info.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/parser/parsed_data/create_schema_info.hpp"
#include "duckdb/parser/parsed_data/create_sequence_info.hpp"
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
#include "duckdb/parser/parsed_data/create_type_info.hpp"
#include "duckdb/parser/parsed_data/create_view_info.hpp"
#include "duckdb/parser/parsed_data/drop_info.hpp"
#include "duckdb/transaction/meta_transaction.hpp"
#include "duckdb/main/attached_database.hpp"
#include "duckdb/planner/constraints/bound_foreign_key_constraint.hpp"
#include "duckdb/planner/parsed_data/bound_create_table_info.hpp"
#include "duckdb/storage/data_table.hpp"
#include "duckdb/transaction/duck_transaction.hpp"
#include "duckdb/transaction/meta_transaction.hpp"

namespace duckdb {

Expand Down
Loading

0 comments on commit 4440802

Please sign in to comment.