Executes the UNNEST operator internally and emits a chunk of unnested data. If include_input is set, then the resulting chunk also contains vectors for all non-UNNEST columns in the projection. If include_input is not set, then the UNNEST behaves as a table function and only emits the unnested data.
198025 {
198026 auto &state = state_p.Cast<UnnestOperatorState>();
198027
198028 do {
198029
198030
198031 if (state.first_fetch) {
198033 }
198034
198035
198036 if (state.current_row >= input.size()) {
198037 state.Reset();
198038 return OperatorResultType::NEED_MORE_INPUT;
198039 }
198040
198041
198042
198043
198044
198045 idx_t result_length = 0;
198046 idx_t unnest_list_count = 0;
198047 auto initial_row = state.current_row;
198048 for (
idx_t col_idx = 0; col_idx < state.list_data.ColumnCount(); col_idx++) {
198049 state.null_counts[col_idx] = 0;
198050 }
198051 while (result_length < STANDARD_VECTOR_SIZE && state.current_row < input.size()) {
198052 auto current_row_length = MinValue<idx_t>(STANDARD_VECTOR_SIZE - result_length,
198053 state.unnest_lengths[state.current_row] - state.list_position);
198054
198055 if (current_row_length > 0) {
198056
198057 if (include_input) {
198058 for (
idx_t r = 0; r < current_row_length; r++) {
198059 state.input_sel.set_index(result_length + r, state.current_row);
198060 }
198061 }
198062 for (
idx_t col_idx = 0; col_idx < state.list_data.ColumnCount(); col_idx++) {
198063 auto &vector_data = state.list_vector_data[col_idx];
198064 auto current_idx = vector_data.sel->get_index(state.current_row);
198065 idx_t list_length = 0;
198066 idx_t list_offset = 0;
198067 if (vector_data.validity.RowIsValid(current_idx)) {
198068 auto list_data = UnifiedVectorFormat::GetData<list_entry_t>(vector_data);
198069 auto list_entry = list_data[current_idx];
198070 list_length = list_entry.length;
198071 list_offset = list_entry.offset;
198072 }
198073
198074 idx_t unnest_length = MinValue<idx_t>(
198075 list_length - MinValue<idx_t>(list_length, state.list_position), current_row_length);
198076 auto &unnest_sel = state.unnest_sels[col_idx];
198077 for (
idx_t r = 0; r < unnest_length; r++) {
198078 unnest_sel.set_index(result_length + r, list_offset + state.list_position + r);
198079 }
198080
198081 auto &null_sel = state.null_sels[col_idx];
198082 for (
idx_t r = unnest_length; r < current_row_length; r++) {
198083
198084
198085
198086
198087 unnest_sel.set_index(result_length + r, 0);
198088 null_sel.set_index(state.null_counts[col_idx]++, result_length + r);
198089 }
198090 }
198091
198092
198093 result_length += current_row_length;
198094 state.list_position += current_row_length;
198095 }
198096 unnest_list_count++;
198097 if (state.list_position == state.unnest_lengths[state.current_row]) {
198098 state.current_row++;
198099 state.list_position = 0;
198100 }
198101 }
198102 idx_t col_offset = 0;
198103 chunk.SetCardinality(result_length);
198104 if (include_input) {
198105 for (
idx_t col_idx = 0; col_idx < input.ColumnCount(); col_idx++) {
198106 if (unnest_list_count == 1) {
198107
198109 } else {
198110
198111 chunk.
data[col_idx].Slice(input.
data[col_idx], state.input_sel, result_length);
198112 }
198113 }
198114 col_offset = input.ColumnCount();
198115 }
198116 for (
idx_t col_idx = 0; col_idx < state.list_data.ColumnCount(); col_idx++) {
198117 auto &list_vector = state.list_data.data[col_idx];
198118 auto &result_vector = chunk.
data[col_offset + col_idx];
198119 if (state.list_data.data[col_idx].GetType() == LogicalType::SQLNULL ||
198120 ListType::GetChildType(state.list_data.data[col_idx].GetType()) == LogicalType::SQLNULL ||
198122
198123
198124 result_vector.SetVectorType(VectorType::CONSTANT_VECTOR);
198125 ConstantVector::SetNull(result_vector, true);
198126 continue;
198127 }
198129 result_vector.Slice(child_vector, state.unnest_sels[col_idx], result_length);
198130 if (state.null_counts[col_idx] > 0) {
198131
198132 result_vector.Flatten(result_length);
198133 auto &null_sel = state.null_sels[col_idx];
198134 for (
idx_t idx = 0; idx < state.null_counts[col_idx]; idx++) {
198135 auto null_index = null_sel.get_index(idx);
198136 FlatVector::SetNull(result_vector, null_index, true);
198137 }
198138 }
198139 }
198140 } while (chunk.size() == 0);
198141 return OperatorResultType::HAVE_MORE_OUTPUT;
198142}
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static DUCKDB_API void Reference(Vector &vector, Vector &source, idx_t position, idx_t count)
Turns "vector" into a constant vector by referencing a value within the source vector.
Definition duckdb.cpp:137793
static DUCKDB_API const Vector & GetEntry(const Vector &vector)
Gets a reference to the underlying child-vector of a list.
Definition duckdb.cpp:138203
static DUCKDB_API idx_t GetListSize(const Vector &vector)
Gets the total size of the underlying child-vector of a list.
Definition duckdb.cpp:138221