如何修复 spark sql 中 window 函数的错误输入 'partition' 不匹配?

How to fix the error mismatched input 'partition' for window functions in spark sql?

我想要 运行 Spark SQL 中的一个 window 函数。我将 Zeppelin 置于带有 Hadoop 的 Spark 集群之上。

我想将行号添加到 table 并按两个 ID 的组合对其进行分组。

这是我的数据。

food      aisle      item    date_added
pear      1          1234    '2020-01-01 10:12'
banana    2.         1233    '2020-01-02 10:12'
banana    2          1211    '2020-01-03 10:12'
banana    2          1412    '2020-01-04 10:12'
apple     1          1452    '2020-01-05 10:12'
apple     1          1334    '2020-01-06 10:12'

我想把数据变成这样

food      aisle      item    date_added             rn
pear      1          1234    '2020-01-01 10:12'     1
banana    2          1233    '2020-01-02 10:12'     3
banana    2          1211    '2020-01-03 10:12'     2
banana    2          1412    '2020-01-04 10:12'     1
apple     1          1452    '2020-01-05 10:12'     2
apple     1          1334    '2020-01-06 10:12'     1

这是我的查询

%sql

select
    food,
    aisle,
    item,
    row_number() over (order by date_added desc 
                            partition by food, aisle 
                            rows between unbounded preceeding and current row) as rn
from fruits

这是错误

mismatched input 'partition' expecting {')', ',', 'RANGE', 'ROWS'}(line 5, pos 28)

如何使用 Spark SQL 解决此错误?

正确的语法是:

row_number() over (partition by food, aisle order by date_added desc) as rn 

排名函数(row_number()rank()dense_rank())不需要 window 框架规范。