| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
|
| 3 |
import operator |
| 4 |
|
| 5 |
from petl.compat import next, text_type |
| 6 |
from petl.transform.basics import stack |
| 7 |
from petl.transform.joins import keys_from_args |
| 8 |
from petl.util.base import Table, asindices, iterpeek, rowgetter |
| 9 |
from petl.util.lookups import lookup, lookupone |
| 10 |
|
| 11 |
|
| 12 |
def hashjoin(left, right, key=None, lkey=None, rkey=None, cache=True, |
| 13 |
lprefix=None, rprefix=None, missing=None): |
| 14 |
"""Alternative implementation of :func:`petl.transform.joins.join`, where |
| 15 |
the join is executed by constructing an in-memory lookup for the right |
| 16 |
hand table, then iterating over rows from the left hand table. |
| 17 |
|
| 18 |
May be faster and/or more resource efficient where the right table is small |
| 19 |
and the left table is large. |
| 20 |
|
| 21 |
By default data from right hand table is cached to improve performance |
| 22 |
(only available when `key` is given). |
| 23 |
|
| 24 |
Left and right tables with different key fields can be handled via the |
| 25 |
`lkey` and `rkey` arguments. |
| 26 |
|
| 27 |
.. versionchanged:: 1.7.16 |
| 28 |
To ensure correct results for tables with uneven rows, tables will be |
| 29 |
squared up and rows will be filled with the value if `missing` keyword |
| 30 |
argument before joining to ensure correct results. |
| 31 |
|
| 32 |
""" |
| 33 |
|
| 34 |
lkey, rkey = keys_from_args(left, right, key, lkey, rkey) |
| 35 |
return HashJoinView(left, right, lkey=lkey, rkey=rkey, cache=cache, |
| 36 |
lprefix=lprefix, rprefix=rprefix, missing=missing) |
| 37 |
|
| 38 |
|
| 39 |
Table.hashjoin = hashjoin |
| 40 |
|
| 41 |
|
| 42 |
class HashJoinView(Table): |
| 43 |
|
| 44 |
def __init__(self, left, right, lkey, rkey, cache=True, lprefix=None, |
| 45 |
rprefix=None, missing=None): |
| 46 |
self.left = stack(left, missing=missing) |
| 47 |
self.right = stack(right, missing=missing) |
| 48 |
self.lkey = lkey |
| 49 |
self.rkey = rkey |
| 50 |
self.cache = cache |
| 51 |
self.rlookup = None |
| 52 |
self.lprefix = lprefix |
| 53 |
self.rprefix = rprefix |
| 54 |
|
| 55 |
def __iter__(self): |
| 56 |
if not self.cache or self.rlookup is None: |
| 57 |
self.rlookup = lookup(self.right, self.rkey) |
| 58 |
return iterhashjoin(self.left, self.right, self.lkey, self.rkey, |
| 59 |
self.rlookup, self.lprefix, self.rprefix) |
| 60 |
|
| 61 |
|
| 62 |
def iterhashjoin(left, right, lkey, rkey, rlookup, lprefix, rprefix): |
| 63 |
lit = iter(left) |
| 64 |
rit = iter(right) |
| 65 |
|
| 66 |
lhdr = next(lit) |
| 67 |
rhdr = next(rit) |
| 68 |
|
| 69 |
# determine indices of the key fields in left and right tables |
| 70 |
lkind = asindices(lhdr, lkey) |
| 71 |
rkind = asindices(rhdr, rkey) |
| 72 |
|
| 73 |
# construct functions to extract key values from left table |
| 74 |
lgetk = operator.itemgetter(*lkind) |
| 75 |
|
| 76 |
# determine indices of non-key fields in the right table |
| 77 |
# (in the output, we only include key fields from the left table - we |
| 78 |
# don't want to duplicate fields) |
| 79 |
rvind = [i for i in range(len(rhdr)) if i not in rkind] |
| 80 |
rgetv = rowgetter(*rvind) |
| 81 |
|
| 82 |
# determine the output fields |
| 83 |
if lprefix is None: |
| 84 |
outhdr = list(lhdr) |
| 85 |
else: |
| 86 |
outhdr = [(text_type(lprefix) + text_type(f)) |
| 87 |
for f in lhdr] |
| 88 |
if rprefix is None: |
| 89 |
outhdr.extend(rgetv(rhdr)) |
| 90 |
else: |
| 91 |
outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)]) |
| 92 |
yield tuple(outhdr) |
| 93 |
|
| 94 |
# define a function to join rows |
| 95 |
def joinrows(_lrow, _rrows): |
| 96 |
for rrow in _rrows: |
| 97 |
# start with the left row |
| 98 |
_outrow = list(_lrow) |
| 99 |
# extend with non-key values from the right row |
| 100 |
_outrow.extend(rgetv(rrow)) |
| 101 |
yield tuple(_outrow) |
| 102 |
|
| 103 |
for lrow in lit: |
| 104 |
k = lgetk(lrow) |
| 105 |
if k in rlookup: |
| 106 |
rrows = rlookup[k] |
| 107 |
for outrow in joinrows(lrow, rrows): |
| 108 |
yield outrow |
| 109 |
|
| 110 |
|
| 111 |
def hashleftjoin(left, right, key=None, lkey=None, rkey=None, missing=None, |
| 112 |
cache=True, lprefix=None, rprefix=None): |
| 113 |
"""Alternative implementation of :func:`petl.transform.joins.leftjoin`, |
| 114 |
where the join is executed by constructing an in-memory lookup for the |
| 115 |
right hand table, then iterating over rows from the left hand table. |
| 116 |
|
| 117 |
May be faster and/or more resource efficient where the right table is small |
| 118 |
and the left table is large. |
| 119 |
|
| 120 |
By default data from right hand table is cached to improve performance |
| 121 |
(only available when `key` is given). |
| 122 |
|
| 123 |
Left and right tables with different key fields can be handled via the |
| 124 |
`lkey` and `rkey` arguments. |
| 125 |
|
| 126 |
.. versionchanged:: 1.7.16 |
| 127 |
To ensure correct results for tables with uneven rows, tables will be |
| 128 |
squared up before joining to ensure correct results. |
| 129 |
|
| 130 |
""" |
| 131 |
|
| 132 |
lkey, rkey = keys_from_args(left, right, key, lkey, rkey) |
| 133 |
return HashLeftJoinView(left, right, lkey, rkey, missing=missing, |
| 134 |
cache=cache, lprefix=lprefix, rprefix=rprefix) |
| 135 |
|
| 136 |
|
| 137 |
Table.hashleftjoin = hashleftjoin |
| 138 |
|
| 139 |
|
| 140 |
class HashLeftJoinView(Table): |
| 141 |
|
| 142 |
def __init__(self, left, right, lkey, rkey, missing=None, cache=True, |
| 143 |
lprefix=None, rprefix=None): |
| 144 |
self.left = stack(left, missing=missing) |
| 145 |
self.right = stack(right, missing=missing) |
| 146 |
self.lkey = lkey |
| 147 |
self.rkey = rkey |
| 148 |
self.missing = missing |
| 149 |
self.cache = cache |
| 150 |
self.rlookup = None |
| 151 |
self.lprefix = lprefix |
| 152 |
self.rprefix = rprefix |
| 153 |
|
| 154 |
def __iter__(self): |
| 155 |
if not self.cache or self.rlookup is None: |
| 156 |
self.rlookup = lookup(self.right, self.rkey) |
| 157 |
return iterhashleftjoin(self.left, self.right, self.lkey, self.rkey, |
| 158 |
self.missing, self.rlookup, self.lprefix, |
| 159 |
self.rprefix) |
| 160 |
|
| 161 |
|
| 162 |
def iterhashleftjoin(left, right, lkey, rkey, missing, rlookup, lprefix, |
| 163 |
rprefix): |
| 164 |
lit = iter(left) |
| 165 |
rit = iter(right) |
| 166 |
|
| 167 |
lhdr = next(lit) |
| 168 |
rhdr = next(rit) |
| 169 |
|
| 170 |
# determine indices of the key fields in left and right tables |
| 171 |
lkind = asindices(lhdr, lkey) |
| 172 |
rkind = asindices(rhdr, rkey) |
| 173 |
|
| 174 |
# construct functions to extract key values from left table |
| 175 |
lgetk = operator.itemgetter(*lkind) |
| 176 |
|
| 177 |
# determine indices of non-key fields in the right table |
| 178 |
# (in the output, we only include key fields from the left table - we |
| 179 |
# don't want to duplicate fields) |
| 180 |
rvind = [i for i in range(len(rhdr)) if i not in rkind] |
| 181 |
rgetv = rowgetter(*rvind) |
| 182 |
|
| 183 |
# determine the output fields |
| 184 |
if lprefix is None: |
| 185 |
outhdr = list(lhdr) |
| 186 |
else: |
| 187 |
outhdr = [(text_type(lprefix) + text_type(f)) |
| 188 |
for f in lhdr] |
| 189 |
if rprefix is None: |
| 190 |
outhdr.extend(rgetv(rhdr)) |
| 191 |
else: |
| 192 |
outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)]) |
| 193 |
yield tuple(outhdr) |
| 194 |
|
| 195 |
# define a function to join rows |
| 196 |
def joinrows(_lrow, _rrows): |
| 197 |
for rrow in _rrows: |
| 198 |
# start with the left row |
| 199 |
_outrow = list(_lrow) |
| 200 |
# extend with non-key values from the right row |
| 201 |
_outrow.extend(rgetv(rrow)) |
| 202 |
yield tuple(_outrow) |
| 203 |
|
| 204 |
for lrow in lit: |
| 205 |
k = lgetk(lrow) |
| 206 |
if k in rlookup: |
| 207 |
rrows = rlookup[k] |
| 208 |
for outrow in joinrows(lrow, rrows): |
| 209 |
yield outrow |
| 210 |
else: |
| 211 |
outrow = list(lrow) # start with the left row |
| 212 |
# extend with missing values in place of the right row |
| 213 |
outrow.extend([missing] * len(rvind)) |
| 214 |
yield tuple(outrow) |
| 215 |
|
| 216 |
|
| 217 |
def hashrightjoin(left, right, key=None, lkey=None, rkey=None, missing=None, |
| 218 |
cache=True, lprefix=None, rprefix=None): |
| 219 |
"""Alternative implementation of :func:`petl.transform.joins.rightjoin`, |
| 220 |
where the join is executed by constructing an in-memory lookup for the |
| 221 |
left hand table, then iterating over rows from the right hand table. |
| 222 |
|
| 223 |
May be faster and/or more resource efficient where the left table is small |
| 224 |
and the right table is large. |
| 225 |
|
| 226 |
By default data from right hand table is cached to improve performance |
| 227 |
(only available when `key` is given). |
| 228 |
|
| 229 |
Left and right tables with different key fields can be handled via the |
| 230 |
`lkey` and `rkey` arguments. |
| 231 |
|
| 232 |
.. versionchanged:: 1.7.16 |
| 233 |
To ensure correct results for tables with uneven rows, tables will be |
| 234 |
squared up before joining to ensure correct results. |
| 235 |
|
| 236 |
""" |
| 237 |
|
| 238 |
lkey, rkey = keys_from_args(left, right, key, lkey, rkey) |
| 239 |
return HashRightJoinView(left, right, lkey, rkey, missing=missing, |
| 240 |
cache=cache, lprefix=lprefix, rprefix=rprefix) |
| 241 |
|
| 242 |
|
| 243 |
Table.hashrightjoin = hashrightjoin |
| 244 |
|
| 245 |
|
| 246 |
class HashRightJoinView(Table): |
| 247 |
|
| 248 |
def __init__(self, left, right, lkey, rkey, missing=None, cache=True, |
| 249 |
lprefix=None, rprefix=None): |
| 250 |
self.left = stack(left, missing=missing) |
| 251 |
self.right = stack(right, missing=missing) |
| 252 |
self.lkey = lkey |
| 253 |
self.rkey = rkey |
| 254 |
self.missing = missing |
| 255 |
self.cache = cache |
| 256 |
self.llookup = None |
| 257 |
self.lprefix = lprefix |
| 258 |
self.rprefix = rprefix |
| 259 |
|
| 260 |
def __iter__(self): |
| 261 |
if not self.cache or self.llookup is None: |
| 262 |
self.llookup = lookup(self.left, self.lkey) |
| 263 |
return iterhashrightjoin(self.left, self.right, self.lkey, self.rkey, |
| 264 |
self.missing, self.llookup, self.lprefix, |
| 265 |
self.rprefix) |
| 266 |
|
| 267 |
|
| 268 |
def iterhashrightjoin(left, right, lkey, rkey, missing, llookup, lprefix, |
| 269 |
rprefix): |
| 270 |
lit = iter(left) |
| 271 |
rit = iter(right) |
| 272 |
|
| 273 |
lhdr = next(lit) |
| 274 |
rhdr = next(rit) |
| 275 |
|
| 276 |
# determine indices of the key fields in left and right tables |
| 277 |
lkind = asindices(lhdr, lkey) |
| 278 |
rkind = asindices(rhdr, rkey) |
| 279 |
|
| 280 |
# construct functions to extract key values from left table |
| 281 |
rgetk = operator.itemgetter(*rkind) |
| 282 |
|
| 283 |
# determine indices of non-key fields in the right table |
| 284 |
# (in the output, we only include key fields from the left table - we |
| 285 |
# don't want to duplicate fields) |
| 286 |
rvind = [i for i in range(len(rhdr)) if i not in rkind] |
| 287 |
rgetv = rowgetter(*rvind) |
| 288 |
|
| 289 |
# determine the output fields |
| 290 |
if lprefix is None: |
| 291 |
outhdr = list(lhdr) |
| 292 |
else: |
| 293 |
outhdr = [(text_type(lprefix) + text_type(f)) |
| 294 |
for f in lhdr] |
| 295 |
if rprefix is None: |
| 296 |
outhdr.extend(rgetv(rhdr)) |
| 297 |
else: |
| 298 |
outhdr.extend([(text_type(rprefix) + text_type(f)) |
| 299 |
for f in rgetv(rhdr)]) |
| 300 |
yield tuple(outhdr) |
| 301 |
|
| 302 |
# define a function to join rows |
| 303 |
def joinrows(_rrow, _lrows): |
| 304 |
for lrow in _lrows: |
| 305 |
# start with the left row |
| 306 |
_outrow = list(lrow) |
| 307 |
# extend with non-key values from the right row |
| 308 |
_outrow.extend(rgetv(_rrow)) |
| 309 |
yield tuple(_outrow) |
| 310 |
|
| 311 |
for rrow in rit: |
| 312 |
k = rgetk(rrow) |
| 313 |
if k in llookup: |
| 314 |
lrows = llookup[k] |
| 315 |
for outrow in joinrows(rrow, lrows): |
| 316 |
yield outrow |
| 317 |
else: |
| 318 |
# start with missing values in place of the left row |
| 319 |
outrow = [missing] * len(lhdr) |
| 320 |
# set key values |
| 321 |
for li, ri in zip(lkind, rkind): |
| 322 |
outrow[li] = rrow[ri] |
| 323 |
# extend with non-key values from the right row |
| 324 |
outrow.extend(rgetv(rrow)) |
| 325 |
yield tuple(outrow) |
| 326 |
|
| 327 |
|
| 328 |
def hashantijoin(left, right, key=None, lkey=None, rkey=None): |
| 329 |
"""Alternative implementation of :func:`petl.transform.joins.antijoin`, |
| 330 |
where the join is executed by constructing an in-memory set for all keys |
| 331 |
found in the right hand table, then iterating over rows from the left |
| 332 |
hand table. |
| 333 |
|
| 334 |
May be faster and/or more resource efficient where the right table is small |
| 335 |
and the left table is large. |
| 336 |
|
| 337 |
Left and right tables with different key fields can be handled via the |
| 338 |
`lkey` and `rkey` arguments. |
| 339 |
|
| 340 |
""" |
| 341 |
|
| 342 |
lkey, rkey = keys_from_args(left, right, key, lkey, rkey) |
| 343 |
return HashAntiJoinView(left, right, lkey, rkey) |
| 344 |
|
| 345 |
|
| 346 |
Table.hashantijoin = hashantijoin |
| 347 |
|
| 348 |
|
| 349 |
class HashAntiJoinView(Table): |
| 350 |
|
| 351 |
def __init__(self, left, right, lkey, rkey): |
| 352 |
self.left = left |
| 353 |
self.right = right |
| 354 |
self.lkey = lkey |
| 355 |
self.rkey = rkey |
| 356 |
|
| 357 |
def __iter__(self): |
| 358 |
return iterhashantijoin(self.left, self.right, self.lkey, self.rkey) |
| 359 |
|
| 360 |
|
| 361 |
def iterhashantijoin(left, right, lkey, rkey): |
| 362 |
lit = iter(left) |
| 363 |
rit = iter(right) |
| 364 |
|
| 365 |
lhdr = next(lit) |
| 366 |
rhdr = next(rit) |
| 367 |
yield tuple(lhdr) |
| 368 |
|
| 369 |
# determine indices of the key fields in left and right tables |
| 370 |
lkind = asindices(lhdr, lkey) |
| 371 |
rkind = asindices(rhdr, rkey) |
| 372 |
|
| 373 |
# construct functions to extract key values from both tables |
| 374 |
lgetk = operator.itemgetter(*lkind) |
| 375 |
rgetk = operator.itemgetter(*rkind) |
| 376 |
|
| 377 |
rkeys = set() |
| 378 |
for rrow in rit: |
| 379 |
rk = rgetk(rrow) |
| 380 |
rkeys.add(rk) |
| 381 |
|
| 382 |
for lrow in lit: |
| 383 |
lk = lgetk(lrow) |
| 384 |
if lk not in rkeys: |
| 385 |
yield tuple(lrow) |
| 386 |
|
| 387 |
|
| 388 |
def hashlookupjoin(left, right, key=None, lkey=None, rkey=None, missing=None, |
| 389 |
lprefix=None, rprefix=None): |
| 390 |
"""Alternative implementation of :func:`petl.transform.joins.lookupjoin`, |
| 391 |
where the join is executed by constructing an in-memory lookup for the |
| 392 |
right hand table, then iterating over rows from the left hand table. |
| 393 |
|
| 394 |
May be faster and/or more resource efficient where the right table is small |
| 395 |
and the left table is large. |
| 396 |
|
| 397 |
Left and right tables with different key fields can be handled via the |
| 398 |
`lkey` and `rkey` arguments. |
| 399 |
|
| 400 |
.. versionchanged:: 1.7.16 |
| 401 |
To ensure correct results for tables with uneven rows, tables will be |
| 402 |
squared up before joining to ensure correct results. |
| 403 |
|
| 404 |
""" |
| 405 |
|
| 406 |
lkey, rkey = keys_from_args(left, right, key, lkey, rkey) |
| 407 |
return HashLookupJoinView(left, right, lkey, rkey, missing=missing, |
| 408 |
lprefix=lprefix, rprefix=rprefix) |
| 409 |
|
| 410 |
|
| 411 |
Table.hashlookupjoin = hashlookupjoin |
| 412 |
|
| 413 |
|
| 414 |
class HashLookupJoinView(Table): |
| 415 |
|
| 416 |
def __init__(self, left, right, lkey, rkey, missing=None, lprefix=None, |
| 417 |
rprefix=None): |
| 418 |
self.left = stack(left, missing=missing) |
| 419 |
self.right = stack(right, missing=missing) |
| 420 |
self.lkey = lkey |
| 421 |
self.rkey = rkey |
| 422 |
self.missing = missing |
| 423 |
self.lprefix = lprefix |
| 424 |
self.rprefix = rprefix |
| 425 |
|
| 426 |
def __iter__(self): |
| 427 |
return iterhashlookupjoin(self.left, self.right, self.lkey, self.rkey, |
| 428 |
self.missing, self.lprefix, self.rprefix) |
| 429 |
|
| 430 |
|
| 431 |
def iterhashlookupjoin(left, right, lkey, rkey, missing, lprefix, rprefix): |
| 432 |
lit = iter(left) |
| 433 |
lhdr = next(lit) |
| 434 |
|
| 435 |
rhdr, rit = iterpeek(right) # need the whole lot to pass to lookup |
| 436 |
rlookup = lookupone(rit, rkey, strict=False) |
| 437 |
|
| 438 |
# determine indices of the key fields in left and right tables |
| 439 |
lkind = asindices(lhdr, lkey) |
| 440 |
rkind = asindices(rhdr, rkey) |
| 441 |
|
| 442 |
# construct functions to extract key values from left table |
| 443 |
lgetk = operator.itemgetter(*lkind) |
| 444 |
|
| 445 |
# determine indices of non-key fields in the right table |
| 446 |
# (in the output, we only include key fields from the left table - we |
| 447 |
# don't want to duplicate fields) |
| 448 |
rvind = [i for i in range(len(rhdr)) if i not in rkind] |
| 449 |
rgetv = rowgetter(*rvind) |
| 450 |
|
| 451 |
# determine the output fields |
| 452 |
if lprefix is None: |
| 453 |
outhdr = list(lhdr) |
| 454 |
else: |
| 455 |
outhdr = [(text_type(lprefix) + text_type(f)) |
| 456 |
for f in lhdr] |
| 457 |
if rprefix is None: |
| 458 |
outhdr.extend(rgetv(rhdr)) |
| 459 |
else: |
| 460 |
outhdr.extend([(text_type(rprefix) + text_type(f)) |
| 461 |
for f in rgetv(rhdr)]) |
| 462 |
yield tuple(outhdr) |
| 463 |
|
| 464 |
# define a function to join rows |
| 465 |
def joinrows(_lrow, _rrow): |
| 466 |
# start with the left row |
| 467 |
_outrow = list(_lrow) |
| 468 |
# extend with non-key values from the right row |
| 469 |
_outrow.extend(rgetv(_rrow)) |
| 470 |
return tuple(_outrow) |
| 471 |
|
| 472 |
for lrow in lit: |
| 473 |
k = lgetk(lrow) |
| 474 |
if k in rlookup: |
| 475 |
rrow = rlookup[k] |
| 476 |
yield joinrows(lrow, rrow) |
| 477 |
else: |
| 478 |
outrow = list(lrow) # start with the left row |
| 479 |
# extend with missing values in place of the right row |
| 480 |
outrow.extend([missing] * len(rvind)) |
| 481 |
yield tuple(outrow) |