# Buggy code
def build_tensor_merge_graph(self, tensor_key):
    from ..tensor.expressions.merge.concatenate import TensorConcatenate
    from ..tensor.expressions.datasource import TensorFetchChunk

    tiled_tensor = self._tensor_to_tiled[tensor_key][-1]
    graph = DAG()
    if len(tiled_tensor.chunks) == 1:
        # only one chunk, just trigger fetch
        c = tiled_tensor.chunks[0]
        op = TensorFetchChunk(dtype=c.dtype, to_fetch_key=c.key)
        fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
        graph.add_node(fetch_chunk)
    else:
        fetch_chunks = []
        for c in tiled_tensor.chunks:
            op = TensorFetchChunk(dtype=c.dtype, to_fetch_key=c.key)
            fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
            graph.add_node(fetch_chunk)
            fetch_chunks.append(fetch_chunk)
        chunk = (
            TensorConcatenate(dtype=tiled_tensor.op.dtype)
            .new_chunk(fetch_chunks, tiled_tensor.shape)
            .data
        )
        graph.add_node(chunk)
        [graph.add_edge(fetch_chunk, chunk) for fetch_chunk in fetch_chunks]

    return serialize_graph(graph)


# Traceback (IndexError)
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 716, in gevent._greenlet.Greenlet.run
File "mars/actors/pool/gevent_pool.pyx", line 88, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/pool/gevent_pool.pyx", line 91, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/pool/gevent_pool.pyx", line 102, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/pool/gevent_pool.pyx", line 96, in mars.actors.pool.gevent_pool.ActorExecutionContext.fire_run
File "mars/actors/core.pyx", line 108, in mars.actors.core.FunctionActor.on_receive
File "mars/actors/core.pyx", line 110, in mars.actors.core.FunctionActor.on_receive
File "/Users/wenjun.swj/Code/mars/mars/scheduler/graph.py", line 421, in prepare_graph
for n in tensor_to_tiled[tk][-1].chunks:
IndexError: list index out of range

# Fixed code
def build_tensor_merge_graph(self, tensor_key):
    from ..tensor.expressions.merge.concatenate import TensorConcatenate
    from ..tensor.expressions.datasource import TensorFetchChunk

    tiled_tensor = self._get_tensor_by_key(tensor_key)
    graph = DAG()
    if len(tiled_tensor.chunks) == 1:
        # only one chunk, just trigger fetch
        c = tiled_tensor.chunks[0]
        op = TensorFetchChunk(dtype=c.dtype, to_fetch_key=c.key)
        fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
        graph.add_node(fetch_chunk)
    else:
        fetch_chunks = []
        for c in tiled_tensor.chunks:
            op = TensorFetchChunk(dtype=c.dtype, to_fetch_key=c.key)
            fetch_chunk = op.new_chunk(None, c.shape, index=c.index, _key=c.key).data
            graph.add_node(fetch_chunk)
            fetch_chunks.append(fetch_chunk)
        chunk = (
            TensorConcatenate(dtype=tiled_tensor.op.dtype)
            .new_chunk(fetch_chunks, tiled_tensor.shape)
            .data
        )
        graph.add_node(chunk)
        [graph.add_edge(fetch_chunk, chunk) for fetch_chunk in fetch_chunks]

    return serialize_graph(graph)