Skip to content

Commit

Permalink
Update vendored DuckDB sources to b611ab5
Browse files Browse the repository at this point in the history
  • Loading branch information
duckdblabs-bot committed Dec 15, 2024
1 parent b611ab5 commit fb8a39a
Show file tree
Hide file tree
Showing 31 changed files with 336 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/duckdb/extension/core_functions/function_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_SCALAR_FUNCTION_SET(MakeDateFun),
DUCKDB_SCALAR_FUNCTION(MakeTimeFun),
DUCKDB_SCALAR_FUNCTION_SET(MakeTimestampFun),
DUCKDB_SCALAR_FUNCTION_SET(MakeTimestampNsFun),
DUCKDB_SCALAR_FUNCTION(MapFun),
DUCKDB_SCALAR_FUNCTION(MapConcatFun),
DUCKDB_SCALAR_FUNCTION(MapEntriesFun),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ struct MakeTimestampFun {
static ScalarFunctionSet GetFunctions();
};

struct MakeTimestampNsFun {
static constexpr const char *Name = "make_timestamp_ns";
static constexpr const char *Parameters = "nanos";
static constexpr const char *Description = "The timestamp for the given nanoseconds since epoch";
static constexpr const char *Example = "make_timestamp(1732117793000000000)";

static ScalarFunctionSet GetFunctions();
};

