Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aliasing to create named tuples #32

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This above is just a format change right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mostly formatting yes. there's an actual change at the bottom, and I guess cargo decided that warranted a reformat

}
}
.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
Loading