# Buggy code
def tile_with_mask(cls, op):
    in_df = op.inputs[0]
    out_df = op.outputs[0]

    out_chunks = []

    if isinstance(op.mask, (SERIES_TYPE, DATAFRAME_TYPE)):
        mask = op.inputs[1]

        if isinstance(op.mask, SERIES_TYPE):
            nsplits, out_shape, df_chunks, mask_chunks = align_dataframe_series(
                in_df, mask, axis="index"
            )
        else:
            nsplits, out_shape, df_chunks, mask_chunks = align_dataframe_dataframe(
                in_df, mask
            )
        out_chunk_indexes = itertools.product(*(range(s) for s in out_shape))

        out_chunks = []
        for i, idx, df_chunk in zip(itertools.count(), out_chunk_indexes, df_chunks):
            if op.mask.ndim == 1:
                mask_chunk = mask_chunks[df_chunk.index[0]]
            else:
                mask_chunk = mask_chunks[i]
            index_value = parse_index(out_df.index_value.to_pandas(), df_chunk)
            out_chunk = (
                op.copy()
                .reset_key()
                .new_chunk(
                    [df_chunk, mask_chunk],
                    index=idx,
                    shape=(np.nan, df_chunk.shape[1]),
                    dtypes=df_chunk.dtypes,
                    index_value=index_value,
                    columns_value=df_chunk.columns_value,
                )
            )
            out_chunks.append(out_chunk)

        nsplits = ((np.nan,) * len(nsplits[0]), nsplits[1])
    else:
        check_chunks_unknown_shape([in_df], TilesError)
        nsplits_acc = np.cumsum((0,) + in_df.nsplits[0])
        for idx in range(in_df.chunk_shape[0]):
            for idxj in range(in_df.chunk_shape[1]):
                in_chunk = in_df.cix[idx, idxj]
                chunk_op = op.copy().reset_key()
                chunk_op._mask = op.mask.iloc[nsplits_acc[idx] : nsplits_acc[idx + 1]]
                out_chunk = chunk_op.new_chunk(
                    [in_chunk],
                    index=in_chunk.index,
                    shape=(np.nan, in_chunk.shape[1]),
                    dtypes=in_chunk.dtypes,
                    index_value=in_df.index_value,
                    columns_value=in_chunk.columns_value,
                )
                out_chunks.append(out_chunk)

    nsplits_on_columns = tuple(c.shape[1] for c in out_chunks if c.index[0] == 0)
    row_chunk_num = len([c.shape[0] for c in out_chunks if c.index[1] == 0])
    nsplits = ((np.nan,) * row_chunk_num, nsplits_on_columns)

    new_op = op.copy()
    return new_op.new_dataframes(
        op.inputs,
        shape=out_df.shape,
        dtypes=out_df.dtypes,
        index_value=out_df.index_value,
        columns_value=out_df.columns_value,
        chunks=out_chunks,
        nsplits=nsplits,
    )


