Skip to main content

risingwave_frontend/binder/expr/
subquery.rs

1// Copyright 2022 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_sqlparser::ast::Query;
16
17use crate::binder::{Binder, Clause};
18use crate::error::{ErrorCode, Result, bail_bind_error};
19use crate::expr::{ExprImpl, Subquery, SubqueryKind};
20
21impl Binder {
22    pub fn bind_subquery_expr(&mut self, query: &Query, kind: SubqueryKind) -> Result<ExprImpl> {
23        // A generated column expression is serialized directly without planning, so a subquery
24        // in it can never be unnested.
25        if let Some(clause @ Clause::GeneratedColumn) = self.context.clause {
26            return Err(ErrorCode::InvalidInputSyntax(format!(
27                "subqueries are not allowed in {}",
28                clause
29            ))
30            .into());
31        }
32        let query = self.bind_query(query)?;
33        if !matches!(kind, SubqueryKind::Existential | SubqueryKind::UpdateSet)
34            && query.data_types().len() != 1
35        {
36            bail_bind_error!("Subquery must return only one column");
37        }
38        Ok(Subquery::new(query, kind, self.context.scope).into())
39    }
40}