Arrow bit-packs boolean values Lets first figure out where we are in the source array
240823 {
240824 D_ASSERT(!array.dictionary);
240825 if (!ignore_extensions && arrow_type.HasExtension()) {
240826 if (arrow_type.extension_data->arrow_to_duckdb) {
240827
240828 Vector input_data(arrow_type.extension_data->GetInternalType());
240829 ColumnArrowToDuckDB(input_data, array, chunk_offset, array_state, size, arrow_type, nested_offset,
240830 parent_mask, parent_offset, true);
240831 arrow_type.extension_data->arrow_to_duckdb(array_state.context, input_data, vector, size);
240832 return;
240833 }
240834 }
240835
240836 if (vector.GetBuffer()) {
240837 vector.GetBuffer()->SetAuxiliaryData(make_uniq<ArrowAuxiliaryData>(array_state.owned_data));
240838 }
240839 switch (vector.GetType().id()) {
240840 case LogicalTypeId::SQLNULL:
240841 vector.Reference(Value());
240842 break;
240843 case LogicalTypeId::BOOLEAN: {
240846 auto effective_offset =
240847 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
240848 auto src_ptr = ArrowBufferData<uint8_t>(array, 1) + effective_offset / 8;
240849 auto tgt_ptr = (
uint8_t *)FlatVector::GetData(vector);
240850 int src_pos = 0;
240851 idx_t cur_bit = effective_offset % 8;
240852 for (
idx_t row = 0; row <
size; row++) {
240853 if ((src_ptr[src_pos] & (1 << cur_bit)) == 0) {
240854 tgt_ptr[row] = 0;
240855 } else {
240856 tgt_ptr[row] = 1;
240857 }
240858 cur_bit++;
240859 if (cur_bit == 8) {
240860 src_pos++;
240861 cur_bit = 0;
240862 }
240863 }
240864 break;
240865 }
240866 case LogicalTypeId::TINYINT:
240867 case LogicalTypeId::SMALLINT:
240868 case LogicalTypeId::INTEGER:
240869 case LogicalTypeId::FLOAT:
240870 case LogicalTypeId::DOUBLE:
240871 case LogicalTypeId::UTINYINT:
240872 case LogicalTypeId::USMALLINT:
240873 case LogicalTypeId::UINTEGER:
240874 case LogicalTypeId::UBIGINT:
240875 case LogicalTypeId::BIGINT:
240876 case LogicalTypeId::HUGEINT:
240877 case LogicalTypeId::UHUGEINT:
240878 case LogicalTypeId::TIMESTAMP:
240879 case LogicalTypeId::TIMESTAMP_SEC:
240880 case LogicalTypeId::TIMESTAMP_MS:
240882 case LogicalTypeId::TIME_TZ: {
240883 DirectConversion(vector, array, chunk_offset, nested_offset, parent_offset);
240884 break;
240885 }
240886 case LogicalTypeId::UUID:
240887 UUIDConversion(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset), size);
240888 break;
240889 case LogicalTypeId::BLOB:
240890 case LogicalTypeId::BIT:
240891 case LogicalTypeId::BIGNUM:
240892 case LogicalTypeId::VARCHAR: {
240893 auto &string_info = arrow_type.GetTypeInfo<ArrowStringInfo>();
240894 auto size_type = string_info.GetSizeType();
240895 switch (size_type) {
240896 case ArrowVariableSizeType::SUPER_SIZE: {
240897 auto cdata = ArrowBufferData<char>(array, 2);
240898 auto offsets = ArrowBufferData<uint64_t>(array, 1) +
240899 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
240900 SetVectorString(vector, size, cdata, offsets);
240901 break;
240902 }
240903 case ArrowVariableSizeType::NORMAL: {
240904 auto cdata = ArrowBufferData<char>(array, 2);
240905 auto offsets = ArrowBufferData<uint32_t>(array, 1) +
240906 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
240907 SetVectorString(vector, size, cdata, offsets);
240908 break;
240909 }
240910 case ArrowVariableSizeType::VIEW: {
240911 SetVectorStringView(
240912 vector, size, array,
240913 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset));
240914 break;
240915 }
240916 case ArrowVariableSizeType::FIXED_SIZE: {
240917 SetValidityMask(vector, array, chunk_offset, size, NumericCast<int64_t>(parent_offset), nested_offset);
240918 auto fixed_size = string_info.FixedSize();
240919
240920 idx_t offset = GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset) *
240921 fixed_size;
240922 auto cdata = ArrowBufferData<char>(array, 1);
240923 auto blob_len = fixed_size;
240924 auto result = FlatVector::GetData<string_t>(vector);
240925 for (
idx_t row_idx = 0; row_idx <
size; row_idx++) {
240926 if (FlatVector::IsNull(vector, row_idx)) {
240927 offset += blob_len;
240928 continue;
240929 }
240930 auto bptr = cdata + offset;
240932 offset += blob_len;
240933 }
240934 }
240935 }
240936 break;
240937 }
240938 case LogicalTypeId::DATE: {
240939 auto &datetime_info = arrow_type.GetTypeInfo<ArrowDateTimeInfo>();
240940 auto precision = datetime_info.GetDateTimeType();
240941 switch (precision) {
240942 case ArrowDateTimeType::DAYS: {
240943 DirectConversion(vector, array, chunk_offset, nested_offset, parent_offset);
240944 break;
240945 }
240946 case ArrowDateTimeType::MILLISECONDS: {
240948 auto src_ptr = ArrowBufferData<uint64_t>(array, 1) +
240949 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
240950 auto tgt_ptr = FlatVector::GetData<date_t>(vector);
240951 for (
idx_t row = 0; row <
size; row++) {
240952 tgt_ptr[row] = date_t(UnsafeNumericCast<int32_t>(
static_cast<int64_t>(src_ptr[row]) /
240953 static_cast<int64_t>(1000 * 60 * 60 * 24)));
240954 }
240955 break;
240956 }
240957 default:
240958 throw NotImplementedException("Unsupported precision for Date Type ");
240959 }
240960 break;
240961 }
240962 case LogicalTypeId::TIME: {
240963 auto &datetime_info = arrow_type.GetTypeInfo<ArrowDateTimeInfo>();
240964 auto precision = datetime_info.GetDateTimeType();
240965 switch (precision) {
240966 case ArrowDateTimeType::SECONDS: {
240967 TimeConversion<int32_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
240968 size, 1000000);
240969 break;
240970 }
240971 case ArrowDateTimeType::MILLISECONDS: {
240972 TimeConversion<int32_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
240973 size, 1000);
240974 break;
240975 }
240976 case ArrowDateTimeType::MICROSECONDS: {
240977 TimeConversion<int64_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
240978 size, 1);
240979 break;
240980 }
240981 case ArrowDateTimeType::NANOSECONDS: {
240982 auto tgt_ptr = FlatVector::GetData<dtime_t>(vector);
240983 auto src_ptr = ArrowBufferData<int64_t>(array, 1) +
240984 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
240985 for (
idx_t row = 0; row <
size; row++) {
240986 tgt_ptr[row].micros = src_ptr[row] / 1000;
240987 }
240988 break;
240989 }
240990 default:
240991 throw NotImplementedException("Unsupported precision for Time Type ");
240992 }
240993 break;
240994 }
240995 case LogicalTypeId::TIME_NS: {
240996 auto &datetime_info = arrow_type.GetTypeInfo<ArrowDateTimeInfo>();
240997 auto precision = datetime_info.GetDateTimeType();
240998 switch (precision) {
240999 case ArrowDateTimeType::SECONDS: {
241000 TimeNSConversion<int32_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
241001 size, 1000000000);
241002 break;
241003 }
241004 case ArrowDateTimeType::MILLISECONDS: {
241005 TimeNSConversion<int32_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
241006 size, 1000000);
241007 break;
241008 }
241009 case ArrowDateTimeType::MICROSECONDS: {
241010 TimeNSConversion<int64_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
241011 size, 1000);
241012 break;
241013 }
241014 case ArrowDateTimeType::NANOSECONDS: {
241015 TimeNSConversion<int64_t>(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
241016 size, 1);
241017 break;
241018 }
241019 default:
241020 throw NotImplementedException("Unsupported precision for TimeNS Type ");
241021 }
241022 break;
241023 }
241024 case LogicalTypeId::TIMESTAMP_TZ: {
241025 auto &datetime_info = arrow_type.GetTypeInfo<ArrowDateTimeInfo>();
241026 auto precision = datetime_info.GetDateTimeType();
241027 switch (precision) {
241028 case ArrowDateTimeType::SECONDS: {
241029 TimestampTZConversion(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset), size,
241030 1000000);
241031 break;
241032 }
241033 case ArrowDateTimeType::MILLISECONDS: {
241034 TimestampTZConversion(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset), size,
241035 1000);
241036 break;
241037 }
241038 case ArrowDateTimeType::MICROSECONDS: {
241039 DirectConversion(vector, array, chunk_offset, nested_offset, parent_offset);
241040 break;
241041 }
241042 case ArrowDateTimeType::NANOSECONDS: {
241043 auto tgt_ptr = FlatVector::GetData<timestamp_t>(vector);
241044 auto src_ptr = ArrowBufferData<int64_t>(array, 1) +
241045 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
241046 for (
idx_t row = 0; row <
size; row++) {
241047 tgt_ptr[row].value = src_ptr[row] / 1000;
241048 }
241049 break;
241050 }
241051 default:
241052 throw NotImplementedException("Unsupported precision for TimestampTZ Type ");
241053 }
241054 break;
241055 }
241056 case LogicalTypeId::INTERVAL: {
241057 auto &datetime_info = arrow_type.GetTypeInfo<ArrowDateTimeInfo>();
241058 auto precision = datetime_info.GetDateTimeType();
241059 switch (precision) {
241060 case ArrowDateTimeType::SECONDS: {
241061 IntervalConversionUs(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset), size,
241062 1000000);
241063 break;
241064 }
241065 case ArrowDateTimeType::DAYS:
241066 case ArrowDateTimeType::MILLISECONDS: {
241067 IntervalConversionUs(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset), size,
241068 1000);
241069 break;
241070 }
241071 case ArrowDateTimeType::MICROSECONDS: {
241072 IntervalConversionUs(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset), size,
241073 1);
241074 break;
241075 }
241076 case ArrowDateTimeType::NANOSECONDS: {
241077 auto tgt_ptr = FlatVector::GetData<interval_t>(vector);
241078 auto src_ptr = ArrowBufferData<int64_t>(array, 1) +
241079 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
241080 for (
idx_t row = 0; row <
size; row++) {
241081 tgt_ptr[row].micros = src_ptr[row] / 1000;
241082 tgt_ptr[row].days = 0;
241083 tgt_ptr[row].months = 0;
241084 }
241085 break;
241086 }
241087 case ArrowDateTimeType::MONTHS: {
241088 IntervalConversionMonths(vector, array, chunk_offset, nested_offset, NumericCast<int64_t>(parent_offset),
241089 size);
241090 break;
241091 }
241092 case ArrowDateTimeType::MONTH_DAY_NANO: {
241093 IntervalConversionMonthDayNanos(vector, array, chunk_offset, nested_offset,
241094 NumericCast<int64_t>(parent_offset), size);
241095 break;
241096 }
241097 default:
241098 throw NotImplementedException("Unsupported precision for Interval/Duration Type ");
241099 }
241100 break;
241101 }
241102 case LogicalTypeId::DECIMAL: {
241103 auto val_mask = FlatVector::Validity(vector);
241104 auto &datetime_info = arrow_type.GetTypeInfo<ArrowDecimalInfo>();
241105 auto bit_width = datetime_info.GetBitWidth();
241106
241107 switch (bit_width) {
241108 case DecimalBitWidth::DECIMAL_32: {
241109 auto src_ptr = ArrowBufferData<int32_t>(array, 1) +
241110 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
241111 ConvertDecimal(src_ptr, vector, array, size, nested_offset, parent_offset, chunk_offset, val_mask,
241112 bit_width);
241113 break;
241114 }
241115
241116 case DecimalBitWidth::DECIMAL_64: {
241117 auto src_ptr = ArrowBufferData<int64_t>(array, 1) +
241118 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
241119 ConvertDecimal(src_ptr, vector, array, size, nested_offset, parent_offset, chunk_offset, val_mask,
241120 bit_width);
241121 break;
241122 }
241123
241124 case DecimalBitWidth::DECIMAL_128: {
241125 auto src_ptr = ArrowBufferData<hugeint_t>(array, 1) +
241126 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
241127 ConvertDecimal(src_ptr, vector, array, size, nested_offset, parent_offset, chunk_offset, val_mask,
241128 bit_width);
241129 break;
241130 }
241131 default:
241132 throw NotImplementedException("Unsupported precision for Arrow Decimal Type.");
241133 }
241134 break;
241135 }
241136 case LogicalTypeId::LIST: {
241137 ArrowToDuckDBList(vector, array, chunk_offset, array_state, size, arrow_type, nested_offset, parent_mask,
241138 NumericCast<int64_t>(parent_offset));
241139 break;
241140 }
241141 case LogicalTypeId::ARRAY: {
241142 ArrowToDuckDBArray(vector, array, chunk_offset, array_state, size, arrow_type, nested_offset, parent_mask,
241143 NumericCast<int64_t>(parent_offset));
241144 break;
241145 }
241146 case LogicalTypeId::MAP: {
241147 ArrowToDuckDBList(vector, array, chunk_offset, array_state, size, arrow_type, nested_offset, parent_mask,
241148 NumericCast<int64_t>(parent_offset));
241149 ArrowToDuckDBMapVerify(vector, size);
241150 break;
241151 }
241152 case LogicalTypeId::STRUCT: {
241154 auto &struct_info = arrow_type.GetTypeInfo<ArrowStructInfo>();
241155 auto &child_entries = StructVector::GetEntries(vector);
241156 auto &struct_validity_mask = FlatVector::Validity(vector);
241157 for (
idx_t child_idx = 0; child_idx < NumericCast<idx_t>(array.n_children); child_idx++) {
241158 auto &child_entry = *child_entries[child_idx];
241159 auto &child_array = *array.children[child_idx];
241160 auto &child_type = struct_info.GetChild(child_idx);
241161 auto &child_state = array_state.GetChild(child_idx);
241162
241163 ArrowToDuckDBConversion::SetValidityMask(child_entry, child_array, chunk_offset, size, array.offset,
241164 nested_offset);
241165 if (!struct_validity_mask.AllValid()) {
241166 auto &child_validity_mark = FlatVector::Validity(child_entry);
241168 if (!struct_validity_mask.RowIsValid(i)) {
241169 child_validity_mark.SetInvalid(i);
241170 }
241171 }
241172 }
241173
241174 auto array_physical_type = child_type.GetPhysicalType();
241175 switch (array_physical_type) {
241176 case ArrowArrayPhysicalType::DICTIONARY_ENCODED:
241178 child_entry, child_array, chunk_offset, child_state, size, child_type, nested_offset,
241179 &struct_validity_mask, NumericCast<uint64_t>(array.offset));
241180 break;
241181 case ArrowArrayPhysicalType::RUN_END_ENCODED:
241182 ColumnArrowToDuckDBRunEndEncoded(child_entry, child_array, chunk_offset, child_state, size, child_type,
241183 nested_offset, &struct_validity_mask,
241184 NumericCast<uint64_t>(array.offset));
241185 break;
241186 case ArrowArrayPhysicalType::DEFAULT:
241187 ColumnArrowToDuckDB(child_entry, child_array, chunk_offset, child_state, size, child_type,
241188 nested_offset, &struct_validity_mask, NumericCast<uint64_t>(array.offset), false);
241189 break;
241190 default:
241191 throw NotImplementedException("ArrowArrayPhysicalType not recognized");
241192 }
241193 }
241194 break;
241195 }
241196 case LogicalTypeId::UNION: {
241197 auto type_ids_buffer_idx = array.n_buffers == 1 ? 0 : 1;
241198 auto effective_offset =
241199 GetEffectiveOffset(array, NumericCast<int64_t>(parent_offset), chunk_offset, nested_offset);
241200 auto type_ids = ArrowBufferData<int8_t>(array, type_ids_buffer_idx) + effective_offset;
241201 D_ASSERT(type_ids);
241202 auto members = UnionType::CopyMemberTypes(vector.GetType());
241203
241204 auto &validity_mask = FlatVector::Validity(vector);
241205 auto &union_info = arrow_type.GetTypeInfo<ArrowStructInfo>();
241206 duckdb::vector<Vector> children;
241207 for (
idx_t child_idx = 0; child_idx < NumericCast<idx_t>(array.n_children); child_idx++) {
241208 Vector child(members[child_idx].second, size);
241209 auto &child_array = *array.children[child_idx];
241210 auto &child_state = array_state.GetChild(child_idx);
241211 auto &child_type = union_info.GetChild(child_idx);
241212
241213 ArrowToDuckDBConversion::SetValidityMask(child, child_array, chunk_offset, size,
241214 NumericCast<int64_t>(parent_offset), nested_offset);
241215 auto array_physical_type = child_type.GetPhysicalType();
241216
241217 switch (array_physical_type) {
241218 case ArrowArrayPhysicalType::DICTIONARY_ENCODED:
241220 size, child_type);
241221 break;
241222 case ArrowArrayPhysicalType::RUN_END_ENCODED:
241223 ArrowToDuckDBConversion::ColumnArrowToDuckDBRunEndEncoded(child, child_array, chunk_offset, child_state,
241224 size, child_type);
241225 break;
241226 case ArrowArrayPhysicalType::DEFAULT:
241228 child_type, nested_offset, &validity_mask, false);
241229 break;
241230 default:
241231 throw NotImplementedException("ArrowArrayPhysicalType not recognized");
241232 }
241233
241234 children.push_back(std::move(child));
241235 }
241236
241237 for (
idx_t row_idx = 0; row_idx <
size; row_idx++) {
241238 auto tag = NumericCast<uint8_t>(type_ids[row_idx]);
241239
241240 auto out_of_range = tag >= array.n_children;
241241 if (out_of_range) {
241242 throw InvalidInputException("Arrow union tag out of range: %d", tag);
241243 }
241244
241245 const Value &value = children[tag].GetValue(row_idx);
241246 vector.SetValue(row_idx, value.IsNull() ? Value() :
Value::UNION(members, tag, value));
241247 }
241248
241249 break;
241250 }
241251 default:
241252 throw NotImplementedException("Unsupported type for arrow conversion: %s", vector.GetType().ToString());
241253 }
241254}
static DUCKDB_API Value UNION(child_list_t< LogicalType > members, uint8_t tag, Value value)
Create a union value from a selected value and a tag from a set of alternatives.
Definition duckdb.cpp:132530
GOpaque< Size > size(const GMat &src)
@ TIMESTAMP_NS
us
Definition duckdb.hpp:2886
static void ArrowToDuckDBList(Vector &vector, ArrowArray &array, idx_t chunk_offset, ArrowArrayScanState &array_state, idx_t size, const ArrowType &arrow_type, int64_t nested_offset, const ValidityMask *parent_mask, int64_t parent_offset)
Definition duckdb.cpp:240194
static void ArrowToDuckDBArray(Vector &vector, ArrowArray &array, idx_t chunk_offset, ArrowArrayScanState &array_state, idx_t size, const ArrowType &arrow_type, int64_t nested_offset, const ValidityMask *parent_mask, int64_t parent_offset)
Definition duckdb.cpp:240254
static void ColumnArrowToDuckDBDictionary(Vector &vector, ArrowArray &array, idx_t chunk_offset, ArrowArrayScanState &array_state, idx_t size, const ArrowType &arrow_type, int64_t nested_offset=-1, const ValidityMask *parent_mask=nullptr, uint64_t parent_offset=0)
Definition duckdb.cpp:241388
static DUCKDB_API string_t AddStringOrBlob(Vector &vector, const char *data, idx_t len)
Definition duckdb.cpp:137893