Skip to content

impl(bigquery): parse date and time related types#6033

Draft
alvarowolfx wants to merge 1 commit into
googleapis:mainfrom
alvarowolfx:impl-bq-from-sql-dates
Draft

impl(bigquery): parse date and time related types#6033
alvarowolfx wants to merge 1 commit into
googleapis:mainfrom
alvarowolfx:impl-bq-from-sql-dates

Conversation

@alvarowolfx

Copy link
Copy Markdown
Contributor

Towards #5844

@product-auto-label product-auto-label Bot added the api: bigquery Issues related to the BigQuery API. label Jul 10, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the FromSql trait for several types, including wkt::Timestamp, google_cloud_type::model::Date, google_cloud_type::model::TimeOfDay, and google_cloud_type::model::DateTime, to support converting BigQuery internal values to Rust types. It also adds comprehensive unit and integration tests. The review feedback correctly identifies that BigQuery's REST API returns TIMESTAMP values as decimal strings representing seconds since the epoch rather than integer microseconds. To prevent parsing failures, the reviewer suggests parsing these values as f64 and adjusting the parser and unit tests accordingly.

Comment on lines +141 to +171
impl FromSql for wkt::Timestamp {
fn from_sql(value: wkt::Value) -> Result<Self, ConvertError> {
match value {
wkt::Value::String(s) => {
let micros = s
.parse::<i64>()
.map_err(|e| ConvertError::Convert(Box::new(e)))?;
timestamp_from_micros(micros)
}
wkt::Value::Number(n) => {
let micros = n.as_i64().ok_or_else(|| {
ConvertError::Convert("timestamp number is not valid i64".into())
})?;
timestamp_from_micros(micros)
}
wkt::Value::Null => Err(ConvertError::NotNull),
other => Err(ConvertError::TypeMismatch {
expected: "string or number",
got: other,
}),
}
}
}

fn timestamp_from_micros(micros: i64) -> Result<wkt::Timestamp, ConvertError> {
wkt::Timestamp::new(
micros.div_euclid(1_000_000),
(micros.rem_euclid(1_000_000) * 1_000) as i32,
)
.map_err(|e| ConvertError::Convert(Box::new(e)))
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

BigQuery's REST API returns TIMESTAMP values as a decimal string representing seconds since the epoch (e.g., "1779982200.000000"), rather than microseconds as an integer string (e.g., "1779982200000000").

Parsing the string directly as i64 will fail with a parsing error due to the decimal point, causing query execution to fail when retrieving timestamp columns.

To fix this, parse the value as f64 (seconds since epoch) and convert it to seconds and nanoseconds. This also correctly handles negative timestamps (before the epoch) and subsecond precision.

impl FromSql for wkt::Timestamp {
    fn from_sql(value: wkt::Value) -> Result<Self, ConvertError> {
        match value {
            wkt::Value::String(s) => {
                let seconds = s
                    .parse::<f64>()
                    .map_err(|e| ConvertError::Convert(Box::new(e)))?;
                timestamp_from_seconds_f64(seconds)
            }
            wkt::Value::Number(n) => {
                let seconds = n.as_f64().ok_or_else(|| {
                    ConvertError::Convert("timestamp number is not valid f64".into())
                })?;
                timestamp_from_seconds_f64(seconds)
            }
            wkt::Value::Null => Err(ConvertError::NotNull),
            other => Err(ConvertError::TypeMismatch {
                expected: "string or number",
                got: other,
            }),
        }
    }
}

fn timestamp_from_seconds_f64(seconds_f64: f64) -> Result<wkt::Timestamp, ConvertError> {
    if !seconds_f64.is_finite() {
        return Err(ConvertError::Convert("timestamp float is not finite".into()));
    }
    let total_nanos = (seconds_f64 * 1_000_000_000.0).round() as i64;
    wkt::Timestamp::new(
        total_nanos.div_euclid(1_000_000_000),
        total_nanos.rem_euclid(1_000_000_000) as i32,
    )
    .map_err(|e| ConvertError::Convert(Box::new(e)))
}

Comment on lines +339 to +340
#[test_case(wkt::Value::String("1779982200000000".to_string()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp micro integer string")]
#[test_case(wkt::Value::Number(1779982200000000i64.into()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp micro integer number")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Update the unit test cases to use the correct BigQuery REST API timestamp format (seconds since epoch as a decimal string or integer) to match the updated parser implementation.

    #[test_case(wkt::Value::String("1779982200.000000".to_string()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp decimal string"]
    #[test_case(wkt::Value::Number(1779982200i64.into()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp integer number"]

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.96%. Comparing base (f17fa28) to head (b18db98).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6033   +/-   ##
=======================================
  Coverage   96.96%   96.96%           
=======================================
  Files         248      248           
  Lines       62379    62477   +98     
=======================================
+ Hits        60483    60580   +97     
- Misses       1896     1897    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the BigQuery API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant