Skip to content

Commit

Permalink
Add SnowflakeColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaroning committed May 17, 2024
1 parent eae8a28 commit 4af6ce6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod session;
use std::time::Duration;

pub use error::{Error, Result};
pub use row::{SnowflakeColumnType, SnowflakeDecode, SnowflakeRow};
pub use row::{SnowflakeColumn, SnowflakeColumnType, SnowflakeDecode, SnowflakeRow};
pub use session::SnowflakeSession;

use auth::login;
Expand Down
21 changes: 15 additions & 6 deletions src/row.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, ops::Deref, sync::Arc};

use chrono::{DateTime, Days, NaiveDate, NaiveDateTime};

use crate::{Error, Result};

#[derive(Debug)]
pub struct SnowflakeColumn {
pub name: String,
pub column_type: SnowflakeColumnType,
}

#[derive(Debug, Clone)]
pub struct SnowflakeColumnType {
/// The index of the column in the row
pub index: usize,
Expand All @@ -31,14 +37,17 @@ impl SnowflakeRow {
pub fn column_names(&self) -> Vec<&str> {
self.column_types.iter().map(|(k, _)| k.as_str()).collect()
}
pub fn column_types(&self) -> Vec<(&str, &SnowflakeColumnType)> {
let mut v: Vec<(&str, &SnowflakeColumnType)> = self
.column_types
pub fn column_types(&self) -> Vec<SnowflakeColumn> {
let column_types = self.column_types.deref();
let mut v: Vec<_> = column_types
.iter()
.map(|(k, v)| (k.as_str(), v))
.map(|(k, v)| SnowflakeColumn {
name: k.clone(),
column_type: v.clone(),
})
.collect();
// sort by column index
v.sort_by_key(|(_, v)| v.index);
v.sort_by_key(|c| c.column_type.index);
v
}
}
Expand Down
30 changes: 13 additions & 17 deletions tests/test-chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,19 @@ async fn test_download_chunked_results() -> Result<()> {
assert!(rows[0].column_names().contains(&"SEQ"));
assert!(rows[0].column_names().contains(&"RAND"));

let col_types = rows[0].column_types();
assert!(col_types.iter().any(|(k, v)| {
let SnowflakeColumnType {
snowflake_type,
nullable,
..
} = v;
*k == "SEQ" && snowflake_type.to_ascii_uppercase() == "FIXED" && !nullable
}));
assert!(col_types.iter().any(|(k, v)| {
let SnowflakeColumnType {
snowflake_type,
nullable,
..
} = v;
*k == "RAND" && snowflake_type.to_ascii_uppercase() == "TEXT" && !nullable
}));
let columns = rows[0].column_types();
assert_eq!(
columns[0].column_type.snowflake_type.to_ascii_uppercase(),
"FIXED"
);
assert_eq!(columns[0].column_type.nullable, false);
assert_eq!(columns[0].column_type.index, 0);
assert_eq!(
columns[1].column_type.snowflake_type.to_ascii_uppercase(),
"TEXT"
);
assert_eq!(columns[1].column_type.nullable, false);
assert_eq!(columns[1].column_type.index, 1);

Ok(())
}

0 comments on commit 4af6ce6

Please sign in to comment.