Skip to content

Commit

Permalink
Release memory that was allocated in result tuple / TIMESTAMP filter
Browse files Browse the repository at this point in the history
* Memory allocated columns needs to be released for each result tuple
* Filter for TIMESTAMP
  • Loading branch information
mkaruza committed May 8, 2024
1 parent dd87b58 commit ce4e16b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/quack_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ FilterOperationSwitch(Datum &value, duckdb::Value &constant, Oid typeOid) {
return TemplatedFilterOperation<int32_t, OP>(dateDatum, constant);
break;
}
case TIMESTAMPOID: {
Datum timeStampDatum = static_cast<int64_t>(value + quack::QUACK_DUCK_TIMESTAMP_OFFSET);
return TemplatedFilterOperation<int64_t, OP>(timeStampDatum, constant);
break;
}
default:
elog(ERROR, "(DuckDB/FilterOperationSwitch) Unsupported quack type: %d", typeOid);
}
Expand Down
10 changes: 9 additions & 1 deletion src/quack_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ Quack_BeginCustomScan(CustomScanState *cscanstate, EState *estate, int eflags) {
static TupleTableSlot *
Quack_ExecCustomScan(CustomScanState *node) {
QuackScanState *quackScanState = (QuackScanState *)node;

TupleTableSlot *slot = quackScanState->css.ss.ss_ScanTupleSlot;
MemoryContext oldContext;


if (!quackScanState->is_executed) {
quackScanState->queryResult = quackScanState->preparedStatement->Execute();
Expand All @@ -66,13 +67,18 @@ Quack_ExecCustomScan(CustomScanState *node) {
quackScanState->currentRow = 0;
quackScanState->fetch_next = false;
if (!quackScanState->currentDataChunk || quackScanState->currentDataChunk->size() == 0) {
MemoryContextReset(quackScanState->css.ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
ExecClearTuple(slot);
return slot;
}
}

MemoryContextReset(quackScanState->css.ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
ExecClearTuple(slot);

/* MemoryContext used for allocation */
oldContext = MemoryContextSwitchTo(quackScanState->css.ss.ps.ps_ExprContext->ecxt_per_tuple_memory);

for (idx_t col = 0; col < quackScanState->columnCount; col++) {
auto value = quackScanState->currentDataChunk->GetValue(col, quackScanState->currentRow);
if (value.IsNull()) {
Expand All @@ -83,6 +89,8 @@ Quack_ExecCustomScan(CustomScanState *node) {
}
}

MemoryContextSwitchTo(oldContext);

quackScanState->currentRow++;
if (quackScanState->currentRow >= quackScanState->currentDataChunk->size()) {
delete quackScanState->currentDataChunk.release();
Expand Down
2 changes: 2 additions & 0 deletions src/quack_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ GetPostgresDuckDBType(duckdb::LogicalTypeId type) {
return INT4OID;
case duckdb::LogicalTypeId::BIGINT:
return INT8OID;
case duckdb::LogicalTypeId::HUGEINT:
return NUMERICOID;
case duckdb::LogicalTypeId::VARCHAR:
return VARCHAROID;
case duckdb::LogicalTypeId::DATE:
Expand Down

0 comments on commit ce4e16b

Please sign in to comment.