-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathvalues.py
More file actions
63 lines (51 loc) · 2.1 KB
/
Copy pathvalues.py
File metadata and controls
63 lines (51 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import TYPE_CHECKING
import dask.dataframe as dd
import pandas as pd
from dask_sql.datacontainer import ColumnContainer, DataContainer
from dask_sql.physical.rel.base import BaseRelPlugin
from dask_sql.physical.rex import RexConverter
if TYPE_CHECKING:
import dask_sql
from dask_planner.rust import LogicalPlan
class DaskValuesPlugin(BaseRelPlugin):
"""
A DaskValue is a table just consisting of
raw values (nothing database-dependent).
For example
SELECT 1 + 1;
We generate a pandas dataframe and a dask
dataframe out of it directly here.
We assume that this will only ever be used for small
data samples.
"""
class_name = "Values"
def convert(self, rel: "LogicalPlan", context: "dask_sql.Context") -> DataContainer:
# There should not be any input. This is the first step.
self.assert_inputs(rel, 0)
rex_expression_rows = rel.values().getValues()
rows = []
for rex_expression_row in rex_expression_rows:
# We convert each of the cells in the row
# using a RexConverter.
# As we do not have any information on the
# column headers, we just name them with
# their index.
rows.append(
{
str(i): RexConverter.convert(rel, rex_cell, None, context=context)
for i, rex_cell in enumerate(rex_expression_row)
}
)
# TODO: we explicitely reference pandas and dask here -> might we worth making this more general
# We assume here that when using the values plan, the resulting dataframe will be quite small
if rows:
df = pd.DataFrame(rows)
else:
field_names = [str(x) for x in rel.getRowType().getFieldNames()]
df = pd.DataFrame(columns=field_names)
df = dd.from_pandas(df, npartitions=1)
cc = ColumnContainer(df.columns)
cc = self.fix_column_to_row_type(cc, rel.getRowType())
dc = DataContainer(df, cc)
dc = self.fix_dtype_to_row_type(dc, rel.getRowType())
return dc