Skip to content

Commit fdc578a

Browse files
committed
Add unit tests for to_proto and from_proto for logical and physical plans
1 parent 05457d4 commit fdc578a

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

python/datafusion/plan.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,17 @@ def display_graphviz(self) -> str:
8585

8686
@staticmethod
8787
def from_proto(ctx: SessionContext, data: bytes) -> LogicalPlan:
88-
"""Create a LogicalPlan from protobuf bytes."""
88+
"""Create a LogicalPlan from protobuf bytes.
89+
90+
Tables created in memory from record batches are currently not supported.
91+
"""
8992
return LogicalPlan(df_internal.LogicalPlan.from_proto(ctx.ctx, data))
9093

9194
def to_proto(self) -> bytes:
92-
"""Convert a LogicalPlan to protobuf bytes."""
95+
"""Convert a LogicalPlan to protobuf bytes.
96+
97+
Tables created in memory from record batches are currently not supported.
98+
"""
9399
return self._raw_plan.to_proto()
94100

95101

@@ -127,9 +133,15 @@ def partition_count(self) -> int:
127133

128134
@staticmethod
129135
def from_proto(ctx: SessionContext, data: bytes) -> ExecutionPlan:
130-
"""Create an ExecutionPlan from protobuf bytes."""
136+
"""Create an ExecutionPlan from protobuf bytes.
137+
138+
Tables created in memory from record batches are currently not supported.
139+
"""
131140
return ExecutionPlan(df_internal.ExecutionPlan.from_proto(ctx.ctx, data))
132141

133142
def to_proto(self) -> bytes:
134-
"""Convert an ExecutionPlan into protobuf bytes."""
143+
"""Convert an ExecutionPlan into protobuf bytes.
144+
145+
Tables created in memory from record batches are currently not supported.
146+
"""
135147
return self._raw_plan.to_proto()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from datafusion import SessionContext, LogicalPlan, ExecutionPlan
19+
import pytest
20+
21+
22+
# Note: We must use CSV because memory tables are currently not supported for
23+
# conversion to/from protobuf.
24+
@pytest.fixture
25+
def df():
26+
ctx = SessionContext()
27+
return ctx.read_csv(path="testing/data/csv/aggregate_test_100.csv").select("c1")
28+
29+
30+
def test_logical_plan_to_proto(ctx, df) -> None:
31+
logical_plan_bytes = df.logical_plan().to_proto()
32+
logical_plan = LogicalPlan.from_proto(ctx, logical_plan_bytes)
33+
34+
df_round_trip = ctx.create_dataframe_from_logical_plan(logical_plan)
35+
36+
assert df.collect() == df_round_trip.collect()
37+
38+
original_execution_plan = df.execution_plan()
39+
execution_plan_bytes = original_execution_plan.to_proto()
40+
execution_plan = ExecutionPlan.from_proto(ctx, execution_plan_bytes)
41+
42+
assert str(original_execution_plan) == str(execution_plan)

0 commit comments

Comments
 (0)