【问题标题】:pig latin relaxed equals == with null?猪拉丁放松等于== null?
【发布时间】:2012-06-14 00:49:35
【问题描述】:

在只有 1 行的关系 X 中

X.A=null
X.B= "blahblah"

现在我想做:

Y = FILTER X BY A != B ;

我的意思是,既然 A 为空而 B 不是,那么条件应该为真。 但实际结果是 Y 为空,条件为 false。

这与 SQL 相同,其中任何涉及 null 的条件都是错误的。而 SQL 可以翻译 NVL()函数为null,PIG似乎没有,有没有干净的方法来做上面的比较?

【问题讨论】:

    标签: null apache-pig latin nvl


    【解决方案1】:

    这有点小技巧,但您可以进行比较并将结果存储在您的关系中,然后使用结果和原始比较进行过滤。

    Y = foreach X generate *,
            (((A is null and B is not null) or (A is not null and B is null) ? 'y' : 'n') as oneNull;
    Y = filter Y by (oneNull == 'y' or A != B);
    Y = foreach Y generate A, B;
    

    使用这个,如果 X 是:

    (,blahblah)
    (blah,blah)
    (,)
    (blah,)
    (abc,def)
    

    那么 Y 将是:

    (,blahblah)
    (blah,)
    (abc,def)
    

    【讨论】:

      【解决方案2】:

      我安装了 Pig 版本 0.12.1;它实际上对我来说可以直接写条件:

      Y = filter X by (A is null and B is not null) or (A is not null and B is null) or A != B;
      

      请注意,对于 null/null 情况,表达式 A != B 的计算结果为 null,因此过滤器会排除元组,根据

      http://pig.apache.org/docs/r0.12.1/basic.html#nulls.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多