robin-sparkless 4.4.0

PySpark-like DataFrame API in Rust on Polars; no JVM.
Documentation
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Root-owned Session API; delegates to robin-sparkless-polars for execution.

use crate::EngineError;
use robin_sparkless_core::SparklessConfig;
use robin_sparkless_polars::{
    DataFrameReader as PolarsDataFrameReader, PlDataFrame, PolarsError,
    SparkSession as PolarsSparkSession, SparkSessionBuilder as PolarsSparkSessionBuilder,
};
use std::collections::HashMap;
use std::path::Path;

use crate::dataframe::DataFrame;

/// Root-owned SparkSession; delegates to the Polars backend.
#[derive(Clone)]
pub struct SparkSession(pub(crate) PolarsSparkSession);

/// Root-owned SparkSessionBuilder; delegates to the Polars backend.
#[derive(Clone)]
pub struct SparkSessionBuilder(pub(crate) PolarsSparkSessionBuilder);

/// Root-owned DataFrameReader; delegates to the Polars backend.
pub struct DataFrameReader(PolarsDataFrameReader);

impl SparkSessionBuilder {
    pub fn new() -> Self {
        SparkSessionBuilder(PolarsSparkSessionBuilder::new())
    }

    pub fn app_name(self, name: impl Into<String>) -> Self {
        SparkSessionBuilder(self.0.app_name(name))
    }

    pub fn master(self, master: impl Into<String>) -> Self {
        SparkSessionBuilder(self.0.master(master))
    }

    pub fn config(self, key: impl Into<String>, value: impl Into<String>) -> Self {
        SparkSessionBuilder(self.0.config(key, value))
    }

    /// Config key-value pairs set on the builder (for singleton compatibility check).
    pub fn get_config(&self) -> &HashMap<String, String> {
        self.0.get_config()
    }

    pub fn get_or_create(self) -> SparkSession {
        SparkSession(self.0.get_or_create())
    }

    pub fn with_config(self, config: &SparklessConfig) -> Self {
        SparkSessionBuilder(self.0.with_config(config))
    }
}

impl Default for SparkSessionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl SparkSession {
    pub fn builder() -> SparkSessionBuilder {
        SparkSessionBuilder(PolarsSparkSession::builder())
    }

    pub fn from_config(config: &SparklessConfig) -> SparkSession {
        SparkSession(PolarsSparkSession::from_config(config))
    }

    pub fn read(&self) -> DataFrameReader {
        DataFrameReader(PolarsDataFrameReader::new(self.0.clone()))
    }

    pub fn create_or_replace_temp_view(&self, name: &str, df: DataFrame) {
        self.0.create_or_replace_temp_view(name, df.0)
    }

    pub fn create_global_temp_view(&self, name: &str, df: DataFrame) {
        self.0.create_global_temp_view(name, df.0)
    }

    pub fn create_or_replace_global_temp_view(&self, name: &str, df: DataFrame) {
        self.0.create_or_replace_global_temp_view(name, df.0)
    }

    pub fn drop_temp_view(&self, name: &str) {
        self.0.drop_temp_view(name)
    }

    pub fn drop_global_temp_view(&self, name: &str) -> bool {
        self.0.drop_global_temp_view(name)
    }

    pub fn register_table(&self, name: &str, df: DataFrame) {
        self.0.register_table(name, df.0)
    }

    pub fn register_database(&self, name: &str) {
        self.0.register_database(name)
    }

    pub fn list_database_names(&self) -> Vec<String> {
        self.0.list_database_names()
    }

    pub fn database_exists(&self, name: &str) -> bool {
        self.0.database_exists(name)
    }

    pub fn get_saved_table(&self, name: &str) -> Option<DataFrame> {
        self.0.get_saved_table(name).map(DataFrame)
    }

    pub fn saved_table_exists(&self, name: &str) -> bool {
        self.0.saved_table_exists(name)
    }

    pub fn table_exists(&self, name: &str) -> bool {
        self.0.table_exists(name)
    }

    pub fn list_global_temp_view_names(&self) -> Vec<String> {
        self.0.list_global_temp_view_names()
    }

    pub fn list_temp_view_names(&self) -> Vec<String> {
        self.0.list_temp_view_names()
    }

