Jump to content

Lateral column alias

From Open Lakehouse Wiki
Revision as of 00:52, 25 November 2026 by Open Lakehouse Wiki sync (talk | contribs) (Sync from open-lakehouse/wiki@7c9abaa)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Lateral column alias
Also known as LCA, reusable column alias, lateral alias reference
Field SQL name resolution
Key parts Alias defined earlier in the same SELECT list; resolution after table columns
Open source implementations Apache Spark (since 3.4.0), Databricks SQL, DuckDB, Snowflake
Related Window functions
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (docs/sql-ref-name-resolution.md, SQLConf.scala, error-conditions.json), the Spark 3.4.0 and 3.5.0 release notes, the Databricks name resolution reference, DuckDB 2.0 friendly SQL page, Snowflake SELECT reference, BigQuery query syntax reference and PostgreSQL 19 SELECT reference.

A lateral column alias is a column alias defined in a SELECT list that later expressions in the same SELECT list can refer to, as in SELECT price * qty AS total, total * 0.2 AS tax; Apache Spark supports it since version 3.4.0.[1][2] It saves repeating an expression or wrapping the query in a subquery. Several engines, among them BigQuery and PostgreSQL, do not allow it,[3][4] so the same query can work in one engine and fail in another.

How does Spark resolve a lateral column alias?

Spark SQL treats lateral column aliasing as one step of its name resolution order. Inside a SELECT list, Spark first tries to match an identifier to a column of the tables in the FROM clause; only if that fails does it match the leading identifier to a preceding alias in the same SELECT list.[2] A real column with the same name therefore wins over an alias. Spark's documentation shows that SELECT c1 AS a, a + c1 FROM VALUES(2, 3) AS T(c1, a) returns 2, 5, because a resolves to the table column (3), not to the alias.[2]

If an identifier matches more than one preceding alias, Spark raises AMBIGUOUS_LATERAL_COLUMN_ALIAS.[2][5] Further parts of a dotted name after the alias are resolved as struct fields or map keys.[2]

The behaviour is controlled by spark.sql.lateralColumnAlias.enableImplicitResolution, an internal configuration added in 3.4.0 whose default is true.[6]

History in Spark

Spark 3.4.0 added lateral column alias references (SPARK-27561).[1] Spark 3.5.0 extended them to aggregate queries (SPARK-41631) and to queries with window functions (SPARK-42217).[7] Databricks documents the same rule for Databricks SQL and Databricks Runtime 12.2 LTS and above.[8]

What are the limits in Spark?

Spark 4.3 rejects some placements of a lateral alias with an UNSUPPORTED_FEATURE error. The error conditions name these cases:[5]

  • inside the arguments of an aggregate function (LATERAL_COLUMN_ALIAS_IN_AGGREGATE_FUNC);
  • inside a window expression (LATERAL_COLUMN_ALIAS_IN_WINDOW);
  • inside a generator expression such as explode (LATERAL_COLUMN_ALIAS_IN_GENERATOR);
  • through GROUP BY alias or GROUP BY ALL (LATERAL_COLUMN_ALIAS_IN_GROUP_BY);
  • in an aggregate query that has both window expressions and a HAVING clause (LATERAL_COLUMN_ALIAS_IN_AGGREGATE_WITH_WINDOW_AND_HAVING).

A lateral alias may refer to the result of an aggregate, as in the second example below; it may not be fed into one.

Example

Spark SQL, using an alias in a later expression of the same SELECT list:

SELECT name,
       salary * 12        AS annual,
       annual * 0.1       AS bonus
FROM employees;

-- An alias of an aggregate result, reused in the same SELECT list
SELECT dept,
       SUM(salary)        AS total,
       total / COUNT(*)   AS avg_salary
FROM employees
GROUP BY dept;

In engines without lateral aliases, the first query has to repeat salary * 12 or compute annual in a subquery.

Which engines allow lateral column aliases?

Engine Alias reusable in the same SELECT list? Notes
Apache Spark (3.4.0 and later) Yes[1] Table columns take precedence over aliases; not allowed inside aggregate, window or generator arguments[2][5]
Databricks SQL and Databricks Runtime Yes, Databricks Runtime 12.2 LTS and above[8] Same rule as Spark: local column references take precedence[8]
DuckDB 2.0 Yes[9] Also allows aliases in WHERE, GROUP BY and HAVING, but not in a join's ON clause[9]
Snowflake Yes[10] Aliases can be used in other column expressions and in WHERE, GROUP BY and joins; the documentation advises against aliases that share a name with a referenced column[10]
BigQuery (GoogleSQL) No[3] SELECT list aliases are visible only to GROUP BY, ORDER BY and HAVING[3]
PostgreSQL 19 No; output names are documented only for ORDER BY and GROUP BY[4] The documentation says an output column name cannot be used in WHERE or HAVING; the expression must be written out[4]

Because precedence rules differ, a query that relies on a lateral alias whose name also matches a table column can return different results across engines. Choosing alias names that do not collide with column names avoids this.[2][10]

See also

References

  1. ↑ 1.0 1.1 1.2 Spark Release 3.4.0. Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 2.3 2.4 2.5 2.6 Name resolution (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 Query syntax (GoogleSQL for BigQuery). Google Cloud.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 4.2 SELECT (PostgreSQL 19). PostgreSQL Global Development Group.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 5.2 error-conditions.json (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ Spark Release 3.5.0. Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ 8.0 8.1 8.2 Name resolution. Databricks.Retrieved 2026-11-23.
  9. ↑ 9.0 9.1 Friendly SQL (DuckDB 2.0). DuckDB Foundation.Retrieved 2026-11-23.
  10. ↑ 10.0 10.1 10.2 SELECT. Snowflake.Retrieved 2026-11-23.