@@ -21,7 +21,9 @@ use super::{
2121use crate :: catalog:: { ColumnCatalog , ColumnRef , ColumnSummary , TableName } ;
2222use crate :: errors:: DatabaseError ;
2323use crate :: execution:: dql:: join:: joins_nullable;
24+ use crate :: expression:: agg:: AggKind ;
2425use crate :: expression:: { AliasType , BinaryOperator } ;
26+ use crate :: planner:: operator:: aggregate:: AggregateOperator ;
2527use crate :: planner:: operator:: function_scan:: FunctionScanOperator ;
2628use crate :: planner:: operator:: insert:: InsertOperator ;
2729use crate :: planner:: operator:: join:: JoinCondition ;
@@ -30,12 +32,14 @@ use crate::planner::operator::union::UnionOperator;
3032use crate :: planner:: { Childrens , LogicalPlan , SchemaOutput } ;
3133use crate :: storage:: Transaction ;
3234use crate :: types:: tuple:: { Schema , SchemaRef } ;
35+ use crate :: types:: value:: Utf8Type ;
36+ use crate :: types:: LogicalType :: Char ;
3337use crate :: types:: { ColumnId , LogicalType } ;
3438use itertools:: Itertools ;
3539use sqlparser:: ast:: {
36- Distinct , Expr , Ident , Join , JoinConstraint , JoinOperator , Offset , OrderByExpr , Query , Select ,
37- SelectInto , SelectItem , SetExpr , SetOperator , SetQuantifier , TableAlias , TableFactor ,
38- TableWithJoins ,
40+ CharLengthUnits , Distinct , Expr , Ident , Join , JoinConstraint , JoinOperator , Offset ,
41+ OrderByExpr , Query , Select , SelectInto , SelectItem , SetExpr , SetOperator , SetQuantifier ,
42+ TableAlias , TableFactor , TableWithJoins ,
3943} ;
4044
4145impl < ' a : ' b , ' b , T : Transaction , A : AsRef < [ ( & ' static str , DataValue ) ] > > Binder < ' a , ' b , T , A > {
@@ -599,6 +603,57 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
599603
600604 let ( mut plan, join_ty) = match sub_query {
601605 SubQueryType :: SubQuery ( plan) => ( plan, JoinType :: Inner ) ,
606+ SubQueryType :: ExistsSubQuery ( is_not, plan) => {
607+ let limit = LimitOperator :: build ( None , Some ( 1 ) , plan) ;
608+ let mut agg = AggregateOperator :: build (
609+ limit,
610+ vec ! [ ScalarExpression :: AggCall {
611+ distinct: false ,
612+ kind: AggKind :: Count ,
613+ args: vec![ ScalarExpression :: Constant ( DataValue :: Utf8 {
614+ value: "*" . to_string( ) ,
615+ ty: Utf8Type :: Fixed ( 1 ) ,
616+ unit: CharLengthUnits :: Characters ,
617+ } ) ] ,
618+ ty: LogicalType :: Integer ,
619+ } ] ,
620+ vec ! [ ] ,
621+ false ,
622+ ) ;
623+ let filter = FilterOperator :: build (
624+ ScalarExpression :: Binary {
625+ op : if is_not {
626+ BinaryOperator :: NotEq
627+ } else {
628+ BinaryOperator :: Eq
629+ } ,
630+ left_expr : Box :: new ( ScalarExpression :: ColumnRef (
631+ agg. output_schema ( ) [ 0 ] . clone ( ) ,
632+ ) ) ,
633+ right_expr : Box :: new ( ScalarExpression :: Constant ( DataValue :: Int32 (
634+ 1 ,
635+ ) ) ) ,
636+ evaluator : None ,
637+ ty : LogicalType :: Boolean ,
638+ } ,
639+ agg,
640+ false ,
641+ ) ;
642+ let projection = ProjectOperator {
643+ exprs : vec ! [ ScalarExpression :: Constant ( DataValue :: Int32 ( 1 ) ) ] ,
644+ } ;
645+ let plan = LogicalPlan :: new (
646+ Operator :: Project ( projection) ,
647+ Childrens :: Only ( filter) ,
648+ ) ;
649+ children = LJoinOperator :: build (
650+ children,
651+ plan,
652+ JoinCondition :: None ,
653+ JoinType :: Cross ,
654+ ) ;
655+ continue ;
656+ }
602657 SubQueryType :: InSubQuery ( is_not, plan) => {
603658 let join_ty = if is_not {
604659 JoinType :: LeftAnti
0 commit comments