Apache Polaris Operators¶
Apache Polaris is an Apache Iceberg REST catalog that adds a Management API (catalogs, principals, roles, grants) and Polaris-only catalog APIs such as policies. These operators manage everything a Polaris catalog defines: catalogs, namespaces, table definitions, access and policies. Reading and writing table data stays with pyiceberg, the Iceberg provider and the engine providers.
All operators:
take
polaris_conn_id(defaultpolaris_default, see Apache Polaris Connection);are idempotent by default: creating something that exists, or deleting something that is gone, succeeds (
if_exists="skip",if_missing="skip"; pass"fail"to raise instead), so tasks are safe to retry and Dags safe to re-run;fail without retrying on errors a retry cannot fix: a misconfigured connection, rejected credentials, missing privileges (HTTP 403), a bad request or a missing entity. They raise
AirflowFailException, so the task’sretriesare not spent waiting on a fix that needs a person. An entity that already exists (withif_exists="fail") and a conflicting policy attachment count as such errors too. Transient errors (network failures, server errors, rate limiting, concurrent updates) are raised as usual and retried. Code calling the hooks directly can check the same classification through theretryableattribute ofPolarisError;accept Polaris entities as plain dicts in Polaris’s JSON shape.
polaris_conn_idand the names and definitions they act on (catalog, namespace, role, principal, policy, grants, properties, targets) are templated, so one Dag can target several environments throughparams; the behaviour switches (if_exists,if_missing,cascade, …) are not. Fields that take a dict or list needrender_template_as_native_obj=Trueon the Dag when they come from a template;return only JSON-serializable, non-secret values.
Catalogs¶
PolarisCreateCatalogOperator¶
PolarisCreateCatalogOperator creates
an internal catalog, or a federated ("type": "EXTERNAL") one. An existing catalog is returned
unchanged, not updated; use PolarisUpdateCatalogOperator for changes.
create_catalog = PolarisCreateCatalogOperator(
task_id="create_catalog",
catalog={
"type": "INTERNAL",
"name": CATALOG,
"properties": {"default-base-location": BASE_LOCATION},
"storageConfigInfo": {"storageType": "FILE", "allowedLocations": [BASE_LOCATION]},
},
)
PolarisUpdateCatalogOperator¶
PolarisUpdateCatalogOperator merges
properties into the catalog’s existing properties (or replaces them with
replace_properties=True) and can change storage_config_info. It reads the catalog’s
current entity version first, as Polaris requires, and starts again from the latest version, after
a short random pause, if the catalog changes in the meantime (Polaris then answers HTTP 409).
update_catalog = PolarisUpdateCatalogOperator(
task_id="update_catalog", catalog_name=CATALOG, properties={"owner": "sales"}
)
PolarisDeleteCatalogOperator¶
PolarisDeleteCatalogOperator deletes a
catalog. Polaris refuses to delete a catalog that still contains namespaces or catalog roles
other than catalog_admin.
Namespaces¶
Namespaces are given as "a.b" or ["a", "b"] (use the list form for names that contain a
dot), and catalog defaults to the connection’s default_catalog.
PolarisCreateNamespaceOperator¶
PolarisCreateNamespaceOperator
creates a namespace, optionally with properties; its parent must exist. An existing namespace is
returned unchanged. Polaris adds a location property.
create_namespace = PolarisCreateNamespaceOperator(
task_id="create_namespace",
catalog=CATALOG,
namespace="sales",
properties={"owner": "data-eng"},
)
PolarisUpdateNamespacePropertiesOperator¶
PolarisUpdateNamespacePropertiesOperator
sets and/or removes properties and returns which keys were updated, removed or already missing.
update_namespace_properties = PolarisUpdateNamespacePropertiesOperator(
task_id="update_namespace_properties",
catalog=CATALOG,
namespace="sales",
updates={"owner": "sales-eng", "tier": "gold"},
)
PolarisDropNamespaceOperator¶
PolarisDropNamespaceOperator drops an
empty namespace. Polaris refuses to drop one that still contains tables, views, namespaces or
policies; the task then fails without retrying.
drop_namespace = PolarisDropNamespaceOperator(
task_id="drop_namespace",
catalog=CATALOG,
namespace="sales",
trigger_rule=TriggerRule.ALL_DONE,
)
Tables¶
These operators manage Iceberg table definitions. They never request storage credentials from Polaris, and the tables they return contain only the name, UUID, locations and format version.
PolarisCreateTableOperator¶
PolarisCreateTableOperator creates a
table. The schema, partition spec and sort order use the JSON shapes of the Iceberg REST
specification and are sent as given, so newer features such as format v3 field defaults work;
Polaris validates them. spec-id and order-id may be omitted, and property values are
strings. An existing table is returned unchanged, without comparing its definition.
create_table = PolarisCreateTableOperator(
task_id="create_table",
catalog=CATALOG,
namespace="sales",
table="orders",
schema={
"type": "struct",
"fields": [
{"id": 1, "name": "order_id", "type": "long", "required": True},
{"id": 2, "name": "order_date", "type": "date", "required": False},
{"id": 3, "name": "amount", "type": "decimal(12, 2)", "required": False},
],
},
partition_spec={
"fields": [{"source-id": 2, "field-id": 1000, "name": "order_day", "transform": "day"}]
},
sort_order={
"fields": [
{"source-id": 1, "transform": "identity", "direction": "asc", "null-order": "nulls-first"}
]
},
properties={"write.format.default": "parquet"},
)
PolarisUpdateTablePropertiesOperator¶
PolarisUpdateTablePropertiesOperator
sets and/or removes table properties. It reads the table first and commits only what would
change, so re-running it does nothing. Like pyiceberg, it commits only if the table’s UUID is
unchanged, so a table dropped and re-created in the meantime is left alone (the task fails and
its retries reload the table).
update_table_properties = PolarisUpdateTablePropertiesOperator(
task_id="update_table_properties",
catalog=CATALOG,
namespace="sales",
table="orders",
updates={"history.expire.max-snapshot-age-ms": "604800000"},
)
PolarisRenameTableOperator¶
PolarisRenameTableOperator renames a
table, optionally into another namespace of the same catalog. When re-run after a successful
rename (the table is gone, its new name exists) it succeeds and returns False.
Since Polaris 1.9.0, a move into another namespace is refused (HTTP 403) unless the table’s
files lie within the destination namespace’s location, which is not the case for a table created
in its source namespace, or the catalog enables ALLOW_UNSTRUCTURED_TABLE_LOCATION.
rename_table = PolarisRenameTableOperator(
task_id="rename_table",
catalog=CATALOG,
namespace="sales",
table="orders",
new_table="orders_2026",
)
PolarisDropTableOperator¶
PolarisDropTableOperator drops a table.
By default only the catalog entry is removed and the data and metadata files stay in storage;
purge=True also deletes them, which Polaris allows only when the catalog property
polaris.config.drop-with-purge.enabled is "true" (or the server enables the
DROP_WITH_PURGE_ENABLED feature).
drop_table = PolarisDropTableOperator(
task_id="drop_table",
catalog=CATALOG,
namespace="sales",
table="orders_2026",
)
PolarisRegisterTableOperator¶
PolarisRegisterTableOperator registers an
existing Iceberg table from its current metadata.json file, for example a table moved from
another catalog or dropped without purging. The file must lie within the catalog’s allowed
locations.
register_table = PolarisRegisterTableOperator(
task_id="register_table",
catalog=CATALOG,
namespace="sales",
table="orders",
# The XCom of a task that read the table's metadata location before it was dropped.
metadata_location=metadata_location,
)
Principals¶
PolarisCreatePrincipalOperator¶
PolarisCreatePrincipalOperator
creates a principal and returns its name, client ID and properties.
Important
Polaris returns a new principal’s client secret only once, in the create response. This
operator never returns, logs or stores it. It suits principals that authenticate through an
external identity provider. For Polaris-issued credentials, obtain them out of band (for
example with polaris principals reset in the Polaris CLI) and put them in your secrets
backend. For the same reason the provider has no operator to rotate credentials.
create_principal = PolarisCreatePrincipalOperator(
task_id="create_principal",
principal_name=f"sales_etl_{ENV_ID}",
properties={"team": "sales"},
)
PolarisDeletePrincipalOperator¶
PolarisDeletePrincipalOperator
deletes a principal. The provisioning example removes everything it created in teardown tasks,
which run even after a failure:
delete_catalog_role = PolarisDeleteCatalogRoleOperator(
task_id="delete_catalog_role",
catalog_name=CATALOG,
role_name="writer",
trigger_rule=TriggerRule.ALL_DONE,
)
delete_principal_role = PolarisDeletePrincipalRoleOperator(
task_id="delete_principal_role",
role_name=f"sales_etl_role_{ENV_ID}",
trigger_rule=TriggerRule.ALL_DONE,
)
delete_principal = PolarisDeletePrincipalOperator(
task_id="delete_principal",
principal_name=f"sales_etl_{ENV_ID}",
trigger_rule=TriggerRule.ALL_DONE,
)
delete_catalog = PolarisDeleteCatalogOperator(
task_id="delete_catalog", catalog_name=CATALOG, trigger_rule=TriggerRule.ALL_DONE
)
Roles and grants¶
Polaris access control has two levels: a principal is assigned principal roles, a principal role is assigned catalog roles, and a catalog role is granted privileges on the catalog, a namespace, a table, a view or a policy.
Roles and assignments¶
PolarisCreatePrincipalRoleOperatorandPolarisCreateCatalogRoleOperatorcreate roles;PolarisDeletePrincipalRoleOperatorandPolarisDeleteCatalogRoleOperatorremove them.PolarisAssignPrincipalRoleOperatorandPolarisAssignCatalogRoleOperatorassign them;PolarisRevokePrincipalRoleOperatorandPolarisRevokeCatalogRoleOperatorundo that. Assigning an already-assigned role is a no-op, and revoking an assignment that does not exist succeeds.
create_principal_role = PolarisCreatePrincipalRoleOperator(
task_id="create_principal_role", role_name=f"sales_etl_role_{ENV_ID}"
)
create_catalog_role = PolarisCreateCatalogRoleOperator(
task_id="create_catalog_role", catalog_name=CATALOG, role_name="writer"
)
assign_principal_role = PolarisAssignPrincipalRoleOperator(
task_id="assign_principal_role",
principal_name=f"sales_etl_{ENV_ID}",
principal_role_name=f"sales_etl_role_{ENV_ID}",
)
assign_catalog_role = PolarisAssignCatalogRoleOperator(
task_id="assign_catalog_role",
principal_role_name=f"sales_etl_role_{ENV_ID}",
catalog_name=CATALOG,
catalog_role_name="writer",
)
PolarisGrantPrivilegeOperator and PolarisRevokePrivilegeOperator¶
PolarisGrantPrivilegeOperator grants one
privilege or a list of them to a catalog role.
PolarisRevokePrivilegeOperator revokes
them, optionally with cascade=True. Grant types are catalog, namespace (with
namespace), table (adds tableName), view (viewName) and policy
(policyName); newer Polaris versions add beta types such as semantic-model.
grant = PolarisGrantPrivilegeOperator(
task_id="grant",
catalog_name=CATALOG,
catalog_role_name="writer",
grants=[
{"type": "catalog", "privilege": "TABLE_READ_DATA"},
{"type": "catalog", "privilege": "TABLE_WRITE_DATA"},
{"type": "catalog", "privilege": "NAMESPACE_CREATE"},
],
)
Revoking undoes the grant and the role assignments:
revoke_privilege = PolarisRevokePrivilegeOperator(
task_id="revoke_privilege",
catalog_name=CATALOG,
catalog_role_name="writer",
grants=[{"type": "catalog", "privilege": "NAMESPACE_CREATE"}],
)
revoke_catalog_role = PolarisRevokeCatalogRoleOperator(
task_id="revoke_catalog_role",
principal_role_name=f"sales_etl_role_{ENV_ID}",
catalog_name=CATALOG,
catalog_role_name="writer",
)
revoke_principal_role = PolarisRevokePrincipalRoleOperator(
task_id="revoke_principal_role",
principal_name=f"sales_etl_{ENV_ID}",
principal_role_name=f"sales_etl_role_{ENV_ID}",
)
Policies¶
Polaris stores policies, attaches them to catalogs, namespaces and tables, and resolves which ones
are in effect on a table through inheritance (a table-level policy overrides a namespace-level
one of the same type). It does not execute them: the table-maintenance types
system.snapshot-expiry, system.data-compaction, system.metadata-compaction and
system.orphan-file-removal only take effect when something runs the matching Iceberg
procedures. Airflow can.
PolarisCreatePolicyOperator¶
PolarisCreatePolicyOperator creates a
policy in a namespace. By default (if_exists="update") an existing policy is updated when its
content or description differ, so the task declares the desired state; an argument left out keeps
its current value. An existing policy of a different type fails the task.
create_policy = PolarisCreatePolicyOperator(
task_id="create_policy",
catalog=CATALOG,
namespace=NAMESPACE,
policy_name=POLICY,
policy_type="system.snapshot-expiry",
content={
"version": "2025-02-03",
"enable": True,
"config": {"min_snapshot_to_keep": 10, "max_snapshot_age_days": 7},
},
description="Keep a week of snapshots, at least ten.",
)
Attaching, detaching and dropping policies¶
PolarisAttachPolicyOperator attaches a
policy to {"type": "catalog"}, {"type": "namespace", "path": [...]} or
{"type": "table-like", "path": [..., "<table>"]};
PolarisDetachPolicyOperator detaches
it. PolarisDropPolicyOperator drops a
policy (detach_all=True detaches it everywhere first). Re-attaching a policy is a no-op, but a
target holds at most one inheritable policy of each type: attaching a second one fails the task,
so detach the old policy first.
attach_policy = PolarisAttachPolicyOperator(
task_id="attach_policy",
catalog=CATALOG,
namespace=NAMESPACE,
policy_name=POLICY,
target={"type": "namespace", "path": [NAMESPACE]},
)
detach_policy = PolarisDetachPolicyOperator(
task_id="detach_policy",
catalog=CATALOG,
namespace=NAMESPACE,
policy_name=POLICY,
target={"type": "namespace", "path": [NAMESPACE]},
trigger_rule=TriggerRule.ALL_DONE,
)
drop_policy = PolarisDropPolicyOperator(
task_id="drop_policy",
catalog=CATALOG,
namespace=NAMESPACE,
policy_name=POLICY,
trigger_rule=TriggerRule.ALL_DONE,
)
PolarisGetApplicablePoliciesOperator¶
PolarisGetApplicablePoliciesOperator
returns the policies in effect on the catalog, a namespace or one table, with content parsed
from JSON. Disabled policies ("enable": false) are dropped unless enabled_only=False;
policy_types narrows the result.
With per_table=True it resolves policies for every table in namespace and returns one item
per (table, policy), ready for dynamic task mapping:
{
"catalog": "analytics",
"namespace": ["sales"],
"table": "orders",
"policy": {
"name": "...",
"type": "system.snapshot-expiry",
"inherited": True,
"content": {...},
},
# only for system.orphan-file-removal: the table's base location, so a task can check
# that the policy's "locations" stay inside it
"table_location": "s3://lake/sales/orders",
}
resolve_policies = PolarisGetApplicablePoliciesOperator(
task_id="resolve_policies",
catalog=CATALOG,
namespace=NAMESPACE,
policy_types=["system.snapshot-expiry"],
per_table=True,
)
@task
def run_maintenance(item: dict[str, Any]) -> str:
# A real Dag would run this on Spark, e.g. with SparkSqlOperator.
return to_spark_sql(item)
maintenance = run_maintenance.expand(item=resolve_policies.output)
Running maintenance on Spark¶
to_spark_sql() turns one item of
PolarisGetApplicablePoliciesOperator(per_table=True) into the Iceberg Spark procedure call
that applies the policy. It makes no network calls; a Spark task runs the result:
from airflow.providers.apache.polaris.utils.maintenance import to_spark_sql
from airflow.providers.apache.spark.operators.spark_sql import SparkSqlOperator
SparkSqlOperator.partial(task_id="run_maintenance", conn_id="spark_default").expand(
sql=resolve_policies.output.map(to_spark_sql)
)
The result is a CALL of one of Iceberg’s
Spark procedures, for example:
CALL analytics.system.expire_snapshots(table => 'analytics.sales.orders',
older_than => TIMESTAMP '2026-10-20 12:00:00+00:00', retain_last => 10)
These are not queries: Iceberg adds CALL to Spark SQL for table maintenance, with named
arguments (name => value). The SQL is built when to_spark_sql runs, so with .map
the age cutoff is taken when the Spark task starts, and a retry recomputes it.
The Spark session that runs the SQL needs:
the Iceberg Spark runtime (and
iceberg-aws-bundleor the matching bundle for your storage) and Iceberg’s SQL extensions,spark.sql.extensions=org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions; without them Spark does not knowCALL;a Spark catalog pointing at the Polaris catalog through Iceberg’s REST catalog. Its name is the one in the SQL: the Polaris catalog name, or
spark_catalog=when Spark names it differently.get_iceberg_rest_properties()returns its settings for a Polaris connection;access to the table’s files (credentials vended by Polaris, or the storage’s own), and a principal allowed to change the table, for example with
TABLE_WRITE_DATA;for orphan-file removal on object storage, either Hadoop support for the storage scheme (
hadoop-awsfors3://), or"prefix_listing": truein the policy’sconfigso Iceberg lists files itself.
Polaris’s policy schemas define only enable, and for orphan-file removal
max_orphan_file_age_in_days and locations; everything else is a free-form config.
to_spark_sql reads these settings:
Policy type |
Procedure |
Settings |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Ages are counted back from the moment the SQL is built and written as UTC timestamps. Keys
not in the table are dropped with a warning; with strict=True they raise instead.
dry_run=True only lists orphan files and raises for the other types, which have no dry run.
A disabled policy, an unknown policy type or schema version, and invalid values raise
PolarisInvalidArgumentError.
spark_catalog names the Polaris catalog in Spark, in the CALL and in its table
argument, when it differs from the Polaris name.
The generated statements were run against Iceberg 1.10 on Spark 3.5 and Iceberg 1.11 on Spark 4.1. Iceberg’s procedure arguments change between versions, so check the ones you use against your Iceberg release.
Reference¶
For further information, look at: