mz_transform/notice/
equals_null.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Hosts [`EqualsNull`].
11
12use std::collections::BTreeSet;
13use std::fmt;
14
15use mz_repr::GlobalId;
16use mz_repr::explain::ExprHumanizer;
17
18use crate::notice::{ActionKind, OptimizerNoticeApi};
19
20/// A notice indicating that the query contains a comparison with NULL using `=` or `<>`.
21///
22/// Comparing a value to `NULL` using `=` or `<>` always results in `NULL`, not a boolean.
23/// This is almost always a mistake. To check if a value is `NULL`, use `IS NULL` instead.
24#[derive(Clone, Debug, Eq, PartialEq, Hash)]
25pub struct EqualsNull;
26
27impl OptimizerNoticeApi for EqualsNull {
28    fn dependencies(&self) -> BTreeSet<GlobalId> {
29        BTreeSet::new()
30    }
31
32    fn fmt_message(
33        &self,
34        f: &mut fmt::Formatter<'_>,
35        _humanizer: &dyn ExprHumanizer,
36        _redacted: bool,
37    ) -> fmt::Result {
38        write!(
39            f,
40            "Comparison with NULL using `=` or `<>` always returns NULL."
41        )
42    }
43
44    fn fmt_hint(
45        &self,
46        f: &mut fmt::Formatter<'_>,
47        _humanizer: &dyn ExprHumanizer,
48        _redacted: bool,
49    ) -> fmt::Result {
50        write!(f, "Use `IS NULL` or `IS NOT NULL` instead.")
51    }
52
53    fn fmt_action(
54        &self,
55        _f: &mut fmt::Formatter<'_>,
56        _humanizer: &dyn ExprHumanizer,
57        _redacted: bool,
58    ) -> fmt::Result {
59        Ok(())
60    }
61
62    fn action_kind(&self, _humanizer: &dyn ExprHumanizer) -> ActionKind {
63        ActionKind::None
64    }
65}