Skip to content

Commit

Permalink
Merge branch 'main' into jemalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
lnkuiper committed Jan 27, 2025
2 parents 507d173 + 97d9063 commit e8acd91
Show file tree
Hide file tree
Showing 73 changed files with 700 additions and 534 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/duckdb/extension/core_functions/function_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_AGGREGATE_FUNCTION(CovarPopFun),
DUCKDB_AGGREGATE_FUNCTION(CovarSampFun),
DUCKDB_SCALAR_FUNCTION(CurrentDatabaseFun),
DUCKDB_SCALAR_FUNCTION(CurrentDateFun),
DUCKDB_SCALAR_FUNCTION(CurrentQueryFun),
DUCKDB_SCALAR_FUNCTION(CurrentSchemaFun),
DUCKDB_SCALAR_FUNCTION(CurrentSchemasFun),
Expand Down Expand Up @@ -192,6 +193,7 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_SCALAR_FUNCTION_ALIAS(GenRandomUuidFun),
DUCKDB_SCALAR_FUNCTION_SET(GenerateSeriesFun),
DUCKDB_SCALAR_FUNCTION(GetBitFun),
DUCKDB_SCALAR_FUNCTION(CurrentTimeFun),
DUCKDB_SCALAR_FUNCTION(GetCurrentTimestampFun),
DUCKDB_SCALAR_FUNCTION_SET_ALIAS(GradeUpFun),
DUCKDB_SCALAR_FUNCTION_SET(GreatestFun),
Expand Down Expand Up @@ -369,6 +371,7 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_SCALAR_FUNCTION(ToTimestampFun),
DUCKDB_SCALAR_FUNCTION(ToWeeksFun),
DUCKDB_SCALAR_FUNCTION(ToYearsFun),
DUCKDB_SCALAR_FUNCTION_ALIAS(TodayFun),
DUCKDB_SCALAR_FUNCTION_ALIAS(TransactionTimestampFun),
DUCKDB_SCALAR_FUNCTION(TranslateFun),
DUCKDB_SCALAR_FUNCTION_SET(TrimFun),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ struct CenturyFun {
static ScalarFunctionSet GetFunctions();
};

struct CurrentDateFun {
static constexpr const char *Name = "current_date";
static constexpr const char *Parameters = "";
static constexpr const char *Description = "Returns the current date";
static constexpr const char *Example = "current_date()";

static ScalarFunction GetFunction();
};

struct TodayFun {
using ALIAS = CurrentDateFun;

static constexpr const char *Name = "today";
};

