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

Unreasonable SQL Error (1356) on select from view

Details

    Description

      DB version: 10.3.4-MariaDB
      Error
      SQL Error (1356): View 'test.v_ar' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

      on select from view:

      select * from v_ar where point_id = 1 and ml_id = 8871 and da = date'2018-01-01'

      I haven't found any rights issues. Then I started to simplify view query and locate the object that causes the error.
      At the end I found that error disappears after changing "ifnull" to "coalesce" in

      ,ifnull(
      Max(
      CASE WHEN r.act_id IS NULL AND r.orig=1 AND r.dstatus IS NULL AND NOT pl_t IS null THEN 1 END
      ) over (PARTITION BY r.point_id,r.ml_id,r.da,r.orig,/*r.pl_t*/CASE WHEN r.per_id IS NULL THEN /*r.pl_t*/CASE WHEN r.act_id IS NULL OR (r.act_enabled = 1 and not r.act_type_id=-1) THEN r.pl_t end ELSE r.bt END)
      ,0) has_pl_custom
      
      

      into

      ,coalesce(
      Max(
      CASE WHEN r.act_id IS NULL AND r.orig=1 AND r.dstatus IS NULL AND NOT pl_t IS null THEN 1 END
      ) over (PARTITION BY r.point_id,r.ml_id,r.da,r.orig,/*r.pl_t*/CASE WHEN r.per_id IS NULL THEN /*r.pl_t*/CASE WHEN r.act_id IS NULL OR (r.act_enabled = 1 and not r.act_type_id=-1) THEN r.pl_t end ELSE r.bt END)
      ,0) has_pl_custom
      
      

      It's funny for me, because coalesce and ifnull are almost the same function. And there's no connection with lack of rights.

      Attachments

        Activity

          alice Alice Sherepa added a comment -

          Thanks for the report!
          Reproducible with 10.2, 10.3, (using window functions)

          create table t1 (id int, n1 int, d1 date);
          insert into t1 values (1,1,'2010-01-01'), (2,1,'2010-01-05'), (3,2,'2010-01-17'), (4,4,'2010-01-01');
           
          create view v1 as SELECT coalesce(max(n1) over (partition by n1),'aaa') FROM t1;
          select * from v1;
           
          create or replace view v1 as SELECT ifnull(max(n1) over (partition by n1),'aaa') FROM t1;
          select * from v1;
          

          query 'select * from v1' failed: 1356: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
          

          alice Alice Sherepa added a comment - Thanks for the report! Reproducible with 10.2, 10.3, (using window functions) create table t1 (id int , n1 int , d1 date ); insert into t1 values (1,1, '2010-01-01' ), (2,1, '2010-01-05' ), (3,2, '2010-01-17' ), (4,4, '2010-01-01' );   create view v1 as SELECT coalesce ( max (n1) over (partition by n1), 'aaa' ) FROM t1; select * from v1;   create or replace view v1 as SELECT ifnull( max (n1) over (partition by n1), 'aaa' ) FROM t1; select * from v1; query 'select * from v1' failed: 1356: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
          varun Varun Gupta (Inactive) added a comment - - edited

          The view's frm

           
          query=select ifnull(`max(n1) over (partition by n1)`,\'aaa\') AS `ifnull(max(n1) over (partition by n1),\'aaa\')` from `test`.`t1`
           
          view_body_utf8=select ifnull(`max(n1) over (partition by n1)`,\'aaa\') AS `ifnull(max(n1) over (partition by n1),\'aaa\')` from `test`.`t1`
          

          So the issue here is the max(n1) over (partition by n1) is treated as an identifier. This is not
          correct , it should be parsed as a window function.

          varun Varun Gupta (Inactive) added a comment - - edited The view's frm   query=select ifnull(`max(n1) over (partition by n1)`,\'aaa\') AS `ifnull(max(n1) over (partition by n1),\'aaa\')` from `test`.`t1`   view_body_utf8=select ifnull(`max(n1) over (partition by n1)`,\'aaa\') AS `ifnull(max(n1) over (partition by n1),\'aaa\')` from `test`.`t1` So the issue here is the max(n1) over (partition by n1) is treated as an identifier. This is not correct , it should be parsed as a window function.
          varun Varun Gupta (Inactive) added a comment - - edited Patch http://lists.askmonty.org/pipermail/commits/2018-December/013206.html

          OK to push!

          sanja Oleksandr Byelkin added a comment - OK to push!

          People

            varun Varun Gupta (Inactive)
            balta Tadas BalaiĊĦis
            Votes:
            0 Vote for this issue
            Watchers:
            6 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.