Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/columnar.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ extern bool columnar_enable_bloom_filter; /* bloom equality skipping (I7) */
/* Phase 6 GUCs (spec 8.3) */
extern bool columnar_enable_vectorization; /* vectorized aggregate path */
extern bool columnar_enable_group_vectorization; /* GROUP BY vectorized agg (#289) */
extern bool columnar_enable_ungrouped_vector_agg; /* filtered/extended ungrouped agg (#289) */
extern int columnar_groupagg_max_groups; /* plan-time group-count cap (#289) */
extern bool columnar_enable_read_stream; /* stream/prefetch block reads (PG17+) */
extern bool columnar_enable_index_only_scan; /* allow index-only scans (gap 28) */
Expand Down Expand Up @@ -566,6 +567,20 @@ extern bool ColumnarReadNextRow(ColumnarReadState *readState,
extern void ColumnarRescanRead(ColumnarReadState *readState);
extern void ColumnarEndRead(ColumnarReadState *readState);

/*
* Batch-fold accessors (#289): expose the current loaded group's decoded buffer
* so an ungrouped aggregate can fold it column-at-a-time instead of one Datum
* tuple per row. See the block comment in columnar_reader.c for the contract.
*/
extern bool ColumnarReadFoldNextGroup(ColumnarReadState *readState);
extern void ColumnarReadFoldGroupInfo(ColumnarReadState *readState, uint64 *nrows,
const char **deleteMask, uint32 *deleteMaskLen,
const bool **skipVec, const uint32 **vecStart,
int *vectorCount);
extern bool ColumnarReadFoldColumn(ColumnarReadState *readState, int attidx,
const char **validity, const char **packed,
int16 *attlen, const uint32 **vecRawLen);

/*
* Restrict a scan to a set of row groups (issue #149). Groups outside the set
* are skipped without their bytes being read. Must be called before the first
Expand Down
77 changes: 77 additions & 0 deletions src/columnar_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,83 @@ ColumnarReadNextRow(ColumnarReadState *readState, Datum *values, bool *nulls,
return columnar_native_next_row(readState, values, nulls, rowNumber);
}

/* -------------------------------------------------------------------------
* Batch-fold accessors (#289)
*
* These expose the current loaded group's decoded buffer so an ungrouped
* aggregate can fold it column-at-a-time, without columnar_native_next_row
* producing one Datum tuple per row. Correctness is the caller's: it must walk
* each column's validity bitmap to map a row to its packed value (nulls have no
* slot), honor the delete mask, and step the per-column present index past a
* ruled-out vector. Only fixed-width by-value columns can be read this way; the
* caller checks that from the tuple descriptor before using ColumnarReadFoldColumn.
* ------------------------------------------------------------------------- */

/*
* Advance to the next row group to fold, loading it (and honoring restrict,
* parallel and zone-map group skipping via columnar_native_load_group). Returns
* false at end of scan; on true, the accessors below describe the loaded group.
*/
bool
ColumnarReadFoldNextGroup(ColumnarReadState *readState)
{
columnar_read_start(readState);
if (readState->exhausted)
return false;
if (!columnar_native_load_group(readState))
{
readState->exhausted = true;
return false;
}
readState->rowInGroup = 0;
return true;
}

/*
* Group-level fold info: row count, the delete mask (row-indexed bits, NULL when
* the group has no deletes) and its byte length, the per-vector skip flags and
* vector row-start offsets (both NULL when per-vector skipping is inactive), and
* the vector count.
*/
void
ColumnarReadFoldGroupInfo(ColumnarReadState *readState, uint64 *nrows,
const char **deleteMask, uint32 *deleteMaskLen,
const bool **skipVec, const uint32 **vecStart,
int *vectorCount)
{
*nrows = readState->groupRowCount;
*deleteMask = readState->nativeDeleteMask;
*deleteMaskLen = readState->nativeDeleteMaskLen;
*skipVec = readState->nativeSkipVec;
*vecStart = readState->nativeVecStart;
*vectorCount = readState->nativeVectorCount;
}

/*
* Column attidx in the loaded group: its validity bitmap (row-indexed; LSB-first),
* the base of its packed present values (contiguous, host-endian), the element
* width, and its per-vector decoded byte lengths (NULL when per-vector skipping is
* inactive, used to step the present index past a skipped vector). Returns false
* when the column is absent from this group (added by a later ALTER TABLE ADD
* COLUMN); the caller then folds it from the missing value or falls back.
*/
bool
ColumnarReadFoldColumn(ColumnarReadState *readState, int attidx,
const char **validity, const char **packed,
int16 *attlen, const uint32 **vecRawLen)
{
if (attidx < 0 || attidx >= readState->natts ||
readState->nativeValidity == NULL ||
readState->nativeValidity[attidx] == NULL)
return false;
*validity = readState->nativeValidity[attidx];
*packed = readState->nativeValueCursor[attidx];
*attlen = TupleDescAttr(readState->tupdesc, attidx)->attlen;
*vecRawLen = (readState->nativeVecRawLen != NULL)
? readState->nativeVecRawLen[attidx] : NULL;
return true;
}

/*
* columnar_next_group_index
* The next native row group to scan, or -1 when none remain. The native
Expand Down
11 changes: 11 additions & 0 deletions src/columnar_tableam.c
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,17 @@ _PG_init(void)
0,
NULL, NULL, NULL);

DefineCustomBoolVariable("pgcolumnar.enable_ungrouped_vector_agg",
"Use the vectorized aggregate fast path for ungrouped "
"aggregates with a filter or sum/avg over "
"int8/float/numeric.",
NULL,
&columnar_enable_ungrouped_vector_agg,
false,
PGC_USERSET,
0,
NULL, NULL, NULL);

DefineCustomIntVariable("pgcolumnar.groupagg_max_groups",
"Cap on the actual group count the grouped vectorized "
"aggregate builds before it stops with an error.",
Expand Down
Loading
Loading