Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to get type information #24

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use http::{
use reqwest::Client;
use tokio::time::sleep;

use crate::row::SnowflakeColumnType;
use crate::{chunk::download_chunk, Error, Result, SnowflakeRow};

pub(super) const SESSION_EXPIRED: &str = "390112";
Expand Down Expand Up @@ -102,17 +103,28 @@ pub(super) async fn query<Q: Into<QueryRequest>>(
row_set.extend(rows);
}

let column_names = row_types
let column_types = row_types
.into_iter()
.enumerate()
.map(|(i, name)| (name.name.to_ascii_uppercase(), i))
.map(|(i, row_type)| {
(
row_type.name.to_ascii_uppercase(),
(
i,
SnowflakeColumnType {
snowflake_type: row_type.data_type,
nullable: row_type.nullable,
},
),
)
})
.collect::<HashMap<_, _>>();
let column_names = Arc::new(column_names);
let column_types = Arc::new(column_types);
Ok(row_set
.into_iter()
.map(|row| SnowflakeRow {
row,
column_names: Arc::clone(&column_names),
column_types: Arc::clone(&column_types),
})
.collect())
}
Expand Down
61 changes: 55 additions & 6 deletions src/row.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
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(super) name: String,
pub(super) index: usize,
pub(super) column_type: SnowflakeColumnType,
}

impl SnowflakeColumn {
pub fn name(&self) -> &str {
&self.name
}
pub fn column_type(&self) -> &SnowflakeColumnType {
&self.column_type
}

pub fn index(&self) -> usize {
self.index
}
}

#[derive(Debug, Clone)]
pub struct SnowflakeColumnType {
pub(super) snowflake_type: String,
pub(super) nullable: bool,
}

impl SnowflakeColumnType {
pub fn snowflake_type(&self) -> &str {
&self.snowflake_type
}
pub fn nullable(&self) -> bool {
self.nullable
}
}

#[derive(Debug)]
pub struct SnowflakeRow {
pub(crate) row: Vec<Option<String>>,
pub(crate) column_names: Arc<HashMap<String, usize>>,
pub(crate) column_types: Arc<HashMap<String, (usize, SnowflakeColumnType)>>,
}

impl SnowflakeRow {
pub fn get<T: SnowflakeDecode>(&self, column_name: &str) -> Result<T> {
let index = self
.column_names
let (idx, _) = self
.column_types
.get(&column_name.to_ascii_uppercase())
.ok_or_else(|| Error::Decode(format!("column not found: {}", column_name)))?;
self.row[*index].try_get()
self.row[*idx].try_get()
}
pub fn column_names(&self) -> Vec<&str> {
self.column_names.iter().map(|(k, _)| k.as_str()).collect()
self.column_types.iter().map(|(k, _)| k.as_str()).collect()
}
pub fn column_types(&self) -> Vec<SnowflakeColumn> {
let column_types = self.column_types.deref();
let mut v: Vec<_> = column_types
.iter()
.map(|(name, (idx, ty))| SnowflakeColumn {
name: name.clone(),
index: *idx,
column_type: ty.clone(),
})
.collect();
// sort by column index
v.sort_by_key(|c| c.index);
v
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/test-chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,25 @@ async fn test_download_chunked_results() -> Result<()> {
assert!(rows[0].column_names().contains(&"SEQ"));
assert!(rows[0].column_names().contains(&"RAND"));

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].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].index(), 1);

Ok(())
}
Loading