4242
4343import pandas as pd
4444from sqlglot import exp
45+ from sqlglot .executor import execute
4546
4647from sqlmesh .core import constants as c
4748from sqlmesh .core ._typing import NotificationTarget
5556from sqlmesh .core .hooks import hook
5657from sqlmesh .core .loader import Loader , SqlMeshLoader , update_model_schemas
5758from sqlmesh .core .macros import ExecutableOrMacro
58- from sqlmesh .core .model import Model
59+ from sqlmesh .core .model import Model , SqlModel
5960from sqlmesh .core .plan import Plan
6061from sqlmesh .core .scheduler import Scheduler
6162from sqlmesh .core .snapshot import (
@@ -521,7 +522,10 @@ def evaluate(
521522 start: The start of the interval to evaluate.
522523 end: The end of the interval to evaluate.
523524 latest: The latest time used for non incremental datasets.
524- limit: A limit applied to the model, this must be > 0.
525+ limit: A limit applied to the model, this must be > 0. If this argument is omitted
526+ and the model contains a LIMIT clause, then the clause's expression will be used
527+ instead. In any case, the final limit is bounded by the DEFAULT_MAX_LIMIT constant.
528+ Default: DEFAULT_MAX_LIMIT
525529 """
526530 if isinstance (model_or_snapshot , str ):
527531 snapshot = self .snapshots [model_or_snapshot ]
@@ -531,15 +535,20 @@ def evaluate(
531535 snapshot = self .snapshots [model_or_snapshot .name ]
532536
533537 if not limit or limit <= 0 :
534- limit = 1000
538+ limit = c .DEFAULT_MAX_LIMIT
539+ if isinstance (snapshot .model , SqlModel ):
540+ model_limit = snapshot .model .render_query ().args .get ("limit" )
541+
542+ if model_limit :
543+ limit = min (limit , execute (exp .select (model_limit .expression )).rows [0 ][0 ])
535544
536545 df = self .snapshot_evaluator .evaluate (
537546 snapshot ,
538547 start ,
539548 end ,
540549 latest ,
541550 snapshots = self .snapshots ,
542- limit = limit ,
551+ limit = t . cast ( int , limit ) ,
543552 )
544553
545554 if df is None :
0 commit comments