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

Create memory table #271

Merged
merged 16 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
268 changes: 151 additions & 117 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ default = ["mimalloc"]
tokio = { version = "1.24", features = ["macros", "rt", "rt-multi-thread", "sync"] }
rand = "0.8"
pyo3 = { version = "0.18.0", features = ["extension-module", "abi3", "abi3-py37"] }
datafusion = { version = "19.0.0", features = ["pyarrow", "avro"] }
datafusion-expr = "19.0.0"
datafusion-optimizer = "19.0.0"
datafusion-common = { version = "19.0.0", features = ["pyarrow"] }
datafusion-sql = "19.0.0"
datafusion-substrait = "19.0.0"
datafusion = { git = "https://github.com/apache/arrow-datafusion.git", rev = "dd98aab", features = ["pyarrow", "avro"]}
datafusion-expr = { git = "https://github.com/apache/arrow-datafusion.git", rev = "dd98aab" }
datafusion-optimizer = { git = "https://github.com/apache/arrow-datafusion.git", rev = "dd98aab" }
datafusion-common = { git = "https://github.com/apache/arrow-datafusion.git", rev = "dd98aab", features = ["pyarrow"]}
datafusion-sql = { git = "https://github.com/apache/arrow-datafusion.git", rev = "dd98aab" }
datafusion-substrait = { git = "https://github.com/apache/arrow-datafusion.git", rev = "dd98aab" }
uuid = { version = "1.2", features = ["v4"] }
mimalloc = { version = "*", optional = true, default-features = false }
async-trait = "0.1"
Expand Down
4 changes: 4 additions & 0 deletions datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
TryCast,
Between,
Explain,
Extension,
CreateMemoryTable,
)

__version__ = importlib_metadata.version(__name__)
Expand Down Expand Up @@ -129,6 +131,8 @@
"TryCast",
"Between",
"Explain",
"Extension",
"CreateMemoryTable",
]


Expand Down
12 changes: 4 additions & 8 deletions datafusion/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,13 @@ def test_logical_plan(aggregate_df):
def test_optimized_logical_plan(aggregate_df):
plan = aggregate_df.optimized_logical_plan()

expected = "Projection: test.c1, SUM(test.c2)"
expected = "Aggregate: groupBy=[[test.c1]], aggr=[[SUM(test.c2)]]"

assert expected == plan.display()

expected = (
"Projection: test.c1, SUM(test.c2)\n"
" Aggregate: groupBy=[[test.c1]], aggr=[[SUM(test.c2)]]\n"
" TableScan: test projection=[c1, c2]"
"Aggregate: groupBy=[[test.c1]], aggr=[[SUM(test.c2)]]\n"
" TableScan: test projection=[c1, c2]"
)

assert expected == plan.display_indent()
Expand All @@ -366,9 +365,7 @@ def test_optimized_logical_plan(aggregate_df):
def test_execution_plan(aggregate_df):
plan = aggregate_df.execution_plan()

expected = (
"ProjectionExec: expr=[c1@0 as c1, SUM(test.c2)@1 as SUM(test.c2)]\n"
)
expected = "AggregateExec: mode=FinalPartitioned, gby=[c1@0 as c1], aggr=[SUM(test.c2)]\n" # noqa: E501

assert expected == plan.display()

Expand All @@ -382,7 +379,6 @@ def test_execution_plan(aggregate_df):

# indent plan will be different for everyone due to absolute path
# to filename, so we just check for some expected content
assert "ProjectionExec:" in indent
assert "AggregateExec:" in indent
assert "CoalesceBatchesExec:" in indent
assert "RepartitionExec:" in indent
Expand Down
6 changes: 6 additions & 0 deletions datafusion/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
Cast,
TryCast,
Between,
Explain,
Extension,
CreateMemoryTable,
)


Expand Down Expand Up @@ -143,6 +146,9 @@ def test_class_module_is_datafusion():
Cast,
TryCast,
Between,
Explain,
Extension,
CreateMemoryTable,
]:
assert klass.__module__ == "datafusion.expr"

