Skip to content
Open
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
4 changes: 2 additions & 2 deletions rust/plex-pg-core/src/db_interpose_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ use pure_helpers::{
bytes_to_pg_hex_impl, contains_binary_bytes_impl, find_insert_column_index_impl,
format_epoch_to_datetime_utc_impl, is_aggregate_alias, is_blobs_db_path_impl,
is_junk_metadata_insert_impl, is_library_db_path_impl, is_library_or_blobs_db_path_impl,
is_related_items_query_impl, normalize_sql_literals_impl, pg_sql_has_timestamp_hint,
rewrite_server_library_uri_bytes, should_mask_collection_metadata_type_impl,
is_related_items_query_impl, normalize_sql_literals_impl, rewrite_server_library_uri_bytes,
should_mask_collection_metadata_type_impl,
};
pub(crate) use string_utils::cstr_to_str_or_empty;
use string_utils::{
Expand Down
38 changes: 27 additions & 11 deletions rust/plex-pg-core/src/db_interpose_helpers/pg_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::ffi::CStr;
use std::os::raw::{c_char, c_int, c_uint, c_void};

use super::{
cstr_to_str, format_epoch_to_datetime_utc_impl, is_aggregate_alias, pg_sql_has_timestamp_hint,
rewrite_server_library_uri_bytes, rust_decode_hex_bytes, write_i32_to_buf, write_i64_to_buf,
cstr_to_str, is_aggregate_alias, rewrite_server_library_uri_bytes, rust_decode_hex_bytes, write_i32_to_buf, write_i64_to_buf,
PGresult, SQLITE_NULL_CONST,
};

Expand All @@ -25,7 +24,7 @@ extern "C" {
pub fn rust_column_text_reformat_aggregate(
col_name: *const c_char,
oid: c_uint,
pg_sql: *const c_char,
_pg_sql: *const c_char,
source_value: *const c_char,
out: *mut c_char,
out_len: usize,
Expand All @@ -48,22 +47,22 @@ pub fn rust_column_text_reformat_aggregate(
}

if oid == 20 {
// Plex's SOCI layer reads integer columns through column_text() and
// parses the result. SQLite returns max()/min() of an integer column
// as plain decimal text, so do the same. (This used to render
// max(viewed_at) as a datetime whenever the SQL mentioned "_at"
// anywhere, which Plex parsed as the year 2026 and dropped every show
// from Continue Watching.)
let val = pg_text_to_int64_impl(source_value);
let pg_sql = cstr_to_str(pg_sql).unwrap_or("");
if (col.eq_ignore_ascii_case("max") || col.eq_ignore_ascii_case("min"))
&& !pg_sql.is_empty()
&& pg_sql_has_timestamp_hint(pg_sql)
&& format_epoch_to_datetime_utc_impl(val, out, out_len) != 0
{
return 1;
}
return c_int::from(write_i64_to_buf(out, out_len, val));
}

let val = pg_text_to_int_impl(source_value);
c_int::from(write_i32_to_buf(out, out_len, val))
}

const PG_BOOL_OID: c_uint = 16;

pub fn rust_column_text_transform(
col_name: *const c_char,
oid: c_uint,
Expand All @@ -85,6 +84,23 @@ pub fn rust_column_text_transform(
return -1;
}

// PostgreSQL booleans arrive as "t"/"f"; SQLite has no boolean type and
// yields 1/0 (e.g. SELECT EXISTS(...)). Plex parses the text, so match it.
if oid == PG_BOOL_OID && out_len >= 2 {
let digit = match bytes {
b"t" => Some(b'1'),
b"f" => Some(b'0'),
_ => None,
};
if let Some(d) = digit {
unsafe {
*out.cast::<u8>() = d;
*out.cast::<u8>().add(1) = 0;
}
return 1;
}
}

if rust_column_text_reformat_aggregate(col_name, oid, pg_sql, source_value, out, out_len) != 0 {
return 1;
}
Expand Down
7 changes: 0 additions & 7 deletions rust/plex-pg-core/src/db_interpose_helpers/pure_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ pub(super) fn is_aggregate_alias(col: &str) -> bool {
.any(|prefix| lower.starts_with(prefix))
}

pub(super) fn pg_sql_has_timestamp_hint(pg_sql: &str) -> bool {
pg_sql.contains("_at")
|| pg_sql.contains("changed_at")
|| pg_sql.contains("updated_at")
|| pg_sql.contains("created_at")
}

pub(super) fn normalize_sql_literals_impl(sql: &str) -> Option<(String, Vec<String>)> {
const MAX_NORMALIZED_PARAMS: usize = 32;

Expand Down
64 changes: 64 additions & 0 deletions rust/plex-pg-core/src/db_interpose_helpers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,39 @@ fn expected_sqlite_type_for_decltype_unknown_returns_negative() {
assert!(rust_expected_sqlite_type_for_decltype(std::ptr::null()) < 0);
}

fn text_transform(oid: u32, src: &str) -> (c_int, String) {
let col = c("exists");
let sql = c("SELECT EXISTS (SELECT 1 FROM t)");
let src_c = c(src);
let mut out = [0 as c_char; 32];
let rc = rust_column_text_transform(
col.as_ptr(),
oid,
sql.as_ptr(),
src_c.as_ptr(),
src.len(),
out.as_mut_ptr(),
out.len(),
);
let out_s = unsafe { CStr::from_ptr(out.as_ptr()) }
.to_string_lossy()
.into_owned();
(rc, out_s)
}

#[test]
fn column_text_transform_converts_pg_bool_to_sqlite_integer_text() {
assert_eq!(text_transform(16, "t"), (1, "1".to_string()));
assert_eq!(text_transform(16, "f"), (1, "0".to_string()));
}

#[test]
fn column_text_transform_leaves_non_bool_t_and_f_alone() {
// A text column (OID 25) holding the letter "t" must not become "1".
assert_eq!(text_transform(25, "t").0, 0);
assert_eq!(text_transform(25, "f").0, 0);
}

#[test]
fn column_text_reformat_aggregate_int8() {
let col = c("count");
Expand All @@ -708,6 +741,37 @@ fn column_text_reformat_aggregate_int8() {
assert_eq!(out_s, "123");
}

#[test]
fn column_text_reformat_aggregate_max_of_epoch_stays_decimal() {
// Plex's SOCI layer parses integer columns from column_text(); SQLite
// returns max(viewed_at) as plain digits. A datetime string here makes
// Plex read the year (2026) as an epoch and drop every show from
// Continue Watching. The SQL mentions "_at" elsewhere (it selects
// originally_available_at), which used to trigger the datetime rendering.
let col = c("max");
let sql = c(
"SELECT grandparents.id, metadata_item_views.originally_available_at, max(viewed_at) \
FROM metadata_item_views GROUP BY 1, 2",
);
let src = c("1785372260");
let mut out = [0 as c_char; 32];

let rc = rust_column_text_reformat_aggregate(
col.as_ptr(),
20,
sql.as_ptr(),
src.as_ptr(),
out.as_mut_ptr(),
out.len(),
);

assert_eq!(rc, 1);
let out_s = unsafe { CStr::from_ptr(out.as_ptr()) }
.to_string_lossy()
.into_owned();
assert_eq!(out_s, "1785372260");
}

#[test]
fn column_text_reformat_aggregate_alias_expression_int8() {
let col = c("max(year)");
Expand Down