thisisnic opened a new issue, #34501:
URL: https://github.com/apache/arrow/issues/34501

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   When I specify a plan containing more than 1 aggregate, the results I get 
back are incorrect - the values representing the grouping variable are returned 
in the wrong column, and the values for the first aggregate is returned in the 
other columns.
   
   I *think* the plan is correct as I'm getting the right results back from 
DuckDB - the only difference is that I've had to supply the `phase` and 
`invocation` for compatibility with Acero, but unsure if this is relevant?
   
   ``` r
   library(substrait, warn.conflicts = FALSE)
   library(dplyr, warn.conflicts = FALSE)
   
   data <- tibble::tibble(
     x = 1:10,
     y = rep(c("A", "B"), each = 5)
   )
   
   data
   #> # A tibble: 10 × 2
   #>        x y    
   #>    <int> <chr>
   #>  1     1 A    
   #>  2     2 A    
   #>  3     3 A    
   #>  4     4 A    
   #>  5     5 A    
   #>  6     6 B    
   #>  7     7 B    
   #>  8     8 B    
   #>  9     9 B    
   #> 10    10 B
   
   # In R directly
   data %>% 
     group_by(y) %>%
     summarise(min_x = min(x, na.rm =TRUE), max_x = max(x, na.rm =TRUE)) %>%
     collect()
   #> # A tibble: 2 × 3
   #>   y     min_x max_x
   #>   <chr> <int> <int>
   #> 1 A         1     5
   #> 2 B         6    10
   
   # DuckDB via Substrait
   ddb <- data %>% 
     duckdb_substrait_compiler() %>%
     group_by(y) %>%
     summarise(min_x = min(x, na.rm =TRUE), max_x = max(x, na.rm =TRUE))
   collect(ddb)
   #> # A tibble: 2 × 3
   #>   y     min_x max_x
   #>   <chr> <int> <int>
   #> 1 A         1     5
   #> 2 B         6    10
   
   # Arrow via Substrait
   arr <- data %>% 
     arrow_substrait_compiler() %>%
     group_by(y) %>%
     summarise(min_x = min(x, na.rm =TRUE), max_x = max(x, na.rm =TRUE))
   
   collect(arr)
   #> # A tibble: 2 × 3
   #>       y min_x max_x
   #>   <int> <int> <chr>
   #> 1     1     1 A    
   #> 2     6     6 B
   
   arr$plan() |> 
     as.raw() |> 
     arrow::buffer() |> 
     arrow:::substrait__internal__SubstraitToJSON() |> 
     jsonlite::prettify(indent = 2)
   #> {
   #>   "extensionUris": [
   #>     {
   #>       "extensionUriAnchor": 1,
   #>       "uri": 
"https://github.com/substrait-io/substrait/blob/main/extensions/functions_arithmetic.yaml";
   #>     },
   #>     {
   #>       "extensionUriAnchor": 2,
   #>       "uri": 
"https://github.com/substrait-io/substrait/blob/main/extensions/functions_comparison.yaml";
   #>     }
   #>   ],
   #>   "extensions": [
   #>     {
   #>       "extensionFunction": {
   #>         "extensionUriReference": 1,
   #>         "functionAnchor": 2,
   #>         "name": "min"
   #>       }
   #>     },
   #>     {
   #>       "extensionFunction": {
   #>         "extensionUriReference": 1,
   #>         "functionAnchor": 3,
   #>         "name": "max"
   #>       }
   #>     }
   #>   ],
   #>   "relations": [
   #>     {
   #>       "root": {
   #>         "input": {
   #>           "aggregate": {
   #>             "input": {
   #>               "project": {
   #>                 "common": {
   #>                   "emit": {
   #>                     "outputMapping": [
   #>                       2,
   #>                       3
   #>                     ]
   #>                   }
   #>                 },
   #>                 "input": {
   #>                   "read": {
   #>                     "baseSchema": {
   #>                       "names": [
   #>                         "x",
   #>                         "y"
   #>                       ],
   #>                       "struct": {
   #>                         "types": [
   #>                           {
   #>                             "i32": {
   #>                               "nullability": "NULLABILITY_NULLABLE"
   #>                             }
   #>                           },
   #>                           {
   #>                             "string": {
   #>                               "nullability": "NULLABILITY_NULLABLE"
   #>                             }
   #>                           }
   #>                         ]
   #>                       }
   #>                     },
   #>                     "namedTable": {
   #>                       "names": [
   #>                         "named_table_1"
   #>                       ]
   #>                     }
   #>                   }
   #>                 },
   #>                 "expressions": [
   #>                   {
   #>                     "selection": {
   #>                       "directReference": {
   #>                         "structField": {
   #> 
   #>                         }
   #>                       },
   #>                       "rootReference": {
   #> 
   #>                       }
   #>                     }
   #>                   },
   #>                   {
   #>                     "selection": {
   #>                       "directReference": {
   #>                         "structField": {
   #>                           "field": 1
   #>                         }
   #>                       },
   #>                       "rootReference": {
   #> 
   #>                       }
   #>                     }
   #>                   }
   #>                 ]
   #>               }
   #>             },
   #>             "groupings": [
   #>               {
   #>                 "groupingExpressions": [
   #>                   {
   #>                     "selection": {
   #>                       "directReference": {
   #>                         "structField": {
   #>                           "field": 1
   #>                         }
   #>                       },
   #>                       "rootReference": {
   #> 
   #>                       }
   #>                     }
   #>                   }
   #>                 ]
   #>               }
   #>             ],
   #>             "measures": [
   #>               {
   #>                 "measure": {
   #>                   "functionReference": 2,
   #>                   "phase": "AGGREGATION_PHASE_INITIAL_TO_RESULT",
   #>                   "outputType": {
   #>                     "fp64": {
   #>                       "nullability": "NULLABILITY_NULLABLE"
   #>                     }
   #>                   },
   #>                   "invocation": "AGGREGATION_INVOCATION_ALL",
   #>                   "arguments": [
   #>                     {
   #>                       "value": {
   #>                         "selection": {
   #>                           "directReference": {
   #>                             "structField": {
   #> 
   #>                             }
   #>                           },
   #>                           "rootReference": {
   #> 
   #>                           }
   #>                         }
   #>                       }
   #>                     }
   #>                   ]
   #>                 }
   #>               },
   #>               {
   #>                 "measure": {
   #>                   "functionReference": 3,
   #>                   "phase": "AGGREGATION_PHASE_INITIAL_TO_RESULT",
   #>                   "outputType": {
   #>                     "i64": {
   #>                       "nullability": "NULLABILITY_NULLABLE"
   #>                     }
   #>                   },
   #>                   "invocation": "AGGREGATION_INVOCATION_ALL",
   #>                   "arguments": [
   #>                     {
   #>                       "value": {
   #>                         "selection": {
   #>                           "directReference": {
   #>                             "structField": {
   #> 
   #>                             }
   #>                           },
   #>                           "rootReference": {
   #> 
   #>                           }
   #>                         }
   #>                       }
   #>                     }
   #>                   ]
   #>                 }
   #>               }
   #>             ]
   #>           }
   #>         },
   #>         "names": [
   #>           "y",
   #>           "min_x",
   #>           "max_x"
   #>         ]
   #>       }
   #>     }
   #>   ]
   #> }
   #> 
   ```
   
   ### Component(s)
   
   C++


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to