Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/filter-anchor-partitions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@platforma-sdk/model": minor
---

Add `filterToAnchorPartitions` option to `getAnchoredPColumns`. When set, restricts each returned column's visible axis-0 key range to the partition keys present in the resolved anchor column(s) by injecting a `pl7.app/axisKeys/0` annotation onto the returned column specs.
48 changes: 46 additions & 2 deletions sdk/model/src/render/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import type { LabelDerivationOps } from "./util/label";
import { deriveLabels } from "./util/label";
import type { APColumnSelectorWithSplit } from "./util/split_selectors";
import { patchInSetFilters } from "./util/pframe_upgraders";
import { allPColumnsReady } from "./util/pcolumn_data";
import { allPColumnsReady, getUniquePartitionKeys } from "./util/pcolumn_data";
import type { PColumnDataUniversal } from "./internal";

/**
Expand Down Expand Up @@ -112,6 +112,15 @@ type UniversalPColumnOpts = {
labelOps?: LabelDerivationOps;
dontWaitAllData?: boolean;
exclude?: AnchoredPColumnSelector | AnchoredPColumnSelector[];
/**
* When true, restricts each returned column's visible axis-0 key range to the
* partition keys present in the resolved anchor column(s), by injecting a
* `pl7.app/axisKeys/0` annotation onto each returned column spec.
* Graph-maker respects this annotation when building sample dropdowns via
* `findLabelsForColumnAxis`, so only dataset-scoped samples are shown.
* Returns undefined if any anchor column's data is not yet available.
*/
filterToAnchorPartitions?: boolean;
} & ResolveAnchorsOptions;

type GetOptionsOpts = {
Expand Down Expand Up @@ -205,6 +214,27 @@ export class ResultPool implements ColumnProvider, AxisLabelProvider {
return new AnchoredIdDeriver(resolvedAnchors);
}

/**
* Collects unique axis-0 partition key values from all PlRef anchors.
* Returns undefined if any anchor's data is not yet ready (caller should propagate undefined).
* Returns an empty array when anchorsOrCtx is an AnchoredIdDeriver (no refs to resolve).
*/
private deriveAnchorPartitionKeys(
anchorsOrCtx: AnchoredIdDeriver | Record<string, PColumnSpec | PlRef>,
): (string | number)[] | undefined {
if (anchorsOrCtx instanceof AnchoredIdDeriver) return [];
const allKeys = new Set<string | number>();
for (const value of Object.values(anchorsOrCtx)) {
if (!isPlRef(value)) continue;
const col = this.getPColumnByRef(value);
if (!col?.data) return undefined;
const uniqueKeys = getUniquePartitionKeys(col.data);
if (uniqueKeys === undefined) return undefined;
for (const key of uniqueKeys[0] ?? []) allKeys.add(key);
}
return [...allKeys];
}

/**
* Returns columns that match the provided anchors and selectors. It applies axis filters and label derivation.
*
Expand All @@ -223,13 +253,27 @@ export class ResultPool implements ColumnProvider, AxisLabelProvider {
): PColumn<PColumnDataUniversal>[] | undefined {
const anchorCtx = this.resolveAnchorCtx(anchorsOrCtx);
if (!anchorCtx) return undefined;
return new PColumnCollection()
const columns = new PColumnCollection()
.addColumnProvider(this)
.addAxisLabelProvider(this)
.getColumns(predicateOrSelectors, {
...opts,
anchorCtx,
});
if (!columns || !opts?.filterToAnchorPartitions) return columns;
const allowedKeys = this.deriveAnchorPartitionKeys(anchorsOrCtx);
if (allowedKeys === undefined) return undefined;
const keysJson = JSON.stringify(allowedKeys);
return columns.map((col) => ({
...col,
spec: {
...col.spec,
annotations: {
...col.spec.annotations,
"pl7.app/axisKeys/0": keysJson,
},
},
}));
}

/**
Expand Down
Loading