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

converting the date times over #242

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
104 changes: 104 additions & 0 deletions rust/quary-databases/src/database_arrow_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::sync::Arc;
use chrono::{DateTime, Utc};
use duckdb::arrow::array::{Array, array};

pub(crate) fn convert_array_to_vec_string(
array: &[Arc<dyn Array>],
) -> Result<Vec<Vec<String>>, String> {
let num_rows = array[0].len();
let num_columns = array.len();
let mut rows = Vec::with_capacity(num_rows);
for _ in 0..num_rows {
let row = vec!["".to_string(); num_columns];
rows.push(row);
}

for (i, row) in rows.iter_mut().enumerate() {
for (j, value) in row.iter_mut().enumerate() {
let array = &array[j];
if let Some(array) = array.as_any().downcast_ref::<array::StringArray>() {
if array.is_null(i) {
*value = "NULL".to_string();
} else {
*value = array.value(i).to_string();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::Int32Array>() {
if array.is_null(i) {
*value = "NULL".to_string();
} else {
*value = array.value(i).to_string();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::Int64Array>() {
if array.is_null(i) {
*value = "NULL".to_string();
} else {
*value = array.value(i).to_string();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::Float32Array>()
{
if array.is_null(i) {
*value = "NULL".to_string();
} else {
*value = array.value(i).to_string();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::Float64Array>()
{
if array.is_null(i) {
*value = "NULL".to_string();
} else {
*value = array.value(i).to_string();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::BooleanArray>()
{
if array.is_null(i) {
*value = "NULL".to_string();
} else {
*value = array.value(i).to_string();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::Date64Array>() {
if array.is_null(i) {
*value = "NULL".to_string();
} else {
let date = array.value(i);
let datetime_utc = DateTime::<Utc>::from_utc(
chrono::NaiveDateTime::from_timestamp(date / 1000, 0),
Utc,
);
*value = datetime_utc.to_rfc3339();
}
} else if let Some(array) = array.as_any().downcast_ref::<array::Date32Array>() {
if array.is_null(i) {
*value = "NULL".to_string();
} else {
let date = array.value(i);
let datetime_utc = DateTime::<Utc>::from_utc(
chrono::NaiveDateTime::from_timestamp(date as i64 * 24 * 60 * 60, 0),
Utc,
);
*value = datetime_utc.to_rfc3339();
}
} else if let Some(array) = array
.as_any()
.downcast_ref::<array::TimestampMicrosecondArray>()
{
if array.is_null(i) {
*value = "NULL".to_string();
} else {
let timestamp_micros = array.value(i);
let datetime_utc = DateTime::<Utc>::from_timestamp(
timestamp_micros / 1_000_000,
(timestamp_micros % 1_000_000) as u32 * 1_000,
)
.ok_or("error converting timestamp to datetime")?;
*value = datetime_utc.format("%Y-%m-%d %H:%M:%S%.6f %Z").to_string();
}
} else {
let array_type = array.data_type();
return Err(format!("Unsupported array type: {:?}", array_type));
}
}
}

// Example for a specific array type, e.g., StringArray
Ok(rows)
}
63 changes: 2 additions & 61 deletions rust/quary-databases/src/databases_duckdb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use chrono::{DateTime, Utc};
use duckdb::arrow::array::{array, Array};
use duckdb::arrow::array::{Array};
use duckdb::arrow::record_batch::RecordBatch;
use duckdb::{params, Connection};
use quary_core::database_duckdb::DatabaseQueryGeneratorDuckDB;
Expand All @@ -8,6 +7,7 @@ use quary_core::databases::{
};
use quary_proto::TableAddress;
use std::sync::{Arc, Mutex};
use crate::database_arrow_helper::convert_array_to_vec_string;

#[derive(Debug, Clone)]
pub struct DuckDB {
Expand Down Expand Up @@ -219,65 +219,6 @@ impl DatabaseConnection for DuckDB {
}
}

pub(crate) fn convert_array_to_vec_string(
array: &[Arc<dyn Array>],
) -> Result<Vec<Vec<String>>, String> {
let num_rows = array[0].len();
let num_columns = array.len();
let mut rows = Vec::with_capacity(num_rows);
for _ in 0..num_rows {
let row = vec!["".to_string(); num_columns];
rows.push(row);
}

for (i, row) in rows.iter_mut().enumerate() {
for (j, value) in row.iter_mut().enumerate() {
let array = &array[j];
if let Some(string_array) = array.as_any().downcast_ref::<array::StringArray>() {
*value = string_array.value(i).to_string();
} else if let Some(int32_array) = array.as_any().downcast_ref::<array::Int32Array>() {
*value = int32_array.value(i).to_string();
} else if let Some(int64_array) = array.as_any().downcast_ref::<array::Int64Array>() {
*value = int64_array.value(i).to_string();
} else if let Some(float32_array) = array.as_any().downcast_ref::<array::Float32Array>()
{
*value = float32_array.value(i).to_string();
} else if let Some(float64_array) = array.as_any().downcast_ref::<array::Float64Array>()
{
*value = float64_array.value(i).to_string();
} else if let Some(boolean_array) = array.as_any().downcast_ref::<array::BooleanArray>()
{
*value = boolean_array.value(i).to_string();
} else if let Some(date_array) = array.as_any().downcast_ref::<array::Date64Array>() {
*value = date_array.value(i).to_string();
} else if let Some(date_array) = array.as_any().downcast_ref::<array::Date32Array>() {
*value = date_array.value(i).to_string();
} else if let Some(timestamp_array) = array
.as_any()
.downcast_ref::<array::TimestampMicrosecondArray>()
{
if timestamp_array.is_null(i) {
*value = "NULL".to_string();
} else {
let timestamp_micros = timestamp_array.value(i);
let datetime_utc = DateTime::<Utc>::from_timestamp(
timestamp_micros / 1_000_000,
(timestamp_micros % 1_000_000) as u32 * 1_000,
)
.ok_or("error converting timestamp to datetime")?;
*value = datetime_utc.format("%Y-%m-%d %H:%M:%S%.6f %Z").to_string();
}
} else {
let array_type = array.data_type();
return Err(format!("Unsupported array type: {:?}", array_type));
}
}
}

// Example for a specific array type, e.g., StringArray
Ok(rows)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion rust/quary-databases/src/databases_snowflake.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::databases_duckdb::convert_array_to_vec_string;
use crate::database_arrow_helper::convert_array_to_vec_string;
use async_trait::async_trait;
use quary_core::database_snowflake::{
validate_snowfalke_account_identifier, DatabaseQueryGeneratorSnowflake,
Expand Down
1 change: 1 addition & 0 deletions rust/quary-databases/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod databases_postgres;
pub mod databases_redshift;
mod databases_snowflake;
mod databases_sqlite;
mod database_arrow_helper;
Loading