airflow.providers.teradata.operators.tpt

Classes

DdlOperator

Operator to execute one or more DDL (Data Definition Language) statements on a Teradata Database.

Module Contents

class airflow.providers.teradata.operators.tpt.DdlOperator(*, ddl, error_list=None, teradata_conn_id=TeradataHook.default_conn_name, ssh_conn_id=None, remote_working_dir=None, ddl_job_name=None, **kwargs)[source]

Bases: airflow.models.BaseOperator

Operator to execute one or more DDL (Data Definition Language) statements on a Teradata Database.

This operator is designed to facilitate DDL operations such as creating, altering, or dropping tables, indexes, views, or other database objects in a scalable and efficient manner.

It leverages the TPT (Teradata Parallel Transporter) utility to perform the operations and supports templating for SQL statements, allowing dynamic generation of SQL at runtime.

Key Features: - Executes one or more DDL statements sequentially on Teradata using TPT - Supports error handling with customizable error code list - Supports XCom push to share execution results with downstream tasks - Integrates with Airflow’s templating engine for dynamic SQL generation - Can execute statements via SSH connection if needed

Parameters:
  • ddl (list[str]) – A list of DDL statements to be executed. Each item should be a valid SQL DDL command supported by Teradata.

  • error_list (int | list[int] | None) – Optional integer or list of error codes to ignore during execution. If provided, the operator will not fail when these specific error codes occur. Example: error_list=3803 or error_list=[3803, 3807]

  • teradata_conn_id (str) – The connection ID for the Teradata database. Defaults to TeradataHook.default_conn_name.

  • ssh_conn_id (str | None) – Optional SSH connection ID if the commands need to be executed through SSH.

  • remote_working_dir (str | None) – Directory on the remote server where temporary files will be stored.

  • ddl_job_name (str | None) – Optional name for the DDL job.

Raises:
  • ValueError – If the ddl parameter or error_list is invalid.

  • RuntimeError – If underlying TPT execution (tbuild) fails with non-zero exit status.

  • ConnectionError – If remote SSH connection cannot be established.

  • TimeoutError – If SSH connection attempt times out.

  • FileNotFoundError – If required TPT utility (tbuild) is missing locally or on remote host.

Example usage:

# Example of creating tables using DdlOperator
create_tables = DdlOperator(
    task_id="create_tables_task",
    ddl=[
        "CREATE TABLE my_database.my_table1 (id INT, name VARCHAR(100))",
        "CREATE TABLE my_database.my_table2 (id INT, value FLOAT)",
    ],
    teradata_conn_id="my_teradata_conn",
    error_list=[3803],  # Ignore "Table already exists" errors
    ddl_job_name="create_tables_job",
)

# Example of dropping tables using DdlOperator
drop_tables = DdlOperator(
    task_id="drop_tables_task",
    ddl=["DROP TABLE my_database.my_table1", "DROP TABLE my_database.my_table2"],
    teradata_conn_id="my_teradata_conn",
    error_list=3807,  # Ignore "Object does not exist" errors
    ddl_job_name="drop_tables_job",
)

# Example using templated SQL file
alter_table = DdlOperator(
    task_id="alter_table_task",
    ddl="{{ var.value.get('ddl_directory') }}/alter_table.sql",
    teradata_conn_id="my_teradata_conn",
    ssh_conn_id="my_ssh_conn",
    ddl_job_name="alter_table_job",
)
template_fields = ('ddl', 'ddl_job_name')[source]
template_ext = ('.sql',)[source]
ui_color = '#a8e4b1'[source]
ddl[source]
error_list = None[source]
teradata_conn_id = 'teradata_default'[source]
ssh_conn_id = None[source]
remote_working_dir = None[source]
ddl_job_name = None[source]
execute(context)[source]

Execute the DDL operations using the TptHook.

on_kill()[source]

Handle termination signals and ensure the hook is properly cleaned up.

Was this entry helpful?