Skip to content

Commit

Permalink
Query COUNT(*)
Browse files Browse the repository at this point in the history
* COUNT(*) doesn't require any columns to be retrieved so we only count
  tuples that pass visibility without fetching.
  • Loading branch information
mkaruza committed Apr 27, 2024
1 parent 28e46d3 commit ebe5014
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/quack/quack_heap_seq_scan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class PostgresHeapSeqParallelScanState {

public:
PostgresHeapSeqParallelScanState()
: m_nblocks(InvalidBlockNumber), m_last_assigned_block_number(InvalidBlockNumber), m_total_row_count(0),
m_last_prefetch_block(0), m_strategy(nullptr) {
: m_nblocks(InvalidBlockNumber), m_last_assigned_block_number(InvalidBlockNumber), m_count_tuple_only(false),
m_total_row_count(0), m_last_prefetch_block(0), m_strategy(nullptr) {
}
~PostgresHeapSeqParallelScanState() {
if (m_strategy)
Expand All @@ -51,6 +51,7 @@ class PostgresHeapSeqParallelScanState {
std::mutex m_lock;
BlockNumber m_nblocks;
BlockNumber m_last_assigned_block_number;
bool m_count_tuple_only;
duckdb::map<duckdb::column_t, duckdb::idx_t> m_columns;
duckdb::map<duckdb::idx_t, duckdb::column_t> m_projections;
duckdb::TableFilterSet *m_filters = nullptr;
Expand Down
8 changes: 8 additions & 0 deletions src/quack_heap_seq_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,21 @@ PostgresHeapSeqScan::InitParallelScanState(const duckdb::vector<duckdb::column_t
const duckdb::vector<duckdb::idx_t> &projections,
duckdb::TableFilterSet *filters) {
m_parallel_scan_state.m_nblocks = RelationGetNumberOfBlocks(m_rel);

if (columns.size() == 1 && columns[0] == UINT64_MAX) {
m_parallel_scan_state.m_count_tuple_only = true;
return;
}

/* We need ordered columns ids for tuple fetch */
for (duckdb::idx_t i = 0; i < columns.size(); i++) {
m_parallel_scan_state.m_columns[columns[i]] = i;
}

for (duckdb::idx_t i = 0; i < columns.size(); i++) {
m_parallel_scan_state.m_projections[projections[i]] = columns[i];
}

//m_parallel_scan_state.PrefetchNextRelationPages(m_rel);
m_parallel_scan_state.m_filters = filters;
}
Expand Down
5 changes: 5 additions & 0 deletions src/quack_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ InsertTupleIntoChunk(duckdb::DataChunk &output, PostgresHeapSeqScanThreadInfo &t
PostgresHeapSeqParallelScanState &parallelScanState) {
HeapTupleReadState heapTupleReadState = {};

if (parallelScanState.m_count_tuple_only) {
threadScanInfo.m_output_vector_size++;
return;
}

Datum *values = (Datum *)duckdb_malloc(sizeof(Datum) * parallelScanState.m_columns.size());
bool *nulls = (bool *)duckdb_malloc(sizeof(bool) * parallelScanState.m_columns.size());

Expand Down

0 comments on commit ebe5014

Please sign in to comment.