1

Disclaimer: I'm new to JavaScript & new to writing JavaScript UDFs in Snowflake.

I'm attempting to build a UDF which will "clean" the string of any characters where the ascii code is not between 32 & 127. When I attempt to use this function, I get the error:

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

After searching around for what in the syntax could be causing this problem, I'm no further along. The value being input is a length 3 string, with the following ascii codes: 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 Answer 1

1

Two things:

1) JavaScript is a case sensitive so keywords like FOR, IF and WHILE need to be lowercased. (Thanks to @waldente for that answer)

Also:

2) Change all references to the input parameter from lowercase value to uppercase VALUE. For example change value.length in your for loop to VALUE.length

In the documentation on JavaScript UDFs there is this part that says:

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

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, that didn't work. I'm still getting the same error.
Make FOR, IF and RETURN lowercase
@waldente is right - JavaScript is case sensitive so that will probably fix your issue. I'll update my answer to include that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.