Expand Down
8 changes: 5 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use datafusion::physical_plan::SendableRecordBatchStream;
use datafusion::prelude::{
AvroReadOptions, CsvReadOptions, DataFrame, NdJsonReadOptions, ParquetReadOptions,
};
use datafusion_common::config::Extensions;
use datafusion_common::ScalarValue;
use pyo3::types::PyTuple;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -698,19 +699,20 @@ impl PySessionContext {
part: usize,
py: Python,
) -> PyResult<PyRecordBatchStream> {
let ctx = Arc::new(TaskContext::new(
let ctx = TaskContext::try_new(
"task_id".to_string(),
"session_id".to_string(),
HashMap::new(),
HashMap::new(),
HashMap::new(),
Arc::new(RuntimeEnv::default()),
));
Extensions::default(),
);
// create a Tokio runtime to run the async code
let rt = Runtime::new().unwrap();
let plan = plan.plan.clone();
let fut: JoinHandle<datafusion_common::Result<SendableRecordBatchStream>> =
rt.spawn(async move { plan.execute(part, ctx) });
rt.spawn(async move { plan.execute(part, Arc::new(ctx?)) });
let stream = wait_for_future(py, fut).map_err(py_datafusion_err)?;
Ok(PyRecordBatchStream::new(stream?))
}
Expand Down
4 changes: 4 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ pub mod bool_expr;
pub mod case;
pub mod cast;
pub mod column;
pub mod create_memory_table;
pub mod cross_join;
pub mod empty_relation;
pub mod exists;
pub mod explain;
pub mod extension;
pub mod filter;
pub mod grouping_set;
pub mod in_list;
Expand Down Expand Up @@ -272,8 +274,10 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<join::PyJoinConstraint>()?;
m.add_class::<cross_join::PyCrossJoin>()?;
m.add_class::<union::PyUnion>()?;
m.add_class::<extension::PyExtension>()?;
m.add_class::<filter::PyFilter>()?;
m.add_class::<projection::PyProjection>()?;
m.add_class::<table_scan::PyTableScan>()?;
m.add_class::<create_memory_table::PyCreateMemoryTable>()?;
Ok(())
}
97 changes: 97 additions & 0 deletions src/expr/create_memory_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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.

use std::fmt::{self, Display, Formatter};

use datafusion_expr::CreateMemoryTable;
use pyo3::prelude::*;

use crate::sql::logical::PyLogicalPlan;

use super::logical_node::LogicalNode;

#[pyclass(name = "CreateMemoryTable", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyCreateMemoryTable {
create: CreateMemoryTable,
}

impl From<PyCreateMemoryTable> for CreateMemoryTable {
fn from(create: PyCreateMemoryTable) -> Self {
create.create
}
}

impl From<CreateMemoryTable> for PyCreateMemoryTable {
fn from(create: CreateMemoryTable) -> PyCreateMemoryTable {
PyCreateMemoryTable { create }
}
}

impl Display for PyCreateMemoryTable {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"CreateMemoryTable
Name: {:?}
Input: {:?}
if_not_exists: {:?}
or_replace: {:?}",
&self.create.name,
&self.create.input,
&self.create.if_not_exists,
&self.create.or_replace,
)
}
}

#[pymethods]
impl PyCreateMemoryTable {
fn name(&self) -> PyResult<String> {
Ok(self.create.name.to_string())
}

fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
Ok(Self::inputs(self))
}

fn if_not_exists(&self) -> bool {
self.create.if_not_exists
}

fn or_replace(&self) -> bool {
self.create.or_replace
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("CreateMemoryTable({})", self))
}

fn __name__(&self) -> PyResult<String> {
Ok("CreateMemoryTable".to_string())
}
}

impl LogicalNode for PyCreateMemoryTable {
fn inputs(&self) -> Vec<PyLogicalPlan> {
vec![PyLogicalPlan::from((*self.create.input).clone())]
}

fn to_variant(&self, py: Python) -> PyResult<PyObject> {
Ok(self.clone().into_py(py))
}
}
52 changes: 52 additions & 0 deletions src/expr/extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.

use datafusion_expr::Extension;
use pyo3::prelude::*;

use crate::sql::logical::PyLogicalPlan;

use super::logical_node::LogicalNode;

#[pyclass(name = "Extension", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyExtension {
pub node: Extension,
}

impl From<Extension> for PyExtension {
fn from(node: Extension) -> PyExtension {
PyExtension { node }
}
}

#[pymethods]
impl PyExtension {
fn name(&self) -> PyResult<String> {
Ok(self.node.node.name().to_string())
}
}

impl LogicalNode for PyExtension {
fn inputs(&self) -> Vec<PyLogicalPlan> {
vec![]
}

fn to_variant(&self, py: Python) -> PyResult<PyObject> {
Ok(self.clone().into_py(py))
}
}
2 changes: 2 additions & 0 deletions src/sql/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::expr::aggregate::PyAggregate;
use crate::expr::analyze::PyAnalyze;
use crate::expr::empty_relation::PyEmptyRelation;
use crate::expr::explain::PyExplain;
use crate::expr::extension::PyExtension;
use crate::expr::filter::PyFilter;
use crate::expr::limit::PyLimit;
use crate::expr::projection::PyProjection;
Expand Down Expand Up @@ -60,6 +61,7 @@ impl PyLogicalPlan {
LogicalPlan::Analyze(plan) => PyAnalyze::from(plan.clone()).to_variant(py),
LogicalPlan::EmptyRelation(plan) => PyEmptyRelation::from(plan.clone()).to_variant(py),
LogicalPlan::Explain(plan) => PyExplain::from(plan.clone()).to_variant(py),
LogicalPlan::Extension(plan) => PyExtension::from(plan.clone()).to_variant(py),
Copy link
Member

Choose a reason for hiding this comment

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

Should there be a new entry here for CreateMemoryTable as well?

LogicalPlan::Filter(plan) => PyFilter::from(plan.clone()).to_variant(py),
LogicalPlan::Limit(plan) => PyLimit::from(plan.clone()).to_variant(py),
LogicalPlan::Projection(plan) => PyProjection::from(plan.clone()).to_variant(py),
Expand Down