Uploaded image for project: 'Hive'
  1. Hive
  2. HIVE-23060

Query failing with error "Grouping sets expression is not in GROUP BY key. Error encountered near token"

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-alpha-1
    • HiveServer2
    • None

    Description

      Synopsis:
      =========
      Query failing with error "Grouping sets expression is not in GROUP BY key. Error encountered near token"

      Problem:
      ========
      A Hive query in a view which fails with the following error:

      Error while compiling statement: FAILED: SemanticException 35:21 [Error 10213]: Grouping sets expression is not in GROUP BY key. Error encountered near token 'l0_equities_region_id'

      Reproduction case:

      create database test;				
      
      create table test.case665558 (c1 string, c2 string);
      
      -- Working query				
      select
         case
            when GROUPING__ID = 255 then `c1`
         end as `col_1`,
         case
            when GROUPING__ID = 255 then 3
         end as `col_2`,
         `c1`,
         `c2`
      from
         `test`.`case665558`
      group by
         `c1`,
         `c2`
      GROUPING SETS 
         (
            (`c1`),
            (`c1`, `c2`)
         );
         
      create view   test.viewcase665558 
      as
      select
         case
            when GROUPING__ID = 255 then `c1`
         end as `col_1`,
         case
            when GROUPING__ID = 255 then 3
         end as `col_2`,
         `c1`,
         `c2`
      from
         `test`.`case665558`
      group by
         `c1`,
         `c2`
      GROUPING SETS 
         (
            (`c1`),
            (`c1`, `c2`)
         );   
         
      Select * from test.viewcase665558 ;
      
      Error: Error while compiling statement: FAILED: SemanticException 17:1 [Error 10213]: Grouping sets expression is not in GROUP BY key. Error encountered near token 'c1' (state=42000,code=40000)
      

      The issue is because when the view is created, it adds the name of the table to the columns. This seems to be confusing Hive:

      +-------------------------------------------------+--+
      |                 createtab_stmt                  |
      +-------------------------------------------------+--+
      | CREATE VIEW `test.viewcase665558` AS select     |
      | case                                            |
      | when GROUPING__ID = 255 then `case665558`.`c1`  |
      | end as `col_1`,                                 |
      | case                                            |
      | when GROUPING__ID = 255 then 3                  |
      | end as `col_2`,                                 |
      | `case665558`.`c1`,                              |
      | `case665558`.`c2`                               |
      | from                                            |
      | `test`.`case665558`                             |
      | group by                                        |
      | `case665558`.`c1`,                              |
      | `case665558`.`c2`                               |
      | GROUPING SETS                                   |
      | (                                               |
      | (c1),                                           |
      | (c1, c2)                                        |
      | )                                               |
      +-------------------------------------------------+--+
      

      Attachments

        1. HIVE-23060.03.patch
          7 kB
          mahesh kumar behera
        2. HIVE-23060.02.patch
          13 kB
          mahesh kumar behera
        3. HIVE-23060.01.patch
          13 kB
          mahesh kumar behera

        Activity

          People

            maheshk114 mahesh kumar behera
            lmartinez@cloudera.com Luis E Martinez-Poblete
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: