Skip to content

Feature Not Supported: LATERAL derived tables (LATERAL JOINs) not supported #23099

Closed as duplicate

Description

@dengn

Description

MatrixOne does not support LATERAL derived tables (also known as LATERAL JOINs). When using CROSS JOIN LATERAL or LEFT JOIN LATERAL syntax, MatrixOne returns a SQL syntax error.

Error Message

ERROR 1064 (HY000) at line 11: SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 4 column 11 near "LATERAL"

Affected Cases

This issue affects 1 test case from our partition complex testing on the industry_radar_test database.

Related Table DDL

Table: test_partition_range_lateral

CREATE TABLE test_partition_range_lateral (
    id INT,
    company_id INT,
    amount DECIMAL(10,2),
    created_date DATE
) PARTITION BY RANGE (YEAR(created_date)) (
    PARTITION p2022 VALUES LESS THAN (2023),
    PARTITION p2023 VALUES LESS THAN (2024),
    PARTITION pmax VALUES LESS THAN MAXVALUE
);

Failing SQL Statements

Example 1: LATERAL JOIN with Partitioned Table

SELECT c.full_name, tp.amount
FROM company c
CROSS JOIN LATERAL (
    SELECT amount
    FROM test_partition_range_lateral
    WHERE company_id = c.id
    AND created_date >= '2022-01-01'
    ORDER BY amount DESC
    LIMIT 1
) as tp
WHERE c.province = '广东'
LIMIT 10;

Error: ERROR 1064 (HY000): SQL parser error: syntax error at line 4 column 11 near "LATERAL"

Example 2: LATERAL JOIN with Aggregate

SELECT c.province, stats.max_value
FROM company c
CROSS JOIN LATERAL (
    SELECT MAX(value) as max_value
    FROM test_partition_hash_lateral
    WHERE company_id = c.id
) as stats
WHERE c.province IS NOT NULL
LIMIT 10;

Error: ERROR 1064 (HY000): SQL parser error: syntax error near "LATERAL"

Expected Behavior

In MySQL 8.0+, LATERAL derived tables are fully supported. A LATERAL derived table allows the subquery to reference columns from tables that appear earlier in the FROM clause, enabling correlated subqueries in the FROM clause.

Example MySQL syntax:

SELECT t1.*, t2.*
FROM table1 t1
CROSS JOIN LATERAL (
    SELECT *
    FROM table2
    WHERE table2.id = t1.foreign_id
    ORDER BY table2.created_date DESC
    LIMIT 1
) as t2;

Impact

  • MySQL Compatibility: LATERAL JOINs are a standard MySQL 8.0+ feature
  • Migration: Applications using LATERAL JOINs will fail when migrating to MatrixOne
  • Functionality: Users cannot write queries that need to reference outer table columns in FROM clause subqueries
  • Query Flexibility: Limits the ability to write complex correlated queries in the FROM clause

Use Cases

LATERAL JOINs are commonly used for:

  1. Top-N per Group: Get the top N records for each group (e.g., latest order per customer)
  2. Correlated Subqueries in FROM: Reference outer table columns in FROM clause subqueries
  3. Complex Data Transformations: Perform row-by-row transformations based on related data
  4. Performance Optimization: Sometimes more efficient than traditional JOINs for certain patterns

Test Context

This issue was discovered during partition complex testing using the industry_radar_test database. The test case attempted to use LATERAL JOIN to get the maximum amount for each company from a partitioned table.

Workaround

As a temporary workaround, users can rewrite LATERAL JOINs using traditional JOINs or subqueries:

-- Instead of LATERAL JOIN:
SELECT c.full_name, tp.amount
FROM company c
CROSS JOIN LATERAL (
    SELECT amount
    FROM test_partition_range_lateral
    WHERE company_id = c.id
    ORDER BY amount DESC
    LIMIT 1
) as tp;

-- Use subquery in SELECT:
SELECT c.full_name,
       (SELECT amount
        FROM test_partition_range_lateral
        WHERE company_id = c.id
        ORDER BY amount DESC
        LIMIT 1) as amount
FROM company c;

However, this workaround:

  • May have different performance characteristics
  • May not be equivalent in all cases
  • Requires query rewriting

Suggested Fix

  1. Parser Update: Update the SQL parser to recognize LATERAL keyword in JOIN clauses
  2. Query Planning: Implement query planner support for LATERAL derived tables
  3. Execution Engine: Implement execution engine support for LATERAL JOINs
  4. Correlation Handling: Properly handle column references from outer tables in LATERAL subqueries
  5. Partition Compatibility: Ensure LATERAL JOINs work correctly with partitioned tables
  6. Documentation: Update documentation to reflect support for LATERAL JOINs

Related MySQL Documentation

Activity

  1. added theissue type on May 31, 2026
  2. dengn commented on May 31, 2026

    @dengn
    ContributorAuthor

    Still reproduces on prod cloud 8.0.30-MatrixOne-v3.0.11 and the 4.0 dev line nightly-0bfda604, found during Prisma ORM (v5.22) compatibility testing.

    CREATE TABLE t (id INT, n INT);
    INSERT INTO t VALUES (1, 5);
    SELECT p.id, d.m FROM t p, LATERAL (SELECT p.n + 1 AS m) d;
    -- ERROR 1064 (HY000): SQL parser error: You have an error in your SQL syntax ...
    -- syntax error at line 1 column 41 near "SELECT p.n+1 AS m) d";

    For context: Prisma's relationLoadStrategy: 'join' does not emit LATERAL — it uses nested correlated subqueries instead (that failure is tracked separately in #24737). So this LATERAL gap is not on Prisma's critical path, but it still blocks hand-written LATERAL queries / raw SQL on MatrixOne.

  3. added
    severity/s0
    on Jun 30, 2026
  4. added this to the MO-202607 milestone on Jun 30, 2026
  5. added
    priority/p1
    and removed
    severity/s0
    on Jul 1, 2026
  6. XuPeng-SH commented on Oct 29, 2026

    @XuPeng-SH
    Contributor

    Consolidated into #25376. The reproduction and acceptance requirements remain tracked there; closing this issue does not mean the problem has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/featurepriority/p1

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions