Skip to content

impl(bigquery): add FromSql trait to read/convert columns from a Row#5924

Open
alvarowolfx wants to merge 1 commit into
googleapis:mainfrom
alvarowolfx:impl-bq-row-from-sql
Open

impl(bigquery): add FromSql trait to read/convert columns from a Row#5924
alvarowolfx wants to merge 1 commit into
googleapis:mainfrom
alvarowolfx:impl-bq-row-from-sql

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 Jun 22, 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 introduces the FromSql trait in src/bigquery/src/query/from_sql.rs to convert BigQuery internal wkt::Value types into Rust types such as String, i64, f64, bool, and Option<T>, along with comprehensive unit tests. The feedback suggests replacing .unwrap() with .expect() in a test case attribute to adhere to the repository's style guide regarding panic handling in tests.

FromSql::from_sql(value).map_err(TestConvertError::from)
}

#[test_case(wkt::Value::Number(serde_json::Number::from_f64(123.45).unwrap()) => Ok(123.45) ; "f64 from 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.

medium

According to the repository style guide, .unwrap() should be avoided in tests unless there is no other option, and .expect() should be preferred. Since this is inside a #[test_case] attribute where the ? operator cannot be used, please use .expect() with a descriptive message instead of .unwrap().

Suggested change
#[test_case(wkt::Value::Number(serde_json::Number::from_f64(123.45).unwrap()) => Ok(123.45) ; "f64 from number")]
#[test_case(wkt::Value::Number(serde_json::Number::from_f64(123.45).expect("valid f64")) => Ok(123.45) ; "f64 from number")]
References
  1. Panics: unwrap() and expect() should typically be avoided in production code and examples (use ? or handle errors). ... In tests, prefer ?. If that is not possible use .expect(). Only use .unwrap() as a last resort. (link)

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.90%. Comparing base (75c4db2) to head (619a076).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5924   +/-   ##
=======================================
  Coverage   97.90%   97.90%           
=======================================
  Files         234      235    +1     
  Lines       59940    60021   +81     
=======================================
+ Hits        58683    58764   +81     
  Misses       1257     1257           

☔ 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.

@alvarowolfx alvarowolfx marked this pull request as ready for review June 22, 2026 20:14
@alvarowolfx alvarowolfx requested a review from a team as a code owner June 22, 2026 20:14
Comment on lines +112 to +130
// Test-only representation of `ConvertError` that implements `PartialEq`.
// This allows testing error outcomes using `test_case` assertions without
// implementing `PartialEq` on the production `ConvertError`.
#[derive(Debug, PartialEq)]
enum TestConvertError {
NotNull,
TypeMismatch(&'static str),
Convert(String),
}

impl From<ConvertError> for TestConvertError {
fn from(err: ConvertError) -> Self {
match err {
ConvertError::NotNull => Self::NotNull,
ConvertError::TypeMismatch { expected, .. } => Self::TypeMismatch(expected),
ConvertError::Convert(e) => Self::Convert(e.to_string()),
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you try something like:

#[derive(...)]
#[cfg_attr(test, derive(PartialEq))]
enum ConvertError { ... }

Comment on lines +45 to +50
wkt::Value::Number(n) => n
.as_i64()
.ok_or_else(|| ConvertError::Convert("number is not a valid i64".into())),
wkt::Value::String(s) => s
.parse::<i64>()
.map_err(|e| ConvertError::Convert(Box::new(e))),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

comment: Ah, we try to accept both. That's good.

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.

2 participants