Snowflake UDF & Javascript - Issue with JavaScript compilation error: Uncaught SyntaxError

Snowflake UDF & Javascript - Issue with JavaScript compilation error: Uncaught SyntaxError

免责声明:我是 JavaScript 的新手,也是在 Snowflake 中编写 JavaScript UDF 的新手。

我正在尝试构建一个 UDF,它将 "clean" ascii 码不在 32 和 127 之间的任何字符的字符串。当我尝试使用此函数时,出现错误:

Reason:
SQL Error [100131] [P0000]: JavaScript compilation error: Uncaught SyntaxError: missing ) after
argument list in CLEAN at '    FOR (i = 0; i < value.length; i++) {' position 13

在搜索了语法中可能导致此问题的内容后,我没有进一步研究。输入的值是长度为 3 的字符串,具有以下 ascii 代码:0, 13, 0

CREATE OR REPLACE FUNCTION clean(value STRING)
    RETURNS string LANGUAGE JAVASCRIPT
    AS 
    $$ 
    var i = 0;
    var letter = "";
    var newValue = "";
    FOR (i = 0; i < value.length; i++) {
        letter = value[i].charCodeAt(0)
        IF ( letter >= 32 && letter <= 126) {
            newValue += value[i];
        } elseif (letter = 0) {
            newValue += value[i];
        }
    }
RETURN newValue
$$;

两件事:

1) JavaScript 区分大小写,因此 FOR、IF 和 WHILE 等关键字需要小写。 (感谢@waldente 的回答)

另外:

2) 将所有对输入参数的引用从小写 value 更改为大写 VALUE。例如,将 for 循环中的 value.length 更改为 VALUE.length

在 JavaScript UDF 的文档中 this part 说:

Note that the JavaScript code must refer to the input parameter names as all upper-case, even if the names are not uppercase in the SQL code