Repository: duckdb/duckdb
File: src/main/appender.cpp (C++)

Code before:
	}
}

void BaseAppender::Flush() {
	// check that all vectors have the same length before appending
	if (column != 0) {
		throw InvalidInputException("Failed to Flush appender: incomplete append to row!");
	}

	FlushChunk();
	if (collection->Count() == 0) {
		return;
	}
	FlushInternal(*collection);

	collection->Reset();
	column = 0;
}

void Appender::FlushInternal(ColumnDataCollection &collection) {
	context->Append(*description, collection);
}

void Appender::AppendDefaultsToVector(Vector &result, idx_t vector_size, idx_t column, SelectionVector &sel,
                                      idx_t count) {
	sel.Verify(count, vector_size);
	if (count > STANDARD_VECTOR_SIZE) {
		throw InvalidInputException(
		    "Please provide a SelectionVector that is at most STANDARD_VECTOR_SIZE (%d) in size", STANDARD_VECTOR_SIZE);
	}
	if (vector_size > STANDARD_VECTOR_SIZE) {
		throw InvalidInputException("Please provide a Vector that is at most STANDARD_VECTOR_SIZE (%d) in size",
		                            STANDARD_VECTOR_SIZE);
	}
	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;

	// Create an empty chunk to tell the ExpressionExecutor how many tuples to execute for
	DataChunk empty_chunk;
	empty_chunk.SetCardinality(count);
	executor.SetChunk(empty_chunk);


Review comment (refactor):
Since we need to simplify `AppendDefault` to basically just add a index to a selection vector anyway (or use a pre-computed default value) - I would vote for removing this function

Code after:
void BaseAppender::Flush() {
	// check that all vectors have the same length before appending
	if (column != 0) {
		throw InvalidInputException("Failed to Flush appender: incomplete append to row!");
	}

	FlushChunk();
	if (collection->Count() == 0) {
		return;
	}
	FlushInternal(*collection);

	collection->Reset();
	column = 0;
}

void Appender::FlushInternal(ColumnDataCollection &collection) {
	context->Append(*description, collection);
}

void Appender::AppendDefault() {
	auto it = default_values.find(column);
	auto &column_def = description->columns[column];
	if (it == default_values.end()) {
		throw NotImplementedException(
		    "AppendDefault is currently not supported for column \"%s\" because default expression is not foldable.",
		    column_def.Name());
	}
	auto &default_value = it->second;
	Append(default_value);
}

void InternalAppender::FlushInternal(ColumnDataCollection &collection) {
	auto binder = Binder::CreateBinder(context);
	auto bound_constraints = binder->BindConstraints(table);
	table.GetStorage().LocalAppend(table, context, collection, bound_constraints);
}

void BaseAppender::Close() {
	if (column == 0 || column == types.size()) {
		Flush();
	}
}

} // namespace duckdb