Syntax
Function.ScalarVector(scalarFunctionType as type, vectorFunction as function) as function
About
返回一个标量函数,该函数的类型 scalarFunctionType 使用单个参数行调用 vectorFunction 并返回其单个输出。 此外,当标量函数重复应用于输入表的每一行时(例如,Table.AddColumn),vectorFunction 将被一次性应用于所有输入。
vectorFunction 将传递一个表,其列的名称和位置与scalarFunctionType的参数相匹配。 此表的每一行都包括一次对标量函数的参数调用,其中各列与scalarFunctionType的参数相对应。
vectorFunction 必须返回与输入表相同的长度的列表,其每个位置的项必须与评估相同位置的输入行上的标量函数的结果相同。
输入表应流式传输到其中,因此 vectorFunction 预期在输入传入时流式传输其输出,一次只处理一个输入块。 具体而言, vectorFunction 不能多次枚举其输入表。
示例 1
通过对输入进行成批处理,每批次处理 100 行,计算输入表中的两列的乘积。
用法
let
Compute.ScoreScalar = (left, right) => left * right,
// When Function.ScalarVector batching kicks in, we'll receive all
// of the inputs for the entire table here at once.
Compute.ScoreVector = (input) => let
chunks = Table.Split(input, 100),
scoreChunk = (chunk) => Table.TransformRows(chunk, each Compute.ScoreScalar([left], [right]))
in
List.Combine(List.Transform(chunks, scoreChunk)),
Compute.Score = Function.ScalarVector(
type function (left as number, right as number) as number,
Compute.ScoreVector
),
Final = Table.AddColumn(
Table.FromRecords({
[a = 1, b = 2],
[a = 3, b = 4]
}),
"Result",
each Compute.Score([a], [b])
)
in
Final
输出
Table.FromRecords({
[a = 1, b = 2, Result = 2],
[a = 3, b = 4, Result = 12]
})
示例 2
计算两批测试分数,并填充可用于验证批处理是否按预期工作的批处理 ID 字段。
用法
let
_GradeTest = (right, total) => Number.Round(right / total, 2),
_GradeTests = (inputs as table) as list => let
batches = Table.Split(inputs, 2),
gradeBatch = (batch as table) as list =>
let
batchId = Text.NewGuid()
in
Table.TransformRows(batch, each [Grade = _GradeTest([right], [total]), BatchId = batchId])
in
List.Combine(List.Transform(batches, gradeBatch)),
GradeTest = Function.ScalarVector(type function (right as number, total as number) as number, _GradeTests),
Tests = #table(type table [Test Name = text, Right = number, Total = number],
{
{"Quiz 1", 3, 4},
{"Test 1", 17, 22},
{"Quiz 2", 10, 10}
}),
// To break batching, replace [Right] with {[Right]}{0}.
TestsWithGrades = Table.AddColumn(Tests, "Grade Info", each GradeTest([Right], [Total]), type record),
// To verify batching, also expand BatchId.
Final = Table.ExpandRecordColumn(TestsWithGrades, "Grade Info", {"Grade"})
in
Final
输出
#table(
type table [Test Name = text, Right = number, Total = number, Grade = number],
{
{"Quiz 1", 3, 4, 0.75},
{"Test 1", 17, 22, 0.77},
{"Quiz 2", 10, 10, 1}
}
)