Raw SQL Statements
Raw SQL Statements
Raw SQL statements allow executing arbitrary SQL directly against the database. Use sparingly for database-specific operations not supported by Trilogy syntax.
Syntax
rawsql_statement: "raw_sql" "(" MULTILINE_STRING ")"
Usage
Wrap raw SQL in triple quotes inside raw_sql():
raw_sql('''
CREATE INDEX idx_customer_name ON customers(name);
''');
Use Cases
- Creating indexes
- Database-specific DDL
- Administrative operations
- Stored procedure calls
- Setting session variables
Example
# Create a materialized view using database-specific syntax
raw_sql('''
CREATE MATERIALIZED VIEW IF NOT EXISTS daily_sales AS
SELECT
date_trunc('day', order_date) as day,
SUM(total) as daily_total
FROM orders
GROUP BY 1;
''');
Warning
Raw SQL bypasses Trilogy's semantic layer. Use only when necessary, as these statements won't benefit from optimization or validation.
