diff --git a/src/Dictionaries/CacheDictionary.cpp b/src/Dictionaries/CacheDictionary.cpp index 4b1f77ab823..3305671dec7 100644 --- a/src/Dictionaries/CacheDictionary.cpp +++ b/src/Dictionaries/CacheDictionary.cpp @@ -130,7 +130,7 @@ void CacheDictionary::toParent(const PaddedPODArray & ids, PaddedPODArray(hierarchical_attribute->null_values); - getItemsNumberImpl("anime", *hierarchical_attribute, ids, out, [&](const size_t) { return null_value; }); + getItemsNumberImpl(*hierarchical_attribute, ids, out, [&](const size_t) { return null_value; }); } @@ -255,7 +255,7 @@ void CacheDictionary::getString(const std::string & attribute_name, const Padded const auto null_value = StringRef{std::get(attribute.null_values)}; - getItemsString(attribute_name, attribute, ids, out, [&](const size_t) { return null_value; }); + getItemsString(attribute, ids, out, [&](const size_t) { return null_value; }); } void CacheDictionary::getString( @@ -264,7 +264,7 @@ void CacheDictionary::getString( auto & attribute = getAttribute(attribute_name); checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::utString); - getItemsString(attribute_name, attribute, ids, out, [&](const size_t row) { return def->getDataAt(row); }); + getItemsString(attribute, ids, out, [&](const size_t row) { return def->getDataAt(row); }); } void CacheDictionary::getString( @@ -273,7 +273,7 @@ void CacheDictionary::getString( auto & attribute = getAttribute(attribute_name); checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::utString); - getItemsString(attribute_name, attribute, ids, out, [&](const size_t) { return StringRef{def}; }); + getItemsString(attribute, ids, out, [&](const size_t) { return StringRef{def}; }); } template @@ -489,7 +489,7 @@ void CacheDictionary::createAttributes() for (const auto & attribute : dict_struct.attributes) { attribute_index_by_name.emplace(attribute.name, attributes.size()); - attributes.push_back(createAttributeWithType(attribute.underlying_type, attribute.null_value)); + attributes.push_back(createAttributeWithTypeAndName(attribute.underlying_type, attribute.name, attribute.null_value)); if (attribute.hierarchical) { @@ -501,9 +501,9 @@ void CacheDictionary::createAttributes() } } -CacheDictionary::Attribute CacheDictionary::createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value) +CacheDictionary::Attribute CacheDictionary::createAttributeWithTypeAndName(const AttributeUnderlyingType type, const String & name, const Field & null_value) { - Attribute attr{type, {}, {}}; + Attribute attr{type, name, {}, {}}; switch (type) { @@ -544,50 +544,25 @@ void CacheDictionary::setDefaultAttributeValue(Attribute & attribute, const Key { switch (attribute.type) { - case AttributeUnderlyingType::utUInt8: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); +#define DISPATCH(TYPE) \ + case AttributeUnderlyingType::ut##TYPE: \ + std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); /* NOLINT */ \ break; - case AttributeUnderlyingType::utUInt16: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utUInt32: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utUInt64: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utUInt128: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utInt8: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utInt16: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utInt32: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utInt64: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utFloat32: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utFloat64: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - - case AttributeUnderlyingType::utDecimal32: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utDecimal64: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - case AttributeUnderlyingType::utDecimal128: - std::get>(attribute.arrays)[idx] = std::get(attribute.null_values); - break; - + DISPATCH(UInt8) + DISPATCH(UInt16) + DISPATCH(UInt32) + DISPATCH(UInt64) + DISPATCH(UInt128) + DISPATCH(Int8) + DISPATCH(Int16) + DISPATCH(Int32) + DISPATCH(Int64) + DISPATCH(Decimal32) + DISPATCH(Decimal64) + DISPATCH(Decimal128) + DISPATCH(Float32) + DISPATCH(Float64) +#undef DISPATCH case AttributeUnderlyingType::utString: { const auto & null_value_ref = std::get(attribute.null_values); @@ -611,37 +586,38 @@ void CacheDictionary::setAttributeValue(Attribute & attribute, const Key idx, co switch (attribute.type) { case AttributeUnderlyingType::utUInt8: - std::get>(attribute.arrays)[idx] = value.get(); + /// Strange code. Why UInt8 and UInt64? I looked through the history. It's always been like this. + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utUInt16: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utUInt32: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utUInt64: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utUInt128: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utInt8: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utInt16: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utInt32: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utInt64: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utFloat32: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utFloat64: - std::get>(attribute.arrays)[idx] = value.get(); + std::get>(attribute.arrays)[idx] = value.safeGet(); break; case AttributeUnderlyingType::utDecimal32: diff --git a/src/Dictionaries/CacheDictionary.h b/src/Dictionaries/CacheDictionary.h index 4a3e2b1383e..7e59bd85b0a 100644 --- a/src/Dictionaries/CacheDictionary.h +++ b/src/Dictionaries/CacheDictionary.h @@ -240,6 +240,7 @@ private: struct Attribute final { AttributeUnderlyingType type; + String name; AttributeValue null_values; std::variant< ContainerPtrType, @@ -262,14 +263,14 @@ private: void createAttributes(); - Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value); + Attribute createAttributeWithTypeAndName(const AttributeUnderlyingType type, const String & name, const Field & null_value); template - void getItemsNumberImpl(const std::string & attribute_name, + void getItemsNumberImpl( Attribute & attribute, const PaddedPODArray & ids, ResultArrayType & out, DefaultGetter && get_default) const; template - void getItemsString(const std::string & attribute_name, Attribute & attribute, const PaddedPODArray & ids, ColumnString * out, DefaultGetter && get_default) const; + void getItemsString(Attribute & attribute, const PaddedPODArray & ids, ColumnString * out, DefaultGetter && get_default) const; PaddedPODArray getCachedIds() const; diff --git a/src/Dictionaries/CacheDictionary.inc.h b/src/Dictionaries/CacheDictionary.inc.h index 6ed3c59ae0e..65acd8dd236 100644 --- a/src/Dictionaries/CacheDictionary.inc.h +++ b/src/Dictionaries/CacheDictionary.inc.h @@ -36,7 +36,7 @@ namespace ErrorCodes } template -void CacheDictionary::getItemsNumberImpl(const std::string & attribute_name, +void CacheDictionary::getItemsNumberImpl( Attribute & attribute, const PaddedPODArray & ids, ResultArrayType & out, DefaultGetter && get_default) const { /// First fill everything with default values @@ -158,7 +158,7 @@ void CacheDictionary::getItemsNumberImpl(const std::string & attribute_name, /// Add updated keys to asnwer. - const size_t attribute_index = getAttributeIndex(attribute_name); + const size_t attribute_index = getAttributeIndex(attribute.name); for (auto & [key, value] : update_unit_ptr->found_ids) { @@ -171,7 +171,7 @@ void CacheDictionary::getItemsNumberImpl(const std::string & attribute_name, } template -void CacheDictionary::getItemsString(const std::string & attribute_name, +void CacheDictionary::getItemsString( Attribute & attribute, const PaddedPODArray & ids, ColumnString * out, DefaultGetter && get_default) const { const auto rows = ext::size(ids); @@ -324,7 +324,7 @@ void CacheDictionary::getItemsString(const std::string & attribute_name, tryPushToUpdateQueueOrThrow(update_unit_ptr); waitForCurrentUpdateFinish(update_unit_ptr); - const size_t attribute_index = getAttributeIndex(attribute_name); + const size_t attribute_index = getAttributeIndex(attribute.name); /// Only calculate the total length. for (auto & [key, value] : update_unit_ptr->found_ids) @@ -357,12 +357,7 @@ void CacheDictionary::getItemsString(const std::string & attribute_name, { const auto found_it = update_unit_ptr->found_ids.find(id); if (found_it->second.found) - { - std::cout << "size " << found_it->second.values.size() << std::endl; - std::cout << "attribute_index " << attribute_index << std::endl; value = std::get(found_it->second.values[attribute_index]); - } - else value = get_default(row); } diff --git a/src/Dictionaries/CacheDictionary_generate1.cpp b/src/Dictionaries/CacheDictionary_generate1.cpp index aa91463e7d8..a041f50ea26 100644 --- a/src/Dictionaries/CacheDictionary_generate1.cpp +++ b/src/Dictionaries/CacheDictionary_generate1.cpp @@ -10,7 +10,7 @@ namespace DB auto & attribute = getAttribute(attribute_name); \ checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::ut##TYPE); \ const auto null_value = std::get(attribute.null_values); \ - getItemsNumberImpl(attribute_name, attribute, ids, out, [&](const size_t) { return null_value; }); \ + getItemsNumberImpl(attribute, ids, out, [&](const size_t) { return null_value; }); \ } DEFINE(UInt8) diff --git a/src/Dictionaries/CacheDictionary_generate2.cpp b/src/Dictionaries/CacheDictionary_generate2.cpp index b3f34921f33..be28a6302c2 100644 --- a/src/Dictionaries/CacheDictionary_generate2.cpp +++ b/src/Dictionaries/CacheDictionary_generate2.cpp @@ -12,7 +12,7 @@ namespace DB { \ auto & attribute = getAttribute(attribute_name); \ checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::ut##TYPE); \ - getItemsNumberImpl(attribute_name, attribute, ids, out, [&](const size_t row) { return def[row]; }); \ + getItemsNumberImpl(attribute, ids, out, [&](const size_t row) { return def[row]; }); \ } DEFINE(UInt8) diff --git a/src/Dictionaries/CacheDictionary_generate3.cpp b/src/Dictionaries/CacheDictionary_generate3.cpp index 8bef52d3bbd..36195f166db 100644 --- a/src/Dictionaries/CacheDictionary_generate3.cpp +++ b/src/Dictionaries/CacheDictionary_generate3.cpp @@ -9,7 +9,7 @@ namespace DB { \ auto & attribute = getAttribute(attribute_name); \ checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::ut##TYPE); \ - getItemsNumberImpl(attribute_name, attribute, ids, out, [&](const size_t) { return def; }); \ + getItemsNumberImpl(attribute, ids, out, [&](const size_t) { return def; }); \ } DEFINE(UInt8)