55from itertools import cycle
66from datetime import datetime
77from operator import itemgetter
8-
8+ from itertools import islice
99
1010class TestForeignDataWrapper (ForeignDataWrapper ):
1111
@@ -15,6 +15,12 @@ def __init__(self, options, columns):
1515 super (TestForeignDataWrapper , self ).__init__ (options , columns )
1616 self .columns = columns
1717 self .test_type = options .get ('test_type' , None )
18+ self .canlimit = options .get ('canlimit' , False )
19+ if isinstance (self .canlimit , str ):
20+ self .canlimit = self .canlimit .lower () == 'true'
21+ self .cansort = options .get ('cansort' , True )
22+ if isinstance (self .cansort , str ):
23+ self .cansort = self .cansort .lower () == 'true'
1824 self .test_subtype = options .get ('test_subtype' , None )
1925 self .tx_hook = options .get ('tx_hook' , False )
2026 self ._modify_batch_size = int (options .get ('modify_batch_size' , 1 ))
@@ -79,7 +85,7 @@ def _as_generator(self, quals, columns):
7985 index )
8086 yield line
8187
82- def execute (self , quals , columns , sortkeys = None ):
88+ def execute (self , quals , columns , sortkeys = None , limit = None , offset = None ):
8389 sortkeys = sortkeys or []
8490 log_to_postgres (str (sorted (quals )))
8591 log_to_postgres (str (sorted (columns )))
@@ -99,14 +105,15 @@ def execute(self, quals, columns, sortkeys=None):
99105 k = sortkeys [0 ];
100106 res = self ._as_generator (quals , columns )
101107 if (self .test_type == 'sequence' ):
102- return sorted (res , key = itemgetter (k .attnum - 1 ),
108+ res = sorted (res , key = itemgetter (k .attnum - 1 ),
103109 reverse = k .is_reversed )
104110 else :
105- return sorted (res , key = itemgetter (k .attname ),
111+ res = sorted (res , key = itemgetter (k .attname ),
106112 reverse = k .is_reversed )
107- return self ._as_generator (quals , columns )
113+ return res [offset :offset + limit ] if offset else res [:limit ]
114+ return islice (self ._as_generator (quals , columns ), offset , (offset or 0 ) + limit if limit else None )
108115
109- def explain (self , quals , columns , sortkeys = None , verbose = False ):
116+ def explain (self , quals , columns , sortkeys = None , verbose = False , limit = None , offset = None ):
110117 if self .noisy_explain :
111118 log_to_postgres ("EXPLAIN quals=%r" % (sorted (quals ),))
112119 log_to_postgres ("EXPLAIN columns=%r" % (sorted (columns ),))
@@ -126,8 +133,13 @@ def get_path_keys(self):
126133 return []
127134
128135 def can_sort (self , sortkeys ):
129- # assume sort pushdown ok for all cols, in any order, any collation
130- return sortkeys
136+ # assume sort pushdown ok only for first sort key
137+ if not self .cansort :
138+ return []
139+ return sortkeys [:1 ]
140+
141+ def can_limit (self , limit , offset ):
142+ return self .canlimit
131143
132144 def update (self , rowid , newvalues ):
133145 if self .test_type == 'nowrite' :
0 commit comments