Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelff committed Jan 22, 2024
1 parent 3f5d84c commit afb38ea
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 21 deletions.
6 changes: 3 additions & 3 deletions quaint/src/connector/connection_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,15 @@ impl SqlFamily {
(*BATCH_SIZE_OVERRIDE).unwrap_or(self.default_max_bind_values())
}

/// Get the default max number of bind parameters for a single query, in Wasm there's no
/// Get the max number of bind parameters for a single query, in Wasm there's no
/// environment, and we omit that knob.
#[cfg(target_arch = "wasm32")]
pub fn max_bind_values(&self) -> usize {
self.default_max_bind_values()
}

#[inline(always)]
fn default_max_bind_values(&self) -> usize {
/// Get the default max number of bind parameters for a single query.
pub fn default_max_bind_values(&self) -> usize {
match self {
#[cfg(feature = "postgresql")]
SqlFamily::Postgres => 32766,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ macro_rules! retry {
}
}};
}

#[macro_export]
macro_rules! with_id_excess {
($runner:expr, $query_template:expr) => {{
let max_bind_values = $runner
.max_bind_values()
.expect("Test expected to run only for relational databases.");

let cycle = |argn: usize| (argn + 1 % 10).to_string();
let id_list = (0..=max_bind_values).map(cycle).collect::<Vec<_>>().join(",");
$query_template.replace(":id_list:", &id_list)
}};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ mod not_in_batching {

assert_error!(
runner,
"query { findManyTestModel(where: { id: { notIn: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] } }) { id }}",
2029 // QueryParameterLimitExceeded
);
with_id_excess!(
runner,
"query { findManyTestModel(where: { id: { notIn: [:id_list:] } }) { id }}"
),
2029
); // QueryParameterLimitExceeded

Ok(())
}
Expand All @@ -28,9 +31,12 @@ mod not_in_batching_cockroachdb {

assert_error!(
runner,
"query { findManyTestModel(where: { id: { notIn: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] } }) { id }}",
2029 // QueryParameterLimitExceeded
);
with_id_excess!(
runner,
"query { findManyTestModel(where: { id: { notIn: [:id_list:] } }) { id }}"
),
2029
); // QueryParameterLimitExceeded

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ mod isb {

assert_error!(
runner,
r#"query {
findManyA(where: {id: { in: [5,4,3,2,1,1,1,2,3,4,5,6,7,6,5,4,3,2,1,2,3,4,5,6] }}, orderBy: { b: { as: { _count: asc } } }) { id }
}"#,
with_id_excess!(&runner, "query { findManyA(where: {id: { in: [:id_list:] }}, orderBy: { b: { as: { _count: asc } } } ) { id } }"),
2029 // QueryParameterLimitExceeded
);

Expand All @@ -109,9 +107,10 @@ mod isb {

assert_error!(
runner,
r#"query {
findManyA(where: {id: { in: [5,4,3,2,1,1,1,2,3,4,5,6,7,6,5,4,3,2,1,2,3,4,5,6] }}, orderBy: { _relevance: { fields: text, search: "something", sort: asc } }) { id }
}"#,
with_id_excess!(
&runner,
r#"query { findManyA(where: {id: { in: [:id_list:] }}, orderBy: { _relevance: { fields: text, search: "something", sort: asc } } ) { id } }"#
),
2029 // QueryParameterLimitExceeded
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,33 @@ impl ConnectorVersion {
}
}

pub(crate) fn sql_family(&self) -> Option<SqlFamily> {
/// The maximum number of rows allowed in a single insert query.
///
/// max_bind_values is overriden by the QUERY_BATCH_SIZE env var in targets other than WASM.
///
/// Connectors which underyling implementation is WASM don'thave any max_bind_values override
/// as there's no such thing as runtime environment.
///
/// From the PoV of the test binary, the target architecture is that of where the test runs,
/// generally x86_64, or aarch64, etc.
///
/// As a consequence there is an mismatch between the the max_bind_values as seen by the test
/// binary (overriden by the QUERY_BATCH_SIZE env var) and the max_bind_values as seen by the
/// WASM engine being exercised in those tests, through the RunnerExecutor::External test runner.
///
/// What we do in here, is returning the number of max_bind_values hat the connector under test
/// will use. i.e. if it's a WASM connector, the default, not overridable one. Otherwise the one
/// as seen by the test binary (which will be the same as the engine exercised)
pub fn max_bind_values(&self) -> Option<usize> {
if self.is_wasm() {
self.sql_family().map(|f| f.default_max_bind_values())
} else {
self.sql_family().map(|f| f.max_bind_values())
}
}

/// SQL family for the connector
fn sql_family(&self) -> Option<SqlFamily> {
match self {
Self::SqlServer(_) => Some(SqlFamily::Mssql),
Self::Postgres(_) => Some(SqlFamily::Postgres),
Expand All @@ -289,6 +315,17 @@ impl ConnectorVersion {
_ => None,
}
}

/// Determines if the connector uses a driver adapter implemented in Wasm
fn is_wasm(&self) -> bool {
matches!(
self,
Self::Postgres(Some(PostgresVersion::PgJsWasm))
| Self::Postgres(Some(PostgresVersion::NeonJsWasm))
| Self::Vitess(Some(VitessVersion::PlanetscaleJsWasm))
| Self::Sqlite(Some(SqliteVersion::LibsqlJsWasm))
)
}
}

impl fmt::Display for ConnectorVersion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
ENGINE_PROTOCOL,
};
use colored::Colorize;
use quaint::connector::SqlFamily;
use query_core::{
protocol::EngineProtocol,
schema::{self, QuerySchemaRef},
Expand Down Expand Up @@ -108,8 +107,8 @@ impl Runner {
self.query_schema.internal_data_model.schema.db.source()
}

pub fn sql_family(&self) -> Option<SqlFamily> {
self.version.sql_family()
pub fn max_bind_values(&self) -> Option<usize> {
self.connector_version().max_bind_values()
}

pub async fn load(
Expand Down
4 changes: 2 additions & 2 deletions query-engine/connectors/sql-query-connector/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ impl<'a> Context<'a> {
let max_insert_rows = sql_family.max_insert_rows();
let max_bind_values = sql_family.max_bind_values();

return Context {
Context {
connection_info,
trace_id,
max_insert_rows,
max_bind_values: Some(max_bind_values),
};
}
}

pub(crate) fn schema_name(&self) -> &str {
Expand Down

0 comments on commit afb38ea

Please sign in to comment.