[master] Fixed #18972 -- Refactored chunking algorithm stealing shameles...

...sly from datagroks code.