@@ -612,20 +612,27 @@ def _concurrent_render_context(self) -> t.Iterator[None]:
612612 - Globally patch the SQLGlot dialect so that any date/time nodes are evaluated at the `execution_time` during generation
613613 """
614614 import time_machine
615+ from sqlglot .generator import _DISPATCH_CACHE
615616
616617 lock_ctx : AbstractContextManager = (
617618 self .CONCURRENT_RENDER_LOCK if self .concurrency else nullcontext ()
618619 )
619620 time_ctx : AbstractContextManager = nullcontext ()
620621 dialect_patch_ctx : AbstractContextManager = nullcontext ()
622+ dispatch_patch_ctx : AbstractContextManager = nullcontext ()
621623
622624 if self ._execution_time :
625+ generator_class = self ._test_adapter_dialect .generator_class
623626 time_ctx = time_machine .travel (self ._execution_time , tick = False )
624- dialect_patch_ctx = patch .dict (
625- self ._test_adapter_dialect .generator_class .TRANSFORMS , self ._transforms
626- )
627+ dialect_patch_ctx = patch .dict (generator_class .TRANSFORMS , self ._transforms )
628+
629+ # sqlglot caches a dispatch table per generator class, so we need to patch
630+ # it as well to ensure the overridden transforms are actually used
631+ dispatch = _DISPATCH_CACHE .get (generator_class )
632+ if dispatch is not None :
633+ dispatch_patch_ctx = patch .dict (dispatch , self ._transforms )
627634
628- with lock_ctx , time_ctx , dialect_patch_ctx :
635+ with lock_ctx , time_ctx , dialect_patch_ctx , dispatch_patch_ctx :
629636 yield
630637
631638 def _execute (self , query : exp .Query | str ) -> pd .DataFrame :
0 commit comments