spark_connect_core/
conf.rs

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
//! Configuration for a Spark application. Used to set various Spark parameters as key-value pairs.

use std::collections::HashMap;

use crate::spark;

use crate::client::{MetadataInterceptor, SparkConnectClient};
use crate::errors::SparkError;

use tonic::service::interceptor::InterceptedService;

#[cfg(not(feature = "wasm"))]
use tonic::transport::Channel;

#[cfg(feature = "wasm")]
use tonic_web_wasm_client::Client;

pub struct RunTimeConfig {
    #[cfg(not(feature = "wasm"))]
    pub(crate) client: SparkConnectClient<InterceptedService<Channel, MetadataInterceptor>>,

    #[cfg(feature = "wasm")]
    pub(crate) client: SparkConnectClient<InterceptedService<Client, MetadataInterceptor>>,
}

/// User-facing configuration API, accessible through SparkSession.conf.
///
/// Options set here are automatically propagated to the Hadoop configuration during I/O.
///
/// # Example
/// ```rust
/// spark
///    .conf()
///    .set("spark.sql.shuffle.partitions", "42")
///    .await?;
/// ```
impl RunTimeConfig {
    #[cfg(not(feature = "wasm"))]
    pub fn new(
        client: &SparkConnectClient<InterceptedService<Channel, MetadataInterceptor>>,
    ) -> RunTimeConfig {
        RunTimeConfig {
            client: client.clone(),
        }
    }

    #[cfg(feature = "wasm")]
    pub fn new(
        client: &SparkConnectClient<InterceptedService<Client, MetadataInterceptor>>,
    ) -> RunTimeConfig {
        RunTimeConfig {
            client: client.clone(),
        }
    }

    pub(crate) async fn set_configs(
        &mut self,
        map: &HashMap<String, String>,
    ) -> Result<(), SparkError> {
        for (key, value) in map {
            self.set(key.as_str(), value.as_str()).await?
        }
        Ok(())
    }

    /// Sets the given Spark runtime configuration property.
    pub async fn set(&mut self, key: &str, value: &str) -> Result<(), SparkError> {
        let op_type = spark::config_request::operation::OpType::Set(spark::config_request::Set {
            pairs: vec![spark::KeyValue {
                key: key.into(),
                value: Some(value.into()),
            }],
        });
        let operation = spark::config_request::Operation {
            op_type: Some(op_type),
        };

        let _ = self.client.config_request(operation).await?;

        Ok(())
    }

    /// Resets the configuration property for the given key.
    pub async fn unset(&mut self, key: &str) -> Result<(), SparkError> {
        let op_type =
            spark::config_request::operation::OpType::Unset(spark::config_request::Unset {
                keys: vec![key.to_string()],
            });
        let operation = spark::config_request::Operation {
            op_type: Some(op_type),
        };

        let _ = self.client.config_request(operation).await?;

        Ok(())
    }

    /// Indicates whether the configuration property with the given key is modifiable in the current session.
    pub async fn get(&mut self, key: &str, default: Option<&str>) -> Result<String, SparkError> {
        let operation = match default {
            Some(default) => {
                let op_type = spark::config_request::operation::OpType::GetWithDefault(
                    spark::config_request::GetWithDefault {
                        pairs: vec![spark::KeyValue {
                            key: key.into(),
                            value: Some(default.into()),
                        }],
                    },
                );
                spark::config_request::Operation {
                    op_type: Some(op_type),
                }
            }
            None => {
                let op_type =
                    spark::config_request::operation::OpType::Get(spark::config_request::Get {
                        keys: vec![key.to_string()],
                    });
                spark::config_request::Operation {
                    op_type: Some(op_type),
                }
            }
        };

        let resp = self.client.config_request(operation).await?;

        let val = resp.pairs.first().unwrap().value().to_string();

        Ok(val)
    }

    /// Indicates whether the configuration property with the given key is modifiable in the current session.
    pub async fn is_modifable(&mut self, key: &str) -> Result<bool, SparkError> {
        let op_type = spark::config_request::operation::OpType::IsModifiable(
            spark::config_request::IsModifiable {
                keys: vec![key.to_string()],
            },
        );
        let operation = spark::config_request::Operation {
            op_type: Some(op_type),
        };

        let resp = self.client.config_request(operation).await?;

        let val = resp.pairs.first().unwrap().value();

        match val {
            "true" => Ok(true),
            "false" => Ok(false),
            _ => Err(SparkError::AnalysisException(
                "Unexpected response value for boolean".to_string(),
            )),
        }
    }
}