Netezza 不对 case 语句进行惰性求值吗?

发布于 2024-10-27 16:17:39 字数 222 浏览 7 评论 0原文

我正在执行一个可能包含除以 0 的计算,在这种情况下我希望结果是任意值 (55)。令我惊讶的是,用 case 语句包装计算并没有完成这项工作!

select case when 1=0 then 3/0 else 55 end

错误 HY000:除以 0

这是为什么?还有其他解决方法吗?

I'm performing a computation which might contain division by 0, in which case I want the result to be an arbitrary value (55). To my surprise, wrapping the computation with a case statement did not do the job!

select case when 1=0 then 3/0 else 55 end

ERROR HY000: Divide by 0

Why is that? Is there another workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-11-03 16:17:39

好吧,我不准确。这是因“除以 0”而失败的确切查询:

select case when min(baba) = 0 then 55 else sum(1/baba) end from t group by baba

这看起来像是一个懒惰的 查询Netezza 评估失败,请注意,我按 baba 分组,因此每当 baba 为 0 时,也意味着 min(baba) 为 0,并且评估应该具有被优雅地停止,没有到达 1/baba 项并且除以 0 失败。对吗?好吧,不。

我猜想这里的问题和失败的原因是 Netezza 在评估聚合项之前先评估行项。因此,它必须在每一行计算 1/babababa,然后才能计算聚合项 min(baba)sum(1/baba)

所以,解决方法(对我来说)是:选择 min(baba) = 0 时的情况,然后 55 else 1/min(baba) end from t group by baba,其含义相同。

ok, I was being inaccurate. This is the exact query that fails with "divide by 0":

select case when min(baba) = 0 then 55 else sum(1/baba) end from t group by baba

This looks like a lazy evaluation failure out of Netezza, as notice that I group by baba, so whenever baba is 0, it also means that min(baba) is 0, and the evaluation should have been gracefully stopped without ever getting to the 1/baba term and failing on division by 0. Right? well, no.

What I guess is the gotcha here and the reason for the failure is that Netezza evaluates the rows terms before it can evaluate the aggregate terms. So it must evaluate 1/baba and baba at every row, and only then can it evaluate the aggregate terms min(baba) and sum(1/baba)

so, the workaround (for me) was: select case when min(baba) = 0 then 55 else 1/min(baba) end from t group by baba, which has the same meaning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文