メインコンテンツまでスキップ
メインコンテンツまでスキップ

Nullable 値を扱うための関数

assumeNotNull

Introduced in: v1.1

Nullable 型の値に対する対応する非 Nullable 値を返します。 元の値が NULL の場合、任意の結果が返される可能性があります。

See also: functions ifNull and coalesce.

構文

assumeNotNull(x)

引数

  • x — 任意の nullable 型の元の値。 Nullable(T)

返される値

元の値が NULL でない場合は非 NULL 値を返します。そうでない場合、入力値が NULL の場合は任意の値が返されます。 Any

使用例

CREATE TABLE t_null (x Int8, y Nullable(Int8))
ENGINE=MergeTree()
ORDER BY x;

INSERT INTO t_null VALUES (1, NULL), (2, 3);

SELECT assumeNotNull(y) FROM table;
SELECT toTypeName(assumeNotNull(y)) FROM t_null;
┌─assumeNotNull(y)─┐
│                0 │
│                3 │
└──────────────────┘
┌─toTypeName(assumeNotNull(y))─┐
│ Int8                         │
│ Int8                         │
└──────────────────────────────┘

coalesce

Introduced in: v1.1

最も左の非 NULL 引数を返します。

構文

coalesce(x[, y, ...])

引数

  • x[, y, ...] — 非複合型の任意の数のパラメータ。すべてのパラメータは互換性のあるデータ型でなければなりません。 Any

返される値

最初の非 NULL 引数を返します。すべての引数が NULL の場合は NULL を返します。 Any または NULL

使用例

-- Consider a list of contacts that may specify multiple ways to contact a customer.

