Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Appender] Add AppendDefault #11905

Merged
merged 30 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
59b7f7f
add AppendDefault
Tishj May 2, 2024
2cdb662
need to use a regular ExpressionExecutor if the expression isnt foldable
Tishj May 2, 2024
4de7b68
reintroduce the NULL behavior for AppendDefault on columns that have …
Tishj May 3, 2024
fab3b8b
what is happening
Tishj May 3, 2024
7d0732d
add tests for random() and now()
Tishj May 3, 2024
b735bc3
add fixme for further optimization
Tishj May 3, 2024
4603fc3
add basic tests for append_default in C API
Tishj May 7, 2024
2149946
append into column with NOT NULL constraint
Tishj May 7, 2024
4d6f9e2
early out
Tishj May 7, 2024
c9ee187
first PoC for AppendDefaultToVector
Tishj May 7, 2024
5432ddb
add duckdb_table_description to query whether columns have a default …
Tishj May 7, 2024
dde090c
might help if I actually add the files to git ;)
Tishj May 8, 2024
107c2c9
use the right variable name for selection vector
Tishj May 8, 2024
735d453
rename AppendDefaultToVector -> AppendDefaultsToVector
Tishj May 8, 2024
5314884
add size check for the Vector/SelectionVector and add debug check ver…
Tishj May 8, 2024
e31cea8
split the default value appender tests into sections
Tishj May 8, 2024
3a7d496
add additional tests for appending default(s) to Vector
Tishj May 8, 2024
8f3634a
add tests for errors thrown by the AppendDefaultsToVector method
Tishj May 8, 2024
4bb8f22
needs an ifdef because the check is only done in debug mode
Tishj May 8, 2024
cceb49c
remove comparison check for NULL value
Tishj May 8, 2024
2250f56
test that we can append the datachunk containing the newly set defaul…
Tishj May 8, 2024
7b43fae
improve comment
Tishj May 8, 2024
e94de30
minimize the explanation
Tishj May 8, 2024
6b3cce9
remove flush call from CAPIAppender destructor
Tishj May 8, 2024
9a10859
Merge remote-tracking branch 'upstream/main' into appender_append_def…
Tishj May 13, 2024
37bb2d8
add SelectionVector::Verify, change prototype of AppendDefaultsToVect…
Tishj May 13, 2024
4271a1f
Merge remote-tracking branch 'upstream/feature' into appender_append_…
Tishj May 22, 2024
ed7e0c7
remove table description
Tishj May 31, 2024
f285237
reduce complexity
Tishj May 31, 2024
783826d
final touches
Tishj May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/execution/expression_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ void ExpressionExecutor::ExecuteExpression(idx_t expr_idx, Vector &result) {
Execute(*expressions[expr_idx], states[expr_idx]->root_state.get(), nullptr, chunk ? chunk->size() : 1, result);
}

void ExpressionExecutor::ExecuteExpression(idx_t expr_idx, Vector &result, SelectionVector &vector) {
Tishj marked this conversation as resolved.
Show resolved Hide resolved
D_ASSERT(expr_idx < expressions.size());
D_ASSERT(result.GetType().id() == expressions[expr_idx]->return_type.id());
Execute(*expressions[expr_idx], states[expr_idx]->root_state.get(), &vector, chunk ? chunk->size() : 1, result);
}

