@@ -95,7 +95,8 @@ GlobalOrderWriter::GlobalOrderWriter(
9595 , processed_conditions_(processed_conditions)
9696 , fragment_size_(fragment_size)
9797 , current_fragment_size_(0 )
98- , rows_written_(0 ) {
98+ , rows_written_(0 )
99+ , start_(0 ) {
99100 // Check the layout is global order.
100101 if (layout_ != Layout::GLOBAL_ORDER) {
101102 throw GlobalOrderWriterException (
@@ -1464,16 +1465,6 @@ NDRange GlobalOrderWriter::ndranges_after_split(uint64_t num) {
14641465
14651466 // Calculate how many tiles each row can hold
14661467 uint64_t tiles_per_row = num_tiles_per_row (domain);
1467- auto dim_num = domain.dim_num ();
1468- auto dim{domain.dimension_ptr (0 )};
1469-
1470- auto l = [&](auto T) {
1471- return static_cast <uint64_t >(dim->tile_extent ().rvalue_as <decltype (T)>());
1472- };
1473-
1474- uint64_t tile_extent = apply_with_type (l, dim->type ());
1475- NDRange nd;
1476- nd.reserve (dim_num);
14771468
14781469 if (num % tiles_per_row != 0 ) {
14791470 throw GlobalOrderWriterException (
@@ -1485,13 +1476,33 @@ NDRange GlobalOrderWriter::ndranges_after_split(uint64_t num) {
14851476 // Calculate how many rows we will write in the current fragment
14861477 uint64_t rows_of_tiles_to_write = num / tiles_per_row;
14871478
1488- // Create the range for the index dim (first).
1489- int start = rows_written_ * tile_extent;
1490- int end = start + (rows_of_tiles_to_write * tile_extent) - 1 ;
1491- Range range (&start, &end, sizeof (int ));
1479+ // Create NDRange object and reserve for dims
1480+ auto dim_num = domain.dim_num ();
1481+ NDRange nd;
1482+ nd.reserve (dim_num);
1483+
1484+ // Calculate the range for the index dim (first).
1485+ auto dim{domain.dimension_ptr (0 )};
1486+ auto dim_dom = dim->domain ();
1487+ auto l = [&](auto T) {
1488+ return static_cast <uint64_t >(dim->tile_extent ().rvalue_as <decltype (T)>());
1489+ };
1490+ uint64_t tile_extent = apply_with_type (l, dim->type ());
1491+
1492+ // Calculate start and end
1493+ if (rows_written_ == 0 ) {
1494+ // It means that the start has not been set yet. Set it to the minimum value
1495+ // of the expanded domain for that dim
1496+ auto dim_dom_data = (const uint64_t *)dim_dom.data ();
1497+ start_ = dim_dom_data[0 ];
1498+ }
1499+ uint64_t end = start_ + (rows_of_tiles_to_write * tile_extent) - 1 ;
1500+
1501+ // Add range
1502+ Range range (&start_, &end, sizeof (int ));
14921503 nd.emplace_back (range);
14931504
1494- // Use the domain as ranges for the rest of the dims
1505+ // For the rest of the dims, use their domains as ranges. No split there.
14951506 for (unsigned d = 1 ; d < dim_num; ++d) {
14961507 // begin from second dim
14971508 auto dim{array_schema_.dimension_ptr (d)};
@@ -1501,6 +1512,7 @@ NDRange GlobalOrderWriter::ndranges_after_split(uint64_t num) {
15011512
15021513 // add rows written to the cache
15031514 rows_written_ += rows_of_tiles_to_write;
1515+ start_ = end + 1 ;
15041516
15051517 return nd;
15061518}
0 commit comments