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

          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.
          shulga Dmitry Shulga added a comment -

          OK to push

          shulga Dmitry Shulga added a comment - OK to push

          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 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.

          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.

          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.