Repository: duckdb/duckdb
File: extension/core_functions/scalar/struct/struct_keys.cpp (C++)

Code before:
		if (ConstantVector::IsNull(input)) {
			Vector null_vec(list_type);
			ConstantVector::SetNull(null_vec, true);
			result.Reference(null_vec);
			result.SetVectorType(VectorType::CONSTANT_VECTOR);
			return;
		}

		Vector list_vec(list_type);
		auto &list_child = ListVector::GetEntry(list_vec);
		list_child.SetVectorType(VectorType::FLAT_VECTOR);
		auto child_data = FlatVector::GetData<string_t>(list_child);
		for (idx_t i = 0; i < key_count; i++) {
			child_data[i] = StringVector::AddString(list_child, keys[i]);
		}
		ListVector::SetListSize(list_vec, key_count);
		auto list_entries = FlatVector::GetData<list_entry_t>(list_vec);
		list_entries[0] = {0, key_count};

		result.Reference(list_vec);
		result.SetVectorType(VectorType::CONSTANT_VECTOR);
		return;
	}

	// Non-constant input: return a DICTIONARY_VECTOR over two entries (keys list and NULL) to preserve per-row NULLs
	Vector dict_child(list_type);
	auto &list_child = ListVector::GetEntry(dict_child);
	list_child.SetVectorType(VectorType::FLAT_VECTOR);

	auto child_data = FlatVector::GetData<string_t>(list_child);
	for (idx_t i = 0; i < key_count; i++) {
		child_data[i] = StringVector::AddString(list_child, keys[i]);
	}
	ListVector::SetListSize(dict_child, key_count);

	// Set list entries: row 0 spans all key_count child elements; row 1 will be NULL
	auto list_entries = FlatVector::GetData<list_entry_t>(dict_child);
	list_entries[0] = {0, key_count};
	list_entries[1] = {0, 0};

	// Mark row 1 as NULL in the dictionary child
	auto &dict_validity = FlatVector::Validity(dict_child);
	dict_validity.EnsureWritable();
	dict_validity.SetInvalid(1);

	// Build the dictionary selection: 0 for non-null input, 1 for null input
	SelectionVector sel(count);
	UnifiedVectorFormat input_data;
	input.ToUnifiedFormat(count, input_data);
	for (idx_t i = 0; i < count; i++) {
		auto idx = input_data.sel->get_index(i);

Review comment (suggestion):
Again, this should just be `result`, without creating a new vector here.

Code after:
			child_data[i] = StringVector::AddString(list_child, child_types[i].first);
		}
		ListVector::SetListSize(keys_vector, count);

		auto list_entries = FlatVector::GetData<list_entry_t>(keys_vector);
		list_entries[0] = {0, count};

		auto &validity = FlatVector::Validity(keys_vector);
		validity.EnsureWritable();
		validity.SetInvalid(1);
	}

	bool Equals(const FunctionData &other) const override {
		auto &o = other.Cast<StructKeysBindData>();
		// Compare type and flag (content is derived from them)
		return type == o.type;
	}

	unique_ptr<FunctionData> Copy() const override {
		return make_uniq<StructKeysBindData>(type);
	}
};

static void StructKeysFunction(DataChunk &args, ExpressionState &state, Vector &result) {
	auto &input = args.data[0];
	const idx_t count = args.size();

	auto &data = state.expr.Cast<BoundFunctionExpression>().bind_info->Cast<StructKeysBindData>();
	auto &keys_vector = data.keys_vector;

	// If the input is a constant, we must return a CONSTANT_VECTOR
	if (args.AllConstant()) {
		if (ConstantVector::IsNull(input)) {
			ConstantVector::SetNull(result, true);
			return;
		}
		ConstantVector::Reference(result, keys_vector, 0, count);
		return;
	}

	// Non-constant input: return a DICTIONARY_VECTOR over two entries (keys list and NULL) to preserve per-row NULLs
	// Build the dictionary selection: 0 for non-null input, 1 for null input
	SelectionVector sel(count);
	UnifiedVectorFormat input_data;
	input.ToUnifiedFormat(count, input_data);
	for (idx_t i = 0; i < count; i++) {
		auto idx = input_data.sel->get_index(i);
		const bool is_valid = input_data.validity.RowIsValid(idx);
		sel.set_index(i, !is_valid);
	}