struct DateDiffFun {
static constexpr const char *Name = "date_diff";
static constexpr const char *Parameters = "part,startdate,enddate";
Expand Down Expand Up @@ -183,6 +198,15 @@ struct EpochNsFun {
static ScalarFunctionSet GetFunctions();
};

struct CurrentTimeFun {
static constexpr const char *Name = "get_current_time";
static constexpr const char *Parameters = "";
static constexpr const char *Description = "Returns the current time";
static constexpr const char *Example = "get_current_time()";

static ScalarFunction GetFunction();
};

struct EraFun {
static constexpr const char *Name = "era";
static constexpr const char *Parameters = "ts";
Expand Down
38 changes: 38 additions & 0 deletions src/duckdb/extension/core_functions/scalar/date/current.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "duckdb/main/client_context.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/transaction/meta_transaction.hpp"
#include "duckdb/planner/expression/bound_cast_expression.hpp"

namespace duckdb {

Expand All @@ -26,4 +27,41 @@ ScalarFunction GetCurrentTimestampFun::GetFunction() {
return current_timestamp;
}

static unique_ptr<Expression> CurrentTimeExpr(FunctionBindExpressionInput &input) {
auto timestamp = GetCurrentTimestampFun::GetFunction();
timestamp.name = GetCurrentTimestampFun::Name;

vector<unique_ptr<Expression>> args;

auto func = make_uniq_base<Expression, BoundFunctionExpression>(LogicalType::TIMESTAMP_TZ, timestamp,
std::move(args), nullptr);

return BoundCastExpression::AddCastToType(input.context, std::move(func), LogicalType::TIME_TZ);
}

static unique_ptr<Expression> CurrentDateExpr(FunctionBindExpressionInput &input) {
auto timestamp = GetCurrentTimestampFun::GetFunction();
timestamp.name = GetCurrentTimestampFun::Name;

vector<unique_ptr<Expression>> args;

auto func = make_uniq_base<Expression, BoundFunctionExpression>(LogicalType::TIMESTAMP_TZ, timestamp,
std::move(args), nullptr);
return BoundCastExpression::AddCastToType(input.context, std::move(func), LogicalType::DATE);
}

ScalarFunction CurrentTimeFun::GetFunction() {
ScalarFunction current_time({}, LogicalType::TIME_TZ, nullptr);
current_time.bind_expression = CurrentTimeExpr;
current_time.stability = FunctionStability::CONSISTENT_WITHIN_QUERY;
return current_time;
}

ScalarFunction CurrentDateFun::GetFunction() {
ScalarFunction current_date({}, LogicalType::DATE, nullptr);
current_date.bind_expression = CurrentDateExpr;
current_date.stability = FunctionStability::CONSISTENT_WITHIN_QUERY;
return current_date;
}

} // namespace duckdb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ static void CanCastImplicitlyFunction(DataChunk &args, ExpressionState &state, V
}

unique_ptr<Expression> BindCanCastImplicitlyExpression(FunctionBindExpressionInput &input) {
auto &source_type = input.function.children[0]->return_type;
auto &target_type = input.function.children[1]->return_type;
auto &source_type = input.children[0]->return_type;
auto &target_type = input.children[1]->return_type;
if (source_type.id() == LogicalTypeId::UNKNOWN || source_type.id() == LogicalTypeId::SQLNULL ||
target_type.id() == LogicalTypeId::UNKNOWN || target_type.id() == LogicalTypeId::SQLNULL) {
// parameter - unknown return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static void TypeOfFunction(DataChunk &args, ExpressionState &state, Vector &resu
}

unique_ptr<Expression> BindTypeOfFunctionExpression(FunctionBindExpressionInput &input) {
auto &return_type = input.function.children[0]->return_type;
auto &return_type = input.children[0]->return_type;
if (return_type.id() == LogicalTypeId::UNKNOWN || return_type.id() == LogicalTypeId::SQLNULL) {
// parameter - unknown return type
return nullptr;
Expand Down
4 changes: 3 additions & 1 deletion src/duckdb/extension/json/json_functions/json_structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ static double CalculateTypeSimilarity(const LogicalType &merged, const LogicalTy
}

// Only maps and structs can be merged into a map
D_ASSERT(type.id() == LogicalTypeId::STRUCT);
if (type.id() != LogicalTypeId::STRUCT) {
return -1;
}
return CalculateMapAndStructSimilarity(merged, type, false, max_depth, depth);
}
case LogicalTypeId::LIST: {
Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/extension/parquet/column_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ void WriteValue(DlbaEncoder &encoder, WriteStream &writer, const string_t &value

// helpers to get size from strings
template <class SRC>
static constexpr idx_t GetDlbaStringSize(const SRC &src_value) {
static idx_t GetDlbaStringSize(const SRC &src_value) {
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/duckdb/extension/parquet/include/resizable_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ResizeableBuffer : public ByteBuffer {
}
if (new_size > alloc_len) {
alloc_len = NextPowerOfTwo(new_size);
allocated_data.Reset(); // Have to reset before allocating new buffer (otherwise we use ~2x the memory)
allocated_data = allocator.Allocate(alloc_len);
ptr = allocated_data.get();
}
Expand Down
26 changes: 14 additions & 12 deletions src/duckdb/extension/parquet/parquet_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,18 +1497,19 @@ static void ParquetCopySerialize(Serializer &serializer, const FunctionData &bin
// We have to std::move them, otherwise MSVC will complain that it's not a "const T &&"
const auto compression_level = SerializeCompressionLevel(bind_data.compression_level);
D_ASSERT(DeserializeCompressionLevel(compression_level) == bind_data.compression_level);
ParquetWriteBindData default_value;
serializer.WritePropertyWithDefault(109, "compression_level", compression_level);
serializer.WritePropertyWithDefault(110, "row_groups_per_file", bind_data.row_groups_per_file,
std::move(ParquetWriteBindData().row_groups_per_file));
default_value.row_groups_per_file);
serializer.WritePropertyWithDefault(111, "debug_use_openssl", bind_data.debug_use_openssl,
std::move(ParquetWriteBindData().debug_use_openssl));
default_value.debug_use_openssl);
serializer.WritePropertyWithDefault(112, "dictionary_size_limit", bind_data.dictionary_size_limit,
std::move(ParquetWriteBindData().dictionary_size_limit));
default_value.dictionary_size_limit);
serializer.WritePropertyWithDefault(113, "bloom_filter_false_positive_ratio",
bind_data.bloom_filter_false_positive_ratio,
std::move(ParquetWriteBindData().bloom_filter_false_positive_ratio));
default_value.bloom_filter_false_positive_ratio);
serializer.WritePropertyWithDefault(114, "parquet_version", bind_data.parquet_version,
std::move(ParquetWriteBindData().parquet_version));
default_value.parquet_version);
}

static unique_ptr<FunctionData> ParquetCopyDeserialize(Deserializer &deserializer, CopyFunction &function) {
Expand All @@ -1528,16 +1529,17 @@ static unique_ptr<FunctionData> ParquetCopyDeserialize(Deserializer &deserialize
deserializer.ReadPropertyWithDefault<optional_idx>(109, "compression_level", compression_level);
data->compression_level = DeserializeCompressionLevel(compression_level);
D_ASSERT(SerializeCompressionLevel(data->compression_level) == compression_level);
ParquetWriteBindData default_value;
data->row_groups_per_file = deserializer.ReadPropertyWithExplicitDefault<optional_idx>(
110, "row_groups_per_file", std::move(ParquetWriteBindData().row_groups_per_file));
data->debug_use_openssl = deserializer.ReadPropertyWithExplicitDefault<bool>(
111, "debug_use_openssl", std::move(ParquetWriteBindData().debug_use_openssl));
110, "row_groups_per_file", default_value.row_groups_per_file);
data->debug_use_openssl =
deserializer.ReadPropertyWithExplicitDefault<bool>(111, "debug_use_openssl", default_value.debug_use_openssl);
data->dictionary_size_limit = deserializer.ReadPropertyWithExplicitDefault<idx_t>(
112, "dictionary_size_limit", std::move(ParquetWriteBindData().dictionary_size_limit));
112, "dictionary_size_limit", default_value.dictionary_size_limit);
data->bloom_filter_false_positive_ratio = deserializer.ReadPropertyWithExplicitDefault<double>(
113, "bloom_filter_false_positive_ratio", std::move(ParquetWriteBindData().bloom_filter_false_positive_ratio));
data->parquet_version = deserializer.ReadPropertyWithExplicitDefault(
114, "parquet_version", std::move(ParquetWriteBindData().parquet_version));
113, "bloom_filter_false_positive_ratio", default_value.bloom_filter_false_positive_ratio);
data->parquet_version =
deserializer.ReadPropertyWithExplicitDefault(114, "parquet_version", default_value.parquet_version);

return std::move(data);
}
Expand Down
3 changes: 0 additions & 3 deletions src/duckdb/src/catalog/default/default_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ static const DefaultMacro internal_macros[] = {

// date functions
{DEFAULT_SCHEMA, "date_add", {"date", "interval", nullptr}, {{nullptr, nullptr}}, "date + interval"},
{DEFAULT_SCHEMA, "current_date", {nullptr}, {{nullptr, nullptr}}, "current_timestamp::DATE"},
{DEFAULT_SCHEMA, "today", {nullptr}, {{nullptr, nullptr}}, "current_timestamp::DATE"},
{DEFAULT_SCHEMA, "get_current_time", {nullptr}, {{nullptr, nullptr}}, "current_timestamp::TIMETZ"},

// regexp functions
{DEFAULT_SCHEMA, "regexp_split_to_table", {"text", "pattern", nullptr}, {{nullptr, nullptr}}, "unnest(string_split_regex(text, pattern))"},
Expand Down
57 changes: 1 addition & 56 deletions src/duckdb/src/common/adbc/adbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ AdbcStatusCode duckdb_adbc_init(int version, void *driver, struct AdbcError *err
adbc_driver->ConnectionGetInfo = duckdb_adbc::ConnectionGetInfo;
adbc_driver->StatementGetParameterSchema = duckdb_adbc::StatementGetParameterSchema;
adbc_driver->ConnectionGetTableSchema = duckdb_adbc::ConnectionGetTableSchema;
adbc_driver->StatementSetSubstraitPlan = duckdb_adbc::StatementSetSubstraitPlan;
return ADBC_STATUS_OK;
}

Expand All @@ -70,7 +69,6 @@ struct DuckDBAdbcStatementWrapper {
ArrowArrayStream ingestion_stream;
IngestionMode ingestion_mode = IngestionMode::CREATE;
bool temporary_table = false;
uint8_t *substrait_plan;
uint64_t plan_length;
};

Expand Down Expand Up @@ -157,36 +155,6 @@ AdbcStatusCode DatabaseNew(struct AdbcDatabase *database, struct AdbcError *erro
return CheckResult(res, error, "Failed to allocate");
}

AdbcStatusCode StatementSetSubstraitPlan(struct AdbcStatement *statement, const uint8_t *plan, size_t length,
struct AdbcError *error) {
if (!statement) {
SetError(error, "Statement is not set");
return ADBC_STATUS_INVALID_ARGUMENT;
}
if (!plan) {
SetError(error, "Substrait Plan is not set");
return ADBC_STATUS_INVALID_ARGUMENT;
}
if (length == 0) {
SetError(error, "Can't execute plan with size = 0");
return ADBC_STATUS_INVALID_ARGUMENT;
}
auto wrapper = static_cast<DuckDBAdbcStatementWrapper *>(statement->private_data);
if (wrapper->ingestion_stream.release) {
// Release any resources currently held by the ingestion stream before we overwrite it
wrapper->ingestion_stream.release(&wrapper->ingestion_stream);
wrapper->ingestion_stream.release = nullptr;
}
if (wrapper->statement) {
duckdb_destroy_prepare(&wrapper->statement);
wrapper->statement = nullptr;
}
wrapper->substrait_plan = static_cast<uint8_t *>(malloc(sizeof(uint8_t) * length));
wrapper->plan_length = length;
memcpy(wrapper->substrait_plan, plan, length);
return ADBC_STATUS_OK;
}

AdbcStatusCode DatabaseSetOption(struct AdbcDatabase *database, const char *key, const char *value,
struct AdbcError *error) {
if (!database) {
Expand Down Expand Up @@ -677,7 +645,6 @@ AdbcStatusCode StatementNew(struct AdbcConnection *connection, struct AdbcStatem
statement_wrapper->ingestion_stream.release = nullptr;
statement_wrapper->ingestion_table_name = nullptr;
statement_wrapper->db_schema = nullptr;
statement_wrapper->substrait_plan = nullptr;
statement_wrapper->temporary_table = false;

statement_wrapper->ingestion_mode = IngestionMode::CREATE;
Expand Down Expand Up @@ -709,10 +676,6 @@ AdbcStatusCode StatementRelease(struct AdbcStatement *statement, struct AdbcErro
free(wrapper->db_schema);
wrapper->db_schema = nullptr;
}
if (wrapper->substrait_plan) {
free(wrapper->substrait_plan);
wrapper->substrait_plan = nullptr;
}
free(statement->private_data);
statement->private_data = nullptr;
return ADBC_STATUS_OK;
Expand Down Expand Up @@ -805,25 +768,7 @@ AdbcStatusCode StatementExecuteQuery(struct AdbcStatement *statement, struct Arr
if (has_stream && to_table) {
return IngestToTableFromBoundStream(wrapper, error);
}
if (wrapper->substrait_plan != nullptr) {
auto plan_str = std::string(reinterpret_cast<const char *>(wrapper->substrait_plan), wrapper->plan_length);
duckdb::vector<duckdb::Value> params;
params.emplace_back(duckdb::Value::BLOB_RAW(plan_str));
duckdb::unique_ptr<duckdb::QueryResult> query_result;
try {
query_result = reinterpret_cast<duckdb::Connection *>(wrapper->connection)
->TableFunction("from_substrait", params)
->Execute();
} catch (duckdb::Exception &e) {
std::string error_msg = "It was not possible to execute substrait query. " + std::string(e.what());
SetError(error, error_msg);
return ADBC_STATUS_INVALID_ARGUMENT;
}
auto arrow_wrapper = new duckdb::ArrowResultWrapper();
arrow_wrapper->result =
duckdb::unique_ptr_cast<duckdb::QueryResult, duckdb::MaterializedQueryResult>(std::move(query_result));
wrapper->result = reinterpret_cast<duckdb_arrow>(arrow_wrapper);
} else if (has_stream) {
if (has_stream) {
// A stream was bound to the statement, use that to bind parameters
duckdb::unique_ptr<duckdb::QueryResult> result;
ArrowArrayStream stream = wrapper->ingestion_stream;
Expand Down
4 changes: 2 additions & 2 deletions src/duckdb/src/common/arrow/arrow_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void SetArrowMapFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child,
bool SetArrowExtension(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, const LogicalType &type,
ClientContext &context) {
auto &config = DBConfig::GetConfig(context);
if (config.HasArrowExtension(type.id())) {
auto arrow_extension = config.GetArrowExtension(type.id());
if (config.HasArrowExtension(type)) {
auto arrow_extension = config.GetArrowExtension(type);
arrow_extension.PopulateArrowSchema(root_holder, child, type, context, arrow_extension);
return true;
}
Expand Down
Loading

0 comments on commit e8acd91

Please sign in to comment.