CREATE TABLE aBook
(
    name String,
    mail Nullable(String),
    phone Nullable(String),
    telegram Nullable(UInt32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO aBook VALUES ('client 1', NULL, '123-45-67', 123), ('client 2', NULL, NULL, NULL);

-- The mail and phone fields are of type String, but the telegram field is UInt32 so it needs to be converted to String.

-- Get the first available contact method for the customer from the contact list

SELECT name, coalesce(mail, phone, CAST(telegram,'Nullable(String)')) FROM aBook;
┌─name─────┬─coalesce(mail, phone, CAST(telegram, 'Nullable(String)'))─┐
│ client 1 │ 123-45-67                                                 │
│ client 2 │ ᴺᵁᴸᴸ                                                      │
└──────────┴───────────────────────────────────────────────────────────┘

firstNonDefault

Introduced in: v25.9

引数のセットから最初の非デフォルト値を返します。

構文

引数

  • arg1 — チェックする最初の引数 - arg2 — チェックする第二の引数 - ... — チェックする追加の引数

返される値

結果の型はすべての引数のスーパータイプです。

整数

SELECT firstNonDefault(0, 1, 2)
1

文字列

SELECT firstNonDefault('', 'hello', 'world')
'hello'

NULL

SELECT firstNonDefault(NULL, 0 :: UInt8, 1 :: UInt8)
1

nullable ゼロ

SELECT firstNonDefault(NULL, 0 :: Nullable(UInt8), 1 :: Nullable(UInt8))
0

ifNull

Introduced in: v1.1

最初の引数が NULL の場合に代替値を返します。

構文

ifNull(x, alt)

引数

  • xNULL をチェックする値。 Any
  • altxNULL の場合に関数が返す値。 Any

返される値

xNULL でない場合は x の値を返し、そうでない場合は alt を返します。 Any

使用例

SELECT ifNull('a', 'b'), ifNull(NULL, 'b');
┌─ifNull('a', 'b')─┬─ifNull(NULL, 'b')─┐
│ a                │ b                 │
└──────────────────┴───────────────────┘

isNotDistinctFrom

Introduced in: v23.8

2つの JOIN キー間でヌル安全な比較を行います。この関数は 2つの NULL 値を同一とみなし、true を返します。これは通常の等価性の動作とは異なり、2つの NULL 値を比較すると NULL が返されます。

参考

この関数は JOIN ON の実装で使用される内部関数です。 クエリで手動で使用しないでください。

完全な例については、JOIN キーにおける NULLを参照してください。

構文

isNotDistinctFrom(x, y)

引数

  • x — 比較する最初の JOIN キー。 Any
  • y — 比較する第二の JOIN キー。 Any

返される値

xy の両方が NULL の場合は true を返し、そうでない場合は false を返します。 Bool

isNotNull

Introduced in: v1.1

引数が NULL でないかどうかを確認します。

Also see: operator IS NOT NULL.

構文

isNotNull(x)

引数

  • x — 非複合データ型の値。 Any

返される値

xNULL でない場合は 1 を返し、そうでない場合は 0 を返します。 UInt8

使用例

CREATE TABLE t_null
(
  x Int32,
  y Nullable(Int32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO t_null VALUES (1, NULL), (2, 3);

SELECT x FROM t_null WHERE isNotNull(y);
┌─x─┐
│ 2 │
└───┘

isNull

Introduced in: v1.1

引数が NULL であるかどうかを確認します。

Also see: operator IS NULL.

構文

isNull(x)

引数

  • x — 非複合データ型の値。 Any

返される値

xNULL の場合は 1 を返し、そうでない場合は 0 を返します。 UInt8

使用例

CREATE TABLE t_null
(
  x Int32,
  y Nullable(Int32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO t_null VALUES (1, NULL), (2, 3);

SELECT x FROM t_null WHERE isNull(y);
┌─x─┐
│ 1 │
└───┘

isNullable

Introduced in: v22.7

引数のデータ型が Nullable であるか(すなわち NULL 値を許可するか)を確認します。

構文

isNullable(x)

引数

  • x — 任意のデータ型の値。 Any

返される値

xNullable データ型である場合は 1 を返し、そうでない場合は 0 を返します。 UInt8

使用例

CREATE TABLE tab (
    ordinary_col UInt32,
    nullable_col Nullable(UInt32)
)
ENGINE = MergeTree
ORDER BY tuple();
INSERT INTO tab (ordinary_col, nullable_col) VALUES (1,1), (2, 2), (3,3);
SELECT isNullable(ordinary_col), isNullable(nullable_col) FROM tab;
┌───isNullable(ordinary_col)──┬───isNullable(nullable_col)──┐
│                           0 │                           1 │
│                           0 │                           1 │
│                           0 │                           1 │
└─────────────────────────────┴─────────────────────────────┘

isZeroOrNull

Introduced in: v20.3

引数がゼロ (0) または NULL のいずれかであるかを確認します。

構文

isZeroOrNull(x)

引数

  • x — 数値。 UInt

返される値

xNULL またはゼロに等しい場合は 1 を返し、そうでない場合は 0 を返します。 UInt8/16/32/64 または Float32/Float64

使用例

CREATE TABLE t_null
(
  x Int32,
  y Nullable(Int32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO t_null VALUES (1, NULL), (2, 0), (3, 3);

SELECT x FROM t_null WHERE isZeroOrNull(y);
┌─x─┐
│ 1 │
│ 2 │
└───┘

nullIf

Introduced in: v1.1

両方の引数が等しい場合に NULL を返します。

構文

nullIf(x, y)

引数

  • x — 最初の値。 Any
  • y — 第二の値。 Any

返される値

両方の引数が等しい場合は NULL を返し、そうでない場合は最初の引数を返します。 NULL または Nullable(x)

使用例

SELECT nullIf(1, 1), nullIf(1, 2);
┌─nullIf(1, 1)─┬─nullIf(1, 2)─┐
│         ᴺᵁᴸᴸ │            1 │
└──────────────┴──────────────┘

toNullable

Introduced in: v1.1

提供された引数型を Nullable に変換します。

構文

toNullable(x)

引数

  • x — いかなる非複合型の値。 Any

返される値

入力値を Nullable 型で返します。 Nullable(Any)

使用例

SELECT toTypeName(10), toTypeName(toNullable(10));
┌─toTypeName(10)─┬─toTypeName(toNullable(10))─┐
│ UInt8          │ Nullable(UInt8)            │
└────────────────┴────────────────────────────┘