View a markdown version of this page

Spark Declarative Pipelines with Iceberg materialized views - AWS Glue

Spark Declarative Pipelines with Iceberg materialized views

This topic describes how to run Spark Declarative Pipelines (SDP) that define Apache Iceberg materialized views on AWS Glue version 6.1 and later. A materialized view stores the precomputed result of a query over one or more Iceberg source tables. When a pipeline runs, AWS Glue refreshes the materialized view so that it reflects the current state of its sources.

By default, an SDP materialized view over Iceberg sources is stored as a plain Iceberg table, which AWS Glue fully recomputes on every pipeline run. On AWS Glue version 6.1 and later, you can instead back a materialized view with a AWS Glue Iceberg materialized view. A AWS Glue Iceberg materialized view is a AWS Glue Data Catalog table that has IsMaterializedView set to true and that AWS Glue can refresh incrementally, merging only the source rows that changed since the last refresh instead of recomputing the entire view.

Incremental refresh is opt-in and applies only when the materialized view meets a set of requirements, including backing it with a AWS Glue Iceberg materialized view and enabling incremental refresh (see Requirements for incremental refresh). When a run cannot refresh incrementally, AWS Glue falls back to a full refresh.

How it works

A Spark Declarative Pipeline is defined by a pipeline specification plus one or more transformation definitions. When you run the pipeline on AWS Glue version 6.1 and later, AWS Glue evaluates each materialized view and chooses one of the following refresh outcomes:

  • Full refresh — AWS Glue recomputes the entire materialized view from its sources. This always happens on the first run of a materialized view and whenever an incremental refresh cannot be performed.

  • Incremental refresh — AWS Glue merges only the source rows that changed since the last refresh. In the run output, this outcome appears as refreshed (incremental). Incremental refresh is available on AWS Glue version 6.1 and later and requires that you opt in.

  • Skip — AWS Glue does not refresh the materialized view because no new Iceberg snapshot has been committed to its sources since the last refresh.

A materialized view is a supported view for incremental refresh when its query uses the restricted SQL surface described in Limitations (for example, a GROUP BY aggregation such as SUM). Even for a supported view with incremental refresh enabled, an individual run falls back to a full refresh in cases such as the following:

  • This is the first run of the materialized view. The first run is always a full refresh.

  • The changed data covers a large fraction of the source table's data files. This is common for small tables, where even a small change can rewrite most of the files. When new data lands in only a small fraction of the files, AWS Glue can refresh incrementally.

  • A large number of rows changed since the last refresh.

  • The query joins three or more tables.

  • A source table was compacted.

  • The query is not eligible for incremental maintenance (for example, it uses a user-defined function).

  • A full refresh was requested for the run.

  • The pipeline configuration changed since the last run, which reseeds the materialized view with a full refresh.

Requirements for incremental refresh

For a materialized view to refresh incrementally, all of the following must be true. If any is not met, AWS Glue falls back to a full refresh, or, with GLUE_ICEBERG_MV_ONLY, the run fails. For the configuration steps, see Required configuration.

  • The materialized view is backed by a AWS Glue Iceberg materialized view. Set spark.sql.pipelines.materializedView.implWithGlueIcebergMVMode to GLUE_ICEBERG_MV_PREFERRED or GLUE_ICEBERG_MV_ONLY. The default, NO_GLUE_ICEBERG_MV, keeps the materialized view a plain Iceberg table.

  • Incremental refresh is enabled: spark.sql.optimizer.incrementalMVRefresh.enabled is set to true.

  • The materialized view and all of its source tables are Apache Iceberg tables in a AWS Glue catalog that has its account (glue.id) and AWS Region set in the catalog's Spark configuration.

  • The source tables and the materialized view are in the same AWS account and AWS Region.

  • AWS Lake Formation fine-grained access control is not in use.

  • The materialized view's database was created with an empty CreateTableDefaultPermissions list.

  • The query uses the supported SQL surface: a single SELECT with FROM, WHERE, GROUP BY, and HAVING, with INNER JOIN as the only join type. It must not use set operations, subqueries, DISTINCT, window functions, or user-defined functions.

