@@ -77,6 +77,63 @@ def split_file_size_rows(df: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
7777 return df [mask ].copy (), df [~ mask ].copy ()
7878
7979
80+ def identity_value (value : Any ) -> Any :
81+ """Normalize missing values so benchmark identities compare reliably."""
82+
83+ return None if pd .isna (value ) else value
84+
85+
86+ def benchmark_identity_rows (df : pd .DataFrame ) -> pd .DataFrame :
87+ """Return timing rows with the identity used to match a PR benchmark."""
88+
89+ _file_size_rows , timing_rows = split_file_size_rows (df )
90+ if timing_rows .empty or "name" not in timing_rows .columns :
91+ return pd .DataFrame (columns = ["commit_id" , "benchmark_identity" ])
92+
93+ timing_rows = timing_rows .copy ()
94+ if "storage" not in timing_rows .columns :
95+ timing_rows ["storage" ] = pd .NA
96+ if "commit_id" not in timing_rows .columns :
97+ timing_rows ["commit_id" ] = pd .NA
98+
99+ timing_rows = extract_dataset_key (timing_rows )
100+ timing_rows ["benchmark_identity" ] = [
101+ tuple (identity_value (row [column ]) for column in ("name" , "storage" , "dataset_key" ))
102+ for _ , row in timing_rows .iterrows ()
103+ ]
104+
105+ return timing_rows [["commit_id" , "benchmark_identity" ]]
106+
107+
108+ def select_latest_baseline_rows (base : pd .DataFrame , pr : pd .DataFrame ) -> pd .DataFrame :
109+ """Select rows from the latest baseline commit containing this benchmark.
110+
111+ The persisted benchmark history is append-only. A row only appears after
112+ that benchmark job uploaded results, so the newest commit with matching row
113+ identities is the latest successful baseline for the benchmark under test.
114+ """
115+
116+ if base .empty or "commit_id" not in base .columns :
117+ return base
118+
119+ commit_ids = base ["commit_id" ].dropna ().unique ()
120+ if len (commit_ids ) <= 1 :
121+ return base
122+
123+ pr_identities = set (benchmark_identity_rows (pr )["benchmark_identity" ])
124+ if not pr_identities :
125+ return base
126+
127+ base_identities = benchmark_identity_rows (base )
128+ matches = base_identities [base_identities ["benchmark_identity" ].isin (pr_identities )]
129+ matches = matches [matches ["commit_id" ].notna ()]
130+ if matches .empty :
131+ raise ValueError ("No baseline rows found for the benchmark under test" )
132+
133+ baseline_commit_id = matches ["commit_id" ].iloc [- 1 ]
134+ return base [base ["commit_id" ] == baseline_commit_id ].copy ()
135+
136+
80137def extract_target_fields (name : str ) -> pd .Series :
81138 """Parse query, engine, and format from the benchmark name."""
82139
@@ -704,6 +761,7 @@ def main() -> None:
704761
705762 base = pd .read_json (sys .argv [1 ], lines = True )
706763 pr = pd .read_json (sys .argv [2 ], lines = True )
764+ base = select_latest_baseline_rows (base , pr )
707765
708766 base_commit_id = set (base ["commit_id" ].unique ())
709767 pr_commit_id = set (pr ["commit_id" ].unique ())
0 commit comments