SQL reference

Name tables, write queries in Spark SQL, and know what each surface allows.

Contrast answers, SQL in Contrast, the MCP query tool, the warehouse CLI (contrast query run), and dbt all run SQL against the same warehouse. This page covers the rules they share.

Dialect

Reads are written in Spark SQL and run as written. That covers answers in Contrast and Slack, SQL in Contrast, the MCP query tool, contrast query run, and API reads that name no dialect. Double-quoted and backtick names are identifiers, as with Spark's ANSI double-quoted identifiers, so "created_at" > ... compares the column; write strings in single quotes, with '' for a quote inside one. Spark's semantics apply: / always returns a double (7 / 2 is 3.5), and Spark's function names apply (datediff(end, start), date_format(ts, 'yyyy-MM'), collect_list(x)).

Writes and dbt models use the warehouse's own dialect, Trino: ANSI SQL with Trino's functions, types, and casts. The Postgres endpoint takes DuckDB SQL.

Saved SQL keeps the dialect it was written in. New or edited verified queries, contract custom SQL rules, notebook SQL cells, and row filters on the Contrast Warehouse are saved as Spark SQL. SQL saved before Spark became the default keeps running as Trino SQL, and resending it unchanged doesn't convert it.

API callers choose per read with sql_dialect on POST /warehouse/queries: spark, athena for the warehouse's own dialect, or duckdb. A read that names no dialect is Spark SQL. Verified queries, contract custom SQL rules, and row filters take the same field to save SQL in a dialect other than the default; a notebook SQL cell takes it in its metadata, as embrasure.sql_dialect.

PostgreSQL habitSpark SQL
created_at::dateCAST(created_at AS date)
name ILIKE '%acme%'lower(name) LIKE '%acme%'
now() - interval '7 days'current_timestamp() - INTERVAL 7 DAYS
date_trunc('week', created_at)date_trunc('week', created_at)
count(DISTINCT user_id) over large tablesapprox_count_distinct(user_id) for a fast estimate
amount::numeric / 100CAST(amount AS decimal(18, 2)) / 100
DATE_PART('epoch', created_at)unix_timestamp(created_at)
A cast that might failtry_cast(value AS int) returns NULL instead of an error

Send one statement per request. A trailing semicolon is fine; a second statement is rejected.

Name tables

Use three-part names: database.schema.table.

SELECT count(*) AS customer_count
FROM contrast.stripe.customers;
  • Database. Sources connected during setup sync into the ember database. A source added later can use a different destination database; check its name on the source's page in Sources or with Copy full name in the catalog.
  • Schema. Synced tables keep their source schema. A PostgreSQL table in public lands in public; application sources use their own schema, such as stripe. Schema and table names are lowercased, characters other than letters, numbers, and underscores become _, and a name that doesn't start with a letter gets an s_ prefix.
  • Table. If two source tables would get the same name, later ones get a _2, _3, … suffix.

Columns that start with _embrasure_ are bookkeeping columns Contrast adds during syncs. They don't appear in the catalog, but some are useful in queries; for example, application syncs such as Stripe keep deleted records with _embrasure_deleted = true.

To explore what exists:

SHOW TABLES IN contrast.stripe;
SHOW COLUMNS FROM contrast.stripe.customers;
DESCRIBE contrast.stripe.customers;

What each surface can run

SurfaceAllowed statements
Contrast in SlackSELECT only. Comments and % placeholders are rejected.
MCP query tool and the warehouse CLISELECT, SHOW, and DESCRIBE.
SQL in ContrastThe above, plus writes for editors and above (see below).
dbtModel builds into <database>.default.<model>.

Writes

Editors can create and change their own tables from SQL or dbt. Writes are limited to tables in a database's default schema, such as analytics.default.weekly_revenue.

Allowed: CREATE DATABASE, CREATE TABLE, CREATE TABLE AS SELECT, INSERT, MERGE, DROP TABLE, and UNDROP TABLE.

Recovering a dropped table

A dropped table stays recoverable for 7 days. Its data is not deleted until then.

SHOW TABLES DROPPED IN analytics;

UNDROP TABLE analytics.default.weekly_revenue;
UNDROP TABLE WITH ID '6f8174f7-f45b-4085-b198-46a7bf56e301';

To delete a table's data without keeping it recoverable, use DROP TABLE analytics.default.weekly_revenue PURGE.

SHOW TABLES DROPPED lists each dropped table with its table_id and the time it stays recoverable until. UNDROP TABLE restores the most recently dropped table with that name; use WITH ID to pick an older one. A table can't be restored while another table has its name, so drop that table first.

Not allowed:

  • Changing synced tables. Tables managed by a source sync are read-only; build a new table from them instead.
  • CREATE VIEW. Use CREATE TABLE AS SELECT.
  • CREATE OR REPLACE TABLE, INSERT OVERWRITE, and partition writes.
  • UPDATE, DELETE, and ALTER.

A database named default is reserved. Database and table names must start with a lowercase letter and use only lowercase letters, numbers, and underscores.

Results

LimitValue
Query timeout120 seconds by default
Data scanned per query25 GiB
Rows kept per result10,000 by default. SQL also offers 100,000 or unlimited.
Result retention7 days

Use LIMIT when you only need a sample, and filter on date columns to scan less data. See limits for every query limit.

In API and MCP results, decimals, integers larger than 253, timestamps, and binary values are returned as strings so no precision is lost.

Common errors

MessageFix
Warehouse table '…' was not found. Use database.schema.table.Check the name with Copy full name in the catalog or SHOW TABLES IN <database>.<schema>.
Table reference '…' is ambiguous …Use the full three-part name.
Warehouse database '…' does not exist or is not ready.Wait for the first sync to finish, or check the destination database name.
This endpoint only supports read-only warehouse statements.Run writes from SQL in Contrast or through dbt.
This is a managed ingestion table. Use its database.schema.table identity for reads.Query the table by its catalog name, not its storage name.
Query cancelled after 120 seconds.Filter on a date column, select fewer columns, or aggregate before joining.
Query stopped after scanning more data than this warehouse allows.Narrow the date range or select fewer columns.

See troubleshooting for more.