2020
2121#include < Common/Exception.h>
2222#include < Common/CurrentMetrics.h>
23+ #include < base/unit.h>
2324#include < Core/Settings.h>
2425#include < Interpreters/Context.h>
2526
@@ -77,8 +78,8 @@ namespace DB::ServerSetting
7778
7879namespace CurrentMetrics
7980{
80- extern const Metric MarkCacheBytes ;
81- extern const Metric MarkCacheFiles ;
81+ extern const Metric GlueCatalogSchemaCacheBytes ;
82+ extern const Metric GlueCatalogSchemaCacheEntries ;
8283}
8384
8485namespace DataLake
@@ -95,7 +96,7 @@ GlueCatalog::GlueCatalog(
9596 , region(settings_.region)
9697 , settings(settings_)
9798 , table_engine_definition(table_engine_definition_)
98- , metadata_objects (CurrentMetrics::MarkCacheBytes , CurrentMetrics::MarkCacheFiles, 1024 )
99+ , current_schema_field_types (CurrentMetrics::GlueCatalogSchemaCacheBytes , CurrentMetrics::GlueCatalogSchemaCacheEntries, 16_MiB )
99100{
100101 DB ::S3 ::CredentialsConfiguration creds_config;
101102 creds_config.use_environment_credentials = true ;
@@ -466,7 +467,7 @@ bool GlueCatalog::classifyTimestampTZ(const String & column_name, const TableMet
466467
467468 const String & metadata_uri = table_specific_properties->iceberg_metadata_file_location ;
468469
469- if (!metadata_objects. get (metadata_uri))
470+ auto [field_types, _] = current_schema_field_types. getOrSet (metadata_uri, [&]
470471 {
471472 auto [object_storage, bucket_name, metadata_path] = createObjectStorageForEarlyTableAccess (metadata_uri, table_metadata);
472473 const auto & read_settings = getContext ()->getReadSettings ();
@@ -479,28 +480,42 @@ bool GlueCatalog::classifyTimestampTZ(const String & column_name, const TableMet
479480 Poco::JSON ::Parser parser;
480481 Poco::Dynamic::Var result = parser.parse (metadata_file_content);
481482 auto metadata_object = result.extract <Poco::JSON ::Object::Ptr>();
482- metadata_objects.set (metadata_uri, std::make_shared<Poco::JSON ::Object::Ptr>(metadata_object));
483- }
483+ return std::make_shared<FieldTypes>(getCurrentSchemaFieldTypes (metadata_object));
484+ });
485+
486+ auto it = field_types->find (column_name);
487+ return it != field_types->end () && it->second == DB ::Iceberg::f_timestamptz;
488+ }
484489
485- auto metadata_object = *metadata_objects.get (metadata_uri);
490+ GlueCatalog::FieldTypes GlueCatalog::getCurrentSchemaFieldTypes (const Poco::JSON ::Object::Ptr & metadata_object)
491+ {
492+ FieldTypes result;
486493 auto current_schema_id = metadata_object->getValue <Int64>(" current-schema-id" );
487494 auto schemas = metadata_object->getArray (DB ::Iceberg::f_schemas);
488495 for (size_t i = 0 ; i < schemas->size (); ++i)
489496 {
490497 auto schema = schemas->getObject (static_cast <UInt32>(i));
491- if (schema->getValue <Int64>(" schema-id" ) == current_schema_id)
498+ if (schema->getValue <Int64>(" schema-id" ) != current_schema_id)
499+ continue ;
500+
501+ auto fields = schema->getArray (DB ::Iceberg::f_fields);
502+ for (size_t j = 0 ; j < fields->size (); ++j)
492503 {
493- auto fields = schema->getArray (DB ::Iceberg::f_fields);
494- for (size_t j = 0 ; j < fields->size (); ++j)
495- {
496- auto field = fields->getObject (static_cast <UInt32>(j));
497- if (field->getValue <String>(DB ::Iceberg::f_name) == column_name)
498- return field->getValue <String>(DB ::Iceberg::f_type) == DB ::Iceberg::f_timestamptz;
499- }
504+ auto field = fields->getObject (static_cast <UInt32>(j));
505+ auto type = field->get (DB ::Iceberg::f_type);
506+ if (type.isString ())
507+ result.emplace (field->getValue <String>(DB ::Iceberg::f_name), type.extract <String>());
500508 }
501509 }
510+ return result;
511+ }
502512
503- return false ;
513+ size_t GlueCatalog::FieldTypesWeightFunction::operator ()(const FieldTypes & field_types) const
514+ {
515+ size_t result = 0 ;
516+ for (const auto & [name, type] : field_types)
517+ result += sizeof (FieldTypes::value_type) + name.size () + type.size ();
518+ return result;
504519}
505520
506521GlueCatalog::ObjectStorageWithPath GlueCatalog::createObjectStorageForEarlyTableAccess (const String & s3_location, const TableMetadata & table_metadata) const
0 commit comments