flotilla.py ```def stream_plan( self, ...
# daft-dev
c
# flotilla.py
Copy code
def stream_plan(
        self,
        plan: DistributedPhysicalPlan,
        partition_sets: dict[str, PartitionSet[ray.ObjectRef]],
    ) -> Iterator[RayMaterializedResult]:
        plan_id = plan.id()
        
        t=time.time()
        sz=len(cp.dumps(plan))
        print("plan pickle bytes:", sz, "secs:", time.time()-t)
      
        ray.get(self.runner.run_plan.remote(plan, partition_sets))
        while True:
            materialized_result = ray.get(self.runner.get_next_partition.remote(plan_id))
            if materialized_result is None:
                break
            yield materialized_result
During the test, I found that the size of DistributedPhysicalPlan reached 13GB, which is very unreasonable. Moreover, the serialization process took a very long time, nearly 10 minutes. Is there any way to easily analyze what is too big in the DistributedPhysicalPlan Object?
Copy code
plan pickle bytes: 13298686802 secs: 596.3822300434113
c
Are you able to do
df.explain(True)
?
c
@Colin Ho I have ran explain(True), and the result is as follows: At first, I suspected that it was caused by too many scan tasks. However, I printed out the size of a single ScanTask, which was only 0.75MB. There were 8,354 Scantasks, and the total size was about 6G. It doesn't reach 13G. But I'm not sure if there are any other objects that take up a lot of memory.
Copy code
== Unoptimized Logical Plan ==

* Limit: 10
|
* PythonScanOperator: LanceDBScanOperator(/mnt/bos-text/python_merged.lance)
|   File schema = seq_id#Int64, ext#Utf8, file_size_in_byte#Int64,
|     program_lang#Utf8, doc_type#Utf8, text#Utf8, filename#Utf8, repo_name#Utf8,
|     sub_path#Utf8, stars#Utf8, dataset#Utf8, pt#Utf8, tar_path#Utf8, file_path#Utf8
|   Partitioning keys = []
|   Output schema = seq_id#Int64, ext#Utf8, file_size_in_byte#Int64,
|     program_lang#Utf8, doc_type#Utf8, text#Utf8, filename#Utf8, repo_name#Utf8,
|     sub_path#Utf8, stars#Utf8, dataset#Utf8, pt#Utf8, tar_path#Utf8, file_path#Utf8


== Optimized Logical Plan ==

* Limit: 10
|   Stats = { Approx num rows = 0, Approx size bytes = 0 B, Accumulated selectivity
|     = 1.00 }
|
* Num Scan Tasks = 8354
|   File schema = seq_id#Int64, ext#Utf8, file_size_in_byte#Int64,
|     program_lang#Utf8, doc_type#Utf8, text#Utf8, filename#Utf8, repo_name#Utf8,
|     sub_path#Utf8, stars#Utf8, dataset#Utf8, pt#Utf8, tar_path#Utf8, file_path#Utf8
|   Partitioning keys = []
|   Limit pushdown = 10
|   Output schema = seq_id#Int64, ext#Utf8, file_size_in_byte#Int64,
|     program_lang#Utf8, doc_type#Utf8, text#Utf8, filename#Utf8, repo_name#Utf8,
|     sub_path#Utf8, stars#Utf8, dataset#Utf8, pt#Utf8, tar_path#Utf8, file_path#Utf8
|   Stats = { Approx num rows = 0, Approx size bytes = 0 B, Accumulated selectivity
|     = 1.00 }


== Physical Plan ==

* Limit: 10
|
* ScanTaskSource:
|   Num Scan Tasks = 8354
|   Estimated Scan Bytes = 0
|   Pushdowns: {limit: 10}
|   Schema: {seq_id#Int64, ext#Utf8, file_size_in_byte#Int64, program_lang#Utf8,
|     doc_type#Utf8, text#Utf8, filename#Utf8, repo_name#Utf8, sub_path#Utf8,
|     stars#Utf8, dataset#Utf8, pt#Utf8, tar_path#Utf8, file_path#Utf8}
|   Scan Tasks: [
|   {daft.io.lance.lance_scan:_lancedb_table_factory_function}
|   {daft.io.lance.lance_scan:_lancedb_table_factory_function}
|   {daft.io.lance.lance_scan:_lancedb_table_factory_function}
|   ...
|   {daft.io.lance.lance_scan:_lancedb_table_factory_function}
|   {daft.io.lance.lance_scan:_lancedb_table_factory_function}
|   {daft.io.lance.lance_scan:_lancedb_table_factory_function}
|   ]
c
The plan is very simple, so i'm pretty sure the cause is because of the scans. Pickled objects can be larger than their in-memory representation, so maybe that could explain it
However, I printed out the size of a single ScanTask, which was only 0.75MB.
What is the size when you pickle a single scantask?
c
0.75MB
c
One more thing that might help. It is a bit unnecessary to create 8000+ scan tasks for a limit 10. Likely a couple files should have been enough. in
daft/io/lance/lance_scan.py
we don't take the limit into consideration when creating scan tasks now. However, each fragment has
physical_rows
and
num_deletions
properties https://github.com/lancedb/lance/blob/main/python/python/lance/fragment.py#L384, which we can use to correctly get the number of rows from the fragment, given that there are no filters. Looks like this is what happens in
count_rows
as well: https://github.com/lancedb/lance/blob/main/rust/lance/src/dataset/fragment.rs#L1049-L1055 cc @Jay @Cory Grinstead
j
count_rows has been push down lance https://github.com/Eventual-Inc/Daft/pull/4969
“we don’t take the limit into consideration when creating scan tasks now”
If limit is supported, the number of tasks should still be the same, right? It would just stop early?
c
Yes but the problem here is that the number of tasks is very large and it makes the plan too big
i have a prototype of the limit pushdown optimization https://github.com/Eventual-Inc/Daft/pull/5120/files
j
make sense,
However, since Lance also supports filter pushdown, it should also be possible to reduce the number of scan tasks for
count_rows
. Do you need me to supplement the implementation for this?
c
Yeah sure!
j
I’ll start after your PR is merged
c
Here I'm just conducting a simple test using limit api. If I don't use limit api, its execution plan will also be very large.
j
@can cai 这个其实意味着你一个fragment 只有不到1MB, 这个感觉是lance本身写入的不太友好?
This actually means that each of your fragments is less than 1MB. Does this seem like Lance’s own writing (or: storage/writing) behavior is not very optimal?