1

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.

1
  • 1
    append! returns a DolphinDB VOID. Either use tableInsert instead or count the number of rows before append! 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. Commented Mar 17 at 16:01

1 Answer 1

3

Thank you @Ted Lyngmo for the tip. After switching to tableInsert instead of append!, I am able to get the actual number of rows inserted:

static FunctionDefSP appendFunc = heap->currentSession()->getFunctionDef("tableInsert");
vector<ConstantSP> appendArgs = {destTable, batchTable};
int insertedRows = 0;
ConstantSP ret = appendFunc->call(heap, appendArgs);
insertedRows = ret->getInt(); 
cout << "Append result: " << insertedRows << " rows" << endl;

For example, in the following test case:

@testing:case="test_load_mismatched_schema"
conn = duckdb::connect(":memory:")
duckdb::execute(conn, "create table t(i int)")
duckdb::execute(conn, "insert into t values (1), (2), (5)")
t = duckdb::query(conn, "select * from t")  // retrieve correct schema
dest = table(1:0, [`s], [STRING])
duckdb::load(conn, "t", dest, batchSize=1)
assert 1, dest.size() == 3 
assert 2, eqObj(string(t.i), dest.s)
duckdb::close(conn)

The console prints as expected:

Append result: 3 rows.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.