Skip to main content

Alignment — Comprehensive

AS alignment, THEN alignment, SET = alignment, operator chains, and their interactions.

AS alignment — longest expression gets no extra padding

The longest expression (count(order_id)) gets just one space before AS. Shorter expressions pad to match.

select customer_id as cust, count(order_id) as order_count, sum(total) as total_spent from orders
select
customer_id as cust,
count(order_id) as order_count,
sum(total) as total_spent
from orders

AS alignment — items without AS don't participate

Items without explicit AS aliases render normally. Only items with AS are padded.

select customer_id as cust, total_amount, name as n from t
select
customer_id as cust,
total_amount,
name as n
from t

THEN alignment — varying condition widths

Longest condition gets no extra padding. Shorter conditions pad so THEN aligns.

select case when status = 'active' then 'A' when status = 'pending_review' then 'P' when s = 1 then 'X' end from t
select
case
when status = 'active' then 'A'
when status = 'pending_review' then 'P'
when s = 1 then 'X'
end
from t

THEN alignment — equal-width conditions get no extra padding

When all conditions are the same width, no padding is added.

select case when a = 1 then 'x' when b = 2 then 'y' when c = 3 then 'z' end from t
select
case
when a = 1 then 'x'
when b = 2 then 'y'
when c = 3 then 'z'
end
from t

THEN alignment — skipped when it would exceed maxLineWidth

When THEN alignment would push a line beyond maxLineWidth, alignment is skipped. The THEN stays on the same line — it does NOT break to a new line.

select case when very_long_column_name = 'very_long_value' then 'result_a' when x = 1 then 'result_b' end from t
select
case
when very_long_column_name = 'very_long_value' then 'result_a'
when x = 1 then 'result_b'
end
from t

SET = alignment — longest target gets no extra padding

update t set first_name = 'John', last_name = 'Doe', email_address = 'john@example.com' where id = 1
update
t
set
first_name = 'John',
last_name = 'Doe',
email_address = 'john@example.com'
where id = 1

Multiple tokens — AS and THEN simultaneously

Both AS and THEN active at the same time. THEN aligns within CASE scope. AS aligns within SELECT scope. No extra space between END and AS.

select case when status = 'active' then 'A' when status = 'pending_review' then 'P' end as status_code, name as n from t
select
case
when status = 'active' then 'A'
when status = 'pending_review' then 'P'
end as status_code,
name as n
from t

Multiple tokens — AS and = simultaneously

UPDATE with short body stays inline — no alignment fires because items don't break to separate lines.

update t set short = 1, very_long_name = 2 where id = 1
update t set short = 1, very_long_name = 2 where id = 1

WHERE = alignment across AND conditions

WHERE fits on one line, so no breaking occurs and = alignment doesn't fire. Alignment only works when items are on separate lines.

select * from t where status = 'active' and priority = 5 and region = 'US'
select *
from t
where status = 'active' and priority = 5 and region = 'US'

WHERE = alignment — actually breaks (narrow width)

With narrow width, WHERE conditions break to separate lines and = alignment fires. The alignment accounts for keyword width: WHERE body starts after 0 chars, AND body starts after "AND " (4 chars). So the dry-run column positions differ.

select * from t where status = 'active' and priority = 5 and region = 'US'
select *
from t
where
status = 'active'
and priority = 5
and region = 'US'

Concat operator chain — short stays flat

select first_name || ' ' || last_name from employees
select first_name || ' ' || last_name from employees

Concat chain — breaks with leading operators

Nested Concat AST is flattened into OpChain. Each || operator leads a new indented line.

select first_name || ' ' || middle_name || ' ' || last_name || ' (' || department || ')' as display_name from employees
select
first_name
|| ' '
|| middle_name
|| ' '
|| last_name
|| ' ('
|| department
|| ')' as display_name
from employees

Arithmetic chain — short stays flat

select base_price + tax_amount + shipping_fee - discount as total from orders
select base_price + tax_amount + shipping_fee - discount as total from orders

Arithmetic chain — breaks with leading operators

TermExpr(first, rest) is flattened into OpChain. Operators lead new indented lines.

select base_price + tax_amount + shipping_fee - discount - returns_amount as total from orders
select
base_price + tax_amount + shipping_fee
- discount
- returns_amount as total
from orders

CASE with ELSE — ELSE value aligned with THEN values

When THEN alignment is active, ELSE value is padded to start at the same column as THEN values.

select case when status = 'active' then 'A' when status = 'pending_review' then 'P' when s = 1 then 'X' else 'Unknown' end from t
select
case
when status = 'active' then 'A'
when status = 'pending_review' then 'P'
when s = 1 then 'X'
else 'Unknown'
end
from t

Alignment disabled — no padding

select customer_id as cust, count(order_id) as order_count, sum(total) as total_spent from orders
select
customer_id as cust,
count(order_id) as order_count,
sum(total) as total_spent
from orders

Single item — no alignment needed (needs 2+ siblings)

select count(*) as cnt from t
select count(*) as cnt from t