Skip to content

Commit be083c6

Browse files
felipecrvfa-assistant
authored andcommitted
Use AdapterType instead of strings (#5415)
* dbt-fusion-adapter: Use AdapterType instead of strings * dbt-schemas: Use AdapterType instead of strings * dbt-jinj-utils: Use AdapterType instead of strings * dbt-init: Use AdapterType instead of strings * dbt-freshness: Use AdapterType instead of strings * dbt-parser: Use AdapterType instead of strings * sdf-linter: Use AdapterType instead of strings * dbt-loader: Use AdapterType instead of strings * dbt-metadata: Use AdapterType instead of strings * dbt-lineage: Use AdapterType instead of strings * dbt-adapter: Use AdapterType instead of strings * dbt-tasks: Use AdapterType instead of strings * dbt-common: Implement Ser/Deserialize for AdapterType * sdf-lp-processor: Use AdapterType instead of strings * sdf-formatter: Use AdapterType instead of strings * dbt-lsp: Use AdapterType instead of strings * dbt-cli: Use AdapterType instead of strings * dbt-adapter-tests: Use AdapterType instead of strings * Add TODOs for Salesforce GitOrigin-RevId: 513366b0376bef50601a4e1f9191f77c219c00d9
1 parent c8cd7ad commit be083c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+348
-235
lines changed

crates/dbt-common/src/adapter.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use arrow_schema::Schema;
44
use dbt_frontend_common::dialect::Dialect;
5+
use serde::{Deserialize, Serialize};
56
use strum::{AsRefStr, Display, EnumString};
67

78
/// Schema registry access interface.
@@ -16,8 +17,11 @@ pub trait SchemaRegistry: Send + Sync {
1617
/// The type of the adapter.
1718
///
1819
/// Used to identify the specific database adapter being used.
19-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, AsRefStr, EnumString)]
20+
#[derive(
21+
Debug, Clone, Copy, PartialEq, Eq, Display, AsRefStr, EnumString, Deserialize, Serialize,
22+
)]
2023
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
24+
#[serde(rename_all = "lowercase")]
2125
pub enum AdapterType {
2226
/// Adapter used in parse phase
2327
Parse,

crates/dbt-common/src/pretty_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub fn pretty_schema_table(
427427
subtitle: &str,
428428
display_format: &DisplayFormat,
429429
table_schema: &Schema,
430-
_dialect: &Dialect,
430+
_dialect: Dialect,
431431
limit: Option<usize>,
432432
show_footer: bool,
433433
) -> FsResult<String> {

crates/dbt-fusion-adapter/src/bridge_adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ impl BaseAdapter for BridgeAdapter {
695695
identifier: &str,
696696
) -> Result<Value, MinijinjaError> {
697697
let temp_relation = relation_object::create_relation(
698-
self.typed_adapter.adapter_type().to_string(),
698+
self.typed_adapter.adapter_type(),
699699
database.to_string(),
700700
schema.to_string(),
701701
Some(identifier.to_string()),

crates/dbt-fusion-adapter/src/cache.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ impl Drop for RelationCache {
351351

352352
#[cfg(test)]
353353
mod tests {
354+
use crate::AdapterType;
355+
354356
use super::*;
355357
use dbt_schemas::schemas::{common::ResolvedQuoting, relations::DEFAULT_RESOLVED_QUOTING};
356358

@@ -362,7 +364,7 @@ mod tests {
362364

363365
// Create relations with different combinations of database, schema, identifier
364366
let relation1 = create_relation(
365-
"postgres".to_string(),
367+
AdapterType::Postgres,
366368
"db1".to_string(),
367369
"schema1".to_string(),
368370
Some("table1".to_string()),
@@ -372,7 +374,7 @@ mod tests {
372374
.unwrap();
373375

374376
let relation2 = create_relation(
375-
"postgres".to_string(),
377+
AdapterType::Postgres,
376378
"db2".to_string(),
377379
"schema1".to_string(),
378380
Some("table1".to_string()),
@@ -382,7 +384,7 @@ mod tests {
382384
.unwrap();
383385

384386
let relation3 = create_relation(
385-
"postgres".to_string(),
387+
AdapterType::Postgres,
386388
"db1".to_string(),
387389
"schema2".to_string(),
388390
Some("table1".to_string()),
@@ -392,7 +394,7 @@ mod tests {
392394
.unwrap();
393395

394396
let relation4 = create_relation(
395-
"postgres".to_string(),
397+
AdapterType::Postgres,
396398
"db1".to_string(),
397399
"schema1".to_string(),
398400
Some("table2".to_string()),
@@ -402,7 +404,7 @@ mod tests {
402404
.unwrap();
403405

404406
let relation1_dup = create_relation(
405-
"postgres".to_string(),
407+
AdapterType::Postgres,
406408
"db1".to_string(),
407409
"schema1".to_string(),
408410
Some("table1".to_string()),
@@ -451,7 +453,7 @@ mod tests {
451453

452454
// With DEFAULT_RESOLVED_QUOTING
453455
let relation_quoted = create_relation(
454-
"postgres".to_string(),
456+
AdapterType::Postgres,
455457
"MyDB".to_string(),
456458
"MySchema".to_string(),
457459
Some("MyTable".to_string()),
@@ -462,7 +464,7 @@ mod tests {
462464

463465
// With no quoting
464466
let relation_unquoted = create_relation(
465-
"postgres".to_string(),
467+
AdapterType::Postgres,
466468
"MyDB".to_string(),
467469
"MySchema".to_string(),
468470
Some("MyTable".to_string()),
@@ -492,7 +494,7 @@ mod tests {
492494

493495
// Test that we find the unquoted relation when searching with unquoted policy
494496
let search_relation_unquoted = create_relation(
495-
"postgres".to_string(),
497+
AdapterType::Postgres,
496498
"MyDB".to_string(),
497499
"MySchema".to_string(),
498500
Some("MyTable".to_string()),
@@ -525,7 +527,7 @@ mod tests {
525527
// Create relations in 3 different schemas
526528
(0..3).map(move |schema_id| {
527529
create_relation(
528-
"postgres".to_string(),
530+
AdapterType::Postgres,
529531
"test_db".to_string(),
530532
format!("schema_{schema_id}"),
531533
Some(format!("table_{schema_id}_{i}")),
@@ -585,7 +587,7 @@ mod tests {
585587
6 => {
586588
// Rename operations (less common but important)
587589
let new_relation = create_relation(
588-
"postgres".to_string(),
590+
AdapterType::Postgres,
589591
"test_db".to_string(),
590592
format!("schema_{}", thread_id % 3),
591593
Some(format!("renamed_table_{thread_id}_{i}")),

crates/dbt-fusion-adapter/src/databricks/relation_configs/comment.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl DatabricksComponentProcessor for CommentProcessor {
9292
#[cfg(test)]
9393
mod tests {
9494
use super::*;
95+
use crate::AdapterType;
9596
use crate::databricks::relation_configs::base::DatabricksRelationResultsBuilder;
9697
use dbt_agate::AgateTable;
9798
use dbt_schemas::schemas::{common::*, nodes::*, project::*};
@@ -135,7 +136,8 @@ mod tests {
135136
let warehouse_config = WarehouseSpecificNodeConfig::default();
136137

137138
// Use the factory method to create adapter attributes
138-
let adapter_attr = AdapterAttr::from_config_and_dialect(&warehouse_config, "databricks");
139+
let adapter_attr =
140+
AdapterAttr::from_config_and_dialect(&warehouse_config, AdapterType::Databricks);
139141

140142
DbtModel {
141143
__common_attr__: CommonAttributes {

crates/dbt-fusion-adapter/src/databricks/relation_configs/partitioning.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl DatabricksComponentProcessor for PartitionedByProcessor {
104104
#[cfg(test)]
105105
mod tests {
106106
use super::*;
107+
use crate::AdapterType;
107108
use crate::databricks::relation_configs::base::DatabricksRelationResultsBuilder;
108109
use dbt_agate::AgateTable;
109110
use dbt_schemas::schemas::{common::*, nodes::*, project::*};
@@ -181,7 +182,8 @@ mod tests {
181182
};
182183

183184
// Use the factory method to create adapter attributes
184-
let adapter_attr = AdapterAttr::from_config_and_dialect(&warehouse_config, "databricks");
185+
let adapter_attr =
186+
AdapterAttr::from_config_and_dialect(&warehouse_config, AdapterType::Databricks);
185187

186188
DbtModel {
187189
__common_attr__: CommonAttributes {

crates/dbt-fusion-adapter/src/databricks/relation_configs/refresh.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl DatabricksComponentProcessor for RefreshProcessor {
117117
#[cfg(test)]
118118
mod tests {
119119
use super::*;
120+
use crate::AdapterType;
120121
use crate::databricks::relation_configs::base::DatabricksRelationResultsBuilder;
121122
use dbt_agate::AgateTable;
122123
use dbt_schemas::schemas::{common::*, nodes::*, project::*};
@@ -190,7 +191,8 @@ mod tests {
190191
};
191192

192193
// Use the factory method to create adapter attributes
193-
let adapter_attr = AdapterAttr::from_config_and_dialect(&warehouse_config, "databricks");
194+
let adapter_attr =
195+
AdapterAttr::from_config_and_dialect(&warehouse_config, AdapterType::Databricks);
194196

195197
DbtModel {
196198
__common_attr__: CommonAttributes {

crates/dbt-fusion-adapter/src/databricks/relation_configs/tblproperties.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl DatabricksComponentProcessor for TblPropertiesProcessor {
157157
#[cfg(test)]
158158
mod tests {
159159
use super::*;
160+
use crate::AdapterType;
160161
use crate::databricks::relation_configs::base::DatabricksRelationResultsBuilder;
161162
use dbt_agate::AgateTable;
162163
use dbt_schemas::schemas::{common::*, nodes::*, project::*};
@@ -218,7 +219,8 @@ mod tests {
218219
};
219220

220221
// Use the factory method to create adapter attributes
221-
let adapter_attr = AdapterAttr::from_config_and_dialect(&warehouse_config, "databricks");
222+
let adapter_attr =
223+
AdapterAttr::from_config_and_dialect(&warehouse_config, AdapterType::Databricks);
222224

223225
DbtModel {
224226
__common_attr__: CommonAttributes {

crates/dbt-fusion-adapter/src/metadata.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ pub trait MetadataAdapter: TypedBaseAdapter + Send + Sync {
201201
resolved_state: &ResolverState,
202202
run_stats: &Stats,
203203
) -> Vec<Arc<dyn BaseRelation>> {
204+
let adapter_type = resolved_state.adapter_type;
204205
let mut relations: Vec<Arc<dyn BaseRelation>> = Vec::new();
205206
let executed_unique_ids = run_stats
206207
.stats
@@ -214,7 +215,7 @@ pub trait MetadataAdapter: TypedBaseAdapter + Send + Sync {
214215
for (unique_id, node) in nodes.models.iter() {
215216
if executed_unique_ids.contains(unique_id) {
216217
let relation = create_relation_internal(
217-
resolved_state.adapter_type.to_string(),
218+
adapter_type,
218219
node.database(),
219220
node.schema(),
220221
Some(node.alias()),
@@ -229,7 +230,7 @@ pub trait MetadataAdapter: TypedBaseAdapter + Send + Sync {
229230
for (unique_id, node) in nodes.snapshots.iter() {
230231
if executed_unique_ids.contains(unique_id) {
231232
let relation = create_relation_internal(
232-
resolved_state.adapter_type.to_string(),
233+
adapter_type,
233234
node.database(),
234235
node.schema(),
235236
Some(node.alias()),
@@ -244,7 +245,7 @@ pub trait MetadataAdapter: TypedBaseAdapter + Send + Sync {
244245
for (unique_id, node) in nodes.seeds.iter() {
245246
if executed_unique_ids.contains(unique_id) {
246247
let relation = create_relation_internal(
247-
resolved_state.adapter_type.to_string(),
248+
adapter_type,
248249
node.database(),
249250
node.schema(),
250251
Some(node.alias()),
@@ -259,7 +260,7 @@ pub trait MetadataAdapter: TypedBaseAdapter + Send + Sync {
259260
for (unique_id, node) in nodes.sources.iter() {
260261
if executed_unique_ids.contains(unique_id) {
261262
let relation = create_relation_internal(
262-
resolved_state.adapter_type.to_string(),
263+
adapter_type,
263264
node.database(),
264265
node.schema(),
265266
Some(node.alias()),

crates/dbt-fusion-adapter/src/parse/adapter.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ use serde::Deserialize;
3131
use std::collections::BTreeMap;
3232
use std::fmt;
3333
use std::rc::Rc;
34-
use std::str::FromStr;
3534
use std::sync::Arc;
3635

3736
/// Parse adapter for Jinja templates.
3837
///
3938
/// Returns stub values to enable the parsing phase.
4039
#[derive(Debug, Clone)]
4140
pub struct ParseAdapter {
42-
/// The type of database adapter (e.g. "snowflake", "postgres", etc.)
43-
adapter_type: String,
41+
/// Adapter type
42+
adapter_type: AdapterType,
4443
/// The call_get_relation method calls found during parse
4544
call_get_relation: DashMap<String, Vec<Value>>,
4645
/// The call_get_columns_in_relation method calls found during parse
@@ -66,12 +65,10 @@ type RelationsToFetch = (
6665
impl ParseAdapter {
6766
/// Make a new adapter
6867
pub fn new(
69-
adapter_type: impl Into<String>,
68+
adapter_type: AdapterType,
7069
package_quoting: DbtQuoting,
7170
token: CancellationToken,
7271
) -> Self {
73-
let adapter_type = adapter_type.into();
74-
AdapterType::from_str(&adapter_type).expect("adapter_type is valid");
7572
Self {
7673
adapter_type,
7774
call_get_relation: DashMap::new(),
@@ -95,7 +92,7 @@ impl ParseAdapter {
9592
identifier: &str,
9693
) -> Result<(), MinijinjaError> {
9794
let relation = create_relation(
98-
self.adapter_type.clone(),
95+
self.adapter_type,
9996
database.to_string(),
10097
schema.to_string(),
10198
Some(identifier.to_string()),
@@ -202,16 +199,7 @@ impl ParseAdapter {
202199

203200
impl AdapterTyping for ParseAdapter {
204201
fn adapter_type(&self) -> AdapterType {
205-
// TODO: check if we need adapterType::Parse
206-
// since even ParseAdapter should be a specific <dialect> type
207-
debug_assert!(
208-
self.adapter_type
209-
.parse::<AdapterType>()
210-
.expect("adapter_type is valid")
211-
!= AdapterType::Parse,
212-
"ParseAdapter should be a specific <dialect> type"
213-
);
214-
self.adapter_type.parse().unwrap_or(AdapterType::Parse)
202+
self.adapter_type
215203
}
216204

217205
fn as_metadata_adapter(&self) -> Option<&dyn MetadataAdapter> {
@@ -762,11 +750,10 @@ impl Object for ParseAdapter {
762750

763751
/// Make parse factory
764752
pub fn create_parse_adapter(
765-
adapter_type: impl Into<String>,
753+
adapter_type: AdapterType,
766754
package_quoting: DbtQuoting,
767755
token: CancellationToken,
768756
) -> FsResult<Arc<dyn BaseAdapter>> {
769-
let adapter_type: String = adapter_type.into();
770757
Ok(Arc::new(ParseAdapter::new(
771758
adapter_type,
772759
package_quoting,

0 commit comments

Comments
 (0)