schema
Accessor
dataclass
¶
An accessor for a specific position in a container that implements the StructProtocol.
Source code in pyiceberg/schema.py
__repr__()
¶
__str__()
¶
get(container)
¶
Return the value at self.position in container
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container
|
StructProtocol
|
A container to access at position |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The value at position |
Source code in pyiceberg/schema.py
PartnerAccessor
¶
Bases: Generic[P]
, ABC
Source code in pyiceberg/schema.py
PreOrderSchemaVisitor
¶
Bases: Generic[T]
, ABC
Source code in pyiceberg/schema.py
PrimitiveWithPartnerVisitor
¶
Bases: SchemaWithPartnerVisitor[P, T]
Source code in pyiceberg/schema.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
|
primitive(primitive, primitive_partner)
¶
Visit a PrimitiveType.
Source code in pyiceberg/schema.py
visit_binary(binary_type, partner)
abstractmethod
¶
visit_boolean(boolean_type, partner)
abstractmethod
¶
visit_date(date_type, partner)
abstractmethod
¶
visit_decimal(decimal_type, partner)
abstractmethod
¶
visit_double(double_type, partner)
abstractmethod
¶
visit_fixed(fixed_type, partner)
abstractmethod
¶
visit_float(float_type, partner)
abstractmethod
¶
visit_integer(integer_type, partner)
abstractmethod
¶
visit_long(long_type, partner)
abstractmethod
¶
visit_string(string_type, partner)
abstractmethod
¶
visit_time(time_type, partner)
abstractmethod
¶
visit_timestamp(timestamp_type, partner)
abstractmethod
¶
visit_timestamptz(timestamptz_type, partner)
abstractmethod
¶
Schema
¶
Bases: IcebergBaseModel
A table Schema.
Example
from pyiceberg import schema from pyiceberg import types
Source code in pyiceberg/schema.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
_lazy_id_to_accessor
cached
property
¶
Return an index of field ID to accessor.
This is calculated once when called for the first time. Subsequent calls to this method will use a cached index.
_lazy_id_to_field
cached
property
¶
Return an index of field ID to NestedField instance.
This is calculated once when called for the first time. Subsequent calls to this method will use a cached index.
_lazy_id_to_name
cached
property
¶
Return an index of field ID to full name.
This is calculated once when called for the first time. Subsequent calls to this method will use a cached index.
_lazy_id_to_parent
cached
property
¶
Returns an index of field ID to parent field IDs.
This is calculated once when called for the first time. Subsequent calls to this method will use a cached index.
_lazy_name_to_id_lower
cached
property
¶
Return an index of lower-case field names to field IDs.
This is calculated once when called for the first time. Subsequent calls to this method will use a cached index.
column_names
property
¶
Return a list of all the column names, including nested fields.
Excludes short names.
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: The column names. |
columns
property
¶
A tuple of the top-level fields.
field_ids
property
¶
Return the IDs of the current schema.
__eq__(other)
¶
Return the equality of two instances of the Schema class.
Source code in pyiceberg/schema.py
__len__()
¶
__repr__()
¶
Return the string representation of the Schema class.
__str__()
¶
_validate_identifier_field(field_id)
¶
Validate that the field with the given ID is a valid identifier field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_id
|
int
|
The ID of the field to validate. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the field is not valid. |
Source code in pyiceberg/schema.py
accessor_for_field(field_id)
¶
Find a schema position accessor given a field ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_id
|
int
|
The ID of the field. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
When the value cannot be found. |
Returns:
Name | Type | Description |
---|---|---|
Accessor |
Accessor
|
An accessor for the given field ID. |
Source code in pyiceberg/schema.py
as_arrow()
¶
as_struct()
¶
find_column_name(column_id)
¶
Find a column name given a column ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_id
|
int
|
The ID of the column. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
Optional[str]
|
The column name (or None if the column ID cannot be found). |
Source code in pyiceberg/schema.py
find_field(name_or_id, case_sensitive=True)
¶
Find a field using a field name or field ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_or_id
|
Union[str, int]
|
Either a field name or a field ID. |
required |
case_sensitive
|
bool
|
Whether to perform a case-sensitive lookup using a field name. Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
When the value cannot be found. |
Returns:
Name | Type | Description |
---|---|---|
NestedField |
NestedField
|
The matched NestedField. |
Source code in pyiceberg/schema.py
find_type(name_or_id, case_sensitive=True)
¶
Find a field type using a field name or field ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_or_id
|
Union[str, int]
|
Either a field name or a field ID. |
required |
case_sensitive
|
bool
|
Whether to perform a case-sensitive lookup using a field name. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
NestedField |
IcebergType
|
The type of the matched NestedField. |
Source code in pyiceberg/schema.py
identifier_field_names()
¶
Return the names of the identifier fields.
Returns:
Type | Description |
---|---|
Set[str]
|
Set of names of the identifier fields |
Source code in pyiceberg/schema.py
select(*names, case_sensitive=True)
¶
Return a new schema instance pruned to a subset of columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names
|
List[str]
|
A list of column names. |
()
|
case_sensitive
|
bool
|
Whether to perform a case-sensitive lookup for each column name. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Schema |
Schema
|
A new schema with pruned columns. |
Raises:
Type | Description |
---|---|
ValueError
|
If a column is selected that doesn't exist. |
Source code in pyiceberg/schema.py
SchemaVisitor
¶
Bases: Generic[T]
, ABC
Source code in pyiceberg/schema.py
after_field(field)
¶
after_list_element(element)
¶
Override this method to perform an action immediately after visiting an element within a ListType.
after_map_key(key)
¶
Override this method to perform an action immediately after visiting a key within a MapType.
after_map_value(value)
¶
Override this method to perform an action immediately after visiting a value within a MapType.
before_field(field)
¶
before_list_element(element)
¶
Override this method to perform an action immediately before visiting an element within a ListType.
before_map_key(key)
¶
Override this method to perform an action immediately before visiting a key within a MapType.
before_map_value(value)
¶
Override this method to perform an action immediately before visiting a value within a MapType.
field(field, field_result)
abstractmethod
¶
list(list_type, element_result)
abstractmethod
¶
map(map_type, key_result, value_result)
abstractmethod
¶
primitive(primitive)
abstractmethod
¶
schema(schema, struct_result)
abstractmethod
¶
SchemaVisitorPerPrimitiveType
¶
Bases: SchemaVisitor[T]
, ABC
Source code in pyiceberg/schema.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
primitive(primitive)
¶
Visit a PrimitiveType.
Source code in pyiceberg/schema.py
visit_binary(binary_type)
abstractmethod
¶
visit_boolean(boolean_type)
abstractmethod
¶
visit_date(date_type)
abstractmethod
¶
visit_decimal(decimal_type)
abstractmethod
¶
visit_double(double_type)
abstractmethod
¶
visit_fixed(fixed_type)
abstractmethod
¶
visit_float(float_type)
abstractmethod
¶
visit_integer(integer_type)
abstractmethod
¶
visit_long(long_type)
abstractmethod
¶
visit_string(string_type)
abstractmethod
¶
visit_time(time_type)
abstractmethod
¶
visit_timestamp(timestamp_type)
abstractmethod
¶
visit_timestamptz(timestamptz_type)
abstractmethod
¶
SchemaWithPartnerVisitor
¶
Bases: Generic[P, T]
, ABC
Source code in pyiceberg/schema.py
after_field(field, field_partner)
¶
after_list_element(element, element_partner)
¶
Override this method to perform an action immediately after visiting an element within a ListType.
after_map_key(key, key_partner)
¶
Override this method to perform an action immediately after visiting a key within a MapType.
after_map_value(value, value_partner)
¶
Override this method to perform an action immediately after visiting a value within a MapType.
before_field(field, field_partner)
¶
before_list_element(element, element_partner)
¶
Override this method to perform an action immediately before visiting an element within a ListType.
before_map_key(key, key_partner)
¶
Override this method to perform an action immediately before visiting a key within a MapType.
before_map_value(value, value_partner)
¶
Override this method to perform an action immediately before visiting a value within a MapType.
field(field, field_partner, field_result)
abstractmethod
¶
list(list_type, list_partner, element_result)
abstractmethod
¶
map(map_type, map_partner, key_result, value_result)
abstractmethod
¶
primitive(primitive, primitive_partner)
abstractmethod
¶
schema(schema, schema_partner, struct_result)
abstractmethod
¶
_BuildPositionAccessors
¶
Bases: SchemaVisitor[Dict[Position, Accessor]]
A schema visitor for generating a field ID to accessor index.
Example
from pyiceberg.schema import Schema from pyiceberg.types import * schema = Schema( ... NestedField(field_id=2, name="id", field_type=IntegerType(), required=False), ... NestedField(field_id=1, name="data", field_type=StringType(), required=True), ... NestedField( ... field_id=3, ... name="location", ... field_type=StructType( ... NestedField(field_id=5, name="latitude", field_type=FloatType(), required=False), ... NestedField(field_id=6, name="longitude", field_type=FloatType(), required=False), ... ), ... required=True, ... ), ... schema_id=1, ... identifier_field_ids=[1], ... ) result = build_position_accessors(schema) expected = { ... 2: Accessor(position=0, inner=None), ... 1: Accessor(position=1, inner=None), ... 5: Accessor(position=2, inner=Accessor(position=0, inner=None)), ... 6: Accessor(position=2, inner=Accessor(position=1, inner=None)) ... } result == expected True
Source code in pyiceberg/schema.py
_IndexById
¶
Bases: SchemaVisitor[Dict[int, NestedField]]
A schema visitor for generating a field ID to NestedField index.
Source code in pyiceberg/schema.py
field(field, field_result)
¶
list(list_type, element_result)
¶
Add the list element ID to the index.
map(map_type, key_result, value_result)
¶
Add the key ID and value ID as individual items in the index.
Source code in pyiceberg/schema.py
_IndexByName
¶
Bases: SchemaVisitor[Dict[str, int]]
A schema visitor for generating a field name to field ID index.
Source code in pyiceberg/schema.py
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 |
|
_add_field(name, field_id)
¶
Add a field name to the index, mapping its full name to its field ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The field name. |
required |
field_id
|
int
|
The field ID. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the field name is already contained in the index. |
Source code in pyiceberg/schema.py
after_field(field)
¶
before_field(field)
¶
before_list_element(element)
¶
Short field names omit element when the element is a StructType.
Source code in pyiceberg/schema.py
by_id()
¶
by_name()
¶
Return an index of combined full and short names.
Note: Only short names that do not conflict with full names are included.
Source code in pyiceberg/schema.py
field(field, field_result)
¶
list(list_type, element_result)
¶
Add the list element name to the index.
map(map_type, key_result, value_result)
¶
Add the key name and value name as individual items in the index.
Source code in pyiceberg/schema.py
_SetFreshIDs
¶
Bases: PreOrderSchemaVisitor[IcebergType]
Traverses the schema and assigns monotonically increasing ids.
Source code in pyiceberg/schema.py
_check_schema_compatible(requested_schema, provided_schema)
¶
Check if the provided_schema
is compatible with requested_schema
.
Both Schemas must have valid IDs and share the same ID for the same field names.
Two schemas are considered compatible when:
1. All required
fields in requested_schema
are present and are also required
in the provided_schema
2. Field Types are consistent for fields that are present in both schemas. I.e. the field type
in the provided_schema
can be promoted to the field type of the same field ID in requested_schema
Raises:
Type | Description |
---|---|
ValueError
|
If the schemas are not compatible. |
Source code in pyiceberg/schema.py
_index_parents(schema_or_type)
¶
Generate an index of field IDs to their parent field IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_or_type
|
Union[Schema, IcebergType]
|
A schema or type to index. |
required |
Returns:
Type | Description |
---|---|
Dict[int, int]
|
Dict[int, int]: An index of field IDs to their parent field IDs. |
Source code in pyiceberg/schema.py
assign_fresh_schema_ids(schema_or_type, next_id=None)
¶
Traverses the schema, and sets new IDs.
build_position_accessors(schema_or_type)
¶
Generate an index of field IDs to schema position accessors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_or_type
|
Union[Schema, IcebergType]
|
A schema or type to index. |
required |
Returns:
Type | Description |
---|---|
Dict[int, Accessor]
|
Dict[int, Accessor]: An index of field IDs to accessors. |
Source code in pyiceberg/schema.py
index_by_id(schema_or_type)
¶
Generate an index of field IDs to NestedField instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_or_type
|
Union[Schema, IcebergType]
|
A schema or type to index. |
required |
Returns:
Type | Description |
---|---|
Dict[int, NestedField]
|
Dict[int, NestedField]: An index of field IDs to NestedField instances. |
Source code in pyiceberg/schema.py
index_by_name(schema_or_type)
¶
Generate an index of field names to field IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_or_type
|
Union[Schema, IcebergType]
|
A schema or type to index. |
required |
Returns:
Type | Description |
---|---|
Dict[str, int]
|
Dict[str, int]: An index of field names to field IDs. |
Source code in pyiceberg/schema.py
index_name_by_id(schema_or_type)
¶
Generate an index of field IDs full field names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_or_type
|
Union[Schema, IcebergType]
|
A schema or type to index. |
required |
Returns:
Type | Description |
---|---|
Dict[int, str]
|
Dict[str, int]: An index of field IDs to full names. |
Source code in pyiceberg/schema.py
pre_order_visit(obj, visitor)
¶
Apply a schema visitor to any point within a schema.
The function traverses the schema in pre-order fashion. This is a slimmed down version compared to the post-order traversal (missing before and after methods), mostly because we don't use the pre-order traversal much.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[Schema, IcebergType]
|
An instance of a Schema or an IcebergType. |
required |
visitor
|
PreOrderSchemaVisitor[T]
|
An instance of an implementation of the generic PreOrderSchemaVisitor base class. |
required |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If attempting to visit an unrecognized object type. |
Source code in pyiceberg/schema.py
promote(file_type, read_type)
¶
Promotes reading a file type to a read type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_type
|
IcebergType
|
The type of the Avro file. |
required |
read_type
|
IcebergType
|
The requested read type. |
required |
Raises:
Type | Description |
---|---|
ResolveError
|
If attempting to resolve an unrecognized object type. |
Source code in pyiceberg/schema.py
prune_columns(schema, selected, select_full_types=True)
¶
Prunes a column by only selecting a set of field-ids.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
The schema to be pruned. |
required |
selected
|
Set[int]
|
The field-ids to be included. |
required |
select_full_types
|
bool
|
Return the full struct when a subset is recorded |
True
|
Returns:
Type | Description |
---|---|
Schema
|
The pruned schema. |
Source code in pyiceberg/schema.py
sanitize_column_names(schema)
¶
Sanitize column names to make them compatible with Avro.
The column name should be starting with '' or digit followed by a string only contains '', digit or alphabet, otherwise it will be sanitized to conform the avro naming convention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
The schema to be sanitized. |
required |
Returns:
Type | Description |
---|---|
Schema
|
The sanitized schema. |
Source code in pyiceberg/schema.py
visit(obj, visitor)
¶
Apply a schema visitor to any point within a schema.
The function traverses the schema in post-order fashion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[Schema, IcebergType]
|
An instance of a Schema or an IcebergType. |
required |
visitor
|
SchemaVisitor[T]
|
An instance of an implementation of the generic SchemaVisitor base class. |
required |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If attempting to visit an unrecognized object type. |