Value ExpressionExecutor::EvaluateScalar(ClientContext &context, const Expression &expr, bool allow_unfoldable) {
D_ASSERT(allow_unfoldable || expr.IsFoldable());
D_ASSERT(expr.IsScalar());
Expand Down
9 changes: 8 additions & 1 deletion src/execution/expression_executor/execute_constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ unique_ptr<ExpressionState> ExpressionExecutor::InitializeState(const BoundConst
void ExpressionExecutor::Execute(const BoundConstantExpression &expr, ExpressionState *state,
const SelectionVector *sel, idx_t count, Vector &result) {
D_ASSERT(expr.value.type() == expr.return_type);
result.Reference(expr.value);
if (sel) {
for (idx_t i = 0; i < count; i++) {
auto index = sel->get_index(i);
result.SetValue(index, expr.value);
}
} else {
Tishj marked this conversation as resolved.
Show resolved Hide resolved
result.Reference(expr.value);
}
}

} // namespace duckdb
2 changes: 2 additions & 0 deletions src/include/duckdb/execution/expression_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ExpressionExecutor {

//! Execute the expression with index `expr_idx` and store the result in the result vector
DUCKDB_API void ExecuteExpression(idx_t expr_idx, Vector &result);
//! Same as above, only executing the expression for the rows provided by the SelectionVector
DUCKDB_API void ExecuteExpression(idx_t expr_idx, Vector &result, SelectionVector &sel);
Tishj marked this conversation as resolved.
Show resolved Hide resolved
//! Evaluate a scalar expression and fold it into a single value
DUCKDB_API static Value EvaluateScalar(ClientContext &context, const Expression &expr,
bool allow_unfoldable = false);
Expand Down
2 changes: 2 additions & 0 deletions src/include/duckdb/main/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ClientContext;
class DuckDB;
class TableCatalogEntry;
class Connection;
struct SelectionVector;

enum class AppenderType : uint8_t {
LOGICAL, // Cast input -> LogicalType
Expand Down Expand Up @@ -129,6 +130,7 @@ class Appender : public BaseAppender {

public:
void AppendDefault();
void AppendDefaultToVector(Vector &result, idx_t column, SelectionVector &sel, idx_t count);

protected:
void FlushInternal(ColumnDataCollection &collection) override;
Expand Down
26 changes: 26 additions & 0 deletions src/main/appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "duckdb/planner/expression_binder/constant_binder.hpp"
#include "duckdb/planner/expression/bound_constant_expression.hpp"
#include "duckdb/execution/expression_executor.hpp"
#include "duckdb/common/types/selection_vector.hpp"

namespace duckdb {

Expand Down Expand Up @@ -410,6 +411,31 @@ void Appender::FlushInternal(ColumnDataCollection &collection) {
context->Append(*description, collection);
}

void Appender::AppendDefaultToVector(Vector &result, idx_t column, SelectionVector &sel, idx_t count) {
Tishj marked this conversation as resolved.
Show resolved Hide resolved
// TODO: throw if count is > STANDARD_VECTOR_SIZE
Tishj marked this conversation as resolved.
Show resolved Hide resolved
if (column >= types.size()) {
throw InvalidInputException(
"Could not append DEFAULT value of column %d, out of range index, table only has %d columns", column,
types.size());
}
auto &type = types[column];
if (result.GetType() != type) {
throw InvalidInputException("Type mismatch, column has type: %s, while the provided Vector has type %s",
type.ToString(), result.GetType().ToString());
}

auto &executor = *expression_executor;

// This is only used to tell the ExpressionExecutor the size of our result
DataChunk empty_chunk;
empty_chunk.SetCardinality(count);
executor.SetChunk(empty_chunk);

// FIXME: if we are feeding the Vector to the ExecuteExpression method, we have to first make sure
Tishj marked this conversation as resolved.
Show resolved Hide resolved
// that the validity mask of the Vector is clean
context->RunFunctionInTransaction([&]() { executor.ExecuteExpression(column, result, sel); });
}

void Appender::AppendDefault() {
auto &default_expr = bound_defaults[column];
if (default_expr->type == ExpressionType::VALUE_CONSTANT) {
Expand Down
35 changes: 35 additions & 0 deletions test/appender/test_appender.cpp
Tishj marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,41 @@ TEST_CASE("Test default value appender", "[appender]") {
con.Query("COMMIT");
}

TEST_CASE("Test append default into Vector", "[appender]") {
DuckDB db(nullptr);
Connection con(db);

REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i iNTEGER)"));

auto result = con.Query("SELECT a::INTEGER FROM RANGE(15) t(a)");
auto chunk = result->Fetch();
D_ASSERT(result->type == QueryResultType::MATERIALIZED_RESULT);

{
Appender appender(con, "integers");

auto &column = chunk->data[0];
SelectionVector sel(3);

sel.set_index(0, 5);
sel.set_index(1, 8);
sel.set_index(2, 3);

appender.AppendDefaultToVector(column, 0, sel, 3);
REQUIRE(column.GetValue(0) == 0);
REQUIRE(column.GetValue(1) == 1);

REQUIRE(column.GetValue(5).IsNull());

REQUIRE(column.GetValue(8).IsNull());

REQUIRE(column.GetValue(3).IsNull());

REQUIRE(!column.GetValue(14).IsNull());
REQUIRE(column.GetValue(14) == 14);
Tishj marked this conversation as resolved.
Show resolved Hide resolved
}
}

TEST_CASE("Test incorrect usage of appender", "[appender]") {
duckdb::unique_ptr<QueryResult> result;
DuckDB db(nullptr);
Expand Down