struct MicrosecondsFun {
static constexpr const char *Name = "microsecond";
static constexpr const char *Parameters = "ts";
Expand Down
16 changes: 16 additions & 0 deletions src/duckdb/extension/core_functions/scalar/date/date_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,19 @@ ScalarFunctionSet EpochFun::GetFunctions() {
return GetTimePartFunction<DatePart::EpochOperator, double>(LogicalType::DOUBLE);
}

struct GetEpochNanosOperator {
static int64_t Operation(timestamp_ns_t timestamp) {
return Timestamp::GetEpochNanoSeconds(timestamp);
}
};

static void ExecuteGetNanosFromTimestampNs(DataChunk &input, ExpressionState &state, Vector &result) {
D_ASSERT(input.ColumnCount() == 1);

auto func = GetEpochNanosOperator::Operation;
UnaryExecutor::Execute<timestamp_ns_t, int64_t>(input.data[0], result, input.size(), func);
}

ScalarFunctionSet EpochNsFun::GetFunctions() {
using OP = DatePart::EpochNanosecondsOperator;
auto operator_set = GetTimePartFunction<OP>();
Expand All @@ -2074,6 +2087,9 @@ ScalarFunctionSet EpochNsFun::GetFunctions() {
auto tstz_stats = OP::template PropagateStatistics<timestamp_t>;
operator_set.AddFunction(
ScalarFunction({LogicalType::TIMESTAMP_TZ}, LogicalType::BIGINT, tstz_func, nullptr, nullptr, tstz_stats));

operator_set.AddFunction(
ScalarFunction({LogicalType::TIMESTAMP_NS}, LogicalType::BIGINT, ExecuteGetNanosFromTimestampNs));
return operator_set;
}

Expand Down
20 changes: 18 additions & 2 deletions src/duckdb/extension/core_functions/scalar/date/make_date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ struct MakeTimestampOperator {
}

template <typename T, typename RESULT_TYPE>
static RESULT_TYPE Operation(T micros) {
return timestamp_t(micros);
static RESULT_TYPE Operation(T value) {
return RESULT_TYPE(value);
}
};

Expand All @@ -121,6 +121,15 @@ static void ExecuteMakeTimestamp(DataChunk &input, ExpressionState &state, Vecto
SenaryExecutor::Execute<T, T, T, T, T, double, timestamp_t>(input, result, func);
}

template <typename T>
static void ExecuteMakeTimestampNs(DataChunk &input, ExpressionState &state, Vector &result) {
D_ASSERT(input.ColumnCount() == 1);

auto func = MakeTimestampOperator::Operation<T, timestamp_ns_t>;
UnaryExecutor::Execute<T, timestamp_ns_t>(input.data[0], result, input.size(), func);
return;
}

ScalarFunctionSet MakeDateFun::GetFunctions() {
ScalarFunctionSet make_date("make_date");
make_date.AddFunction(ScalarFunction({LogicalType::INTEGER}, LogicalType::DATE, MakeDateFromEpoch));
Expand Down Expand Up @@ -149,4 +158,11 @@ ScalarFunctionSet MakeTimestampFun::GetFunctions() {
return operator_set;
}

ScalarFunctionSet MakeTimestampNsFun::GetFunctions() {
ScalarFunctionSet operator_set("make_timestamp_ns");
operator_set.AddFunction(
ScalarFunction({LogicalType::BIGINT}, LogicalType::TIMESTAMP_NS, ExecuteMakeTimestampNs<int64_t>));
return operator_set;
}

} // namespace duckdb
3 changes: 1 addition & 2 deletions src/duckdb/src/common/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ void DuckDBAssertInternal(bool condition, const char *condition_name, const char
if (condition) {
return;
}
throw InternalException("Assertion triggered in file \"%s\" on line %d: %s%s", file, linenr, condition_name,
Exception::GetStackTrace());
throw InternalException("Assertion triggered in file \"%s\" on line %d: %s", file, linenr, condition_name);
}

} // namespace duckdb
25 changes: 21 additions & 4 deletions src/duckdb/src/common/error_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/to_string.hpp"
#include "duckdb/common/types.hpp"
#include "duckdb/common/stacktrace.hpp"
#include "duckdb/parser/parsed_expression.hpp"
#include "duckdb/parser/query_error_context.hpp"
#include "duckdb/parser/tableref.hpp"
Expand Down Expand Up @@ -102,12 +103,28 @@ void ErrorData::ConvertErrorToJSON() {
final_message = raw_message;
}

void ErrorData::FinalizeError() {
auto entry = extra_info.find("stack_trace_pointers");
if (entry != extra_info.end()) {
auto stack_trace = StackTrace::ResolveStacktraceSymbols(entry->second);
extra_info["stack_trace"] = std::move(stack_trace);
extra_info.erase("stack_trace_pointers");
}
}

void ErrorData::AddErrorLocation(const string &query) {
auto entry = extra_info.find("position");
if (entry == extra_info.end()) {
return;
if (!query.empty()) {
auto entry = extra_info.find("position");
if (entry != extra_info.end()) {
raw_message = QueryErrorContext::Format(query, raw_message, std::stoull(entry->second));
}
}
{
auto entry = extra_info.find("stack_trace");
if (entry != extra_info.end()) {
raw_message += "\n\nStack Trace:\n" + entry->second;
}
}
raw_message = QueryErrorContext::Format(query, raw_message, std::stoull(entry->second));
final_message = ConstructFinalMessage();
}

Expand Down
38 changes: 13 additions & 25 deletions src/duckdb/src/common/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#include <stdio.h>
#include <stdlib.h>
#endif
#ifdef DUCKDB_DEBUG_STACKTRACE
#include <execinfo.h>
#endif
#include "duckdb/common/stacktrace.hpp"

namespace duckdb {

Expand All @@ -32,13 +30,16 @@ string Exception::ToJSON(ExceptionType type, const string &message) {
}

string Exception::ToJSON(ExceptionType type, const string &message, const unordered_map<string, string> &extra_info) {
#ifdef DUCKDB_DEBUG_STACKTRACE
auto extended_extra_info = extra_info;
extended_extra_info["stack_trace"] = Exception::GetStackTrace();
return StringUtil::ExceptionToJSONMap(type, message, extended_extra_info);
#else
return StringUtil::ExceptionToJSONMap(type, message, extra_info);
#ifndef DUCKDB_DEBUG_STACKTRACE
// by default we only enable stack traces for internal exceptions
if (type == ExceptionType::INTERNAL)
#endif
{
auto extended_extra_info = extra_info;
extended_extra_info["stack_trace_pointers"] = StackTrace::GetStacktracePointers();
return StringUtil::ExceptionToJSONMap(type, message, extended_extra_info);
}
return StringUtil::ExceptionToJSONMap(type, message, extra_info);
}

bool Exception::UncaughtException() {
Expand Down Expand Up @@ -73,22 +74,8 @@ bool Exception::InvalidatesDatabase(ExceptionType exception_type) {
}
}

string Exception::GetStackTrace(int max_depth) {
#ifdef DUCKDB_DEBUG_STACKTRACE
string result;
auto callstack = unique_ptr<void *[]>(new void *[max_depth]);
int frames = backtrace(callstack.get(), max_depth);
char **strs = backtrace_symbols(callstack.get(), frames);
for (int i = 0; i < frames; i++) {
result += strs[i];
result += "\n";
}
free(strs);
return "\n" + result;
#else
// Stack trace not available. Toggle DUCKDB_DEBUG_STACKTRACE in exception.cpp to enable stack traces.
return "";
#endif
string Exception::GetStackTrace(idx_t max_depth) {
return StackTrace::GetStackTrace(max_depth);
}

string Exception::ConstructMessageRecursive(const string &msg, std::vector<ExceptionFormatValue> &values) {
Expand Down Expand Up @@ -333,6 +320,7 @@ FatalException::FatalException(ExceptionType type, const string &msg) : Exceptio
InternalException::InternalException(const string &msg) : Exception(ExceptionType::INTERNAL, msg) {
#ifdef DUCKDB_CRASH_ON_ASSERT
Printer::Print("ABORT THROWN BY INTERNAL EXCEPTION: " + msg);
Printer::Print(StackTrace::GetStackTrace());
abort();
#endif
}
Expand Down
88 changes: 88 additions & 0 deletions src/duckdb/src/common/stacktrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "duckdb/common/stacktrace.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/to_string.hpp"

#if defined(__GLIBC__) || defined(__APPLE__)
#include <execinfo.h>
#include <cxxabi.h>
#endif

namespace duckdb {

#if defined(__GLIBC__) || defined(__APPLE__)
static string UnmangleSymbol(string symbol) {
// find the mangled name
idx_t mangle_start = symbol.size();
idx_t mangle_end = 0;
for (idx_t i = 0; i < symbol.size(); ++i) {
if (symbol[i] == '_') {
mangle_start = i;
break;
}
}
for (idx_t i = mangle_start; i < symbol.size(); i++) {
if (StringUtil::CharacterIsSpace(symbol[i])) {
mangle_end = i;
break;
}
}
if (mangle_start >= mangle_end) {
return symbol;
}
string mangled_symbol = symbol.substr(mangle_start, mangle_end - mangle_start);

int status;
auto demangle_result = abi::__cxa_demangle(mangled_symbol.c_str(), nullptr, nullptr, &status);
if (status != 0 || !demangle_result) {
return symbol;
}
string result;
result += symbol.substr(0, mangle_start);
result += demangle_result;
result += symbol.substr(mangle_end);
free(demangle_result);
return result;
}

string StackTrace::GetStacktracePointers(idx_t max_depth) {
string result;
auto callstack = unique_ptr<void *[]>(new void *[max_depth]);
int frames = backtrace(callstack.get(), NumericCast<int32_t>(max_depth));
// skip two frames (these are always StackTrace::...)
for (idx_t i = 2; i < NumericCast<idx_t>(frames); i++) {
if (!result.empty()) {
result += ";";
}
result += to_string(CastPointerToValue(callstack[i]));
}
return result;
}

string StackTrace::ResolveStacktraceSymbols(const string &pointers) {
auto splits = StringUtil::Split(pointers, ";");
idx_t frame_count = splits.size();
auto callstack = unique_ptr<void *[]>(new void *[frame_count]);
for (idx_t i = 0; i < frame_count; i++) {
callstack[i] = cast_uint64_to_pointer(StringUtil::ToUnsigned(splits[i]));
}
string result;
char **strs = backtrace_symbols(callstack.get(), NumericCast<int>(frame_count));
for (idx_t i = 0; i < frame_count; i++) {
result += UnmangleSymbol(strs[i]);
result += "\n";
}
free(reinterpret_cast<void *>(strs));
return "\n" + result;
}

#else
string StackTrace::GetStacktracePointers(idx_t max_depth) {
return string();
}

string StackTrace::ResolveStacktraceSymbols(const string &pointers) {
return string();
}
#endif

} // namespace duckdb
4 changes: 4 additions & 0 deletions src/duckdb/src/common/string_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ bool StringUtil::Contains(const string &haystack, const char &needle_char) {
return (haystack.find(needle_char) != string::npos);
}

idx_t StringUtil::ToUnsigned(const string &str) {
return std::stoull(str);
}

void StringUtil::LTrim(string &str) {
auto it = str.begin();
while (it != str.end() && CharacterIsSpace(*it)) {
Expand Down
5 changes: 5 additions & 0 deletions src/duckdb/src/common/types/timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ int64_t Timestamp::GetEpochNanoSeconds(timestamp_t timestamp) {
return result;
}

int64_t Timestamp::GetEpochNanoSeconds(timestamp_ns_t timestamp) {
D_ASSERT(Timestamp::IsFinite(timestamp));
return timestamp.value;
}

int64_t Timestamp::GetEpochRounded(timestamp_t input, int64_t power_of_ten) {
D_ASSERT(Timestamp::IsFinite(input));
// Round away from the epoch.
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/src/function/table/version/pragma_version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DUCKDB_PATCH_VERSION
#define DUCKDB_PATCH_VERSION "4-dev3588"
#define DUCKDB_PATCH_VERSION "4-dev3633"
#endif
#ifndef DUCKDB_MINOR_VERSION
#define DUCKDB_MINOR_VERSION 1
Expand All @@ -8,10 +8,10 @@
#define DUCKDB_MAJOR_VERSION 1
#endif
#ifndef DUCKDB_VERSION
#define DUCKDB_VERSION "v1.1.4-dev3588"
#define DUCKDB_VERSION "v1.1.4-dev3633"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "a91feadf8c"
#define DUCKDB_SOURCE_ID "78ebe44ef9"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
14 changes: 14 additions & 0 deletions src/duckdb/src/include/duckdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -3892,6 +3892,20 @@ Append a DEFAULT value (NULL if DEFAULT not available for column) to the appende
*/
DUCKDB_API duckdb_state duckdb_append_default(duckdb_appender appender);

/*!
Append a DEFAULT value, at the specified row and column, (NULL if DEFAULT not available for column) to the chunk created
from the specified appender. The default value of the column must be a constant value. Non-deterministic expressions
like nextval('seq') or random() are not supported.
* @param appender The appender to get the default value from.
* @param chunk The data chunk to append the default value to.
* @param col The chunk column index to append the default value to.
* @param row The chunk row index to append the default value to.
* @return `DuckDBSuccess` on success or `DuckDBError` on failure.
*/
DUCKDB_API duckdb_state duckdb_append_default_to_chunk(duckdb_appender appender, duckdb_data_chunk chunk, idx_t col,
idx_t row);

/*!
Append a bool value to the appender.
*/
Expand Down
1 change: 1 addition & 0 deletions src/duckdb/src/include/duckdb/common/error_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ErrorData {
return extra_info;
}

DUCKDB_API void FinalizeError();
DUCKDB_API void AddErrorLocation(const string &query);
DUCKDB_API void ConvertErrorToJSON();

Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/src/include/duckdb/common/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Exception : public std::runtime_error {

DUCKDB_API static bool UncaughtException();

DUCKDB_API static string GetStackTrace(int max_depth = 120);
DUCKDB_API static string GetStackTrace(idx_t max_depth = 120);
static string FormatStackTrace(const string &message = "") {
return (message + "\n" + GetStackTrace());
}
Expand Down
Loading

0 comments on commit fb8a39a

Please sign in to comment.