I have thousands of script that include loop over dataset and use variables for accumulation. Eg:
// assuming that 'ids' is populated from some BQ table
ids = [1, 2, 3, 4, 5]
var1 = "v1" //initialize variable
var2 = "v2" //initialize variable
for id in ids
var1 = var2
if (id > 2)
var2 = var1 + "-" + item
else
var2 = id
print(id, var1, var2)
This would produce below output:
1,v2,1
2,1,2
3,2,2-3
4,2-3,2-3-4
5,2-3-4,2-3-4-5
Few other things to consider:
- Script can involve n number of variables.
- Variables can act as accumulators or can simply have literals.
- Condition in loop can be
more complex, multiple nested
if
/else
conditions. - Script can have nested loop as well which would loop for n times or till certain condition is not met.
Is it possible to support it in BigQuery SQL? I'm open for BigQuery UDF as well but UDF does not support mutable state.
Also since there are thousands of such scripts, instead of doing it manually, I want to automate conversion. Parsing these scripts is not an issue for me, I only need to convert this to equivalent BigQuery SQL. Let me know how I can approach this problem.
Edit: Though I'm more inclined to use SQL only approach but BigQuery script also seems promising option.
I was able to convert SQL query for the aforementioned sample script:
And like that I'm also able to convert most of the scripts in pure SQL. It's just building model around it is more involved but at least I'm moving ahead with manual conversions.
I have managed to reproduce your script using SQL UDF in BigQuery, I have used StandardSQL instead of Legacy SQL, you can read more about it here. I would like to state that UDF suports JavaScript and I found it better to write it using its functionality.
I used this dummy data instead of declaring an array:
Below it is the script I developed to reproduce what you accomplished using your code.
Notice that I had to write two different functions, one for each row var1 and var2. Also, the array starts at position 1 in the test_v1 function.
Finally, the output was:
I hope it helps.