次の方法で共有


assert()

条件をチェックします。 条件が false の場合、エラー メッセージが出力され、クエリは失敗します。

Note

関数は assert 、定数フォールディングや述語ショートサーキットなどの最適化が適用される前に、クエリ分析フェーズ中に評価されます。

Note

assert 指定されたパラメーターは、クエリ分析フェーズ中に定数に評価する必要があります。 つまり、定数のみを参照する他の式から構築でき、行コンテキストにバインドすることはできません。

構文

assert(condition,message)

構文規則について詳しく知る。

パラメーター

名前 必須 説明
condition bool ✔️ 評価する条件式です。 クエリ分析フェーズでは、条件を定数に評価する必要があります。
message string ✔️ アサーションが に評価される場合に false使用されるメッセージ。

戻り値

条件trueが のtrue場合は を返します。 条件が に評価される場合は、セマンティック エラーを false発生させます。

次のクエリを使用すると、入力文字列の長さをチェックする checkLength() 関数が定義され、assert を使用して入力長パラメーターを検証します (これが 0 より大きいことを確認します)。

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

次のクエリは常に失敗し、 が assert の場合bfalseに 演算子がデータをwhere b返さない場合でも、関数が評価されることを示します。

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