Executing Scripts and Functions

This section introduces how to use member function DBConnection::run to execute DolphinDB script and call DolphinDB built-in functions and user-defined functions with detailed examples.

Executing Scripts

The function declaration for DBConnection::run to execute scripts is as follows:

ConstantSP run(const string& script, int priority=4, int parallelism=64, int fetchSize=0, bool clearMemory = false);

Arguments

  • script: The script for execution.
  • priority: Specifies job priority. The default value is 4. In DolphinDB, jobs are executed based on priority, which ranges from 0 to 9 - the bigger the value, the higher the priority. For details, see DolphinDB tutorial - Job Management.
  • parallelism: Specifies the maximum number of threads to execute the tasks of a job on a data node at the same time. The default value is 64. For details, see DolphinDB tutorial - Job Management.
  • fetchSize: Specifies the size of a block (defaults to 0), which returns a BlockReader object. Use its read function to read data in blocks when querying tables of large size. Note that fetchSize cannot be smaller than 8192 (records).
  • clearMemory: Removes all variables created during that run call immediately after it completes. The default value is false. This helps prevent unnecessary variable buildup and reduces memory usage.

Examples

Example 1

Use the conn.run function to perform some basic operations as follows:

//calculate "1+1"
std::cout << conn.run("1 + 1")->getInt() << std::endl;
//create a DolphinDB table "t"
conn.run("t = table(1:0, `time`avgPrice`volume`dollarVolume`count, [TIMESTAMP,DOUBLE,INT,DOUBLE,INT])");
//query on table "t" with a SQL statement
TableSP tbl = conn.run("select time, avgPrice from t");

Example 2

The example with parameter fetchSize specified to 10000.

//create a table with 100001 rows
std::string testBlock = R"(
    rows=100001;
    testblock=table(take(1,rows) as id,take(`A,rows) as symbol,take(2020.08.01..2020.10.01,rows) as date, rand(50,rows) as size,rand(50.5,rows) as price);
)";
conn.run(testBlock);
//read the records of the table, 10000 rows at a time
BlockReaderSP reader = conn.run("select * from testblock", 4, 2, 10000);
//get the number of rows read
int totalNum = 0;
while(reader->hasNext()){
    TableSP t = reader->read();
    totalNum += t->size();
}
//output the total count
std::cout << totalNum;

Example 3

The example with parameter clearMemory specified to true.

//create a variable "n=1"
conn.run("n = 1", 4, 2, 0, true);
//The following operation will throw an exception since "n" has been removed with clearMemory set to true.
conn.run("n");

Executing Functions

The function declaration for DBConnection::run to execute functions is as follows:

ConstantSP run(const string& funcName, vector<ConstantSP>& args, int priority=4, int parallelism=64, int fetchSize=0, bool clearMemory = false);

Arguments

  • funcName: The name of the function to be executed.
  • args: The arguments of the function.
  • priority: Specifies job priority. The default value is 4.
  • parallelism: Specifies the maximum number of threads to execute the tasks of a job on a data node at the same time. The default value is 64.
  • fetchSize: Specifies the size of a block (defaults to 0), which returns a BlockReader object. Use its read function to read data in blocks when querying tables of large size. Note that fetchSize cannot be smaller than 8192 (records).
  • clearMemory: Removes all variables created during that run call immediately after it completes. The default value is false. This helps prevent unnecessary variable buildup and reduces memory usage.

Examples

The following examples demonstrate how to use the run function to call DolphinDB built-in function add(X,Y).

Example 1

When both parameters X and Y in C++:

vector<ConstantSP> args; 
//initialize a vector "x" for parameter X
ConstantSP x = Util::createVector(DT_DOUBLE, 3); 
double array_x[] = {1.5, 2.5, 7}; 
x->setDouble(0, 3, array_x);
args.push_back(x);
//initialize a vector "y" for parameter Y
ConstantSP y = Util::createVector(DT_DOUBLE, 3); 
double array_y[] = {8.5, 7.5, 3}; 
y->setDouble(0, 3, array_y); 
args.push_back(y);
//execute function add
ConstantSP result = conn.run("add", args);
//output the result: [10, 10, 10]
std::cout << result->getString() << std::endl;

Example 2

When one parameter (Y) in C++, and the other (X) in DolphinDB server. In such case, parameter in server should be fixed in function add. For more information, refer to Partial Application.

//define a vector "y" for parameter Y in DolphinDB server
conn.run("y = [1, 3, 5]"); 
//initialize a vector "x" for parameter X in C++
vector<ConstantSP> args;
ConstantSP x = Util::createVector(DT_DOUBLE, 3);
double array_x[] = {9, 7, 5};
x->setDouble(0, 3, array_x);
args.push_back(x);
//execute function add
ConstantSP result = conn.run("add{,y}", args);
//output the result: [10, 10, 10]
std::cout << result->getString() << std::endl;