You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Cloud Dataform中JavaScript常量偶现undefined问题求助

背景

我在使用Google Cloud的Dataform做数据处理,把常量定义和SQL渲染函数统一放在JavaScript文件中,供sqlx文件调用。大部分场景下能正常引用JS里的常量,但有部分常量会变为undefined,找不到问题根源,寻求解决办法。

示例代码

example.js

const CONST_STRING_01 = "hoge";
const CONST_STRING_02 = "fuga";
// 另有40+个同类字符串常量

const CONST_MAP_01 = {"A": "A-", "B": null}
const CONST_MAP_02 = {"X": "X-", "Y": "Y-", "Z": "Z-"}
const CONST_MAP_03 = {"1": null, "2": null}
// 另有20+个同类映射常量

function render_function_01 (map) {
    let renderedCaseSQL = "(根据map生成when子句)";
    return renderedCaseSQL;
}
// 另有更多函数

module.exports = {
    CONST_STRING_01,
    CONST_STRING_02,
    // 其余40+字符串常量
    CONST_MAP_01,
    CONST_MAP_02,
    CONST_MAP_03,
    // 其余20+映射常量
    render_function_01,
    // 其余函数
}

example.sqlx

config {
    type: "table",
    schema: "example_schema",
    name: "example_transform",
}

SELECT
    ${example.CONST_STRING_01} AS column_a,
    CASE input_b ${example.render_function_01(example.CONST_MAP_01)} END AS column_b,
    CASE input_c ${example.render_function_01(example.CONST_MAP_02)} END AS column_c,
    CASE input_d ${example.render_function_01(example.CONST_MAP_03)} END AS column_d,
FROM
    ${ref("example_schema", "example_input_table")}
预期行为

example.js中定义的所有常量值,都能在example.sqlx中被正确引用。

实际问题

多数常量可正常获取定义值,但部分会变为undefined。此问题仅在包含大量常量定义的.js文件中出现,我做了多组类似的.js与.sqlx组合测试,示例情况如下:

  • CONST_STRING_01 -> 正常
  • CONST_STRING_02 -> 正常
  • 其余40+字符串常量 -> 全部正常
  • CONST_MAP_01 -> 正常
  • CONST_MAP_02 -> undefined
  • CONST_MAP_03 -> 正常
  • 其余20+映射常量 -> 仅一个出现undefined,其余正常
已排查项
  • example.js无编译错误
  • example.sqlx无编译错误,但因常量为undefined会导致SQL语法错误
  • 所有常量与函数名称均唯一
  • module.exports包含所有正确的常量名称
  • Dataform配额使用正常
已尝试操作
  • 复制example.js创建example2.js,在example.sqlx中引用example2.js:暂时有效,但修改.js或.sqlx时会突然再次报错
  • 修改常量名称(如将CONST_MAP_02改为CONST_MAP_02a):暂时有效,但修改文件后仍会突然报错
  • 编写简单函数将MAP转为字符串做检查,未发现异常

内容的提问来源于stack exchange,提问作者TAKUYAK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:23:18