共用方式為


assert()

檢查條件。 如果條件為 false,則會輸出錯誤訊息,並導致查詢失敗。

注意

assert 式會在查詢分析階段進行評估,然後才會套用常數折疊和述詞等優化。

注意

提供給 assert 的參數必須在查詢分析階段評估為常數。 換句話說,它只能從參考常數的其他表達式建構,而且無法系結至數據列內容。

語法

assert(condition,message)

深入瞭解 語法慣例

參數

名稱 類型 必要 Description
condition bool ✔️ 要評估的條件運算式。 條件必須在查詢分析階段評估為常數。
message string ✔️ 判斷提示評估為 false時所使用的訊息。

傳回

true如果條件為 true,則傳回 。 如果條件評估為 false,就會引發語意錯誤。

範例

下列查詢會定義檢查輸入字串長度的函 checkLength() 式,並使用 assert 來驗證輸入長度參數, (檢查其是否大於零) 。

let checkLength = (len:long, s:string)
{
    assert(len > 0, "Length must be greater than zero") and
    strlen(s) > len
};
datatable(input:string)
[
    '123',
    '4567'
]
| where checkLength(len=long(-1), input)

執行此查詢會產生錯誤: assert() has failed with message: 'Length must be greater than zero'

使用有效 len 輸入執行的範例:

let checkLength = (len:long, s:string)
{
    assert(len > 0, "Length must be greater than zero") and strlen(s) > len
};
datatable(input:string)
[
    '123',
    '4567'
]
| where checkLength(len=3, input)

輸出

input
4567

下列查詢一律會失敗,示範即使 where b 運算符在 為falseb不會傳回任何數據,仍會assert評估函式:

let b=false;
print x="Hello"
| where b
| where assert(b, "Assertion failed")