Azure Data Factory中Lookup活动firstRow属性不存在问题求助
问题场景
管道中LastRun步骤偶发失败,报错信息:
Operation on target LastRun failed: The expression 'formatDateTime(activity('Last Run').output.firstRow.lastdate, 'yyyy-MM-dd HH:mm:ss')' cannot be evaluated because property 'firstRow' doesn't exist, available properties are 'effectiveIntegrationRuntime, durationInQueue'.
失败发生在Lookup活动后的Set Variable步骤:
- Lookup活动执行Snowflake查询:
select max(PROCESSINGCOMPLETE) as lastdate from schema.loadcontrol where status = 'Completed' and filedirection = 'Inbound' - Set Variable使用表达式转换日期格式:
@formatDateTime(activity('Last Run').output.firstRow.lastdate, 'yyyy-MM-dd HH:mm:ss')
重跑管道即可正常执行,多数场景下(10次中9次)无问题,数据预览也能正常显示单行结果。
核心原因
当Lookup活动的SQL查询返回空结果集时,ADF不会生成firstRow属性,仅返回effectiveIntegrationRuntime、durationInQueue等元数据,导致后续表达式访问firstRow时报错。
触发空结果集的常见场景:
schema.loadcontrol中暂时没有满足status = 'Completed' and filedirection = 'Inbound'的记录,此时max(PROCESSINGCOMPLETE)返回NULL,但查询结果集为空行(而非一行NULL值)。- Snowflake存在查询缓存或元数据同步延迟,首次执行查询时未读取到刚写入的符合条件记录,重跑时数据已完成同步,查询返回有效结果。
解决方案
1. 修改SQL查询,确保始终返回一行数据
通过COALESCE构造默认值,保证无论是否有匹配记录,查询都返回一行数据:
select coalesce(max(PROCESSINGCOMPLETE), '1900-01-01'::timestamp) as lastdate from schema.loadcontrol where status = 'Completed' and filedirection = 'Inbound'
这样Lookup活动必然生成firstRow属性,避免表达式报错。
2. 在Set Variable表达式中增加空值判断
通过empty()函数检查firstRow是否存在,为空时返回默认值:
@if(empty(activity('Last Run').output.firstRow), '1900-01-01 00:00:00', formatDateTime(activity('Last Run').output.firstRow.lastdate, 'yyyy-MM-dd HH:mm:ss') )
3. 配置Lookup活动的"允许无行"属性
在Lookup活动的设置中开启**"Allow no rows"**为True,配合表达式中的空值处理,避免因无结果集直接报错。
内容的提问来源于stack exchange,提问作者MikeLanglois

