Query

Query hooks allow you to execute SQL queries against any defined connection in your environment. This is particularly useful for pre and post-processing tasks, data validation, and maintaining metadata.

Configuration

- type: query
  connection: target_db   # Connection name (required)
  query:      "SELECT * FROM table"  # Required
  into:       "my_results"  # Optional: Store results in variable
  transient:  false       # Optional: Use transient connection
  transaction: true      # Optional: Use transaction, or set isolation level
  on_failure: abort       # Optional: abort/warn/quiet/skip
  id:         my_id       # Optional. Will be generated. Use `log` hook with {runtime_state} to view state.

Properties

Property
Required
Description

connection

Yes

The name of the connection to execute the query against

query

Yes

The SQL query to execute (can also be the path of a local .sql file with file:// prefix)

into

No

Variable name to store the query results. If not specified, results are included in hook output.

transient

No

Whether to use a transient connection (default: false)

transaction

No

Whether to use transaction, and optionally set isolation level. Ideal for multiple statements. Supported values: default, true ( same as default), read_uncommitted, read_committed, repeatable_read, serializable

on_failure

No

What to do if the query fails (abort/warn/quiet/skip)

Output

When the query hook executes successfully, it returns the following output that can be accessed in subsequent hooks:

You can access these values in subsequent hooks using the following syntax (jmespath):

  • {state.hook_id.status} - Status of the hook execution

  • {state.hook_id.query} - The executed query

  • {state.hook_id.connection} - The connection used

  • {state.hook_id.columns} - List of column names

  • {state.hook_id.result} - Array of result records (only if 'into' is not specified)

  • {state.hook_id.result[0].column_name} - Access specific values from the result

  • {store.variable_name} - Stored results when using 'into' parameter

Examples

Store Query Results for Later Use

Execute a query and store results for use in subsequent hooks:

Transactional Session Settings

Use pre_merge and post_merge (formally pre_sql and post_sql) (available in v1.4.24+) for tight session settings. Below example uses the same connection session/transaction to run custom SET queries.

Get Configuration Values

Retrieve configuration values from database and use them in other hooks:

Conditional Processing Based on Query Results

Use query results to control subsequent hook execution:

Update Status Table

Track when a replication starts by updating a status table:

Data Quality Check

Verify data quality after loading and raise an alert if issues are found:

Cleanup Old Data

Clean up old data before loading new data in incremental mode:

Update Metadata

Update a metadata table after successful load:

Validate and Rollback

Check data consistency and rollback if issues are found:

Aggregate Statistics

Calculate and store aggregated statistics after data load:

Transaction Support

Execute multiple operations within a transaction with specific isolation level:

When using transactions, if any statement fails, all operations within the transaction will be rolled back automatically.

Last updated

Was this helpful?