---
title: create_auto_cdc_flow
description: Process change data capture (CDC) data from a change data feed in Lakeflow pipelines with the `create_auto_cdc_flow()` Python function.
canonical_url: https://docs.databricks.com/aws/en/ldp/developer/ldp-python-ref-apply-changes
---

# create\_auto\_cdc\_flow

The `create_auto_cdc_flow()` function creates a flow that uses Lakeflow pipelines change data capture (CDC) functionality to process source data from a change data feed (CDF).

> [!NOTE]
>
> This function replaces the previous function `apply_changes()`. The two functions have the same signature. Databricks recommends updating to use the new name.

> [!IMPORTANT]
>
> You must declare a target streaming table to apply changes into. You can optionally specify the schema for your target table. When specifying the schema of the `create_auto_cdc_flow()` target table, you must include the `__START_AT` and `__END_AT` columns with the same data type as the `sequence_by` fields.
>
> To create the required target table, you can use the [create\_streaming\_table()](https://docs.databricks.com/aws/en/ldp/developer/ldp-python-ref-streaming-table.md) function in the pipeline Python interface.

## Syntax

```python
from pyspark import pipelines as dp

dp.create_auto_cdc_flow(
  target = "<target-table>",
  source = "<data-source>",
  keys = ["key1", "key2", "keyN"],
  sequence_by = "<sequence-column>",
  system_sequence_by = None, # optional
  ignore_null_updates = False, # optional
  ignore_null_updates_column_list = None, # optional
  ignore_null_updates_except_column_list = None, # optional
  columns_to_update = None, # optional
  apply_as_deletes = None, # optional
  apply_as_truncates = None, # optional
  column_list = None, # optional
  except_column_list = None, # optional
  stored_as_scd_type = "1", # optional
  track_history_column_list = None, # optional
  track_history_except_column_list = None, # optional
  name = None, # optional
  once = False # optional
)
```

For `create_auto_cdc_flow` processing, the default behavior for `INSERT` and `UPDATE` events is to *upsert* CDC events from the source: update any rows in the target table that match the specified key(s) or insert a new row when a matching record does not exist in the target table. Handling for `DELETE` events can be specified with the `apply_as_deletes` parameter.

To learn more about CDC processing with a change feed, see [The AUTO CDC APIs: Simplify change data capture with pipelines](https://docs.databricks.com/aws/en/ldp/cdc.md). For an example of using the `create_auto_cdc_flow()` function, see [AUTO CDC examples](https://docs.databricks.com/aws/en/ldp/cdc.md#cdc-example).

## Parameters

| Parameter                                                         | Type                         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ----------------------------------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `target`                                                          | `str`                        | Required. The name of the table to be updated. You can use the [create\_streaming\_table()](https://docs.databricks.com/aws/en/ldp/developer/ldp-python-ref-streaming-table.md) function to create the target table before executing the `create_auto_cdc_flow()` function.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `source`                                                          | `str`                        | Required. The data source containing CDC records.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `keys`                                                            | `list`                       | Required. The column or combination of columns that uniquely identify a row in the source data. This is used to identify which CDC events apply to specific records in the target table. You can specify either:<br /><ul><li>A list of strings: `["userId", "orderId"]`</li><li>A list of Spark SQL `col()` functions: `[col("userId"), col("orderId")]`. Arguments to `col()` functions cannot include qualifiers. For example, you can use `col(userId)`, but you cannot use `col(source.userId)`.</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `sequence_by`                                                     | `str`, `col()` or `struct()` | Required. The column names specifying the logical order of CDC events in the source data. Lakeflow pipelines use this sequencing to handle change events that arrive out of order. The specified column must be a sortable data type. You can specify either:<br /><ul><li>A string: `"sequenceNum"`</li><li>A Spark SQL `col()` function: `col("sequenceNum")`. Arguments to `col()` functions cannot include qualifiers. For example, you can use `col(userId)`, but you cannot use `col(source.userId)`.</li><li>A `struct()` combining multiple columns to break ties: `struct("timestamp_col", "id_col")`, it orders by the first struct field first, then by the second field if there's a tie, and so on.</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `system_sequence_by`                                              | `str` or `col()`             | The column specifying the system time at which each CDC event is known to the system. Used with `stored_as_scd_type="bitemporal"` to track changes across both business time (`sequence_by`) and system time. The specified column must be a sortable data type. See [Bitemporal AUTO CDC](https://docs.databricks.com/aws/en/ldp/cdc-bitemporal.md).<br />This parameter is optional and applies only to bitemporal tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `ignore_null_updates`                                             | `bool`                       | Controls how `null` values in incoming CDC updates are handled. When `ignore_null_updates` is `True`, `null` columns in an incoming update are ignored; the existing value in the target row is preserved. This also applies to nested columns with `null` values. When `ignore_null_updates` is `False`, `null` columns in an incoming update overwrite existing values in the target. Defaults to `False`.<br />Set to `True` when source events only include the columns that changed, so unchanged columns are not overwritten with `null`.<br />The default is `False`.<br />For finer control over which columns ignore `null` values, use `ignore_null_updates_column_list` or `ignore_null_updates_except_column_list`.                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `ignore_null_updates_column_list`                                 | `list`                       | A subset of columns for which `null` values in an incoming change record are ignored, so each of those columns keeps its existing value in the target. Columns outside the list apply explicit `null` values. Use this parameter to apply partial updates when your source sends only the columns that changed. Equivalent to the SQL `IGNORE NULL UPDATES ON columnList` clause. Use either `ignore_null_updates_column_list` or `ignore_null_updates_except_column_list`, not both.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `ignore_null_updates_except_column_list`                          | `list`                       | A subset of columns that apply explicit `null` values. All other columns ignore `null` values in an incoming change record and keep their existing value in the target. Equivalent to the SQL `IGNORE NULL UPDATES ON * EXCEPT (...)` clause. Use either `ignore_null_updates_column_list` or `ignore_null_updates_except_column_list`, not both.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `columns_to_update`                                               | `str` or `col()`             | The name of a source column that holds, for each change record, the set of columns to update as an array of column-name strings (`array<string>`). Columns not in the array keep their existing target values, while listed columns are written from the source, including explicit `null` values. Use this parameter when each change record updates a different set of columns and you need to apply explicit `null` values. Equivalent to the SQL `COLUMNS TO UPDATE` clause.<br />You cannot set `columns_to_update` together with `ignore_null_updates`, `ignore_null_updates_column_list`, or `ignore_null_updates_except_column_list`. `columns_to_update` is not supported for bitemporal tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `apply_as_deletes`                                                | `str` or `expr()`            | Specifies when a CDC event should be treated as a `DELETE` rather than an upsert. You can specify either:<br /><ul><li>A string: `"Operation = 'DELETE'"`</li><li>A Spark SQL `expr()` function: `expr("Operation = 'DELETE'")`</li></ul><br />To handle out-of-order data, the deleted row is temporarily retained as a tombstone in the underlying Delta table, and a view is created in the metastore that filters out these tombstones. The retention interval defaults to two days, and can be configured with the `pipelines.cdc.tombstoneGCThresholdInSeconds` table property.<br />If you use Auto Loader as the source for your CDC pipeline, Auto Loader does not guarantee file processing order. For details, see [Handle out-of-order data](https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/index.md#handle-out-of-order-data). Set `pipelines.cdc.tombstoneGCThresholdInSeconds` to a value that exceeds the maximum expected delay between event arrival and pipeline execution. This ensures that delete tombstones are retained long enough to correctly handle late-arriving or out-of-order deletion events. |
| `apply_as_truncates`                                              | `str` or `expr()`            | Specifies when a CDC event should be treated as a full table `TRUNCATE`. You can specify either:<br /><ul><li>A string: `"Operation = 'TRUNCATE'"`</li><li>A Spark SQL `expr()` function: `expr("Operation = 'TRUNCATE'")`</li></ul><br />Because this clause triggers a full truncate of the target table, it should be used only for specific use cases requiring this functionality. The `apply_as_truncates` parameter is supported only for SCD type 1. SCD type 2 does not support truncate operations.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `column_list` or `except_column_list`                             | `list`                       | A subset of columns to include in the target table. Use `column_list` to specify the complete list of columns to include. Use `except_column_list` to specify the columns to exclude. You can declare either value as a list of strings or as Spark SQL `col()` functions:<br /><ul><li>`column_list = ["userId", "name", "city"]`</li><li>`column_list = [col("userId"), col("name"), col("city")]`</li><li>`except_column_list = ["operation", "sequenceNum"]`</li><li>`except_column_list = [col("operation"), col("sequenceNum")`</li></ul><br />Arguments to `col()` functions cannot include qualifiers. For example, you can use `col(userId)`, but you cannot use `col(source.userId)`. The default is to include all columns in the target table when no `column_list` or `except_column_list` argument is passed to the function.                                                                                                                                                                                                                                                                                                                   |
| `stored_as_scd_type`                                              | `str` or `int`               | Whether to store records as SCD type 1, SCD type 2, or bitemporal. Set to `1` for SCD type 1, `2` for SCD type 2, or `"bitemporal"` to track changes across both business time and system time. Bitemporal requires `system_sequence_by`. See [Bitemporal AUTO CDC](https://docs.databricks.com/aws/en/ldp/cdc-bitemporal.md). The default is SCD type 1.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `track_history_column_list` or `track_history_except_column_list` | `list`                       | A subset of output columns to be tracked for history in the target table. Use `track_history_column_list` to specify the complete list of columns to be tracked. Use `track_history_except_column_list` to specify the columns to be excluded from tracking. You can declare either value as a list of strings or as Spark SQL `col()` functions:<br /><ul><li>`track_history_column_list = ["userId", "name", "city"]`</li><li>`track_history_column_list = [col("userId"), col("name"), col("city")]`</li><li>`track_history_except_column_list = ["operation", "sequenceNum"]`</li><li>`track_history_except_column_list = [col("operation"), col("sequenceNum")`</li></ul><br />Arguments to `col()` functions cannot include qualifiers. For example, you can use `col(userId)`, but you cannot use `col(source.userId)`. The default is to include all columns in the target table when no `track_history_column_list` or `track_history_except_column_list` argument is passed to the function.                                                                                                                                                        |
| `name`                                                            | `str`                        | The flow name. If not provided, defaults to the same value as `target`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `once`                                                            | `bool`                       | Optionally, define the flow as a one-time flow, such as a backfill. Using `once=True` changes the flow in two ways:<br /><ul><li>The return value. `streaming-query`. must be a batch DataFrame in this case, not a streaming DataFrame.</li><li>The flow is run one time by default. If the pipeline is updated with a complete refresh, then the `ONCE` flow runs again to recreate the data.</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |

## Notes

For SCD Type 1 targets in triggered pipelines, one or more `create_auto_cdc_flow()` flows can share a target with one `AUTO CDC FROM SNAPSHOT` flow defined in Python or SQL. The following requirements apply:

* Give each `create_auto_cdc_flow()` a unique `name`.
* Use the same number of keys in the same order for all flows. Snapshot-flow key names are compared case-insensitively with `AUTO CDC` key names. Multiple `create_auto_cdc_flow()` flows must use identical key names and casing.
* Use exactly the same data type for `sequence_by` and the snapshot version.
* Do not define expectations on the `AUTO CDC FROM SNAPSHOT` flow.
* Set `ignore_null_updates` to `False`, and do not set `ignore_null_updates_column_list` or `ignore_null_updates_except_column_list`.
* If you add an `AUTO CDC` flow to an existing `AUTO CDC FROM SNAPSHOT` target, the target must not contain user columns whose names conflict with reserved `AUTO CDC` system columns.
* Do not use this pattern for SCD Type 2 or bitemporal targets.

For an example, see [Add a backfill to an AUTO CDC SCD Type 1 table](https://docs.databricks.com/aws/en/ldp/flows-backfill.md#auto-cdc-scd1-backfill).
