Skip to content

Commit

Permalink
Update vendored DuckDB sources to ec95d5f
Browse files Browse the repository at this point in the history
  • Loading branch information
duckdblabs-bot committed Oct 11, 2024
1 parent ec95d5f commit 16f6120
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/duckdb/extension/json/include/json_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ struct JSONCommon {
//! Get JSON pointer (/field/index/... syntax)
static inline yyjson_val *GetPointer(yyjson_val *val, const char *ptr, const idx_t &len) {
yyjson_ptr_err err;
return len == 1 ? val : unsafe_yyjson_ptr_getx(val, ptr, len, &err);
return unsafe_yyjson_ptr_getx(val, ptr, len, &err);
}
//! Get JSON path ($.field[index]... syntax)
static yyjson_val *GetPath(yyjson_val *val, const char *ptr, const idx_t &len);
Expand Down
17 changes: 11 additions & 6 deletions src/duckdb/extension/json/json_functions/json_structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ void JSONStructureNode::RefineCandidateTypesObject(yyjson_val *vals[], const idx
D_ASSERT(it != key_map.end());
const auto child_idx = it->second;
child_vals[child_idx][i] = child_val;
found_key_count += !found_keys[child_idx];
found_keys[child_idx] = true;
found_key_count++;
}

if (found_key_count != child_count) {
Expand Down Expand Up @@ -562,10 +562,12 @@ static void MergeNodeVal(JSONStructureNode &merged, const JSONStructureDescripti
}
if (!merged.initialized) {
merged_desc.candidate_types = child_desc.candidate_types;
} else if (!merged_desc.candidate_types.empty() && !child_desc.candidate_types.empty() &&
merged_desc.candidate_types.back() != child_desc.candidate_types.back()) {
} else if (merged_desc.candidate_types.empty() != child_desc.candidate_types.empty() // both empty or neither empty
|| (!merged_desc.candidate_types.empty() &&
merged_desc.candidate_types.back() != child_desc.candidate_types.back())) { // non-empty: check type
merged_desc.candidate_types.clear(); // Not the same, default to VARCHAR
}

merged.initialized = true;
}

Expand Down Expand Up @@ -704,10 +706,13 @@ static LogicalType StructureToTypeObject(ClientContext &context, const JSONStruc
D_ASSERT(node.descriptions.size() == 1 && node.descriptions[0].type == LogicalTypeId::STRUCT);
auto &desc = node.descriptions[0];

// If it's an empty struct we do MAP of JSON instead
if (desc.children.empty()) {
// Empty struct - let's do MAP of JSON instead
return LogicalType::MAP(LogicalType::VARCHAR, null_type);
if (map_inference_threshold != DConstants::INVALID_INDEX) {
// Empty struct - let's do MAP of JSON instead
return LogicalType::MAP(LogicalType::VARCHAR, null_type);
} else {
return LogicalType::JSON();
}
}

// If it's an inconsistent object we also just do MAP with the best-possible, recursively-merged value type
Expand Down
10 changes: 5 additions & 5 deletions src/duckdb/src/catalog/default/default_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace duckdb {
static const DefaultMacro internal_macros[] = {
{DEFAULT_SCHEMA, "current_role", {nullptr}, {{nullptr, nullptr}}, "'duckdb'"}, // user name of current execution context
{DEFAULT_SCHEMA, "current_user", {nullptr}, {{nullptr, nullptr}}, "'duckdb'"}, // user name of current execution context
{DEFAULT_SCHEMA, "current_catalog", {nullptr}, {{nullptr, nullptr}}, "current_database()"}, // name of current database (called "catalog" in the SQL standard)
{DEFAULT_SCHEMA, "current_catalog", {nullptr}, {{nullptr, nullptr}}, "main.current_database()"}, // name of current database (called "catalog" in the SQL standard)
{DEFAULT_SCHEMA, "user", {nullptr}, {{nullptr, nullptr}}, "current_user"}, // equivalent to current_user
{DEFAULT_SCHEMA, "session_user", {nullptr}, {{nullptr, nullptr}}, "'duckdb'"}, // session user name
{"pg_catalog", "inet_client_addr", {nullptr}, {{nullptr, nullptr}}, "NULL"}, // address of the remote connection
Expand All @@ -27,10 +27,10 @@ static const DefaultMacro internal_macros[] = {

{"pg_catalog", "pg_typeof", {"expression", nullptr}, {{nullptr, nullptr}}, "lower(typeof(expression))"}, // get the data type of any value

{"pg_catalog", "current_database", {nullptr}, {{nullptr, nullptr}}, "current_database()"}, // name of current database (called "catalog" in the SQL standard)
{"pg_catalog", "current_query", {nullptr}, {{nullptr, nullptr}}, "current_query()"}, // the currently executing query (NULL if not inside a plpgsql function)
{"pg_catalog", "current_schema", {nullptr}, {{nullptr, nullptr}}, "current_schema()"}, // name of current schema
{"pg_catalog", "current_schemas", {"include_implicit"}, {{nullptr, nullptr}}, "current_schemas(include_implicit)"}, // names of schemas in search path
{"pg_catalog", "current_database", {nullptr}, {{nullptr, nullptr}}, "main.current_database()"}, // name of current database (called "catalog" in the SQL standard)
{"pg_catalog", "current_query", {nullptr}, {{nullptr, nullptr}}, "main.current_query()"}, // the currently executing query (NULL if not inside a plpgsql function)
{"pg_catalog", "current_schema", {nullptr}, {{nullptr, nullptr}}, "main.current_schema()"}, // name of current schema
{"pg_catalog", "current_schemas", {"include_implicit"}, {{nullptr, nullptr}}, "main.current_schemas(include_implicit)"}, // names of schemas in search path

// privilege functions
{"pg_catalog", "has_any_column_privilege", {"table", "privilege", nullptr}, {{nullptr, nullptr}}, "true"}, //boolean //does current user have privilege for any column of table
Expand Down
78 changes: 49 additions & 29 deletions src/duckdb/src/common/types/bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ idx_t Bit::ComputeBitstringLen(idx_t len) {
return result;
}

static inline idx_t GetBitPadding(const string_t &bit_string) {
static inline idx_t GetBitPadding(const bitstring_t &bit_string) {
auto data = const_data_ptr_cast(bit_string.GetData());
D_ASSERT(idx_t(data[0]) <= 8);
return data[0];
Expand All @@ -37,14 +37,14 @@ static inline idx_t GetBitSize(const string_t &str) {
return str_len;
}

uint8_t Bit::GetFirstByte(const string_t &str) {
uint8_t Bit::GetFirstByte(const bitstring_t &str) {
D_ASSERT(str.GetSize() > 1);

auto data = const_data_ptr_cast(str.GetData());
return data[1] & ((1 << (8 - data[0])) - 1);
}

void Bit::Finalize(string_t &str) {
void Bit::Finalize(bitstring_t &str) {
// bit strings require all padding bits to be set to 1
// this method sets all padding bits to 1
auto padding = GetBitPadding(str);
Expand All @@ -55,23 +55,23 @@ void Bit::Finalize(string_t &str) {
Bit::Verify(str);
}

void Bit::SetEmptyBitString(string_t &target, string_t &input) {
void Bit::SetEmptyBitString(bitstring_t &target, string_t &input) {
char *res_buf = target.GetDataWriteable();
const char *buf = input.GetData();
memset(res_buf, 0, input.GetSize());
res_buf[0] = buf[0];
Bit::Finalize(target);
}

void Bit::SetEmptyBitString(string_t &target, idx_t len) {
void Bit::SetEmptyBitString(bitstring_t &target, idx_t len) {
char *res_buf = target.GetDataWriteable();
memset(res_buf, 0, target.GetSize());
res_buf[0] = ComputePadding(len);
Bit::Finalize(target);
}

// **** casting functions ****
void Bit::ToString(string_t bits, char *output) {
void Bit::ToString(bitstring_t bits, char *output) {
auto data = const_data_ptr_cast(bits.GetData());
auto len = bits.GetSize();

Expand All @@ -87,7 +87,7 @@ void Bit::ToString(string_t bits, char *output) {
}
}

string Bit::ToString(string_t str) {
string Bit::ToString(bitstring_t str) {
auto len = BitLength(str);
auto buffer = make_unsafe_uniq_array_uninitialized<char>(len);
ToString(str, buffer.get());
Expand Down Expand Up @@ -117,7 +117,7 @@ bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_messag
return true;
}

void Bit::ToBit(string_t str, string_t &output_str) {
void Bit::ToBit(string_t str, bitstring_t &output_str) {
auto data = const_data_ptr_cast(str.GetData());
auto len = str.GetSize();
auto output = output_str.GetDataWriteable();
Expand Down Expand Up @@ -151,12 +151,12 @@ void Bit::ToBit(string_t str, string_t &output_str) {
string Bit::ToBit(string_t str) {
auto bit_len = GetBitSize(str);
auto buffer = make_unsafe_uniq_array_uninitialized<char>(bit_len);
string_t output_str(buffer.get(), UnsafeNumericCast<uint32_t>(bit_len));
bitstring_t output_str(buffer.get(), UnsafeNumericCast<uint32_t>(bit_len));
Bit::ToBit(str, output_str);
return output_str.GetString();
}

void Bit::BlobToBit(string_t blob, string_t &output_str) {
void Bit::BlobToBit(string_t blob, bitstring_t &output_str) {
auto data = const_data_ptr_cast(blob.GetData());
auto output = output_str.GetDataWriteable();
idx_t size = blob.GetSize();
Expand All @@ -167,12 +167,12 @@ void Bit::BlobToBit(string_t blob, string_t &output_str) {

string Bit::BlobToBit(string_t blob) {
auto buffer = make_unsafe_uniq_array_uninitialized<char>(blob.GetSize() + 1);
string_t output_str(buffer.get(), UnsafeNumericCast<uint32_t>(blob.GetSize() + 1));
bitstring_t output_str(buffer.get(), UnsafeNumericCast<uint32_t>(blob.GetSize() + 1));
Bit::BlobToBit(blob, output_str);
return output_str.GetString();
}

void Bit::BitToBlob(string_t bit, string_t &output_blob) {
void Bit::BitToBlob(bitstring_t bit, string_t &output_blob) {
D_ASSERT(bit.GetSize() == output_blob.GetSize() + 1);

auto data = const_data_ptr_cast(bit.GetData());
Expand All @@ -189,7 +189,7 @@ void Bit::BitToBlob(string_t bit, string_t &output_blob) {
}
}

string Bit::BitToBlob(string_t bit) {
string Bit::BitToBlob(bitstring_t bit) {
D_ASSERT(bit.GetSize() > 1);

auto buffer = make_unsafe_uniq_array_uninitialized<char>(bit.GetSize() - 1);
Expand All @@ -199,7 +199,7 @@ string Bit::BitToBlob(string_t bit) {
}

// **** scalar functions ****
void Bit::BitString(const string_t &input, const idx_t &bit_length, string_t &result) {
void Bit::BitString(const string_t &input, idx_t bit_length, bitstring_t &result) {
char *res_buf = result.GetDataWriteable();
const char *buf = input.GetData();

Expand All @@ -217,15 +217,35 @@ void Bit::BitString(const string_t &input, const idx_t &bit_length, string_t &re
Bit::Finalize(result);
}

idx_t Bit::BitLength(string_t bits) {
void Bit::ExtendBitString(const bitstring_t &input, idx_t bit_length, bitstring_t &result) {
uint8_t *res_buf = reinterpret_cast<uint8_t *>(result.GetDataWriteable());

auto padding = ComputePadding(bit_length);
res_buf[0] = static_cast<uint8_t>(padding);

idx_t original_length = Bit::BitLength(input);
D_ASSERT(bit_length >= original_length);
idx_t shift = bit_length - original_length;
for (idx_t i = 0; i < bit_length; i++) {
if (i < shift) {
Bit::SetBit(result, i, 0);
} else {
idx_t bit = Bit::GetBit(input, i - shift);
Bit::SetBit(result, i, bit);
}
}
Bit::Finalize(result);
}

idx_t Bit::BitLength(bitstring_t bits) {
return ((bits.GetSize() - 1) * 8) - GetBitPadding(bits);
}

idx_t Bit::OctetLength(string_t bits) {
idx_t Bit::OctetLength(bitstring_t bits) {
return bits.GetSize() - 1;
}

idx_t Bit::BitCount(string_t bits) {
idx_t Bit::BitCount(bitstring_t bits) {
idx_t count = 0;
const char *buf = bits.GetData();
for (idx_t byte_idx = 1; byte_idx < OctetLength(bits) + 1; byte_idx++) {
Expand All @@ -236,7 +256,7 @@ idx_t Bit::BitCount(string_t bits) {
return count - GetBitPadding(bits);
}

idx_t Bit::BitPosition(string_t substring, string_t bits) {
idx_t Bit::BitPosition(bitstring_t substring, bitstring_t bits) {
const char *buf = bits.GetData();
auto len = bits.GetSize();
auto substr_len = BitLength(substring);
Expand Down Expand Up @@ -270,28 +290,28 @@ idx_t Bit::BitPosition(string_t substring, string_t bits) {
return 0;
}

idx_t Bit::GetBit(string_t bit_string, idx_t n) {
idx_t Bit::GetBit(bitstring_t bit_string, idx_t n) {
return Bit::GetBitInternal(bit_string, n + GetBitPadding(bit_string));
}

idx_t Bit::GetBitIndex(idx_t n) {
return n / 8 + 1;
}

idx_t Bit::GetBitInternal(string_t bit_string, idx_t n) {
idx_t Bit::GetBitInternal(bitstring_t bit_string, idx_t n) {
const char *buf = bit_string.GetData();
auto idx = Bit::GetBitIndex(n);
D_ASSERT(idx < bit_string.GetSize());
auto byte = buf[idx] >> (7 - (n % 8));
return (byte & 1 ? 1 : 0);
}

void Bit::SetBit(string_t &bit_string, idx_t n, idx_t new_value) {
void Bit::SetBit(bitstring_t &bit_string, idx_t n, idx_t new_value) {
SetBitInternal(bit_string, n + GetBitPadding(bit_string), new_value);
Bit::Finalize(bit_string);
}

void Bit::SetBitInternal(string_t &bit_string, idx_t n, idx_t new_value) {
void Bit::SetBitInternal(bitstring_t &bit_string, idx_t n, idx_t new_value) {
uint8_t *buf = reinterpret_cast<uint8_t *>(bit_string.GetDataWriteable());

auto idx = Bit::GetBitIndex(n);
Expand All @@ -306,7 +326,7 @@ void Bit::SetBitInternal(string_t &bit_string, idx_t n, idx_t new_value) {
}

// **** BITWISE operators ****
void Bit::RightShift(const string_t &bit_string, const idx_t &shift, string_t &result) {
void Bit::RightShift(const bitstring_t &bit_string, idx_t shift, bitstring_t &result) {
uint8_t *res_buf = reinterpret_cast<uint8_t *>(result.GetDataWriteable());
const uint8_t *buf = reinterpret_cast<const uint8_t *>(bit_string.GetData());

Expand All @@ -323,7 +343,7 @@ void Bit::RightShift(const string_t &bit_string, const idx_t &shift, string_t &r
Bit::Finalize(result);
}

void Bit::LeftShift(const string_t &bit_string, const idx_t &shift, string_t &result) {
void Bit::LeftShift(const bitstring_t &bit_string, idx_t shift, bitstring_t &result) {
uint8_t *res_buf = reinterpret_cast<uint8_t *>(result.GetDataWriteable());
const uint8_t *buf = reinterpret_cast<const uint8_t *>(bit_string.GetData());

Expand All @@ -340,7 +360,7 @@ void Bit::LeftShift(const string_t &bit_string, const idx_t &shift, string_t &re
Bit::Finalize(result);
}

void Bit::BitwiseAnd(const string_t &rhs, const string_t &lhs, string_t &result) {
void Bit::BitwiseAnd(const bitstring_t &rhs, const bitstring_t &lhs, bitstring_t &result) {
if (Bit::BitLength(lhs) != Bit::BitLength(rhs)) {
throw InvalidInputException("Cannot AND bit strings of different sizes");
}
Expand All @@ -356,7 +376,7 @@ void Bit::BitwiseAnd(const string_t &rhs, const string_t &lhs, string_t &result)
Bit::Finalize(result);
}

void Bit::BitwiseOr(const string_t &rhs, const string_t &lhs, string_t &result) {
void Bit::BitwiseOr(const bitstring_t &rhs, const bitstring_t &lhs, bitstring_t &result) {
if (Bit::BitLength(lhs) != Bit::BitLength(rhs)) {
throw InvalidInputException("Cannot OR bit strings of different sizes");
}
Expand All @@ -372,7 +392,7 @@ void Bit::BitwiseOr(const string_t &rhs, const string_t &lhs, string_t &result)
Bit::Finalize(result);
}

void Bit::BitwiseXor(const string_t &rhs, const string_t &lhs, string_t &result) {
void Bit::BitwiseXor(const bitstring_t &rhs, const bitstring_t &lhs, bitstring_t &result) {
if (Bit::BitLength(lhs) != Bit::BitLength(rhs)) {
throw InvalidInputException("Cannot XOR bit strings of different sizes");
}
Expand All @@ -388,7 +408,7 @@ void Bit::BitwiseXor(const string_t &rhs, const string_t &lhs, string_t &result)
Bit::Finalize(result);
}

void Bit::BitwiseNot(const string_t &input, string_t &result) {
void Bit::BitwiseNot(const bitstring_t &input, bitstring_t &result) {
uint8_t *result_buf = reinterpret_cast<uint8_t *>(result.GetDataWriteable());
const uint8_t *buf = reinterpret_cast<const uint8_t *>(input.GetData());

Expand All @@ -399,7 +419,7 @@ void Bit::BitwiseNot(const string_t &input, string_t &result) {
Bit::Finalize(result);
}

void Bit::Verify(const string_t &input) {
void Bit::Verify(const bitstring_t &input) {
#ifdef DEBUG
// bit strings require all padding bits to be set to 1
auto padding = GetBitPadding(input);
Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/src/core_functions/function_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static const StaticFunctionDefinition internal_functions[] = {
DUCKDB_AGGREGATE_FUNCTION_SET(BitOrFun),
DUCKDB_SCALAR_FUNCTION(BitPositionFun),
DUCKDB_AGGREGATE_FUNCTION_SET(BitXorFun),
DUCKDB_SCALAR_FUNCTION(BitStringFun),
DUCKDB_SCALAR_FUNCTION_SET(BitStringFun),
DUCKDB_AGGREGATE_FUNCTION_SET(BitstringAggFun),
DUCKDB_AGGREGATE_FUNCTION(BoolAndFun),
DUCKDB_AGGREGATE_FUNCTION(BoolOrFun),
Expand Down
28 changes: 23 additions & 5 deletions src/duckdb/src/core_functions/scalar/bit/bitstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,46 @@ namespace duckdb {
//===--------------------------------------------------------------------===//
// BitStringFunction
//===--------------------------------------------------------------------===//
template <bool FROM_STRING>
static void BitStringFunction(DataChunk &args, ExpressionState &state, Vector &result) {
BinaryExecutor::Execute<string_t, int32_t, string_t>(
args.data[0], args.data[1], result, args.size(), [&](string_t input, int32_t n) {
if (n < 0) {
throw InvalidInputException("The bitstring length cannot be negative");
}
if (idx_t(n) < input.GetSize()) {
idx_t input_length;
if (FROM_STRING) {
input_length = input.GetSize();
} else {
input_length = Bit::BitLength(input);
}
if (idx_t(n) < input_length) {
throw InvalidInputException("Length must be equal or larger than input string");
}
idx_t len;
Bit::TryGetBitStringSize(input, len, nullptr); // string verification
if (FROM_STRING) {
Bit::TryGetBitStringSize(input, len, nullptr); // string verification
}

len = Bit::ComputeBitstringLen(UnsafeNumericCast<idx_t>(n));
string_t target = StringVector::EmptyString(result, len);
Bit::BitString(input, UnsafeNumericCast<idx_t>(n), target);
if (FROM_STRING) {
Bit::BitString(input, UnsafeNumericCast<idx_t>(n), target);
} else {
Bit::ExtendBitString(input, UnsafeNumericCast<idx_t>(n), target);
}
target.Finalize();
return target;
});
}

ScalarFunction BitStringFun::GetFunction() {
return ScalarFunction({LogicalType::VARCHAR, LogicalType::INTEGER}, LogicalType::BIT, BitStringFunction);
ScalarFunctionSet BitStringFun::GetFunctions() {
ScalarFunctionSet bitstring;
bitstring.AddFunction(
ScalarFunction({LogicalType::VARCHAR, LogicalType::INTEGER}, LogicalType::BIT, BitStringFunction<true>));
bitstring.AddFunction(
ScalarFunction({LogicalType::BIT, LogicalType::INTEGER}, LogicalType::BIT, BitStringFunction<false>));
return bitstring;
}

//===--------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit 16f6120

Please sign in to comment.