    pub fn list_table_names(&self) -> Vec<String> {
        self.0.list_table_names()
    }

    pub fn app_name(&self) -> Option<String> {
        self.0.app_name()
    }

    pub fn new_session(&self) -> SparkSession {
        SparkSession(self.0.new_session())
    }

    pub fn current_database(&self) -> String {
        self.0.current_database()
    }

    pub fn set_current_database(&self, name: &str) -> Result<(), EngineError> {
        self.0.set_current_database(name)
    }

    pub fn cache_table(&self, name: &str) {
        self.0.cache_table(name)
    }

    pub fn uncache_table(&self, name: &str) {
        self.0.uncache_table(name)
    }

    pub fn is_cached(&self, name: &str) -> bool {
        self.0.is_cached(name)
    }

    pub fn drop_table(&self, name: &str) -> bool {
        self.0.drop_table(name)
    }

    pub fn drop_database(&self, name: &str) -> bool {
        self.0.drop_database(name)
    }

    pub fn warehouse_dir(&self) -> Option<&str> {
        self.0.warehouse_dir()
    }

    pub fn table(&self, name: &str) -> Result<DataFrame, PolarsError> {
        self.0.table(name).map(DataFrame)
    }

    pub fn get_config(&self) -> &HashMap<String, String> {
        self.0.get_config()
    }

    pub fn set_config(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.0.set_config(key, value);
    }

    pub fn is_case_sensitive(&self) -> bool {
        self.0.is_case_sensitive()
    }

    pub fn register_udf<F>(&self, name: &str, f: F) -> Result<(), PolarsError>
    where
        F: Fn(
                &[robin_sparkless_polars::Series],
            ) -> Result<robin_sparkless_polars::Series, PolarsError>
            + Send
            + Sync
            + 'static,
    {
        self.0.register_udf(name, f)
    }

    pub fn create_dataframe(
        &self,
        data: Vec<(i64, i64, String)>,
        column_names: Vec<&str>,
    ) -> Result<DataFrame, PolarsError> {
        self.0.create_dataframe(data, column_names).map(DataFrame)
    }

    pub fn create_dataframe_engine(
        &self,
        data: Vec<(i64, i64, String)>,
        column_names: Vec<&str>,
    ) -> Result<DataFrame, EngineError> {
        self.0
            .create_dataframe_engine(data, column_names)
            .map(DataFrame)
    }

    pub fn create_dataframe_from_polars(&self, df: PlDataFrame) -> DataFrame {
        DataFrame(self.0.create_dataframe_from_polars(df))
    }

    pub fn create_dataframe_from_rows(
        &self,
        rows: Vec<Vec<serde_json::Value>>,
        schema: Vec<(String, String)>,
        verify_schema: bool,
        schema_was_inferred: bool,
    ) -> Result<DataFrame, PolarsError> {
        self.0
            .create_dataframe_from_rows(rows, schema, verify_schema, schema_was_inferred)
            .map(DataFrame)
    }

    pub fn create_dataframe_from_rows_engine(
        &self,
        rows: Vec<Vec<serde_json::Value>>,
        schema: Vec<(String, String)>,
        verify_schema: bool,
        schema_was_inferred: bool,
    ) -> Result<DataFrame, EngineError> {
        self.0
            .create_dataframe_from_rows_engine(rows, schema, verify_schema, schema_was_inferred)
            .map(DataFrame)
    }

    /// #419: Create a DataFrame with a single column "value" from scalar values (e.g. createDataFrame([1,2,3], "bigint")).
    pub fn create_dataframe_from_single_column(
        &self,
        values: Vec<serde_json::Value>,
        type_str: &str,
    ) -> Result<DataFrame, PolarsError> {
        self.0
            .create_dataframe_from_single_column(values, type_str)
            .map(DataFrame)
    }

    pub fn range(&self, start: i64, end: i64, step: i64) -> Result<DataFrame, PolarsError> {
        self.0.range(start, end, step).map(DataFrame)
    }

    pub fn read_csv(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.read_csv(path).map(DataFrame)
    }

    pub fn read_csv_engine(&self, path: impl AsRef<Path>) -> Result<DataFrame, EngineError> {
        self.0.read_csv_engine(path).map(DataFrame)
    }