Launching a pipeline on AWS Glue version 6.1 and later

You package and launch the pipeline the same way as any other Spark Declarative Pipeline. For the pipeline file layout and upload steps, see Creating a pipeline. You run the pipeline on a standard AWS Glue version 6.1 or later job; no custom container or image is required.

Create the job with AWS Glue version 6.1 and the glueetl type, set ScriptLocation to your pipeline archive in Amazon S3, and set the following job arguments:

--enable-spark-declarative-pipeline true --datalake-formats iceberg --conf spark.glue.sdp.jobMode=RUN

When the job starts, AWS Glue runs the pipeline inside the AWS Glue job: it starts an in-driver Spark Connect server automatically and executes the pipeline defined in your pipeline specification. You do not use an external pipeline launcher.

Required configuration

To run Iceberg materialized views in a Spark Declarative Pipeline on AWS Glue version 6.1 and later, configure the following. Note where each setting goes: the Iceberg catalog settings are AWS Glue job --conf arguments, while the materialized-view implementation mode and the incremental-refresh opt-in are entries in the pipeline specification configuration: block.

Iceberg catalog

Configure the AWS Glue Data Catalog as an Iceberg catalog named glue_catalog, using the following AWS Glue job --conf arguments:

--conf spark.sql.catalog.glue_catalog=org.apache.iceberg.spark.SparkCatalog --conf spark.sql.catalog.glue_catalog.type=glue --conf spark.sql.catalog.glue_catalog.glue.id=111122223333 --conf spark.sql.catalog.glue_catalog.glue.region=us-east-1 --conf spark.sql.catalog.glue_catalog.warehouse=s3://amzn-s3-demo-bucket/warehouse/

The glue.id (the AWS account ID of the catalog) and glue.region settings are required for materialized-view support.

Materialized-view implementation mode

In the pipeline specification configuration block, set the materialized-view implementation mode, which controls how AWS Glue backs each materialized view:

spark.sql.pipelines.materializedView.implWithGlueIcebergMVMode: GLUE_ICEBERG_MV_PREFERRED
  • GLUE_ICEBERG_MV_PREFERRED — Back the materialized view with a AWS Glue Iceberg materialized view when eligible, and otherwise fall back to a plain Iceberg table. This is the recommended setting, and it is required for incremental refresh.

  • GLUE_ICEBERG_MV_ONLY — Require a AWS Glue Iceberg materialized view. The pipeline fails if the materialized view is not eligible to be backed by one.

  • NO_GLUE_ICEBERG_MV — Back the materialized view with a plain Iceberg table (do not use a AWS Glue Iceberg materialized view). This is the default. A plain Iceberg table does not support incremental refresh.

Incremental refresh

Opt in to incremental refresh in the pipeline specification configuration block. Set it explicitly to true:

spark.sql.optimizer.incrementalMVRefresh.enabled: "true"

When incremental refresh is enabled, AWS Glue refreshes the materialized view each time the pipeline runs (with spark.glue.sdp.jobMode=RUN): it performs an incremental refresh when the run is eligible, and otherwise falls back to a full refresh.

Target database permissions

Create the target database that holds the materialized view with CreateTableDefaultPermissions set to an empty list ([]).

aws glue create-database --database-input '{ "Name": "sdp_demo", "CreateTableDefaultPermissions": [] }'

An empty CreateTableDefaultPermissions list is required for the pipeline to manage the materialized view in the database.

Example: incremental refresh

This example defines a materialized view that aggregates a sales fact table. Use a source table with enough data files that a run's new rows land in only a small fraction of them (for example, a table spread across dozens of data files, where a run appends roughly one new file). With a small table, AWS Glue falls back to a full refresh because the changed rows cover too large a fraction of the files.

