Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snowflake removing backslashes during Procedure compilation?

For some reason Snowflake is removing backslashes from my regex function, but only when I put the function in between the "$$" when creating a Javascript procedure.

For context here is my Regex Function:

CREATE OR REPLACE FUNCTION "REGEX_REPLACE_ME"("SUBJECT" VARCHAR(16777216), "PATTERN" VARCHAR(16777216), "REPLACEMENT" VARCHAR(16777216))
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
AS '
  
    const p = SUBJECT;
    let regex = new RegExp(PATTERN, ''i'') 
    return p.replace(regex, REPLACEMENT);
  ';

When I run it simply in SQL, it works; it will change "APPLE.com" to "APPLE".

SELECT REXP_REPLACE_ME('APPLE.COM','\\.[A-Z]{2,3}',' ') -- the regex pattern \\.[A-Z]{2,3} is meant to remove domains i.e. ".com",".org", etc.. 

However, when I run it within $$ of a stored procedure, it removes the backslashes from my regex pattern and consequently changing the regex pattern completely.

My regex changes from \\.[A-Z]{2,3} to -> .[A-Z]{2,3}

CREATE or replace PROCEDURE TESTING_FUNC_1_THIS_CAN_BE_DELETED()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$

var rs = snowflake.execute( { sqlText:
`
    CREATE OR REPLACE VIEW Database.Schema.Table AS

    SELECT REXP_REPLACE_ME('APPLE.COM','\\.[A-Z]{2,3}',' ')  as column_cleaned
 --   ,REXP_REPLACE_ME_WTF('APPLE.COM','\\.[A-Z]{2,3}',' ') AS WHAT_PATTERN_IS_BEING_OUTPUTTED -- function logic in code block below
    
`
                            } );
  $$;

call TESTING_FUNC_1_THIS_CAN_BE_DELETED();

select * from Database.Schema.Table

Scratching my head here, I created this function to show what pattern it was outputting and this is how I came to the conclusion (I can be wrong here..) that when Snowflake is compiling it's removing the backslash...

CREATE OR REPLACE FUNCTION "REXP_REPLACE_ME_WTF"("SUBJECT" VARCHAR(16777216), "PATTERN" VARCHAR(16777216), "REPLACEMENT" VARCHAR(16777216))
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
AS '
  
    const p = SUBJECT;
    let regex = new RegExp(PATTERN, ''i'');
    //return p.replace(regex, REPLACEMENT);
    return PATTERN;
  ';

Any ideas?

like image 401
mikelowry Avatar asked Oct 07 '21 16:10

mikelowry


People also ask

How do you escape a backslash in a snowflake?

If you are escaping a metacharacter with a backslash, you must escape the backslash with a second backslash. See Example of Using Metacharacters in a Single-Quoted String Constant.

How do you use a stored procedure for a snowflake variable?

When you declare a variable, you must specify the type of the variable by either: Explicitly specifying the data type. Specifying an expression for the initial value for the variable. Snowflake Scripting uses the expression to determine the data type of the variable.

Does Snowflake support JavaScript?

Both Snowflake SQL and JavaScript support string values.

How do you print text in a snowflake?

Use getSqlText() to return a statement as text, see docs. Add the return statement from below to the end of your procedure to have it print the statement when it is called. Show activity on this post. Show activity on this post.


2 Answers

Snowflake SQL uses \ as an escape character, so to represent a backslash in Snowflake SQL you have to use \\.

JavaScript also uses \ as an escape character, so to represent a backslash in JavaScript you also have to use \\

If you want to represent a single backslash in Snowflake SQL through JavaScript, you have to send 2 x 2 = 4 backslashes \\\\

like image 127
Greg Pavlik Avatar answered Oct 07 '22 04:10

Greg Pavlik


Probably something to do with escape characters etc

Try wrapping your SQL statement in backticks instead of single quotes: ``

like image 28
Dean Flinter Avatar answered Oct 07 '22 05:10

Dean Flinter