tbar4 commented on code in PR #1100:
URL: 
https://github.com/apache/datafusion-ballista/pull/1100#discussion_r1844733777


##########
python/src/lib.rs:
##########
@@ -15,18 +15,189 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use ballista::prelude::*;
+use datafusion_python::context::PySessionContext as 
DataFusionPythonSessionContext;
+use datafusion_python::utils::wait_for_future;
+
+use std::collections::HashMap;
+
 use pyo3::prelude::*;
-pub mod context;
 mod utils;
-
-pub use crate::context::PySessionContext;
+use utils::to_pyerr;
 
 #[pymodule]
-fn pyballista_internal(_py: Python, m: Bound<'_, PyModule>) -> PyResult<()> {
+fn ballista_internal(_py: Python, m: Bound<'_, PyModule>) -> PyResult<()> {
     pyo3_log::init();
     // Ballista structs
-    m.add_class::<PySessionContext>()?;
+    m.add_class::<PyBallista>()?;
+    m.add_class::<PyBallistaBuilder>()?;
     // DataFusion structs
     m.add_class::<datafusion_python::dataframe::PyDataFrame>()?;
+    // Ballista Config
+    /*
+    // Future implementation will include more state and config options
+    m.add_class::<PySessionStateBuilder>()?;
+    m.add_class::<PySessionState>()?;
+    m.add_class::<PySessionConfig>()?;
+    */
     Ok(())
 }
+
+// Ballista Builder will take a HasMap/Dict Cionfg
+#[pyclass(name = "BallistaBuilder", module = "ballista", subclass)]
+pub struct PyBallistaBuilder {
+    conf: HashMap<String, String>,
+}
+
+#[pymethods]
+impl PyBallistaBuilder {
+    #[new]
+    pub fn new() -> Self {
+        Self { conf: HashMap::new() }
+    }
+
+    pub fn set(
+        mut slf: PyRefMut<'_, Self>,
+        k: &str,
+        v: &str,
+        py: Python,
+    ) -> PyResult<PyObject> {
+        slf.conf.insert(k.into(), v.into());
+
+        Ok(slf.into_py(py))
+    }
+
+    pub fn show_config(&self) {
+        println!("Ballista Config:");
+        for ele in self.conf.iter() {
+            println!("\t{}: {}", ele.0, ele.1)
+        }
+    }
+
+    pub fn build(slf: PyRef<'_, Self>) -> PyBallista {
+        PyBallista {
+            conf: PyBallistaBuilder {
+                conf: slf.conf.clone(),
+            },
+        }
+    }
+}
+
+#[pyclass(name = "Ballista", module = "ballista", subclass)]
+pub struct PyBallista {
+    pub conf: PyBallistaBuilder,
+}
+
+#[pymethods]
+impl PyBallista {
+    #[new]
+    pub fn new() -> Self {
+        Self {
+            conf: PyBallistaBuilder::new(),
+        }
+    }
+
+    pub fn show_config(&self) {
+        println!("Ballista Config:");
+        for ele in self.conf.conf.clone() {
+            println!("{:4}: {}", ele.0, ele.1)
+        }
+    }
+
+    /// Construct the standalone instance from the SessionContext
+    #[pyo3(signature = (concurrent_tasks = 4))]
+    pub fn standalone(
+        &self,
+        concurrent_tasks: usize,
+        py: Python,
+    ) -> PyResult<DataFusionPythonSessionContext> {
+        // Build the config
+        let config = 
&BallistaConfig::with_settings(self.conf.conf.clone()).unwrap();
+        // Define the SessionContext
+        let session_context = BallistaContext::standalone(&config, 
concurrent_tasks);
+        // SessionContext is an async function
+        let ctx = wait_for_future(py, session_context)
+            .map_err(to_pyerr)?
+            .context()
+            .clone();
+
+        // Convert the SessionContext into a Python SessionContext
+        Ok(ctx.into())
+    }
+
+    /// Construct the remote instance from the SessionContext
+    pub fn remote(
+        &self,
+        host: &str,
+        port: u16,
+        py: Python,
+    ) -> PyResult<DataFusionPythonSessionContext> {
+        // Build the config
+        let config = 
&BallistaConfig::with_settings(self.conf.conf.clone()).unwrap();
+        // Create the BallistaContext
+        let session_context = BallistaContext::remote(host, port, config);
+        let ctx = wait_for_future(py, session_context)
+            .map_err(to_pyerr)?
+            .context()
+            .clone();
+
+        // Convert the SessionContext into a Python SessionContext
+        Ok(ctx.into())
+    }
+}
+
+/*
+Plan to implement Session Config and State in a future issue

Review Comment:
   removed dead code



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to