Copy Composite records and replace one projected numeric field.
172 {
174 if (!projected.source_structure.composite_layout)
175 return std::nullopt;
176
177 const auto& original_layout = *projected.source_structure.composite_layout;
178 const auto& current_layout =
source.layout();
179 if (original_layout.stride_bytes() != current_layout.stride_bytes()
180 || original_layout.presence_bytes() != current_layout.presence_bytes()
181 || original_layout.fields().size() != current_layout.fields().size())
182 return std::nullopt;
183
184 for (
size_t i = 0; i < current_layout.fields().
size(); ++i) {
185 const auto& original = original_layout.fields()[i];
186 const auto&
current = current_layout.fields()[i];
187 if (original.name !=
current.name || original.type !=
current.type
188 || original.offset_bytes !=
current.offset_bytes
189 || original.size_bytes !=
current.size_bytes)
190 return std::nullopt;
191 }
192
193 const auto field_index =
source.layout().find_field(projected.field_name);
194 if (!field_index ||
source.layout().fields()[*field_index].type !=
typeid(ValueT)
195 || projected.values.size() !=
source.size()
196 || projected.presence.size() !=
source.size())
197 return std::nullopt;
198
199 Kakshya::CompositeArray result(source);
200 for (
size_t row = 0; row <
source.size(); ++row) {
201 if (!projected.presence[row]) {
202 if (!result.clear(row, projected.field_name))
203 return std::nullopt;
204 continue;
205 }
206
207 const double value = projected.values[row];
208 if constexpr (std::integral<ValueT>) {
210 || value < static_cast<double>(std::numeric_limits<ValueT>::lowest())
211 ||
value >
static_cast<double>(std::numeric_limits<ValueT>::max()))
212 return std::nullopt;
213 } else if constexpr (std::same_as<ValueT, float>) {
214 if (std::isfinite(
value)
215 && (
value < -
static_cast<double>(std::numeric_limits<float>::max())
216 ||
value >
static_cast<double>(std::numeric_limits<float>::max())))
217 return std::nullopt;
218 }
219
220 if (!result.set(row, projected.field_name,
static_cast<ValueT
>(
value)))
221 return std::nullopt;
222 }
223
224 return std::optional<Kakshya::CompositeArray>(std::move(result));
225 }
static const Kakshya::CompositeArray & extract_composite_data(const Kakshya::CompositeArray &data) noexcept
Borrow an owning Composite array without converting its fields.