Skip to content

Commit

Permalink
Fixing DateTime as Timestamp performance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Feb 22, 2025
1 parent 8390442 commit 0df965c
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 20 deletions.
2 changes: 1 addition & 1 deletion my-postgres-core/src/connection/connection_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ pub enum ConnectionStringFormat<'s> {
}

impl<'s> ConnectionStringFormat<'s> {
pub fn parse_and_detect(conn_string: &'s str) -> ConnectionStringFormat {
pub fn parse_and_detect(conn_string: &'s str) -> ConnectionStringFormat<'s> {
if conn_string.trim().starts_with(PREFIX) {
return ConnectionStringFormat::AsUrl(conn_string);
}
Expand Down
8 changes: 6 additions & 2 deletions my-postgres-core/src/connection/postgres_row_read_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn start_reading(
use crate::connection::get_sql_telemetry_tags;

pin_mut!(stream);

#[cfg(feature = "with-logs-and-telemetry")]
let mut read_ok_rows = 0;

loop {
Expand All @@ -65,7 +65,11 @@ async fn start_reading(

match read_result.unwrap() {
Ok(row) => {
read_ok_rows += 1;
#[cfg(feature = "with-logs-and-telemetry")]
{
read_ok_rows += 1;
}

if row.is_none() {
#[cfg(feature = "with-logs-and-telemetry")]
ctx.write_success(
Expand Down
2 changes: 1 addition & 1 deletion my-postgres-core/src/sql/select_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub fn fill_select_fields(sql: &mut String, items: &[SelectFieldValue]) {
sql.push_str("(extract(EPOCH FROM ");
sql.push_str(field_name.db_column_name);
sql.push_str(") * 1000000)::bigint as \"");
sql.push_str(field_name.db_column_name);
crate::utils::fill_adjusted_column_name(field_name.db_column_name, sql);
sql.push('"');
}
SelectFieldValue::DateTimeAsBigint(field_name) => {
Expand Down
8 changes: 6 additions & 2 deletions my-postgres-core/src/sql_select/from_db_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ impl<'s> FromDbRow<'s, DateTimeAsMicroseconds> for DateTimeAsMicroseconds {
column_name: DbColumnName,
_metadata: &Option<SqlValueMetadata>,
) -> DateTimeAsMicroseconds {
let unix_microseconds: i64 = row.get(column_name.db_column_name);
let mut db_column_name = String::new();
crate::utils::fill_adjusted_column_name(column_name.db_column_name, &mut db_column_name);
let unix_microseconds: i64 = row.get(db_column_name.as_str());
DateTimeAsMicroseconds::new(unix_microseconds)
}

Expand All @@ -258,7 +260,9 @@ impl<'s> FromDbRow<'s, DateTimeAsMicroseconds> for DateTimeAsMicroseconds {
column_name: DbColumnName,
_metadata: &Option<SqlValueMetadata>,
) -> Option<DateTimeAsMicroseconds> {
let unix_microseconds: Option<i64> = row.get(column_name.db_column_name);
let mut db_column_name = String::new();
crate::utils::fill_adjusted_column_name(column_name.db_column_name, &mut db_column_name);
let unix_microseconds: Option<i64> = row.get(db_column_name.as_str());
let unix_microseconds = unix_microseconds?;
Some(DateTimeAsMicroseconds::new(unix_microseconds))
}
Expand Down
6 changes: 3 additions & 3 deletions my-postgres-core/src/sql_select/select_with_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ pub struct SqlWithParams<'s> {
}

pub trait WithSqlParams<'s> {
fn inject_sql_params_data(&'s self, params: SqlValues) -> SqlWithParams;
fn inject_sql_params_data(&'s self, params: SqlValues) -> SqlWithParams<'s>;
}

impl<'s> WithSqlParams<'s> for String {
fn inject_sql_params_data(&'s self, params: SqlValues) -> SqlWithParams {
fn inject_sql_params_data(&'s self, params: SqlValues) -> SqlWithParams<'s> {
SqlWithParams { sql: self, params }
}
}

impl<'s> WithSqlParams<'s> for &'s str {
fn inject_sql_params_data(&'s self, params: SqlValues) -> SqlWithParams {
fn inject_sql_params_data(&'s self, params: SqlValues) -> SqlWithParams<'s> {
SqlWithParams { sql: self, params }
}
}
Expand Down
5 changes: 5 additions & 0 deletions my-postgres-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub fn get_case_and_model<'s>(
(case.unwrap(), model.unwrap())
}

pub fn fill_adjusted_column_name(column_name: &str, out: &mut String) {
out.push_str(column_name);
out.push_str("_at_adjusted");
}

/*
pub struct CaseAndModelValue {
case: JsonKeyValue,
Expand Down
6 changes: 3 additions & 3 deletions my-postgres-macros/src/postgres_struct_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub struct DbIndexField<'s> {
}

pub trait PostgresStructSchema<'s> {
fn get_fields(&'s self) -> Vec<&'s StructProperty>;
fn get_fields(&'s self) -> Vec<&'s StructProperty<'s>>;

fn get_name(&'s self) -> &'s TypeName;

fn get_select_properties_to_generate(
&'s self,
) -> Result<BTreeMap<String, Vec<&'s StructProperty>>, syn::Error> {
) -> Result<BTreeMap<String, Vec<&'s StructProperty<'s>>>, syn::Error> {
let mut result = BTreeMap::new();

for prop in self.get_fields() {
Expand Down Expand Up @@ -174,7 +174,7 @@ pub trait PostgresStructSchema<'s> {
}

impl<'s> PostgresStructSchema<'s> for StructureSchema<'s> {
fn get_fields(&'s self) -> Vec<&'s StructProperty> {
fn get_fields(&'s self) -> Vec<&'s StructProperty<'s>> {
let all = self.get_all();
let mut result = Vec::with_capacity(all.len());

Expand Down
9 changes: 2 additions & 7 deletions my-postgres-macros/src/table_schema/generate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use proc_macro2::TokenStream;

use types_reader::{ macros::{MacrosParameters, MacrosEnum}, StructureSchema};
use types_reader::{ macros::MacrosEnum, StructureSchema};

use crate::{postgres_struct_ext::PostgresStructPropertyExt, postgres_struct_schema::PostgresStructSchema};
#[derive(MacrosEnum)]
Expand All @@ -12,12 +12,7 @@ pub enum GenerateType{
Update,
}

#[derive(MacrosParameters)]
pub struct GenerateAdditionalUpdateModelAttributeParams {
#[default]
pub name: String,
pub param_type: GenerateType,
}



pub fn generate(ast: &syn::DeriveInput) -> Result<proc_macro::TokenStream, syn::Error> {
Expand Down
3 changes: 2 additions & 1 deletion my-postgres-tests/src/dto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mod test_json_field;
mod test_json_field_where_cases;
mod test_json_hashmap_dto;
mod test_primary_key;
mod test_various_select_attributes;
mod test_union;
mod test_various_select_attributes;
mod test_where_inline_case;
mod test_where_inline_case_with_no_value;
mod test_where_json_field;
Expand All @@ -25,4 +25,5 @@ mod where_from_real_life;
mod where_model;
mod where_model_with_ignore_if_none;
mod where_model_with_str;
mod where_model_with_timestamp;
mod where_model_with_vec;
34 changes: 34 additions & 0 deletions my-postgres-tests/src/dto/where_model_with_timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use my_postgres::macros::WhereDbModel;
use rust_extensions::date_time::DateTimeAsMicroseconds;

#[derive(WhereDbModel)]
pub struct FindOlderThanPsqlWhere {
#[operator("<")]
#[sql_type("timestamp")]
#[db_column_name("created_at")]
pub created_at: DateTimeAsMicroseconds,
#[limit]
pub limit: usize,
}

#[cfg(test)]
mod tests {
use my_postgres::{sql::SqlValues, sql_where::SqlWhereModel};
use rust_extensions::date_time::DateTimeAsMicroseconds;

use super::FindOlderThanPsqlWhere;

#[test]
fn test() {
let where_model = FindOlderThanPsqlWhere {
created_at: DateTimeAsMicroseconds::from_str("2024-05-12T12:34:56.789012").unwrap(),
limit: 10,
};

let mut params = SqlValues::new();
let mut sql = String::new();
where_model.fill_where_component(&mut sql, &mut params);

println!("sql: {}", sql);
}
}

0 comments on commit 0df965c

Please sign in to comment.