Skip to content

feat: track rows processed during model evaluation #5162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

treysp
Copy link
Contributor

@treysp treysp commented Aug 14, 2025

Captures the number of rows processed (and potentially other information) during a model evaluation for display to the user.

Example running sushi on Bigquery (includes bytes processed for each model batch):

Screenshot 2025-08-18 at 4 21 44 PM

Context

  • Python client libraries conformant to the DB-API 2.0 spec provide a cursor.row_count property that contains the number of rows affected by a DML command execution (e.g., INSERT, CTAS)
  • Variations
    • Some clients provide the property, but they don't actually track rows so always return -1
    • Snowflake conforms other than CTAS
    • Bigquery python API necessary to handle all scenarios

Implementation context

  • SQLMesh makes many execute calls that should not be tracked
  • In most cases, SQLMesh DML actions take place via the engine adapter _execute method, so we read the row_count property there immediately after cursor.execute
  • Snapshot evaluation may take place concurrently, where a single snapshot evaluation occurs within one thread

Determining what to track

  • We should only track specific operations, so execute does NOT track rows by default
  • Some engine adapter methods should always track, so they pass track_row_count=True in the execute call within them
  • Other engine methods vary in their need to track so expose a track_row_count arg
    • Example: engine_adapter.insert_append
      • Should almost always be tracked during snapshot evaluation
      • Should never be tracked when called from state sync

Tracker implementation

  • Row count values are collected in the QueryExecutionContext dataclass
    • Accumulates row counts if called multiple times
    • Ignores row counts of -1 (default value when cursor.row_count not populated)
  • Trackers implemented as context manager
  • Engine adapter _execute calls single record_execution method, which determines which tracker is active and should be used (if any)

@treysp treysp changed the title feat: add rows tracking feat: track rows processed during model evaluation Aug 14, 2025
@treysp treysp force-pushed the trey/rows-tracker branch 2 times, most recently from d722c67 to c3a8760 Compare August 14, 2025 20:34
Copy link
Collaborator

@erindru erindru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice start, I think I follow the logic. I've left some comments for your consideration.

I'm not sure about the track_row_count parameter on the engine adapter methods. I'd like to understand more about the undesirable queries that were getting picked up - perhaps that indicates we were being too coarse with the statistics gathering / wrapping in the wrong place

@eakmanrq
Copy link
Contributor

Building on @erindru's feedback, I wonder if in general this tracking logic could get pushed to the SnapshotEvaluator layer. Right now the engine adapter methods return nothing but if they returned row count then it seems like the SnapshotEvaluator could then determine what to do with that information.

I don't think though I deeply understand this problem and this comes from a quick review and my general intuition.

@treysp treysp force-pushed the trey/rows-tracker branch 5 times, most recently from 1e405ce to 9377241 Compare August 15, 2025 23:23
@erindru
Copy link
Collaborator

erindru commented Aug 18, 2025

Right now the engine adapter methods return nothing but if they returned row count then it seems like the SnapshotEvaluator could then determine what to do with that information.

If we pass row counts around through the entire call stack between the scheduler and the engine adapter, I believe it starts getting very messy with the number of method signature changes.

The scheduler is where we want to display the information, but it's typically on a different thread and reasonably far removed from EngineAdapter._execute where the relevant queries are executed. In addition, the EngineAdapter layer is a driver that deliberately has no idea what Snapshot it's executing because that's a higher level concept.

This design that Trey is proposing means that we don't have to change the return type of the engine adapter methods or have to worry about passing things back up the stack / rely on every component in the chain between the Scheduler and EngineAdapter._execute to pass things back correctly.

It essentially uses a combination of thread-local + global state to bypass the call stack. It emits row counts in EngineAdapter._execute, assigns meaning to them in SnapshotEvaluator and allows the information to be queried back in Scheduler so it can be outputted to the console for the user to see - without making every method in between return some kind of Stats object.

imo this is a much simpler / less invasive implementation

@treysp treysp force-pushed the trey/rows-tracker branch from 9377241 to 6957330 Compare August 18, 2025 21:11
@treysp treysp force-pushed the trey/rows-tracker branch from 6957330 to d4d5afa Compare August 18, 2025 22:35
@treysp treysp force-pushed the trey/rows-tracker branch 14 times, most recently from d00e0b9 to a2bdfdc Compare August 20, 2025 23:18
@treysp treysp force-pushed the trey/rows-tracker branch 2 times, most recently from f072873 to 97db434 Compare August 22, 2025 20:58
@treysp treysp force-pushed the trey/rows-tracker branch from 97db434 to b66fc6a Compare August 22, 2025 21:24
@treysp treysp force-pushed the trey/rows-tracker branch from b66fc6a to eebb26e Compare August 22, 2025 21:38
@treysp treysp marked this pull request as ready for review August 22, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants