Created a jar file and use it as function. I created it with same user role for both function and snowflake stage. Uploaded the jar file to the stage using snowsql.
When I run the the following command in snowflake ui (browser), it works.
ls @~/stage_name
However, when I use the service account with similar role that I have using DBeaver. It does not work. It comes up empty.
Same thing with the function, it works in the Snowflake UI, but not in DBeaver. Please note that both users have the same role. Also, added grant "all privileges" and "usage" (which be part of all) to the roles I want them to use. But again, it does not work. It shows error below
**> SQL Error [91016] [22000]: Remote file 'stage_name/java_udf.jar' was
not found. If you are running a copy command, please make sure files are not deleted when they are being loaded or files are not being loaded into two different tables concurrently with auto purge option.**
However, when I run the function in Snowflake UI using my user account, it works fine. Please note my user account has the same role as the service account. But it doesn't work on the service account. Any ideas?
Followed steps here in the documentation: https://docs.snowflake.com/en/developer-guide/udf/java/udf-java-creating.html#label-udf-java-in-line-examples
CodePudding user response:
So I think I know the issue.
The stage could be shared using the same role. But the files uploaded in stage are not. They belong to the users who uploaded them. I just loaded exactly the same file a the same internal stage. And they did not overwrite each other:
Service Account: name: xxxxxxx.jar size: 389568 md5: be8b59593ae8c4b8baebaa8474bda0a7 last_modified: Tue, 8 Feb 2022 03:26:29 GMT
User account: namne: xxxxxxx.jar size: 389568 md5: 0c4d85a3a6581fa3007f0a4113570dbc last_modified: Mon, 7 Feb 2022 17:03:58 GMT
CodePudding user response:
~@ is the USER LOCAL stoage only area.
thus, unless the automation is the "same" user, it will not be able to access it.
This should be provable by getting the same "run" command that works from the WebUI for your user, and logging in as the automation user, and seeing you get the error there.
Reading that link document, full you can see that you should use a table storage, or a named storage, which you can grant access to the role you both have.
working proof:
on user simeon:
create or replace stage my_stage;
create or replace function echo_varchar(x varchar)
returns varchar
language java
called on null input
handler='TestFunc.echo_varchar'
target_path='@my_stage/testfunc.jar'
as
'class TestFunc {
public static String echo_varchar(String x) {
return x;
}
}';
create role my_role;
grant usage on function echo_varchar(varchar) to my_role;
grant all on stage my_stage to my_role;
grant usage on database test to my_role;
grant usage on schema not_test to my_role;
grant usage on warehouse compute_wh to my_role;
then I test it:
use role my_role;
select current_user(), current_role();
/*CURRENT_USER() CURRENT_ROLE()
SIMEON MY_ROLE*/
select test.not_test.echo_varchar('Hello');
/*TEST.NOT_TEST.ECHO_VARCHAR('HELLO')
Hello*/
I created a new user test_two set them to role my_role
on user test_two:
use role my_role;
select current_user(), current_role();
/*CURRENT_USER() CURRENT_ROLE()
TEST_TWO MY_ROLE*/
select test.not_test.echo_varchar('Hello');
/*TEST.NOT_TEST.ECHO_VARCHAR('HELLO')
Hello*/
Ok so a function put on a accessible stage works, lets put another on my user simeon local stage ~@
on user Simeon:
returns varchar
language java
called on null input
handler='TestFuncB.echo_varcharb'
target_path='@~/testfuncb.jar'
as
'class TestFuncB {
public static String echo_varcharb(String x) {
return x;
}
}';
grant usage on function echo_varcharb(varchar) to my_role;
select test.not_test.echo_varcharb('Hello');
/*TEST.NOT_TEST.ECHO_VARCHARB('HELLO')
Hello*/
back on user test_two:
select test.not_test.echo_varcharb('Hello');
/*Remote file 'testfuncb.jar' was not found. If you are running a copy command, please make sure files are not deleted when they are being loaded or files are not being loaded into two different tables concurrently with auto purge option.*/
