Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cudf as a physical execution engine #205

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion conda/environments/datafusion-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies:
- pytest
- toml
- importlib_metadata
- python>=3.7,<3.11
- python>=3.10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do want to point out that cudf has a minimum python version of 3.10. That was the reason for this change. Hopefully that isn't a problem for anyone.

# Packages useful for building distributions and releasing
- mamba
- conda-build
Expand All @@ -38,4 +38,7 @@ dependencies:
- pydata-sphinx-theme==0.8.0
- myst-parser
- jinja2
# GPU packages
- cudf
- cudatoolkit=11.8
name: datafusion-dev
62 changes: 62 additions & 0 deletions datafusion/cudf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import cudf
import datafusion
from datafusion.expr import Projection, TableScan, Column


class SessionContext:
def __init__(self):
self.datafusion_ctx = datafusion.SessionContext()
self.parquet_tables = {}

def register_parquet(self, name, path):
self.parquet_tables[name] = path
self.datafusion_ctx.register_parquet(name, path)

def to_cudf_expr(self, expr):

# get Python wrapper for logical expression
expr = expr.to_variant()

if isinstance(expr, Column):
return expr.name()
else:
raise Exception("unsupported expression: {}".format(expr))

def to_cudf_df(self, plan):
# recurse down first to translate inputs into pandas data frames
inputs = [self.to_cudf_df(x) for x in plan.inputs()]

# get Python wrapper for logical operator node
node = plan.to_variant()

if isinstance(node, Projection):
args = [self.to_cudf_expr(expr) for expr in node.projections()]
return inputs[0][args]
elif isinstance(node, TableScan):
return cudf.read_parquet(self.parquet_tables[node.table_name()])
else:
raise Exception(
"unsupported logical operator: {}".format(type(node))
)

def sql(self, sql):
datafusion_df = self.datafusion_ctx.sql(sql)
plan = datafusion_df.logical_plan()
return self.to_cudf_df(plan)
26 changes: 26 additions & 0 deletions examples/sql-on-cudf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from datafusion.cudf import SessionContext


ctx = SessionContext()
ctx.register_parquet(
"taxi", "/home/jeremy/Downloads/yellow_tripdata_2021-01.parquet"
)
df = ctx.sql("select passenger_count from taxi")
print(df)