Spaces:
Sleeping
Sleeping
Update SQL
Browse files
SQL
CHANGED
|
@@ -1,318 +1,321 @@
|
|
| 1 |
-
Card Lab
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Rip out the current Card Lab selector logic and replace it with deterministic ID-based selector builders.
|
| 2 |
+
|
| 3 |
+
Do not patch the existing logic.
|
| 4 |
+
Do not preserve batter_count > pitcher_count.
|
| 5 |
+
Do not preserve tuple selectbox options.
|
| 6 |
+
Do not preserve any format_func-based search behavior.
|
| 7 |
+
|
| 8 |
+
Implement the following exact architecture.
|
| 9 |
+
|
| 10 |
+
==================================================
|
| 11 |
+
1) REPLACE get_card_lab_pitchers() COMPLETELY
|
| 12 |
+
==================================================
|
| 13 |
+
|
| 14 |
+
Goal:
|
| 15 |
+
- one row per pitcher ID
|
| 16 |
+
- stable name resolution
|
| 17 |
+
- no duplicates ever
|
| 18 |
+
|
| 19 |
+
Function contract:
|
| 20 |
+
- input: conn, year=None, name_map=None
|
| 21 |
+
- output: list[tuple[int, str]]
|
| 22 |
+
- output format: [(pitcher_id, normalized_display_name), ...]
|
| 23 |
+
|
| 24 |
+
Implementation:
|
| 25 |
+
|
| 26 |
+
def get_card_lab_pitchers(conn, year=None, name_map=None):
|
| 27 |
+
params = {}
|
| 28 |
+
where = "WHERE pitcher IS NOT NULL"
|
| 29 |
+
if year is not None:
|
| 30 |
+
where += " AND source_season = :year"
|
| 31 |
+
params["year"] = int(year)
|
| 32 |
+
|
| 33 |
+
rows = conn.execute(
|
| 34 |
+
text(f"""
|
| 35 |
+
SELECT
|
| 36 |
+
p.pitcher_id,
|
| 37 |
+
COALESCE(seed.pitcher_name, pife.pitcher_name, api_name.player_name) AS raw_name
|
| 38 |
+
FROM (
|
| 39 |
+
SELECT DISTINCT pitcher AS pitcher_id
|
| 40 |
+
FROM statcast_event_core
|
| 41 |
+
{where}
|
| 42 |
+
) p
|
| 43 |
+
LEFT JOIN (
|
| 44 |
+
SELECT pitcher_id, MAX(pitcher_name) AS pitcher_name
|
| 45 |
+
FROM pitcher_inning_first_seed_events
|
| 46 |
+
GROUP BY pitcher_id
|
| 47 |
+
) pife
|
| 48 |
+
ON pife.pitcher_id = p.pitcher_id
|
| 49 |
+
LEFT JOIN (
|
| 50 |
+
SELECT pitcher AS pitcher_id, MAX(player_name) AS pitcher_name
|
| 51 |
+
FROM statcast_event_core
|
| 52 |
+
WHERE pitcher IS NOT NULL AND player_name IS NOT NULL
|
| 53 |
+
GROUP BY pitcher
|
| 54 |
+
) seed
|
| 55 |
+
ON seed.pitcher_id = p.pitcher_id
|
| 56 |
+
LEFT JOIN (
|
| 57 |
+
SELECT batter AS player_id, MAX(player_name) AS player_name
|
| 58 |
+
FROM statcast_event_core
|
| 59 |
+
WHERE batter IS NOT NULL AND player_name IS NOT NULL
|
| 60 |
+
GROUP BY batter
|
| 61 |
+
) api_name
|
| 62 |
+
ON api_name.player_id = p.pitcher_id
|
| 63 |
+
"""),
|
| 64 |
+
params,
|
| 65 |
+
).fetchall()
|
| 66 |
+
|
| 67 |
+
pitcher_map = {}
|
| 68 |
+
|
| 69 |
+
for pid, raw_name in rows:
|
| 70 |
+
if pid is None:
|
| 71 |
+
continue
|
| 72 |
+
if pid in pitcher_map:
|
| 73 |
+
continue
|
| 74 |
+
|
| 75 |
+
if raw_name and str(raw_name).strip() and not str(raw_name).startswith("Pitcher "):
|
| 76 |
+
resolved = str(raw_name).strip()
|
| 77 |
+
elif name_map and pid in name_map:
|
| 78 |
+
resolved = str(name_map[pid]).strip()
|
| 79 |
+
else:
|
| 80 |
+
resolved = f"Pitcher {pid}"
|
| 81 |
+
|
| 82 |
+
pitcher_map[int(pid)] = normalize_name(resolved)
|
| 83 |
+
|
| 84 |
+
pitchers = [(pid, name) for pid, name in pitcher_map.items()]
|
| 85 |
+
pitchers.sort(key=lambda t: t[1].lower())
|
| 86 |
+
return pitchers
|
| 87 |
+
|
| 88 |
+
Rules:
|
| 89 |
+
- dedupe ONLY by pitcher_id
|
| 90 |
+
- never dedupe by display name first
|
| 91 |
+
- normalize only after resolution
|
| 92 |
+
- if two different pitcher IDs normalize to same display name, append " ({id})" to the later one
|
| 93 |
+
|
| 94 |
+
Add collision handling after pitcher_map is built:
|
| 95 |
+
- build final_display_to_id
|
| 96 |
+
- if normalized name already exists for a different ID, rename second one as "{name} ({id})"
|
| 97 |
+
|
| 98 |
+
==================================================
|
| 99 |
+
2) REPLACE get_card_lab_hitters() COMPLETELY
|
| 100 |
+
==================================================
|
| 101 |
+
|
| 102 |
+
Goal:
|
| 103 |
+
- hitters only
|
| 104 |
+
- no primary pitchers
|
| 105 |
+
- deterministic rule
|
| 106 |
+
|
| 107 |
+
Use a hard pitcher exclusion set.
|
| 108 |
+
|
| 109 |
+
Constants:
|
| 110 |
+
- _HITTER_MIN_PITCHES = keep existing hitter minimum if one already exists
|
| 111 |
+
- _PITCHER_EXCL_THRESHOLD = 100
|
| 112 |
+
|
| 113 |
+
Function contract:
|
| 114 |
+
- input: conn, year=None
|
| 115 |
+
- output: list[str]
|
| 116 |
+
- output format: raw DB names, sorted unique
|
| 117 |
+
|
| 118 |
+
Implementation:
|
| 119 |
+
|
| 120 |
+
def get_card_lab_hitters(conn, year=None):
|
| 121 |
+
params = {}
|
| 122 |
+
hitter_where = "WHERE batter IS NOT NULL AND player_name IS NOT NULL"
|
| 123 |
+
pitcher_where = "WHERE pitcher IS NOT NULL"
|
| 124 |
+
|
| 125 |
+
if year is not None:
|
| 126 |
+
hitter_where += " AND source_season = :year"
|
| 127 |
+
pitcher_where += " AND source_season = :year"
|
| 128 |
+
params["year"] = int(year)
|
| 129 |
+
|
| 130 |
+
excl_rows = conn.execute(
|
| 131 |
+
text(f"""
|
| 132 |
+
SELECT pitcher
|
| 133 |
+
FROM statcast_event_core
|
| 134 |
+
{pitcher_where}
|
| 135 |
+
GROUP BY pitcher
|
| 136 |
+
HAVING COUNT(*) >= :pitcher_excl_threshold
|
| 137 |
+
"""),
|
| 138 |
+
{**params, "pitcher_excl_threshold": 100},
|
| 139 |
+
).fetchall()
|
| 140 |
+
|
| 141 |
+
pitcher_exclusion_ids = {int(row[0]) for row in excl_rows if row[0] is not None}
|
| 142 |
+
|
| 143 |
+
batter_rows = conn.execute(
|
| 144 |
+
text(f"""
|
| 145 |
+
SELECT
|
| 146 |
+
batter,
|
| 147 |
+
MAX(player_name) AS player_name,
|
| 148 |
+
COUNT(*) AS batter_count
|
| 149 |
+
FROM statcast_event_core
|
| 150 |
+
{hitter_where}
|
| 151 |
+
GROUP BY batter
|
| 152 |
+
"""),
|
| 153 |
+
params,
|
| 154 |
+
).fetchall()
|
| 155 |
+
|
| 156 |
+
hitter_map = {}
|
| 157 |
+
|
| 158 |
+
for batter_id, player_name, batter_count in batter_rows:
|
| 159 |
+
if batter_id is None:
|
| 160 |
+
continue
|
| 161 |
+
if batter_count is None or int(batter_count) < _HITTER_MIN_PITCHES:
|
| 162 |
+
continue
|
| 163 |
+
if int(batter_id) in pitcher_exclusion_ids:
|
| 164 |
+
continue
|
| 165 |
+
if not player_name:
|
| 166 |
+
continue
|
| 167 |
+
|
| 168 |
+
hitter_map[int(batter_id)] = str(player_name).strip()
|
| 169 |
+
|
| 170 |
+
names = list(hitter_map.values())
|
| 171 |
+
names = sorted({normalize_name(n) for n in names}, key=str.lower)
|
| 172 |
+
return names
|
| 173 |
+
|
| 174 |
+
Rules:
|
| 175 |
+
- hitter exclusion is ID-based only
|
| 176 |
+
- do not compare batter_count to pitcher_count
|
| 177 |
+
- do not reuse pitcher selector outputs
|
| 178 |
+
- do not allow any ID in pitcher_exclusion_ids into hitter results
|
| 179 |
+
|
| 180 |
+
==================================================
|
| 181 |
+
3) DISABLE CACHING TEMPORARILY
|
| 182 |
+
==================================================
|
| 183 |
+
|
| 184 |
+
Temporarily remove/comment out all @st.cache_data decorators related to:
|
| 185 |
+
- _cached_pitchers
|
| 186 |
+
- _cached_hitters
|
| 187 |
+
- _cached_player_name_map
|
| 188 |
+
- _cached_player_image
|
| 189 |
+
|
| 190 |
+
Do not leave caches on during validation.
|
| 191 |
+
|
| 192 |
+
==================================================
|
| 193 |
+
4) REPLACE PAGE SELECTOR LOGIC
|
| 194 |
+
==================================================
|
| 195 |
+
|
| 196 |
+
Do not use tuple options in Streamlit selectboxes.
|
| 197 |
+
Do not use format_func.
|
| 198 |
+
|
| 199 |
+
Pitcher branch:
|
| 200 |
+
|
| 201 |
+
pitchers = get_card_lab_pitchers(conn, year=year if mode_key == "season" else None, name_map=name_map)
|
| 202 |
+
pitcher_display_to_id = {}
|
| 203 |
+
for pid, display in pitchers:
|
| 204 |
+
final_display = display
|
| 205 |
+
if final_display in pitcher_display_to_id and pitcher_display_to_id[final_display] != pid:
|
| 206 |
+
final_display = f"{display} ({pid})"
|
| 207 |
+
pitcher_display_to_id[final_display] = pid
|
| 208 |
+
|
| 209 |
+
pitcher_display_names = sorted(pitcher_display_to_id.keys(), key=str.lower)
|
| 210 |
+
|
| 211 |
+
selected_pitcher_name = st.selectbox(
|
| 212 |
+
"Player",
|
| 213 |
+
pitcher_display_names,
|
| 214 |
+
key="cl_player_pitcher",
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
pitcher_id = pitcher_display_to_id[selected_pitcher_name]
|
| 218 |
+
player_name = selected_pitcher_name
|
| 219 |
+
|
| 220 |
+
Hitter branch:
|
| 221 |
+
|
| 222 |
+
hitter_names = get_card_lab_hitters(conn, year=year if mode_key == "season" else None)
|
| 223 |
+
hitter_display_names = sorted({normalize_name(n) for n in hitter_names}, key=str.lower)
|
| 224 |
+
|
| 225 |
+
selected_hitter_name = st.selectbox(
|
| 226 |
+
"Player",
|
| 227 |
+
hitter_display_names,
|
| 228 |
+
key="cl_player_hitter",
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
player_name = selected_hitter_name
|
| 232 |
+
|
| 233 |
+
Rules:
|
| 234 |
+
- pitcher tab uses pitcher builder only
|
| 235 |
+
- hitter tab uses hitter builder only
|
| 236 |
+
- do not reuse shared variable names like "players" across branches
|
| 237 |
+
- use separate variables:
|
| 238 |
+
- pitchers
|
| 239 |
+
- pitcher_display_names
|
| 240 |
+
- hitter_names
|
| 241 |
+
- hitter_display_names
|
| 242 |
+
|
| 243 |
+
==================================================
|
| 244 |
+
5) ADD HARD VALIDATION
|
| 245 |
+
==================================================
|
| 246 |
+
|
| 247 |
+
Immediately after building both final lists, validate:
|
| 248 |
+
|
| 249 |
+
pitcher_names_set = {name for _, name in get_card_lab_pitchers(conn, year=year if mode_key == "season" else None, name_map=name_map)}
|
| 250 |
+
hitter_names_set = set(get_card_lab_hitters(conn, year=year if mode_key == "season" else None))
|
| 251 |
+
|
| 252 |
+
overlap = pitcher_names_set & hitter_names_set
|
| 253 |
+
assert len(overlap) == 0, f"CRITICAL: overlap between hitter and pitcher selectors: {list(sorted(overlap))[:10]}"
|
| 254 |
+
|
| 255 |
+
For debug UI:
|
| 256 |
+
|
| 257 |
+
Pitcher mode debug must show:
|
| 258 |
+
- total raw pitcher rows
|
| 259 |
+
- total final unique pitchers
|
| 260 |
+
- first 20 pitcher names
|
| 261 |
+
- whether Tarik Skubal exists
|
| 262 |
+
- whether Sonny Gray exists
|
| 263 |
+
- count of names starting with "Pitcher "
|
| 264 |
+
|
| 265 |
+
Hitter mode debug must show:
|
| 266 |
+
- total raw hitter rows
|
| 267 |
+
- total final unique hitters
|
| 268 |
+
- first 20 hitter names
|
| 269 |
+
- whether Freddy Peralta exists
|
| 270 |
+
- whether Tarik Skubal exists
|
| 271 |
+
- whether Gerrit Cole exists
|
| 272 |
+
- whether Paul Skenes exists
|
| 273 |
+
|
| 274 |
+
==================================================
|
| 275 |
+
6) IMAGE SYSTEM
|
| 276 |
+
==================================================
|
| 277 |
+
|
| 278 |
+
Keep current image hierarchy:
|
| 279 |
+
- local Kasper asset first
|
| 280 |
+
- MLB fallback second
|
| 281 |
+
- silhouette last
|
| 282 |
+
|
| 283 |
+
Do not change renderer layout in this pass.
|
| 284 |
+
Do not add uploader back.
|
| 285 |
+
Do not touch visual styling in this pass.
|
| 286 |
+
|
| 287 |
+
==================================================
|
| 288 |
+
7) SUCCESS CRITERIA
|
| 289 |
+
==================================================
|
| 290 |
+
|
| 291 |
+
This pass is not done until ALL are true:
|
| 292 |
+
|
| 293 |
+
- Tarik Skubal appears in pitcher selector
|
| 294 |
+
- searching "Skub" finds Tarik Skubal
|
| 295 |
+
- Sonny Gray appears in pitcher selector
|
| 296 |
+
- Paul Skenes appears exactly once
|
| 297 |
+
- Aaron Nola appears exactly once
|
| 298 |
+
- Freddy Peralta does not appear in hitter selector
|
| 299 |
+
- Tarik Skubal does not appear in hitter selector
|
| 300 |
+
- Gerrit Cole does not appear in hitter selector
|
| 301 |
+
- Paul Skenes does not appear in hitter selector
|
| 302 |
+
- there is zero overlap between final hitter and pitcher selector names
|
| 303 |
+
|
| 304 |
+
==================================================
|
| 305 |
+
8) IMPORTANT CONSTRAINTS
|
| 306 |
+
==================================================
|
| 307 |
+
|
| 308 |
+
Do NOT:
|
| 309 |
+
- reintroduce batter_count > pitcher_count
|
| 310 |
+
- dedupe by display name before ID
|
| 311 |
+
- use tuple selectbox options
|
| 312 |
+
- use format_func
|
| 313 |
+
- leave cache enabled during validation
|
| 314 |
+
- mix selector repair with visual renderer redesign
|
| 315 |
+
|
| 316 |
+
This is a selector/data repair pass only.
|
| 317 |
+
|
| 318 |
+
At the end, report exactly:
|
| 319 |
+
- what caused duplicate pitchers
|
| 320 |
+
- what caused pitchers in hitter list
|
| 321 |
+
- what was changed to make both impossible
|