@@ -178,16 +178,9 @@ func (c *ClickHouseSourceConnector) readRows(ctx context.Context, msgChan chan *
178178
179179 var query string
180180 if c .config .Query != "" {
181- query = c .config .Query
181+ query = c .wrapQueryWithStableOrder ( c . config .Query )
182182 } else {
183- if lastReadID > 0 {
184- query = fmt .Sprintf ("SELECT * FROM %s WHERE id > %d ORDER BY id" , c .config .Table , lastReadID )
185- } else if lastReadTime != nil {
186- query = fmt .Sprintf ("SELECT * FROM %s WHERE created_at > '%s' ORDER BY created_at" ,
187- c .config .Table , lastReadTime .Format ("2006-01-02 15:04:05" ))
188- } else {
189- query = fmt .Sprintf ("SELECT * FROM %s" , c .config .Table )
190- }
183+ query = c .buildTableReadQuery (lastReadID , lastReadTime )
191184 }
192185
193186 c .logger .V (1 ).Info ("Executing ClickHouse query" , "query" , query , "table" , c .config .Table )
@@ -209,16 +202,9 @@ func (c *ClickHouseSourceConnector) readRows(ctx context.Context, msgChan chan *
209202 return fmt .Errorf ("clickhouse columns: %w" , err )
210203 }
211204
212- var idIndex = - 1
213- var createdAtIndex = - 1
214- for i , col := range columns {
215- if col == "id" {
216- idIndex = i
217- }
218- if col == "created_at" {
219- createdAtIndex = i
220- }
221- }
205+ orderByCol := ResolveOrderByColumn (c .config .OrderByColumn )
206+ idIndex := ColumnIndex (columns , orderByCol )
207+ createdAtIndex := ColumnIndex (columns , "created_at" )
222208
223209 var maxReadID int64 = lastReadID
224210 var maxReadTime * time.Time
@@ -278,7 +264,7 @@ func (c *ClickHouseSourceConnector) readRows(ctx context.Context, msgChan chan *
278264 msg := types .NewMessage (jsonData )
279265 msg .Metadata ["table" ] = c .config .Table
280266 if idIndex >= 0 && len (values ) > idIndex {
281- msg . Metadata [ "id" ] = values [idIndex ]
267+ SetSourceRowIDMetadata ( msg , values [idIndex ])
282268 }
283269 // Ack advances checkpoint only after sink successfully writes; prevents data loss on crash
284270 rowID , rowTime := c .extractRowCheckpoint (values , idIndex , createdAtIndex )
@@ -299,6 +285,24 @@ func (c *ClickHouseSourceConnector) readRows(ctx context.Context, msgChan chan *
299285 return nil
300286}
301287
288+ func (c * ClickHouseSourceConnector ) buildTableReadQuery (lastReadID int64 , lastReadTime * time.Time ) string {
289+ table := c .config .Table
290+ col := ResolveOrderByColumn (c .config .OrderByColumn )
291+ if lastReadID > 0 {
292+ return fmt .Sprintf ("SELECT * FROM %s WHERE %s > %d ORDER BY %s" , table , col , lastReadID , col )
293+ }
294+ if lastReadTime != nil {
295+ return fmt .Sprintf ("SELECT * FROM %s WHERE created_at > '%s' ORDER BY created_at, %s" ,
296+ table , lastReadTime .Format ("2006-01-02 15:04:05" ), col )
297+ }
298+ return fmt .Sprintf ("SELECT * FROM %s ORDER BY created_at, %s" , table , col )
299+ }
300+
301+ func (c * ClickHouseSourceConnector ) wrapQueryWithStableOrder (userQuery string ) string {
302+ col := ResolveOrderByColumn (c .config .OrderByColumn )
303+ return WrapQueryStableOrder (userQuery , col )
304+ }
305+
302306// extractRowCheckpoint returns (id, created_at) for checkpoint advancement.
303307func (c * ClickHouseSourceConnector ) extractRowCheckpoint (values []interface {}, idIndex , createdAtIndex int ) (int64 , * time.Time ) {
304308 var rowID int64
0 commit comments