Function View

Function view provides a flexible way to control user access to databases and tables. A function view is a user-defined function that encapsulates statements that need access to databases. Even if a user does not have the privilege to read the data of a database, the user can access the database indirectly by executing the function view to obtain the desired calculation result.

In the following example, the user-defined function getSpread calculates the average bid-ask spread for a specified stock in the table dfs://TAQ/quotes. A user (user1) does not have the privilege to read table dfs://TAQ/quotes. Now define function getSpread to be a function view, and grant user1 the priviledge to execute the function view. Although user1 cannot read table dfs://TAQ/quotes, now he/she can execute the function view to calculate the average bid-ask spread of any stock in the table.

Please note that as table dfs://TAQ/quotes is a distributed database, the following script needs to be executed by a system administrator on the controller node. User1 can execute function getSpread on any data node.

$ def getSpread(s, d){
$    return select avg((ofr-bid)/(ofr+bid)*2) as spread from loadTable("dfs://TAQ","quotes") where symbol=s, date=d
$ }
$ addFunctionView(getSpread)
$ grant("user1", VIEW_EXEC, "getSpread")

If the DolphinDB cluster is restarted, a previously defined function view can still be used. DolphinDB, however, does not allow the definition of a function view to be modified. To modify a function view, we need to delete the function view with command dropFunctionView then redefine it.

$ dropFunctionView("getSpread")