Skip to content

Commit

Permalink
scope tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gil Mizrahi committed Aug 23, 2024
1 parent 1d28034 commit 9a6a29f
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 119 deletions.
32 changes: 28 additions & 4 deletions crates/query-engine/translation/src/translation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,42 @@ impl CurrentTableAndScope {
scope: vec![],
}
}
pub fn get(&self, i: usize) -> Result<&TableSourceAndReference, Error> {
if i == 0 {
pub fn get(&self, scope_index: usize) -> Result<&TableSourceAndReference, Error> {
if scope_index == 0 {
Ok(&self.current_table)
} else {
self.scope
.get(self.scope.len() - i)
.get(self.scope.len() - scope_index)
.ok_or(Error::ScopeOutOfRange {
requested: i,
requested: scope_index,
size: self.scope.len() + 1,
})
}
}
/// Rewind scope to get scope in index.
pub fn get_scope(&self, scope_index: usize) -> Result<CurrentTableAndScope, Error> {
if scope_index == 0 {
Ok(self.clone())
} else {
let mut scope = self.scope.clone();
for _ in 1..scope_index {
scope.pop().ok_or(Error::ScopeOutOfRange {
requested: scope_index,
size: self.scope.len() + 1,
})?;
}

let current_table = scope.pop().ok_or(Error::ScopeOutOfRange {
requested: scope_index,
size: self.scope.len() + 1,
})?;

Ok(CurrentTableAndScope {
current_table,
scope,
})
}
}
pub fn push(&self, table: TableSourceAndReference) -> Self {
let mut scope = self.scope.clone();
scope.push(self.current_table.clone());
Expand Down
37 changes: 22 additions & 15 deletions crates/query-engine/translation/src/translation/query/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,22 +475,29 @@ fn translate_comparison_value(
path,
scope,
} => {
if !path.is_empty() {
todo!("comparison value with path not supported");
}
if scope.is_some() {
todo!("comparison value with scope not supported");
}
// get the scope.
let (table_ref, joins) = match *scope {
Some(scope_index) if scope_index > 0 => {
let tables = tables.get_scope(scope_index)?;
translate_comparison_pathelements(env, state, &tables, &path)?
}
_ => translate_comparison_pathelements(env, state, tables, &path)?,
};

translate_comparison_target(
env,
state,
tables,
&models::ComparisonTarget::Column {
name: name.clone(),
field_path: field_path.clone(),
},
)
// get the unrelated table information from the metadata.
let collection_info = env.lookup_fields_info(&table_ref.source)?;
let ColumnInfo { name, .. } = collection_info.lookup_column(name)?;

Ok((
wrap_in_field_path(
&field_path.into(),
sql::ast::Expression::ColumnReference(sql::ast::ColumnReference::TableColumn {
table: table_ref.reference.clone(),
name,
}),
),
joins,
))
}
models::ComparisonValue::Scalar { value: json_value } => {
Ok((values::translate(env, state, json_value, typ)?, vec![]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@
"operatorKind": "equal",
"argumentType": "varchar",
"isInfix": true
},
"_like": {
"operatorName": "LIKE",
"operatorKind": "custom",
"argumentType": "varchar",
"isInfix": true
}
},
"typeRepresentation": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@
"type": "column",
"name": "Title"
},
"operator": "_eq",
"operator": "_like",
"value": {
"type": "scalar",
"value": "The album title (1)"
"value": "%t%"
}
},
{
Expand All @@ -97,23 +97,23 @@
"type": "column",
"name": "Name"
},
"operator": "_eq",
"operator": "_like",
"value": {
"type": "scalar",
"value": "The Artist name"
"value": "%o%"
}
},
{
"type": "binary_comparison_operator",
"column": {
"type": "column",
"name": "ArtistId"
"name": "Name"
},
"operator": "_lte",
"operator": "_eq",
"value": {
"type": "column",
"name": "AlbumId",
"scope": 1
"name": "Name",
"scope": 2
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,14 @@
{
"type": "binary_comparison_operator",
"column": {
"type": "root_collection_column",
"name": "artist_id"
"type": "column",
"name": "id"
},
"operator": "_eq",
"value": {
"type": "column",
"column": {
"type": "column",
"name": "id",
"path": []
}
"name": "artist_id",
"scope": 1
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ FROM
(
"%1_artist"."Name" = cast($1 as "pg_catalog"."varchar")
)
AND ("%0_album"."ArtistId" = "%1_artist"."ArtistId")
AND ("%1_artist"."ArtistId" = "%0_album"."ArtistId")
)
)
) AS "%3_rows"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ source: crates/query-engine/translation/tests/tests.rs
expression: result
---
SELECT
coalesce(json_agg(row_to_json("%14_universe")), '[]') AS "universe"
coalesce(json_agg(row_to_json("%11_universe")), '[]') AS "universe"
FROM
(
SELECT
*
FROM
(
SELECT
coalesce(json_agg(row_to_json("%15_rows")), '[]') AS "rows"
coalesce(json_agg(row_to_json("%12_rows")), '[]') AS "rows"
FROM
(
SELECT
Expand All @@ -30,125 +30,89 @@ FROM
FROM
(
SELECT
coalesce(json_agg(row_to_json("%12_rows")), '[]') AS "rows"
coalesce(json_agg(row_to_json("%9_rows")), '[]') AS "rows"
FROM
(
SELECT
"%7_Album"."Title" AS "album",
"%8_RELATIONSHIP_Artist"."Artist" AS "Artist"
"%4_Album"."Title" AS "album",
"%5_RELATIONSHIP_Artist"."Artist" AS "Artist"
FROM
"public"."Album" AS "%7_Album"
"public"."Album" AS "%4_Album"
LEFT OUTER JOIN LATERAL (
SELECT
row_to_json("%8_RELATIONSHIP_Artist") AS "Artist"
row_to_json("%5_RELATIONSHIP_Artist") AS "Artist"
FROM
(
SELECT
*
FROM
(
SELECT
coalesce(json_agg(row_to_json("%10_rows")), '[]') AS "rows"
coalesce(json_agg(row_to_json("%7_rows")), '[]') AS "rows"
FROM
(
SELECT
"%9_Artist"."Name" AS "artist",
"%9_Artist"."ArtistId" AS "ArtistId"
"%6_Artist"."Name" AS "artist",
"%6_Artist"."ArtistId" AS "ArtistId"
FROM
"public"."Artist" AS "%9_Artist"
"public"."Artist" AS "%6_Artist"
WHERE
("%7_Album"."ArtistId" = "%9_Artist"."ArtistId")
) AS "%10_rows"
) AS "%10_rows"
) AS "%8_RELATIONSHIP_Artist"
) AS "%8_RELATIONSHIP_Artist" ON ('true')
("%4_Album"."ArtistId" = "%6_Artist"."ArtistId")
) AS "%7_rows"
) AS "%7_rows"
) AS "%5_RELATIONSHIP_Artist"
) AS "%5_RELATIONSHIP_Artist" ON ('true')
WHERE
("%0_Track"."AlbumId" = "%7_Album"."AlbumId")
) AS "%12_rows"
) AS "%12_rows"
("%0_Track"."AlbumId" = "%4_Album"."AlbumId")
) AS "%9_rows"
) AS "%9_rows"
) AS "%1_RELATIONSHIP_Album"
) AS "%1_RELATIONSHIP_Album" ON ('true')
WHERE
EXISTS (
SELECT
1
1 AS "one"
FROM
"public"."Album" AS "%2_Album"
WHERE
(
SELECT
"%2_BOOLEXP_Album".*
FROM
(
SELECT
*
FROM
"public"."Album" AS "%2_BOOLEXP_Album"
WHERE
(
(
"%2_BOOLEXP_Album"."Title" = cast($1 as "pg_catalog"."varchar")
)
AND (
"%0_Track"."AlbumId" = "%2_BOOLEXP_Album"."AlbumId"
)
)
) AS "%2_BOOLEXP_Album"
) AS "%3_BOOLEXP_Album" FULL
OUTER JOIN LATERAL (
SELECT
"%5_BOOLEXP_Artist".*
FROM
(
(
"%2_Album"."Title" LIKE cast($1 as "pg_catalog"."varchar")
)
AND EXISTS (
SELECT
*
1 AS "one"
FROM
"public"."Album" AS "%4_BOOLEXP_Album"
"public"."Artist" AS "%3_Artist"
WHERE
(
(
"%4_BOOLEXP_Album"."Title" = cast($2 as "pg_catalog"."varchar")
)
AND (
"%0_Track"."AlbumId" = "%4_BOOLEXP_Album"."AlbumId"
(
"%3_Artist"."Name" LIKE cast($2 as "pg_catalog"."varchar")
)
AND ("%3_Artist"."Name" = "%0_Track"."Name")
)
AND ("%2_Album"."ArtistId" = "%3_Artist"."ArtistId")
)
) AS "%4_BOOLEXP_Album"
INNER JOIN LATERAL (
SELECT
*
FROM
"public"."Artist" AS "%5_BOOLEXP_Artist"
WHERE
(
(
"%5_BOOLEXP_Artist"."Name" = cast($3 as "pg_catalog"."varchar")
)
AND (
"%4_BOOLEXP_Album"."ArtistId" = "%5_BOOLEXP_Artist"."ArtistId"
)
)
) AS "%5_BOOLEXP_Artist" ON ('true')
) AS "%6_BOOLEXP_Artist" ON ('true')
WHERE
(
"%3_BOOLEXP_Album"."AlbumId" > "%6_BOOLEXP_Artist"."ArtistId"
)
)
AND ("%0_Track"."AlbumId" = "%2_Album"."AlbumId")
)
)
ORDER BY
"%0_Track"."TrackId" ASC
LIMIT
5
) AS "%15_rows"
) AS "%15_rows"
) AS "%14_universe";
) AS "%12_rows"
) AS "%12_rows"
) AS "%11_universe";

{
1: String(
"The album title (1)",
"%t%",
),
2: String(
"The album title (2)",
),
3: String(
"The Artist name",
"%o%",
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -5895,5 +5895,14 @@ expression: result
"name": "v1_insert_phone_numbers_response"
}
}
]
],
"capabilities": {
"query": {
"aggregates": {
"filter_by": {
"count_scalar_type": "int4"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5232,5 +5232,14 @@ expression: result
"name": "v1_insert_pg_extension_spatial_ref_sys_response"
}
}
]
],
"capabilities": {
"query": {
"aggregates": {
"filter_by": {
"count_scalar_type": "int4"
}
}
}
}
}
Loading

0 comments on commit 9a6a29f

Please sign in to comment.