    pub fn read_parquet(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.read_parquet(path).map(DataFrame)
    }

    pub fn read_parquet_engine(&self, path: impl AsRef<Path>) -> Result<DataFrame, EngineError> {
        self.0.read_parquet_engine(path).map(DataFrame)
    }

    pub fn read_json(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.read_json(path).map(DataFrame)
    }

    pub fn read_json_engine(&self, path: impl AsRef<Path>) -> Result<DataFrame, EngineError> {
        self.0.read_json_engine(path).map(DataFrame)
    }

    pub fn sql(&self, query: &str) -> Result<DataFrame, PolarsError> {
        self.0.sql(query).map(DataFrame)
    }

    pub fn table_engine(&self, name: &str) -> Result<DataFrame, EngineError> {
        self.0.table_engine(name).map(DataFrame)
    }

    #[cfg(feature = "delta")]
    pub fn read_delta_path(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.read_delta_path(path).map(DataFrame)
    }

    pub fn read_delta_from_path(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.read_delta_from_path(path).map(DataFrame)
    }

    #[cfg(feature = "delta")]
    pub fn read_delta_path_with_version(
        &self,
        path: impl AsRef<Path>,
        version: Option<i64>,
    ) -> Result<DataFrame, PolarsError> {
        self.0
            .read_delta_path_with_version(path, version)
            .map(DataFrame)
    }

    #[cfg(feature = "delta")]
    pub fn read_delta(&self, name_or_path: &str) -> Result<DataFrame, PolarsError> {
        self.0.read_delta(name_or_path).map(DataFrame)
    }

    #[cfg(feature = "delta")]
    pub fn read_delta_with_version(
        &self,
        name_or_path: &str,
        version: Option<i64>,
    ) -> Result<DataFrame, PolarsError> {
        self.0
            .read_delta_with_version(name_or_path, version)
            .map(DataFrame)
    }

    pub fn stop(&self) {
        self.0.stop()
    }

    /// Get the UDF registry. Used internally for thread context management.
    pub fn udf_registry(&self) -> &robin_sparkless_polars::UdfRegistry {
        self.0.udf_registry()
    }
}

impl DataFrameReader {
    pub fn option(self, key: impl Into<String>, value: impl Into<String>) -> Self {
        DataFrameReader(self.0.option(key, value))
    }

    pub fn options(self, opts: impl IntoIterator<Item = (String, String)>) -> Self {
        DataFrameReader(self.0.options(opts))
    }

    pub fn format(self, fmt: impl Into<String>) -> Self {
        DataFrameReader(self.0.format(fmt))
    }

    pub fn schema(self, schema: impl Into<String>) -> Self {
        DataFrameReader(self.0.schema(schema))
    }

    pub fn load(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.load(path).map(DataFrame)
    }

    pub fn table(&self, name: &str) -> Result<DataFrame, PolarsError> {
        self.0.table(name).map(DataFrame)
    }

    pub fn csv(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.csv(path).map(DataFrame)
    }

    pub fn parquet(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.parquet(path).map(DataFrame)
    }

    pub fn json(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.json(path).map(DataFrame)
    }

    #[cfg(feature = "delta")]
    pub fn delta(&self, path: impl AsRef<Path>) -> Result<DataFrame, PolarsError> {
        self.0.delta(path).map(DataFrame)
    }

    /// JDBC read: load a table from an external database (e.g. PostgreSQL).
    /// Requires the `jdbc` or `sqlite` feature. Mirror of PySpark's spark.read.jdbc(url, table, properties).
    #[cfg(any(
        feature = "jdbc",
        feature = "jdbc_mysql",
        feature = "jdbc_mariadb",
        feature = "jdbc_mssql",
        feature = "jdbc_oracle",
        feature = "jdbc_db2",
        feature = "sqlite"
    ))]
    pub fn jdbc(
        &self,
        url: &str,
        table: &str,
        properties: &HashMap<String, String>,
    ) -> Result<DataFrame, PolarsError> {
        self.0
            .jdbc_with_properties(url, table, properties)
            .map(DataFrame)
            .map_err(|e| PolarsError::ComputeError(e.to_string().into()))
    }
}