# Traceback (TypeError)
Traceback (most recent call last):
File "/Users/wenjun.swj/miniconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-60fd09690beb>", line 1, in <module>
df[df[0] > 0.5].execute()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 643, in execute
return run()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 639, in run
self.data.execute(session, **kw)
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 379, in execute
return run()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 374, in run
session.run(self, **kw)
File "/Users/wenjun.swj/Code/mars/mars/session.py", line 505, in run
result = self._sess.run(*tileables, **kw)
File "/Users/wenjun.swj/Code/mars/mars/session.py", line 111, in run
res = self._executor.execute_tileables(tileables, **kw)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 451, in _inner
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/executor.py", line 860, in execute_tileables
chunk_graph = chunk_graph_builder.build(
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 451, in _inner
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 347, in build
chunk_graph = super().build(
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 451, in _inner
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 262, in build
self._on_tile_failure(tileable_data.op, exc_info)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 301, in inner
raise exc_info[1].with_traceback(exc_info[2]) from None
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 242, in build
tiled = self._tile(tileable_data, tileable_graph)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 337, in _tile
return super()._tile(tileable_data, tileable_graph)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 201, in _tile
tds[0]._inplace_tile()
File "/Users/wenjun.swj/Code/mars/mars/core.py", line 168, in _inplace_tile
return handler.inplace_tile(self)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 136, in inplace_tile
dispatched = self.dispatch(to_tile.op)
File "/Users/wenjun.swj/Code/mars/mars/utils.py", line 451, in _inner
return func(*args, **kwargs)
File "/Users/wenjun.swj/Code/mars/mars/tiles.py", line 119, in dispatch
tiled = op_cls.tile(op)
File "/Users/wenjun.swj/Code/mars/mars/dataframe/indexing/getitem.py", line 273, in tile
return cls.tile_with_mask(op)
File "/Users/wenjun.swj/Code/mars/mars/dataframe/indexing/getitem.py", line 287, in tile_with_mask
align_dataframe_series(in_df, mask, axis='index')
File "/Users/wenjun.swj/Code/mars/mars/dataframe/align.py", line 713, in align_dataframe_series
index_splits, index_nsplits = _calc_axis_splits(left.index_value, right.index_value,
File "/Users/wenjun.swj/Code/mars/mars/dataframe/align.py", line 490, in _calc_axis_splits
right_splits = left_splits = [[c] for c in left_chunk_index_min_max]
TypeError: 'NoneType' object is not iterable

# Fixed code
def tile_with_mask(cls, op):
    in_df = op.inputs[0]
    out_df = op.outputs[0]

    out_chunks = []

    if isinstance(op.mask, (SERIES_TYPE, DATAFRAME_TYPE, TENSOR_TYPE)):
        mask = op.inputs[1]

        if isinstance(op.mask, SERIES_TYPE):
            nsplits, out_shape, df_chunks, mask_chunks = align_dataframe_series(
                in_df, mask, axis="index"
            )
        elif isinstance(op.mask, DATAFRAME_TYPE):
            nsplits, out_shape, df_chunks, mask_chunks = align_dataframe_dataframe(
                in_df, mask
            )
        else:
            # tensor
            nsplits = in_df.nsplits
            mask = mask.rechunk(nsplits[: mask.ndim])._inplace_tile()
            out_shape = in_df.chunk_shape
            df_chunks = in_df.chunks
            mask_chunks = mask.chunks
        out_chunk_indexes = itertools.product(*(range(s) for s in out_shape))

        out_chunks = []
        for i, idx, df_chunk in zip(itertools.count(), out_chunk_indexes, df_chunks):
            if op.mask.ndim == 1:
                mask_chunk = mask_chunks[df_chunk.index[0]]
            else:
                mask_chunk = mask_chunks[i]
            index_value = parse_index(out_df.index_value.to_pandas(), df_chunk)
            out_chunk = (
                op.copy()
                .reset_key()
                .new_chunk(
                    [df_chunk, mask_chunk],
                    index=idx,
                    shape=(np.nan, df_chunk.shape[1]),
                    dtypes=df_chunk.dtypes,
                    index_value=index_value,
                    columns_value=df_chunk.columns_value,
                )
            )
            out_chunks.append(out_chunk)
    else:
        check_chunks_unknown_shape([in_df], TilesError)
        nsplits_acc = np.cumsum((0,) + in_df.nsplits[0])
        for idx in range(in_df.chunk_shape[0]):
            for idxj in range(in_df.chunk_shape[1]):
                in_chunk = in_df.cix[idx, idxj]
                chunk_op = op.copy().reset_key()
                chunk_op._mask = op.mask.iloc[nsplits_acc[idx] : nsplits_acc[idx + 1]]
                out_chunk = chunk_op.new_chunk(
                    [in_chunk],
                    index=in_chunk.index,
                    shape=(np.nan, in_chunk.shape[1]),
                    dtypes=in_chunk.dtypes,
                    index_value=in_df.index_value,
                    columns_value=in_chunk.columns_value,
                )
                out_chunks.append(out_chunk)

    nsplits_on_columns = tuple(c.shape[1] for c in out_chunks if c.index[0] == 0)
    row_chunk_num = len([c.shape[0] for c in out_chunks if c.index[1] == 0])
    nsplits = ((np.nan,) * row_chunk_num, nsplits_on_columns)

    new_op = op.copy()
    return new_op.new_dataframes(
        op.inputs,
        shape=out_df.shape,
        dtypes=out_df.dtypes,
        index_value=out_df.index_value,
        columns_value=out_df.columns_value,
        chunks=out_chunks,
        nsplits=nsplits,
    )