I am developing a DolphinDB plugin and need to write data to either an in-memory table or a distributed table. In the plugin, I call the built-in function append! to perform the write, and I want to obtain the actual number of rows inserted. Below is a simplified version of my code:
static FunctionDefSP appendFunc = heap->currentSession()->getFunctionDef("append!");
vector<ConstantSP> appendArgs = {destTable, batchTable};
int insertedRows = 0;
ConstantSP ret = appendFunc->call(heap, appendArgs);
insertedRows = ret->getInt(); // This fails with an error
However, when executing this code, I get the error:
Append to DolphinDB failed: The object can't be converted to int scalar.
Upon debugging, I found that ret is actually a dictionary (when the target table is a distributed table), so getInt() cannot be used directly. I then tried to extract the row count from the dictionary using:
ConstantSP keys = ret->getKeys();
ConstantSP firstCol = ret->getMember(keys->get(0));
insertedRows = ((VectorSP)firstCol)->size(); // Not correct
But this returns the row count of the source table (batchTable), not the actual inserted rows. Moreover, the returned value seems to be constant regardless of whether the write succeeded or not.
I also attempted to use the built-in function matchedRowCount based on the documentation, which states that matchedRowCount returns the number of rows affected by the last INSERT INTO, DELETE, or UPDATE statement in the current session. My code looks like this:
static FunctionDefSP matchedRowCountFunc = heap->currentSession()->getFunctionDef("matchedRowCount");
appendFunc->call(heap, appendArgs); // Execute tableInsert first
vector<ConstantSP> emptyArgs;
ConstantSP ret = matchedRowCountFunc->call(heap, emptyArgs);
insertedRows = ret->getInt(); // Always returns 0
However, insertedRows is always 0, even when the write was successful.
How can I correctly obtain the number of rows actually inserted by append! when the target is a distributed table (where the return value is a dictionary)?
Environment:
DolphinDB version: 3.0.0.4
Plugin written in C++
Target tables can be either in-memory or distributed (DFS tables)
Any guidance would be greatly appreciated.
append!returns a DolphinDBVOID. Either usetableInsertinstead or count the number of rows beforeappend!and after:auto before = destTable->rows(); appendFunc->call(heap, appendArgs); auto after = destTable->rows(); auto inserted = after - before;. Can't test to verify myself though.