@@ -791,42 +791,62 @@ def query_and_load_streaming(
791791 self .logger .warning (f'Failed to load checkpoint, starting from beginning: { e } ' )
792792
793793 try :
794- # Execute streaming query with Flight SQL
795- # Create a CommandStatementQuery message
796- command_query = FlightSql_pb2 .CommandStatementQuery ()
797- command_query .query = query
798-
799- # Add resume watermark if provided
800- if resume_watermark :
801- # TODO: Add watermark to query metadata when Flight SQL supports it
802- self .logger .info (f'Resuming stream from watermark: { resume_watermark } ' )
803-
804- # Wrap the CommandStatementQuery in an Any type
805- any_command = Any ()
806- any_command .Pack (command_query )
807- cmd = any_command .SerializeToString ()
808-
809- self .logger .info ('Establishing Flight SQL connection...' )
810- flight_descriptor = flight .FlightDescriptor .for_command (cmd )
811- info = self .conn .get_flight_info (flight_descriptor )
812- reader = self .conn .do_get (info .endpoints [0 ].ticket )
813-
814- # Create streaming iterator
815- stream_iterator = StreamingResultIterator (reader )
816- self .logger .info ('Stream connection established, waiting for data...' )
817-
818- # Optionally wrap with reorg detection
819- if with_reorg_detection :
820- stream_iterator = ReorgAwareStream (stream_iterator , resume_watermark = resume_watermark )
821- self .logger .info ('Reorg detection enabled for streaming query' )
822-
823- # Start continuous loading with checkpoint support
824794 with loader_instance :
825- self .logger .info (f'Starting continuous load to { destination } . Press Ctrl+C to stop.' )
826- # Pass connection_name for checkpoint saving
827- yield from loader_instance .load_stream_continuous (
828- stream_iterator , destination , connection_name = connection_name , ** load_config .__dict__
829- )
795+ while True :
796+ # Execute streaming query with Flight SQL
797+ # Create a CommandStatementQuery message
798+ command_query = FlightSql_pb2 .CommandStatementQuery ()
799+ command_query .query = query
800+
801+ # Add resume watermark if provided
802+ if resume_watermark :
803+ # TODO: Add watermark to query metadata when Flight SQL supports it
804+ self .logger .info (f'Resuming stream from watermark: { resume_watermark } ' )
805+
806+ # Wrap the CommandStatementQuery in an Any type
807+ any_command = Any ()
808+ any_command .Pack (command_query )
809+ cmd = any_command .SerializeToString ()
810+
811+ self .logger .info ('Establishing Flight SQL connection...' )
812+ flight_descriptor = flight .FlightDescriptor .for_command (cmd )
813+ info = self .conn .get_flight_info (flight_descriptor )
814+ reader = self .conn .do_get (info .endpoints [0 ].ticket )
815+
816+ # Create streaming iterator
817+ stream_iterator = StreamingResultIterator (reader )
818+ self .logger .info ('Stream connection established, waiting for data...' )
819+
820+ # Optionally wrap with reorg detection
821+ if with_reorg_detection :
822+ stream_iterator = ReorgAwareStream (stream_iterator , resume_watermark = resume_watermark )
823+ self .logger .info ('Reorg detection enabled for streaming query' )
824+
825+ # Start continuous loading with checkpoint support
826+ self .logger .info (f'Starting continuous load to { destination } . Press Ctrl+C to stop.' )
827+
828+ reorg_result = None
829+ # Pass connection_name for checkpoint saving
830+ for result in loader_instance .load_stream_continuous (
831+ stream_iterator , destination , connection_name = connection_name , ** load_config .__dict__
832+ ):
833+ yield result
834+ # Break on reorg to restart stream
835+ if result .is_reorg :
836+ reorg_result = result
837+ break
838+
839+ # Check if we need to restart due to reorg
840+ if reorg_result :
841+ # Close the old stream before restarting
842+ if hasattr (stream_iterator , 'close' ):
843+ stream_iterator .close ()
844+ self .logger .info ('Reorg detected, restarting stream with new resume position...' )
845+ resume_watermark = loader_instance .state_store .get_resume_position (connection_name , destination )
846+ continue
847+
848+ # Normal exit - stream completed
849+ break
830850
831851 except Exception as e :
832852 self .logger .error (f'Streaming query failed: { e } ' )
0 commit comments