Glue Iceberg Table Data Types and Mapping
In this article
Glue Iceberg tables use a set of standardized data types that map between the Iceberg specification and the AWS Glue Data Catalog. For example, a column defined as a string in an Iceberg table appears as a string in the Glue Catalog, while a timestamp with a specific timezone is handled as a timestamp type.
How Do Glue Iceberg Data Types Map?
Iceberg uses a strongly typed system to ensure that data remains consistent across different compute engines. When you create a table in Glue, the catalog stores the schema as a set of column names and their associated types. These types are designed to be compatible with common query engines like Athena or Spark.
Basic types include integers, longs, floats, and doubles for numeric data. For text, the string type is the standard. Boolean types handle true or false values. More complex data is handled through nested types. These include lists, maps, and structs. A struct allows you to group related fields together into a single column, which is useful for organizing hierarchical data without flattening the entire table.
Time and date handling is a critical part of the mapping. Iceberg provides specific types for dates, timestamps with local time, and timestamps with UTC offsets. Glue translates these into its own internal type system so that the catalog can track the schema. If you change a type in the underlying Iceberg metadata, Glue updates the catalog to reflect that change, which is part of the schema evolution feature.
How Does Schema Evolution Work with Types?
One of the main reasons to use Iceberg tables in Glue is the ability to change data types without rewriting the entire dataset. This process is called schema evolution. Instead of requiring a full table rewrite, Iceberg tracks the changes in metadata files. When you request a type change, the system updates the current schema version.
Certain type promotions are allowed because they do not risk losing data. For instance, promoting an integer to a long is a safe operation because a long can hold any value an integer can. Similarly, promoting a float to a double is permitted. These changes happen instantly in the Glue Catalog and do not require you to touch the actual data files stored in S3.
If you need to change a type to something incompatible, such as changing a string to an integer, you cannot use a simple promotion. In these cases, you must create a new column with the correct type and migrate the data. This ensures that the query engine does not encounter a value it cannot parse, which would cause the query to fail.
Which Types Cause Data Mismatches?
Data mismatches often happen when the type defined in the Glue Catalog does not align with the type used by the engine writing the data. This is common when using different versions of Spark or when mixing different query engines on the same table. A common issue involves decimal types. Decimals require a specific precision and scale. If the writer uses a different precision than what is defined in the Glue table, the write operation may fail or the data may be truncated.
Another area for errors is the handling of nulls. Iceberg allows columns to be required or optional. If a Glue table expects a value but the incoming data contains a null, the system will reject the record. This is a strict check that prevents dirty data from entering the table, but it can be frustrating if your source data is not cleaned first.
Timestamp mismatches are also frequent. Some engines treat timestamps as milliseconds since an epoch, while others use microseconds. If the Glue Iceberg table is configured for one and the engine provides the other, you may see dates that are wildly incorrect. To fix this, you should check the configuration of your compute engine to ensure it matches the Iceberg timestamp specification.
How Do I Choose the Right Type for My Column?
Choosing the right type depends on how you plan to query the data and the nature of the source. For identifiers that will never be used in math, use a string. Even if the ID looks like a number, treating it as a string prevents the engine from trying to perform calculations on it and avoids issues with leading zeros.
For numeric values, consider the range of the data. Use integers for whole numbers that fit within standard limits. Use longs for larger counts, such as event IDs or global transaction counts. For measurements, doubles provide more precision than floats, which is important for scientific data or financial calculations where rounding errors must be minimized.
When dealing with complex objects, decide between a map and a struct. Use a struct when the fields are fixed and known, such as an address with a street, city, and zip code. Use a map when the keys are dynamic or unknown, such as a set of user-defined tags. Maps are more flexible but can be slower to query than structs because the engine must search for keys.
For dates, use the date type if you only need the year, month, and day. Use timestamps when the exact time of the event is necessary for analysis. If your data spans multiple time zones, always use the timestamp with local time zone type to avoid confusion when users in different regions query the same table.
Iceberg Types for Complex Data Architectures
In large data lakes, you might encounter situations where a single column needs to hold multiple values. This is where the list type becomes useful. A list is an ordered collection of elements of the same type. For example, a column for tags on a blog post would be a list of strings.
When nesting these types, such as a list of structs, the Glue Catalog maintains the hierarchy. This allows you to perform complex queries, such as finding all records where a specific field inside a struct within a list matches a value. While powerful, deep nesting can impact performance. A rule of thumb is to keep nesting to a few levels to ensure the query engine can scan the data efficiently.
You should also consider the impact of types on storage. While Iceberg optimizes how data is stored in Parquet files, choosing an overly large type for every column can increase the size of the metadata. This can slow down the planning phase of a query because the engine has to process more information about the schema before it even looks at the data files.