support where exists and where not exists

This address #155
parent 3e161ea7
......@@ -2834,3 +2834,26 @@ func (r *emptyFieldsPlan) do(ctx *execCtx, f func(id interface{}, data []interfa
_, err = f(nil, v)
return
}
type wrapFilterPlan struct {
*filterDefaultPlan
}
func (r *wrapFilterPlan) do(ctx *execCtx, f func(id interface{}, data []interface{}) (bool, error)) (err error) {
var match bool
err = r.filterDefaultPlan.do(ctx, func(id interface{}, data []interface{}) (bool, error) {
if len(data) > 0 {
match = true
}
return false, nil
})
if match {
return r.filterDefaultPlan.do(ctx, f)
}
v := make([]interface{}, len(r.fieldNames()))
for i := range v {
v[i] = ""
}
_, err = f(nil, v)
return
}
......@@ -527,6 +527,7 @@ func (r *whereRset) planUnaryOp(x *unaryOperation) (plan, error) {
}
func (r *whereRset) plan(ctx *execCtx) (plan, error) {
o := r.src
if r.sel != nil {
var exists bool
p, err := r.sel.plan(ctx)
......@@ -542,11 +543,12 @@ func (r *whereRset) plan(ctx *execCtx) (plan, error) {
if err != nil {
return nil, err
}
if r.exists && exists {
return p, nil
if r.exists && exists || !r.exists && !exists {
return o, nil
}
np := &nullPlan{fields: p.fieldNames()}
return &emptyFieldsPlan{nullPlan: np}, nil
x := value{val: false}
return &wrapFilterPlan{&filterDefaultPlan{o, x, nil}}, nil
}
return r.planExpr(ctx)
}
......
......@@ -8404,38 +8404,46 @@ SELECT 42;
└Output field names [""]
---- 1348
SELECT * FROM t WHERE EXISTS ( SELECT * FROM t WHERE i == 2 );
SELECT * FROM t WHERE EXISTS ( SELECT * FROM t WHERE i == 2 ) ORDER BY i;
┌Iterate all rows of table "t"
└Output field names ["i"]
┌Filter on i == 2
│Possibly useful indices
│CREATE INDEX xt_i ON t(i);
┌Order by i,
└Output field names ["i"]
---- 1349
SELECT * FROM t WHERE EXISTS ( SELECT * FROM t WHERE i == 2 );
┌Iterate no rows
┌Iterate all rows of table "t"
└Output field names ["i"]
┌Filter on false
└Output field names ["i"]
---- 1350
SELECT * FROM t WHERE EXISTS ( SELECT * FROM t WHERE i == 4 );
┌Iterate no rows
┌Iterate all rows of table "t"
└Output field names ["i"]
┌Filter on false
└Output field names ["i"]
---- 1351
SELECT * FROM t WHERE NOT EXISTS ( SELECT * FROM t WHERE i == 2 );
┌Iterate no rows
┌Iterate all rows of table "t"
└Output field names ["i"]
┌Filter on false
└Output field names ["i"]
---- 1352
SELECT * FROM t WHERE NOT EXISTS ( SELECT * FROM t WHERE i == 4 );
┌Iterate no rows
SELECT * FROM t WHERE NOT EXISTS ( SELECT * FROM t WHERE i == 4 ) ORDER BY i;
┌Iterate all rows of table "t"
└Output field names ["i"]
┌Order by i,
└Output field names ["i"]
---- 1353
SELECT * FROM t WHERE true;
SELECT * FROM t WHERE true ORDER BY i;
┌Iterate all rows of table "t"
└Output field names ["i"]
┌Filter on true
└Output field names ["i"]
┌Order by i,
└Output field names ["i"]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment