assumeNotNull
引入版本:v1.1.0
返回 Nullable 类型的值对应的非 Nullable 值。
如果原始值为 NULL,则可能返回任意结果。
语法
assumeNotNull(x)参数
x— 任意 Nullable 类型的原始值。Nullable(T)
返回值
如果原始值不是 NULL,则返回其非 Nullable 值;如果输入值为 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 t_null;
SELECT toTypeName(assumeNotNull(y)) FROM t_null;┌─assumeNotNull(y)─┐
│ 0 │
│ 3 │
└──────────────────┘
┌─toTypeName(assumeNotNull(y))─┐
│ Int8 │
│ Int8 │
└──────────────────────────────┘coalesce
首次引入版本:v1.1.0
返回第一个非 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.0
返回一组参数中第一个非默认值
语法
firstNonDefault(arg1[, arg2[ ...]])参数
arg1— 要检查的第一个参数 -arg2— 要检查的第二个参数 -...— 要检查的其他参数
返回值
结果类型为所有参数的超类型
示例
整数
SELECT firstNonDefault(0, 1, 2)1String
SELECT firstNonDefault('', 'hello', 'world')helloNULL 值
SELECT firstNonDefault(NULL, 0 :: UInt8, 1 :: UInt8)1Nullable 的零值
SELECT firstNonDefault(NULL, 0 :: Nullable(UInt8), 1 :: Nullable(UInt8))0ifNull
引入版本:v1.1.0
如果第一个参数为 NULL,则返回替代值。
语法
ifNull(x, alt)参数
返回值
如果 x 不为 NULL,则返回 x;否则返回 alt。Any
示例
用法示例
SELECT ifNull('a', 'b'), ifNull(NULL, 'b');┌─ifNull('a', 'b')─┬─ifNull(NULL, 'b')─┐
│ a │ b │
└──────────────────┴───────────────────┘isNotNull
引入版本:v1.1.0
检查参数是否不为 NULL。
另请参见:运算符 IS NOT NULL。
语法
isNotNull(x)参数
x— 非复合数据类型的值。Any
返回值
如果 x 不是 NULL,则返回 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
引入版本:v1.1.0
检查参数是否为 NULL。
另请参见:运算符 IS NULL。
语法
isNull(x)参数
x— 非复合数据类型的一个值。Any
返回值
如果 x 为 NULL,则返回 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
引入版本:v22.7.0
检查参数的数据类型是否为 Nullable (即允许 NULL 值) 。
语法
isNullable(x)参数
x— 任意数据类型的值。Any
返回值
如果 x 的数据类型是 Nullable,则返回 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
引入版本:v20.3.0
检查参数是否为零 (0) 或 NULL。
语法
isZeroOrNull(x)参数
x— 数值。UInt
返回值
如果 x 为 NULL 或等于零,则返回 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
引入版本:v1.1.0
当两个参数相等时,返回 NULL。
语法
nullIf(x, y)参数
返回值
如果两个参数相等,则返回 NULL;否则返回第一个参数。NULL 或 Nullable(x)
示例
使用示例
SELECT nullIf(1, 1), nullIf(1, 2);┌─nullIf(1, 1)─┬─nullIf(1, 2)─┐
│ ᴺᵁᴸᴸ │ 1 │
└──────────────┴──────────────┘toNullable
引入版本:v1.1.0
将提供的参数类型转换为 Nullable。
语法
toNullable(x)参数
x— 任意非复合类型的值。Any
返回值
返回输入值,但其类型为 Nullable。Nullable(Any)
示例
使用示例
SELECT toTypeName(10), toTypeName(toNullable(10));┌─toTypeName(10)─┬─toTypeName(toNullable(10))─┐
│ UInt8 │ Nullable(UInt8) │
└────────────────┴────────────────────────────┘