:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(propdefs): skip lowercasing already-lowercase property keys (#77909)
Eli Reismane95c87fecffd
diff --git a/rust/property-defs-rs/src/types.rs b/rust/property-defs-rs/src/types.rsindex 2e4be62bd634..cac71d9c5f13 100644--- a/rust/property-defs-rs/src/types.rs+++ b/rust/property-defs-rs/src/types.rs@@ -1,4 +1,4 @@-use std::{fmt, hash::Hash, str::FromStr, sync::LazyLock};+use std::{borrow::Cow, fmt, hash::Hash, str::FromStr, sync::LazyLock};use chrono::{DateTime, Duration, DurationRound, RoundingError, Utc};use regex::Regex;@@ -392,7 +392,20 @@ impl Event {}pub fn detect_property_type(key: &str, value: &Value) -> Option<PropertyValueType> {- let key = key.to_lowercase();+ // This runs for every property of every event, so the allocation matters. Borrow when the key+ // is already lowercase ASCII, which is effectively all real traffic. The ASCII gate is what+ // makes the borrow equivalent to an unconditional `to_lowercase()`: over ASCII with no+ // uppercase bytes, Unicode lowercasing is the identity, so no case analysis of the patterns+ // below is needed to see that the result is unchanged. Gating on `char::is_uppercase` instead+ // would reach the same answers, but only because every pattern below is an ASCII literal;+ // that argument has to be redone whenever a pattern changes, and it costs a char decode plus+ // a Unicode property lookup per character rather than a byte scan.+ let lowered: Cow<str> = if key.is_ascii() && !key.bytes().any(|b| b.is_ascii_uppercase()) {+ Cow::Borrowed(key)+ } else {+ Cow::Owned(key.to_lowercase())+ };+ let key: &str = &lowered;// There are a whole set of special cases here, taken from the TSif key.starts_with("utm_") || key.starts_with("$initial_utm_") {@@ -428,7 +441,7 @@ pub fn detect_property_type(key: &str, value: &Value) -> Option<PropertyValueTypreturn Some(PropertyValueType::String);}- if detect_timestamp_property_by_key_and_value(&key, value) {+ if detect_timestamp_property_by_key_and_value(key, value) {return Some(PropertyValueType::DateTime);}diff --git a/rust/property-defs-rs/tests/types.rs b/rust/property-defs-rs/tests/types.rsindex eb2691f2adf8..8d83baacb588 100644--- a/rust/property-defs-rs/tests/types.rs+++ b/rust/property-defs-rs/tests/types.rs@@ -427,6 +427,77 @@ fn test_bare_utm_properties_still_string() {}}+// Case normalization is what makes each special-case branch in detect_property_type match. Only+// the DATETIME-keyword branch had mixed-case coverage before (the "TIMESTAMP" assertion in+// test_property_timestamp_detection); the utm, feature-flag and survey cases are all spelled+// lowercase everywhere else. Each case here pairs a mixed-case key with a lowercase twin and a+// value that would classify differently if the key were left as-is, so dropping the conversion+// fails this instead of silently mistyping properties. The last case is non-ASCII, covering the+// owning fallback rather than the borrow.+#[rstest]+#[case(+ "UTM_Source",+ "utm_source",+ Value::Number(Number::from(12345)),+ PropertyValueType::String+)]+#[case(+ "$Initial_UTM_Campaign",+ "$initial_utm_campaign",+ Value::from("2023-12-13"),+ PropertyValueType::String+)]+#[case(+ "$FEATURE/My-Flag",+ "$feature/my-flag",+ Value::Bool(true),+ PropertyValueType::String+)]+#[case(+ "$Feature_Flag_Response",+ "$feature_flag_response",+ Value::Bool(true),+ PropertyValueType::String+)]+#[case(+ "$Survey_Response_2",+ "$survey_response_2",+ Value::Number(Number::from(7)),+ PropertyValueType::String+)]+// A numeric value only reads as a timestamp when the key carries a DATETIME keyword, so this is+// the case where normalizing "_At" to "_at" is the whole decision. The epoch is computed rather+// than fixed because the value-side check only accepts the last six months.+#[case(+ "Created_At",+ "created_at",+ Value::Number(Number::from(Utc::now().timestamp())),+ PropertyValueType::DateTime+)]+#[case(+ "UTM_SourceDž",+ "utm_sourceDž",+ Value::Number(Number::from(12345)),+ PropertyValueType::String+)]+fn test_property_type_detection_normalizes_key_case(+ #[case] mixed_case: &str,+ #[case] lower_case: &str,+ #[case] value: Value,+ #[case] expected: PropertyValueType,+) {+ assert_eq!(+ detect_property_type(mixed_case, &value),+ Some(expected.clone()),+ "expected {expected:?} for mixed-case key={mixed_case}, value={value}"+ );+ assert_eq!(+ detect_property_type(lower_case, &value),+ Some(expected.clone()),+ "expected {expected:?} for lowercase key={lower_case}, value={value}"+ );+}+#[test]fn test_property_keys_are_sanitized_of_null_bytes() {// Property keys with embedded null bytes must be sanitized before reaching Postgres,