ASSERT_TRUE

assert_true(expr) assert_true(expr, message)

功能描述

ASSERT_TRUE
ASSERT_TRUE
函数用于验证布尔表达式是否为真。如果表达式为
true
true
,则返回
NULL
NULL
;如果表达式为
false
false
NULL
NULL
,则抛出异常。可选的
message
message
参数允许自定义错误信息。

此函数常用于数据质量检查、单元测试和运行时断言验证。

参数说明

  • expr
    expr
    :布尔类型的表达式,必填。待验证的条件。
  • message
    message
    :字符串类型,可选。当断言失败时显示的自定义错误消息。如果未指定或为
    NULL
    NULL
    ,则使用默认错误消息
    "ASSERT_TRUE expr is not true!"
    "ASSERT_TRUE expr is not true!"

返回结果

  • expr
    expr
    true
    true
    时:返回
    NULL
    NULL
  • expr
    expr
    false
    false
    NULL
    NULL
    时:抛出异常并显示错误消息。

使用示例

  1. 基本用法 - 断言成功,返回 NULL:

    SELECT assert_true(1 = 1); +---------------------+ | assert_true(1 = 1) | +---------------------+ | NULL | +---------------------+

  2. 使用自定义错误消息 - 断言成功:

    SELECT assert_true(10 > 5, 'Ten is greater than five'); +--------------------------------------------------+ | assert_true(10 > 5, 'Ten is greater than five') | +--------------------------------------------------+ | NULL | +--------------------------------------------------+

  3. 断言失败 - 抛出默认错误消息:

    SELECT assert_true(1 > 2); -- Error: ASSERT_TRUE expr is not true!

  4. 断言失败 - 抛出自定义错误消息:

    SELECT assert_true(1 > 2, 'One should be greater than two'); -- Error: One should be greater than two

  5. 输入为 NULL - 视为断言失败:

    SELECT assert_true(NULL); -- Error: ASSERT_TRUE expr is not true!

注意事项

  • expr
    expr
    NULL
    NULL
    时,视为断言失败,会抛出异常(而非返回 NULL)。
  • message
    message
    NULL
    NULL
    时,使用默认错误消息
    "ASSERT_TRUE expr is not true!"
    "ASSERT_TRUE expr is not true!"
  • 该函数兼容 Spark 的
    assert_true
    assert_true
    语义。
  • 该函数不会被常量折叠优化,确保在运行时始终执行断言检查。
联系我们
预约咨询
微信咨询
电话咨询
邮件咨询