Pipeline specification (spark-pipeline.yml)

The specification lists the transformations and enables incremental refresh:

name: sales_pipeline catalog: glue_catalog database: sdp_demo storage: s3://amzn-s3-demo-bucket/pipeline-storage/ configuration: spark.sql.optimizer.incrementalMVRefresh.enabled: "true" spark.sql.pipelines.materializedView.implWithGlueIcebergMVMode: "GLUE_ICEBERG_MV_PREFERRED" libraries: - glob: include: transformations/**
Transformation (transformations/daily_sales_mv.sql)

The transformation defines the materialized view over the Iceberg source table:

CREATE MATERIALIZED VIEW daily_sales_mv PARTITIONED BY (sale_date) AS SELECT sale_date, region, SUM(amount) AS total_amount FROM glue_catalog.sdp_demo.sales GROUP BY sale_date, region;

The materialized view uses a GROUP BY aggregation, so it is a supported view for incremental refresh.

Refresh behavior across runs

With a realistic-scale source table, successive pipeline runs behave as follows:

  1. First run — A full refresh. AWS Glue computes the entire materialized view from the source table.

  2. Run after appending a small amount of new data — An incremental refresh. When the new rows land in only a small fraction of the table's data files, AWS Glue merges only the changed rows and reports refreshed (incremental). The resulting materialized-view snapshot summary shows the rows added and deleted by the merge.

  3. Run with no new snapshot — A skip. AWS Glue does not refresh the materialized view because no new Iceberg snapshot was committed to the source.

  4. Run after a change that is not eligible for incremental refresh — A full refresh. For example, if the changed data covers a large fraction of the files, a third table is joined, the source is compacted, or the query uses a UDF, AWS Glue recomputes the entire view.

Verifying and troubleshooting incremental refresh

Use the following checks to confirm that incremental refresh is in effect and to understand why a particular run performed a full refresh.

Confirm that incremental refresh is enabled

Check that the materialized view meets all of the requirements in Requirements for incremental refresh, and that the materialized view is registered as a AWS Glue Iceberg materialized view: in the AWS Glue Data Catalog, the table has IsMaterializedView set to true (check the output of the get-table AWS CLI command).

Read the refresh outcome

For each materialized view, the pipeline run records the refresh outcome in the Spark driver log (available in the AWS Glue job's Amazon CloudWatch Logs). Search the driver log for one of the following:

  • refreshed (incremental) — The run merged only the changed rows (for example, IVM-backed SDP MV <name> refreshed (incremental).). The materialized-view snapshot summary shows the rows added and deleted.

  • refreshed (full) — The run recomputed the entire view (for example, IVM-backed SDP MV <name> refreshed (full, with IVM setup)., preceded by a line that gives the reason).

  • A skip message — The run did not refresh the view because its definition and sources were unchanged (for example, Skipping refresh of up-to-date SDP materialized view <name> (definition and sources unchanged); existing data left in place (no write).).

Why a run performed a full refresh

If a run reports a full refresh when you expected an incremental one, the run was not eligible for incremental refresh. For the full list of cases that cause a fallback to a full refresh, see How it works.

Limitations

The following limitations apply to Iceberg materialized views in Spark Declarative Pipelines on AWS Glue version 6.1 and later.

  • For incremental refresh, a materialized view must use a restricted SQL surface: a single SELECT with FROM, WHERE, GROUP BY, and HAVING, with INNER JOIN as the only join type. It must not use set operations, subqueries, DISTINCT, window functions, or user-defined functions. A query outside this surface is not eligible for incremental refresh, so AWS Glue falls back to a full refresh.

  • To refresh a materialized view, re-run the pipeline. The REFRESH MATERIALIZED VIEW statement is not supported.

  • AWS Lake Formation fine-grained access control is not supported for these materialized views.

  • The Iceberg source tables and the materialized view must be in the same AWS account and AWS Region.