Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-25002

ON expressions cannot contain outer references

Details

    Description

      Outside references in LEFT JOIN's ON expression produce "unknown column" error.

      Create the test dataset (As far as I'm aware, there's nothing special about this choice of tables/columns):

      create table t10 (
        pk int primary key,
        a int
      );
      insert into t10 values (1,1);
       
      create table t11 (
        pk int primary key,
        b int
      );
      create table t12 (a int);
      insert into t12 values (1),(2);
      

      Run the query Q1:

      select
        a,
        (select count(*)
         from 
           t10 left join t11 on t11.pk=t12.a
         where 
           t10.pk=1
         ) as SUBQ
      from t12;
      

      ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause'
      

      Wrapping the outside reference in a subquery helps: Q2:

      select
        a,
        (select count(*)
         from 
           t10 left join t11 on t11.pk=(select t12.a from dual)
         where 
           t10.pk=1
         ) as SUBQ
      from t12;
      

      +------+------+
      | a    | SUBQ |
      +------+------+
      |    1 |    1 |
      |    2 |    1 |
      +------+------+
      

      I've tried Q1 on MySQL 8.0.22 or PostgreSQL and it worked in both.

      Is it a bug MariaDB produces this error?

      Attachments

        Activity

          psergei Sergei Petrunia created issue -
          psergei Sergei Petrunia made changes -
          Field Original Value New Value
          Assignee Oleksandr Byelkin [ sanja ]
          igor Igor Babaev (Inactive) made changes -
          Assignee Oleksandr Byelkin [ sanja ] Igor Babaev [ igor ]
          igor Igor Babaev (Inactive) made changes -
          Fix Version/s 10.2 [ 14601 ]
          igor Igor Babaev (Inactive) made changes -
          Status Open [ 1 ] In Progress [ 3 ]

          A similar query

          select
            a,
            (select count(*)
             from 
               t10 join t11 on t11.pk=t12.a
             where 
               t10.pk=1
             ) as SUBQ
          from t12;
          

          with JOIN instead of LEFT JOIN also returns an error message:

          MariaDB [test]> select
              ->   a,
              ->   (select count(*)
              ->    from 
              ->      t10 join t11 on t11.pk=t12.a
              ->    where 
              ->      t10.pk=1
              ->    ) as SUBQ
              -> from t12;
          ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause'
          

          If we move the ON condition to WHERE

          select
            a,
            (select count(*)
             from 
               t10 join t11 
             where 
              t11.pk=t12.a and t10.pk=1
             ) as SUBQ
          from t12;
          

          the query returns the expected result set:

          MariaDB [test]> select
              ->   a,
              ->   (select count(*)
              ->    from 
              ->      t10 join t11 
              ->    where 
              ->     t11.pk=t12.a and t10.pk=1
              ->    ) as SUBQ
              -> from t12;
          +------+------+
          | a    | SUBQ |
          +------+------+
          |    1 |    0 |
          |    2 |    0 |
          +------+------+
          

          igor Igor Babaev (Inactive) added a comment - A similar query select a, ( select count (*) from t10 join t11 on t11.pk=t12.a where t10.pk=1 ) as SUBQ from t12; with JOIN instead of LEFT JOIN also returns an error message: MariaDB [test]> select -> a, -> (select count(*) -> from -> t10 join t11 on t11.pk=t12.a -> where -> t10.pk=1 -> ) as SUBQ -> from t12; ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause' If we move the ON condition to WHERE select a, ( select count (*) from t10 join t11 where t11.pk=t12.a and t10.pk=1 ) as SUBQ from t12; the query returns the expected result set: MariaDB [test]> select -> a, -> (select count(*) -> from -> t10 join t11 -> where -> t11.pk=t12.a and t10.pk=1 -> ) as SUBQ -> from t12; +------+------+ | a | SUBQ | +------+------+ | 1 | 0 | | 2 | 0 | +------+------+

          Let's add a couple of rows to t11

          insert into t11 values (1,1), (2,3);
          

          and run the query

          select a from t12 where a in (select t11.b from t10 join t11 where t11.pk=t12.a and t10.pk=1);
          

          we get

          MariaDB [test]> select a from t12 where a in (select t11.b from t10 join t11 where t11.pk=t12.a and t10.pk=1);
          +------+
          | a    |
          +------+
          |    1 |
          +------+
          

          However if in the subquery we move t11.pk=t12.a to the ON clause

          select a from t12 where a in (select t11.b from t10 join t11 on t11.pk=t12.a where t10.pk=1);
          

          again we get an error message

          MariaDB [test]> select a from t12 where a in (select t11.b from t10 join t11 on t11.pk=t12.a where t10.pk=1);
          ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause'
          

          igor Igor Babaev (Inactive) added a comment - Let's add a couple of rows to t11 insert into t11 values (1,1), (2,3); and run the query select a from t12 where a in ( select t11.b from t10 join t11 where t11.pk=t12.a and t10.pk=1); we get MariaDB [test]> select a from t12 where a in (select t11.b from t10 join t11 where t11.pk=t12.a and t10.pk=1); +------+ | a | +------+ | 1 | +------+ However if in the subquery we move t11.pk=t12.a to the ON clause select a from t12 where a in ( select t11.b from t10 join t11 on t11.pk=t12.a where t10.pk=1); again we get an error message MariaDB [test]> select a from t12 where a in (select t11.b from t10 join t11 on t11.pk=t12.a where t10.pk=1); ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause'

          Apparently outer references in ON clauses cannot be identified.
          Debugging shows that the problem appears because the outer context for Name resolution context of fields in ON clauses is set to 0.

          igor Igor Babaev (Inactive) added a comment - Apparently outer references in ON clauses cannot be identified. Debugging shows that the problem appears because the outer context for Name resolution context of fields in ON clauses is set to 0.
          igor Igor Babaev (Inactive) made changes -
          Assignee Igor Babaev [ igor ] Dmitry Shulga [ JIRAUSER47315 ]
          Status In Progress [ 3 ] In Review [ 10002 ]
          shulga Dmitry Shulga added a comment -

          OK to push

          shulga Dmitry Shulga added a comment - OK to push
          shulga Dmitry Shulga made changes -
          Status In Review [ 10002 ] Stalled [ 10000 ]

          A fix for this bug was pushed into 10.2

          igor Igor Babaev (Inactive) added a comment - A fix for this bug was pushed into 10.2
          igor Igor Babaev (Inactive) made changes -
          Fix Version/s 10.2.38 [ 25207 ]
          Fix Version/s 10.3.29 [ 25206 ]
          Fix Version/s 10.4.19 [ 25205 ]
          Fix Version/s 10.5.10 [ 25204 ]
          Fix Version/s 10.2 [ 14601 ]
          Resolution Fixed [ 1 ]
          Status Stalled [ 10000 ] Closed [ 6 ]

          igor or shulga, can you please provide a 10.4 version of this?
          My attempt of merging this shows some test failures.

          In my opinion, fixes should be tested at least in the oldest and newest major version branch to find this kind of problems in advance.

          marko Marko Mäkelä added a comment - igor or shulga , can you please provide a 10.4 version of this? My attempt of merging this shows some test failures. In my opinion, fixes should be tested at least in the oldest and newest major version branch to find this kind of problems in advance.
          marko Marko Mäkelä made changes -
          Resolution Fixed [ 1 ]
          Status Closed [ 6 ] Stalled [ 10000 ]
          marko Marko Mäkelä made changes -
          Fix Version/s 10.4 [ 22408 ]
          Fix Version/s 10.5 [ 23123 ]
          Fix Version/s 10.5.10 [ 25204 ]
          Fix Version/s 10.4.19 [ 25205 ]
          Priority Major [ 3 ] Blocker [ 1 ]

          This is now merged up to 10.4 (with adjustment provided by igor) and 10.5.

          marko Marko Mäkelä added a comment - This is now merged up to 10.4 (with adjustment provided by igor ) and 10.5.
          marko Marko Mäkelä made changes -
          issue.field.resolutiondate 2021-03-19 11:15:05.0 2021-03-19 11:15:05.287
          marko Marko Mäkelä made changes -
          Fix Version/s 10.4.19 [ 25205 ]
          Fix Version/s 10.5.10 [ 25204 ]
          Fix Version/s 10.4 [ 22408 ]
          Fix Version/s 10.5 [ 23123 ]
          Resolution Fixed [ 1 ]
          Status Stalled [ 10000 ] Closed [ 6 ]
          igor Igor Babaev (Inactive) made changes -
          Assignee Dmitry Shulga [ JIRAUSER47315 ] Igor Babaev [ igor ]
          serg Sergei Golubchik made changes -
          Workflow MariaDB v3 [ 119573 ] MariaDB v4 [ 158969 ]

          People

            igor Igor Babaev (Inactive)
            psergei Sergei Petrunia
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.