Skip to content

Commit

Permalink
Merge pull request #32 from hasura/fix-aliasing
Browse files Browse the repository at this point in the history
Add aliasing to create named tuples
  • Loading branch information
BenoitRanque authored Oct 9, 2024
2 parents 246028e + 2443226 commit 81000ac
Show file tree
Hide file tree
Showing 45 changed files with 288 additions and 217 deletions.
23 changes: 19 additions & 4 deletions crates/ndc-clickhouse/src/sql/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,26 +685,41 @@ impl fmt::Display for WindowSpec {
pub struct FunctionArg {
name: Option<Ident>,
value: FunctionArgExpr,
alias: Option<Ident>,
}

impl FunctionArg {
pub fn new(value: FunctionArgExpr) -> Self {
Self { value, name: None }
Self {
value,
name: None,
alias: None,
}
}
pub fn name(self, name: Ident) -> Self {
Self {
name: Some(name),
..self
}
}
pub fn with_alias(self, alias: Ident) -> Self {
Self {
alias: Some(alias),
..self
}
}
}

impl fmt::Display for FunctionArg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.name {
Some(name) => write!(f, "{name}={}", self.value),
None => write!(f, "{}", self.value),
if let Some(name) = &self.name {
write!(f, "{name}=")?;
}
write!(f, "{}", self.value)?;
if let Some(alias) = &self.alias {
write!(f, " AS {alias}")?;
}
Ok(())
}
}

Expand Down
99 changes: 53 additions & 46 deletions crates/ndc-clickhouse/src/sql/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,74 +198,81 @@ impl<'r, 'c> QueryBuilder<'r, 'c> {
Ident::new_quoted(format!("_field_{alias}")),
])
.into_arg()
.with_alias(Ident::new_quoted(alias.as_str()))
})
.collect();
Function::new_unquoted("tuple").args(args).into_expr()
}
.into_arg();

Some(
Function::new_unquoted("groupArray")
.args(vec![row])
.into_expr(),
.into_expr()
.into_arg()
.with_alias(Ident::new_quoted("rows")),
)
} else {
None
};

let aggregates = if let Some(aggregates) = &query.aggregates {
Some(if aggregates.is_empty() {
Function::new_unquoted("map").into_expr()
} else {
let args = aggregates
.iter()
.map(|(alias, aggregate)| {
Ok(match aggregate {
models::Aggregate::StarCount {} => Function::new_unquoted("COUNT")
.args(vec![FunctionArgExpr::Wildcard.into_arg()])
.into_expr(),
models::Aggregate::ColumnCount {
distinct,
column: _,
field_path: _,
} => {
let column = Expr::CompoundIdentifier(vec![
Ident::new_quoted("_row"),
Ident::new_quoted(format!("_agg_{alias}")),
]);
Function::new_unquoted("COUNT")
.args(vec![column.into_arg()])
.distinct(*distinct)
.into_expr()
}
models::Aggregate::SingleColumn {
function,
column: _,
field_path: _,
} => {
let column = Expr::CompoundIdentifier(vec![
Ident::new_quoted("_row"),
Ident::new_quoted(format!("_agg_{alias}")),
]);
apply_function(&aggregate_function(function)?, column)
Some(
if aggregates.is_empty() {
Function::new_unquoted("map").into_expr()
} else {
let args = aggregates
.iter()
.map(|(alias, aggregate)| {
Ok(match aggregate {
models::Aggregate::StarCount {} => Function::new_unquoted("COUNT")
.args(vec![FunctionArgExpr::Wildcard.into_arg()])
.into_expr(),
models::Aggregate::ColumnCount {
distinct,
column: _,
field_path: _,
} => {
let column = Expr::CompoundIdentifier(vec![
Ident::new_quoted("_row"),
Ident::new_quoted(format!("_agg_{alias}")),
]);
Function::new_unquoted("COUNT")
.args(vec![column.into_arg()])
.distinct(*distinct)
.into_expr()
}
models::Aggregate::SingleColumn {
function,
column: _,
field_path: _,
} => {
let column = Expr::CompoundIdentifier(vec![
Ident::new_quoted("_row"),
Ident::new_quoted(format!("_agg_{alias}")),
]);
apply_function(&aggregate_function(function)?, column)
}
}
}
.into_arg())
})
.collect::<Result<Vec<_>, QueryBuilderError>>()?;
Function::new_unquoted("tuple").args(args).into_expr()
})
.into_arg()
.with_alias(Ident::new_quoted(alias.as_str())))
})
.collect::<Result<Vec<_>, QueryBuilderError>>()?;
Function::new_unquoted("tuple").args(args).into_expr()
}
.into_arg()
.with_alias(Ident::new_quoted("aggregates")),
)
} else {
None
};

let rowset = match (fields, aggregates) {
(None, None) => Function::new_unquoted("map"),
(None, Some(aggregates)) => {
Function::new_unquoted("tuple").args(vec![aggregates.into_arg()])
}
(Some(fields), None) => Function::new_unquoted("tuple").args(vec![fields.into_arg()]),
(None, Some(aggregates)) => Function::new_unquoted("tuple").args(vec![aggregates]),
(Some(fields), None) => Function::new_unquoted("tuple").args(vec![fields]),
(Some(fields), Some(aggregates)) => {
Function::new_unquoted("tuple").args(vec![fields.into_arg(), aggregates.into_arg()])
Function::new_unquoted("tuple").args(vec![fields, aggregates])
}
}
.into_expr()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title",
"_row"."_field_Artist"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title",
"_row"."_field_Artist" AS "Artist"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand All @@ -38,8 +38,11 @@ FROM
SELECT
tuple(
groupArray(
tuple("_row"."_field_artistId", "_row"."_field_name")
)
tuple(
"_row"."_field_artistId" AS "artistId",
"_row"."_field_name" AS "name"
)
) AS "rows"
) AS "_rowset",
"_row"."_relkey_ArtistId" AS "_relkey_ArtistId"
FROM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_albumId",
"_row"."_field_artistId",
"_row"."_field_title",
"_row"."_field_Tracks"
"_row"."_field_albumId" AS "albumId",
"_row"."_field_artistId" AS "artistId",
"_row"."_field_title" AS "title",
"_row"."_field_Tracks" AS "Tracks"
)
)
) AS "rows"
) AS "_rowset"
FROM
(
Expand All @@ -39,11 +39,11 @@ FROM
tuple(
groupArray(
tuple(
"_row"."_field_trackId",
"_row"."_field_name",
"_row"."_field_unitPrice"
"_row"."_field_trackId" AS "trackId",
"_row"."_field_name" AS "name",
"_row"."_field_unitPrice" AS "unitPrice"
)
)
) AS "rows"
) AS "_rowset",
"_row"."_relkey_AlbumId" AS "_relkey_AlbumId"
FROM
Expand Down
Loading

0 comments on commit 81000ac

Please sign in to comment.