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

fix: Raise an error on unavailable excludes #2342

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
24 changes: 10 additions & 14 deletions prql-compiler/src/sql/gen_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ pub(super) fn translate_select_items(

// excluded columns
let opts = (excluded.remove(&cid))
.and_then(|excluded| translate_exclude(ctx, excluded))
.unwrap_or_default();
.and_then(|x| translate_exclude(ctx, x).transpose())
.unwrap_or_else(|| Ok(WildcardAdditionalOptions::default()))?;

Ok(if ident.len() > 1 {
let mut object_name = ident;
Expand All @@ -158,27 +158,23 @@ pub(super) fn translate_select_items(
fn translate_exclude(
ctx: &mut Context,
excluded: HashSet<CId>,
) -> Option<WildcardAdditionalOptions> {
) -> Result<Option<WildcardAdditionalOptions>> {
let excluded = as_col_names(&excluded, &ctx.anchor);

let Some(supported) = ctx.dialect.column_exclude() else {
// TODO: eventually this should throw an error
// I don't want to do this now, because we have no way around it.
// We could also ask the user to add table definitions.
if log::log_enabled!(log::Level::Warn) {
let excluded = excluded.join(", ");

log::warn!("Columns {excluded} will be included with *, but were not requested.")
}
return None;
let excluded = excluded.join(", ");
// TODO: can we specify `span` here?
// TODO: can we get a nicer name for the dialect?
let dialect = &ctx.dialect;
return Err(Error::new_simple(format!("Excluding columns ({excluded}) is not supported by the current dialect, {dialect:?}")).with_help("Consider specifying the full set of columns prior with a `select`").into());
};

let mut excluded = excluded
.into_iter()
.map(|name| translate_ident_part(name.to_string(), ctx))
.collect_vec();

Some(match supported {
Ok(Some(match supported {
ColumnExclude::Exclude => WildcardAdditionalOptions {
opt_exclude: Some(ExcludeSelectItem::Multiple(excluded)),
..Default::default()
Expand All @@ -190,7 +186,7 @@ fn translate_exclude(
}),
..Default::default()
},
})
}))
}

fn as_col_names<'a>(cids: &'a HashSet<CId>, ctx: &'a AnchorContext) -> Vec<&'a str> {
Expand Down
1 change: 1 addition & 0 deletions prql-compiler/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test;
mod test_bad_error_messages;
mod test_error_messages;

pub use test::compile;
12 changes: 12 additions & 0 deletions prql-compiler/src/tests/test_error_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ fn test_hint_missing_args() {
───╯
"###)
}

#[test]
fn test_missing_exclude() {
// Not a great error message, would be better to have a span & a better
// name. Not quite bad enough to put in `bad_error_messages` yet though...
assert_display_snapshot!(compile(r###"
from tracks
select ![milliseconds,bytes]
"###).unwrap_err(), @r###"
Error: Excluding columns (milliseconds, bytes) is not supported by the current dialect, GenericDialect
"###)
}