I’m looking to see what is the most efficient way of writing SQL in snowflake. Below is the problem statement.
- Table A with events data, which has 3 columns X, Y. Table will have duplicate X values.
- Table B with Mapping data, which has 2 columns X1, T
- Snowflake UDF function which makes API call to generate value.
- Input for function is value from X or X1 column and output value will be of T column.
Output could be new table C or Table A which consists of one extra column T with corresponding values to column X
Ideally if all mapping values available in mapping table B, just a left join between TABLE A AND TABLE B would be sufficient but in some cases only few mapping values exist in TABLE B, so i need to make an apply API call over distinct column X of TABLE A which doesn’t have mapping values in mapping table B.
Only bottle neck is i need to make as less as possible API calls, that is why using distinct function on column X and applying only ones which doesn’t have mapping value.
and also i need to update the mapping table B with new values.
TABLE A
Column X | Column Y |
---|---|
ab | vv |
zx | xx |
ab | bb |
cb | bb |
cc | mm |
cc | nm |
Table B
Column X1 | Column T |
---|---|
ab | tom |
zx | banks |
UDF function taking each row value:
def some_functin(X):
……
return T
output tables:
Column X | Column Y | Column T |
---|---|---|
ab | vv | tom |
zx | xx | banks |
ab | bb | tom |
cb | bb | silver |
cc | mm | gold |
cc | nm | gold |
Table B
Column X1 | Column T |
---|---|
ab | tom |
zx | banks |
cb | silver |
cc | gold |
I tried
- Getting result of TABLE A LEFT JOIN TABLE B to get all mapped values (T columns) which are existing
- Taking the above result and deriving another result set where T column is NULL and doing distinct on column X.
- Calling UDF on above deduplicated result and getting Column T and updating MAPPING table with new T and X column values
- Finally again doing LEFT join TABLE A AND TABLE B ( this time mapping table will have all X, T values)
- planning to keep above sqls in an procedure and schedule it with task when ever new data arrives in TABLE A.
This doesn’t seem rite and trying to see if there is an efficient way to solve this.
Tom Banks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.