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

Error if subqueries contain more than one result #5651

Merged
merged 18 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
error if subqueries contain more than one result
  • Loading branch information
frankmcsherry committed Mar 29, 2021
commit a5d6a8974df558ada70a4e1cf240946c75ecff96
8 changes: 8 additions & 0 deletions src/expr/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,13 @@ pub enum EvalError {
Parse(ParseError),
ParseHex(ParseHexError),
Internal(String),
<<<<<<< HEAD
InfinityOutOfDomain(String),
NegativeOutOfDomain(String),
ZeroOutOfDomain(String),
=======
TooManyDamnRecords,
>>>>>>> ad2034333 (error if subqueries contain more than one result)
}

impl fmt::Display for EvalError {
Expand Down Expand Up @@ -956,6 +960,7 @@ impl fmt::Display for EvalError {
EvalError::Parse(e) => e.fmt(f),
EvalError::ParseHex(e) => e.fmt(f),
EvalError::Internal(s) => write!(f, "internal error: {}", s),
<<<<<<< HEAD
EvalError::InfinityOutOfDomain(s) => {
write!(f, "function {} is only defined for finite arguments", s)
}
Expand All @@ -980,6 +985,9 @@ impl EvalError {
"Input data is missing padding, is truncated, or is otherwise corrupted.".into(),
),
_ => None,
=======
EvalError::TooManyDamnRecords => write!(f, "more than one record produced in subquery"),
>>>>>>> ad2034333 (error if subqueries contain more than one result)
}
}
}
Expand Down
37 changes: 36 additions & 1 deletion src/sql/src/plan/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ impl HirScalarExpr {
);
SS::Column(inner.arity() - 1)
}

Select(expr) => {
*inner = branch(
id_gen,
Expand All @@ -659,9 +660,43 @@ impl HirScalarExpr {
// compute for every row in get_inner
.applied_to(id_gen, get_inner.clone(), col_map);
let col_type = select.typ().column_types.into_last();

let inner_arity = get_inner.arity();
let guarded = select.let_in(id_gen, |_id_gen, get_select| {
// We must determine a count for each `get_inner` prefix,
// and report an error if that count exceeds one.
let counts = get_select.clone().reduce(
(0..inner_arity).collect::<Vec<_>>(),
vec![expr::AggregateExpr {
func: expr::AggregateFunc::Count,
expr: expr::MirScalarExpr::literal_ok(
Datum::True,
ScalarType::Bool,
),
distinct: false,
}],
None,
);
// Errors should result from counts > 1.
let errors = counts
.filter(vec![expr::MirScalarExpr::Column(inner_arity).call_binary(
expr::MirScalarExpr::literal_ok(
Datum::Int64(1),
ScalarType::Int64,
),
expr::BinaryFunc::Gt,
)])
.project((0..inner_arity).collect::<Vec<_>>())
.map(vec![expr::MirScalarExpr::literal(
Err(expr::EvalError::TooManyDamnRecords),
col_type.clone().scalar_type,
)]);
// Return `get_select` and any errors added in.
get_select.union(errors)
});
// append Null to anything that didn't return any rows
let default = vec![(Datum::Null, col_type.nullable(true))];
get_inner.lookup(id_gen, select, default)
get_inner.lookup(id_gen, guarded, default)
},
);
SS::Column(inner.arity() - 1)
Expand Down