SQL Queries

A model project uses SQL in its data fetchers, sensitivities, run datetime query, input aggregations, and views. This page covers which SQL dialect each query is written in and the parts of pd4castr that behave differently from a plain database. Each query’s own page covers what it must return.

Dialect

Data fetcher queries run in PostgreSQL against pd4castr’s AEMO MMS database. Every other query runs in DuckDB, so write it in DuckDB SQL. The DuckDB SQL reference covers the syntax and functions available.

Data fetcher queries

The check and fetch queries of an AEMO MMS fetcher read tables under the mms schema, such as mms.dispatchprice. Both run in a read-only session under the fetcher’s statementTimeoutSeconds, 10 seconds by default, so a query that scans too much data fails instead of returning late.

Two callers wait on a data fetcher query while it runs: pd4castr fetch, and the platform’s checks when you publish. For both, the platform stops each query after 20 seconds, even when statementTimeoutSeconds is longer, so pd4castr fetch can’t fetch test data with a query that runs longer than 20 seconds. Each request pd4castr fetch makes ends after 30 seconds, so for a fetcher with fetchQueries, the check query and the fetch query it runs with need to finish inside 30 seconds together. A publish’s checks all share one 30 second request. Scheduled fetches run under the full statementTimeoutSeconds.

When you publish the model, the platform plans the queries with EXPLAIN without running them, and rejects the model if a query references a table or column that doesn’t exist. For a fetcher with fetchQueries, the platform runs the check query and plans each fetch query with the check result in place of its :<column> references. It rejects a fetch query that reads a column the check query doesn’t return, and rejects the model when the check query runs longer than 20 seconds.

Table names

How a query refers to an input depends on where the query runs:

  • Sensitivity, run datetime, and input aggregation queries use the input’s key as the table name, so an input with "key": "demand" is the table demand.
  • View queries and the Data Explorer prefix every table by its kind: input_<key>, dataset_<key>, and output. The same input is input_demand there.
  • An input whose fetcher declares fetchQueries is one table per query: <input_key>_<query_key>, or input_<input_key>_<query_key> in a view.

The platform rewrites a key into a valid table name. Each - in a key becomes _minus_, so an input with "key": "spot-price" is the table spot_minus_price, or input_spot_minus_price in a view.

Next steps