Skip to content

Commit

Permalink
Refactor the table AM: scan_begin_extractcolumns to contain execution…
Browse files Browse the repository at this point in the history
… context

Refactor the signature of scan_begin_extractcolumns to contain execution
context, so that the scan node is able to filter data by evaluating
expressions in low level. Low-level filter may have less cost than extracting
all columns in a row and filtered out by the upper execution node.
  • Loading branch information
wuhao authored and my-ship-it committed Dec 12, 2023
1 parent a6a1992 commit 1a6372e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
11 changes: 8 additions & 3 deletions src/backend/access/aocs/aocsam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,14 @@ extractcolumns_from_node(Node *expr, bool *cols, AttrNumber natts)
}

static TableScanDesc
aoco_beginscan_extractcolumns(Relation rel, Snapshot snapshot, ParallelTableScanDesc parallel_scan,
List *targetlist, List *qual,
uint32 flags)
aoco_beginscan_extractcolumns(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key,
ParallelTableScanDesc parallel_scan,
PlanState *ps, uint32 flags)
{
AOCSScanDesc aoscan;
AttrNumber natts = RelationGetNumberOfAttributes(rel);
List *targetlist = ps->plan->targetlist;
List *qual = ps->plan->qual;
bool *cols;
bool found = false;

Expand All @@ -620,6 +622,9 @@ aoco_beginscan_extractcolumns(Relation rel, Snapshot snapshot, ParallelTableScan

pfree(cols);

if (gp_enable_predicate_pushdown)
ps->qual = aocs_predicate_pushdown_prepare(aoscan, qual, ps->qual, ps->ps_ExprContext, ps);

return (TableScanDesc)aoscan;
}

Expand Down
12 changes: 12 additions & 0 deletions src/backend/access/appendonly/appendonlyam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,18 @@ appendonly_beginscan(Relation relation,
return (TableScanDesc) aoscan;
}

TableScanDesc
appendonly_beginscan_extractcolumns(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key,
ParallelTableScanDesc parallel_scan,
PlanState *ps, uint32 flags)
{
AppendOnlyScanDesc aoscan;
aoscan = (AppendOnlyScanDesc) appendonly_beginscan(rel, snapshot, nkeys, key, parallel_scan, flags);
if (gp_enable_predicate_pushdown)
ps->qual = appendonly_predicate_pushdown_prepare(aoscan, ps->qual, ps->ps_ExprContext);
return (TableScanDesc) aoscan;
}

/* ----------------
* appendonly_rescan - restart a relation scan
*
Expand Down
5 changes: 5 additions & 0 deletions src/backend/access/appendonly/appendonlyam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,11 @@ static const TableAmRoutine ao_row_methods = {

.slot_callbacks = appendonly_slot_callbacks,

/*
* appendonly row table doesn't extract columns, but handles
* predicate pushdown.
*/
.scan_begin_extractcolumns = appendonly_beginscan_extractcolumns,
.scan_begin = appendonly_beginscan,
.scan_end = appendonly_endscan,
.scan_rescan = appendonly_rescan,
Expand Down
28 changes: 4 additions & 24 deletions src/backend/executor/nodeSeqscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ SeqNext(SeqScanState *node)
/* try parallel mode for AOCO extract columns */
scandesc = table_beginscan_es(node->ss.ss_currentRelation,
estate->es_snapshot,
0, NULL,
pscan,
node->ss.ps.plan->targetlist,
node->ss.ps.plan->qual);
&node->ss.ps);
}
else
{
Expand All @@ -120,31 +120,11 @@ SeqNext(SeqScanState *node)
*/
scandesc = table_beginscan_es(node->ss.ss_currentRelation,
estate->es_snapshot,
0, NULL,
NULL,
node->ss.ps.plan->targetlist,
node->ss.ps.plan->qual);
&node->ss.ps);
node->ss.ss_currentScanDesc = scandesc;
}
if (gp_enable_predicate_pushdown)
{
if (RelationIsAoRows(node->ss.ss_currentRelation))
{
/* eval predicate push down */
node->ss.ps.qual =
appendonly_predicate_pushdown_prepare((AppendOnlyScanDesc)node->ss.ss_currentScanDesc,
node->ss.ps.qual,
node->ss.ps.ps_ExprContext);
}
else if (RelationIsAoCols(node->ss.ss_currentRelation))
{
node->ss.ps.qual =
aocs_predicate_pushdown_prepare((AOCSScanDesc)node->ss.ss_currentScanDesc,
node->ss.ps.plan->qual,
node->ss.ps.qual,
node->ss.ps.ps_ExprContext,
&node->ss.ps);
}
}
}

/*
Expand Down
26 changes: 18 additions & 8 deletions src/include/access/tableam.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ typedef void (*IndexBuildCallback) (Relation index,
bool tupleIsAlive,
void *state);

/*
* CBDB: Execution structure shouldn't appear here in PG design. We need to
* tell the scan node to do low-level and efficient filtering. The scan node
* will use low-level APIs exposed by the storage.
*/
struct PlanState;

/*
* API struct for a table AM. Note this must be allocated in a
* server-lifetime manner, typically as a static const struct, which then gets
Expand Down Expand Up @@ -315,9 +322,9 @@ typedef struct TableAmRoutine
*/
TableScanDesc (*scan_begin_extractcolumns) (Relation rel,
Snapshot snapshot,
int nkeys, struct ScanKeyData *key,
ParallelTableScanDesc parallel_scan,
List *targetlist,
List *qual,
struct PlanState *ps,
uint32 flags);

/*
Expand Down Expand Up @@ -925,8 +932,10 @@ table_beginscan(Relation rel, Snapshot snapshot,
* Like table_beginscan_parallel, it will be parallel mode if parallel_scan is not NULL.
*/
static inline TableScanDesc
table_beginscan_es(Relation relation, Snapshot snapshot, ParallelTableScanDesc parallel_scan,
List *targetList, List *qual)
table_beginscan_es(Relation relation, Snapshot snapshot,
int nkeys, struct ScanKeyData *key,
ParallelTableScanDesc parallel_scan,
struct PlanState *ps)
{
bool isParallel = parallel_scan != NULL;
uint32 flags = SO_TYPE_SEQSCAN |
Expand All @@ -951,12 +960,13 @@ table_beginscan_es(Relation relation, Snapshot snapshot, ParallelTableScanDesc p
}

if (relation->rd_tableam->scan_begin_extractcolumns)
return relation->rd_tableam->scan_begin_extractcolumns(relation, snapshot, parallel_scan,
targetList, qual,
flags);
return relation->rd_tableam->scan_begin_extractcolumns(relation, snapshot,
nkeys, key,
parallel_scan,
ps, flags);

return relation->rd_tableam->scan_begin(relation, snapshot,
0, NULL,
nkeys, key,
parallel_scan, flags);
}

Expand Down
6 changes: 6 additions & 0 deletions src/include/cdb/cdbappendonlyam.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ extern TableScanDesc appendonly_beginscan(Relation relation,
int nkeys, struct ScanKeyData *key,
ParallelTableScanDesc pscan,
uint32 flags);
extern TableScanDesc appendonly_beginscan_extractcolumns(Relation rel,
Snapshot snapshot,
int nkeys, struct ScanKeyData *key,
ParallelTableScanDesc parallel_scan,
PlanState *ps,
uint32 flags);
extern void appendonly_rescan(TableScanDesc scan, ScanKey key,
bool set_params, bool allow_strat,
bool allow_sync, bool allow_pagemode);
Expand Down

0 comments on commit 1a6372e

Please sign in to comment.