Esempio n. 1
0
        public override bool Appliable(CGroupMember expr)
        {
            LogicScanTable log = expr.logic_ as LogicScanTable;

            if (log != null && log.filter_ != null)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 2
0
        public override ulong LogicScanTableCE(LogicScanTable node)
        {
            var nrows = Catalog.sysstat_.EstCardinality(node.tabref_.relname_);

            if (node.filter_ != null)
            {
                var selectivity = node.filter_.EstSelectivity();
                nrows = (ulong)(selectivity * nrows);
            }
            return(Math.Max(1, nrows));
        }
Esempio n. 3
0
        // from clause -
        //  pair each from item with cross join, their join conditions will be handled
        //  with where clause processing.
        //
        LogicNode transformFromClause()
        {
            LogicNode transformOneFrom(TableRef tab)
            {
                LogicNode from;

                switch (tab)
                {
                case BaseTableRef bref:
                    if (bref.Table().source_ == TableDef.TableSource.Table)
                    {
                        from = new LogicScanTable(bref);
                    }
                    else
                    {
                        from = new LogicScanStream(bref);
                    }
                    if (bref.tableSample_ != null)
                    {
                        from = new LogicSampleScan(from, bref.tableSample_);
                    }
                    break;

                case ExternalTableRef eref:
                    from = new LogicScanFile(eref);
                    break;

                case CTEQueryRef cref:
                    if (this is CteSelectStmt css && css.CteId() == cref.cte_.cteId_)
                    {
                        cref.query_.cteInfo_ = cteInfo_;
                        var        ctePlan = cref.query_.CreatePlan();
                        string     alias   = null;
                        NamedQuery key;

                        alias = cref.alias_;
                        key   = new NamedQuery(cref.query_, alias, NamedQuery.QueryType.FROM);

                        from = new LogicFromQuery(cref, ctePlan);
                        subQueries_.Add(key);
                        if (!fromQueries_.ContainsKey(key))
                        {
                            fromQueries_.Add(key, from as LogicFromQuery);
                        }
                    }
                    else
                    {
                        from = new LogicCteConsumer(cteInfo_.GetCteInfoEntryByCteId(cref.cte_.cteId_), cref);
                    }
                    break;
Esempio n. 4
0
        public override CGroupMember Apply(CGroupMember expr)
        {
            LogicScanTable log   = expr.logic_ as LogicScanTable;
            var            index = log.filter_.FilterCanUseIndex(log.tabref_);

            if (index is null)
            {
                return(expr);
            }
            else
            {
                var phy = new PhysicIndexSeek(log, index);
                return(new CGroupMember(phy, expr.group_));
            }
        }
Esempio n. 5
0
        // from clause -
        //  pair each from item with cross join, their join conditions will be handled
        //  with where clause processing.
        //
        LogicNode transformFromClause()
        {
            LogicNode transformOneFrom(TableRef tab)
            {
                LogicNode from;

                switch (tab)
                {
                case BaseTableRef bref:
                    if (bref.Table().source_ == TableDef.TableSource.Table)
                    {
                        from = new LogicScanTable(bref);
                    }
                    else
                    {
                        from = new LogicScanStream(bref);
                    }
                    if (bref.tableSample_ != null)
                    {
                        from = new LogicSampleScan(from, bref.tableSample_);
                    }
                    break;

                case ExternalTableRef eref:
                    from = new LogicScanFile(eref);
                    break;

                case QueryRef qref:
                    var plan = qref.query_.CreatePlan();
                    if (qref is FromQueryRef && queryOpt_.optimize_.remove_from_)
                    {
                        from = plan;
                    }
                    else
                    {
                        string alias = null;
                        if (qref is CTEQueryRef cq)
                        {
                            alias = cq.alias_;
                        }
                        else if (qref is FromQueryRef fq)
                        {
                            alias = fq.alias_;
                        }
                        var key = new NamedQuery(qref.query_, alias);
                        from = new LogicFromQuery(qref, plan);
                        subQueries_.Add(key);

                        // if from CTE, then it could be duplicates
                        if (!fromQueries_.ContainsKey(key))
                        {
                            fromQueries_.Add(key, from as LogicFromQuery);
                        }
                    }
                    break;

                case JoinQueryRef jref:
                    // We will form join group on all tables and put a filter on top
                    // of the joins as a normalized form for later processing.
                    //
                    //      from a join b on a1=b1 or a3=b3 join c on a2=c2;
                    //   => from a , b, c where  (a1=b1 or a3=b3) and a2=c2;
                    //
                    LogicJoin subjoin    = new LogicJoin(null, null);
                    Expr      filterexpr = null;
                    for (int i = 0; i < jref.tables_.Count; i++)
                    {
                        LogicNode t        = transformOneFrom(jref.tables_[i]);
                        var       children = subjoin.children_;
                        if (children[0] is null)
                        {
                            children[0] = t;
                        }
                        else
                        {
                            if (children[1] is null)
                            {
                                children[1] = t;
                            }
                            else
                            {
                                subjoin = new LogicJoin(t, subjoin);
                            }
                            subjoin.type_ = jref.joinops_[i - 1];
                            filterexpr    = filterexpr.AddAndFilter(jref.constraints_[i - 1]);
                        }
                    }
                    Debug.Assert(filterexpr != null);
                    from = new LogicFilter(subjoin, filterexpr);
                    break;

                default:
                    throw new InvalidProgramException();
                }

                return(from);
            }

            LogicNode root;

            if (from_.Count >= 2)
            {
                var join     = new LogicJoin(null, null);
                var children = join.children_;
                from_.ForEach(x =>
                {
                    LogicNode from = transformOneFrom(x);
                    if (children[0] is null)
                    {
                        children[0] = from;
                    }
                    else
                    {
                        children[1] = (children[1] is null) ? from :
                                      new LogicJoin(from, children[1]);
                    }
                });
                root = join;
            }
            else if (from_.Count == 1)
            {
                root = transformOneFrom(from_[0]);
            }
            else
            {
                root = new LogicResult(selection_);
            }

            // distributed plan is required if any table is distributed, subquery
            // referenced tables are not counted here. So we may have the query
            // shape with main queyr not distributed but subquery is.
            //
            bool hasdtable      = false;
            bool onlyreplicated = true;

            from_.ForEach(x => checkifHasdtableAndifOnlyReplicated(x, ref hasdtable, ref onlyreplicated));
            if (hasdtable)
            {
                Debug.Assert(!distributed_);
                distributed_ = true;

                // distributed table query can also use memo
                // remote exchange is considered in memo optimization
                queryOpt_.optimize_.memo_use_remoteexchange_ = true;

                if (onlyreplicated)
                {
                    root = new LogicGather(root, new List <int> {
                        0
                    });
                }
                else
                {
                    root = new LogicGather(root);
                }
            }

            return(root);
        }
Esempio n. 6
0
 public abstract ulong LogicScanTableCE(LogicScanTable node);
Esempio n. 7
0
    public static void Run(SQLStatement stmt, ExecContext context)
    {
        PhysicCollect   PhysicCollect108   = stmt.physicPlan_.LocateNode("108") as PhysicCollect;
        PhysicProfiling PhysicProfiling109 = stmt.physicPlan_.LocateNode("109") as PhysicProfiling;
        LogicLimit      LogicLimit109      = PhysicProfiling109.logic_ as LogicLimit;
        var             filter109          = LogicLimit109.filter_;
        var             output109          = LogicLimit109.output_;
        PhysicLimit     PhysicLimit110     = stmt.physicPlan_.LocateNode("110") as PhysicLimit;
        LogicLimit      LogicLimit110      = PhysicLimit110.logic_ as LogicLimit;
        var             filter110          = LogicLimit110.filter_;
        var             output110          = LogicLimit110.output_;
        PhysicProfiling PhysicProfiling111 = stmt.physicPlan_.LocateNode("111") as PhysicProfiling;
        LogicAgg        LogicAgg111        = PhysicProfiling111.logic_ as LogicAgg;
        var             filter111          = LogicAgg111.filter_;
        var             output111          = LogicAgg111.output_;
        PhysicHashAgg   PhysicHashAgg112   = stmt.physicPlan_.LocateNode("112") as PhysicHashAgg;
        LogicAgg        LogicAgg112        = PhysicHashAgg112.logic_ as LogicAgg;
        var             filter112          = LogicAgg112.filter_;
        var             output112          = LogicAgg112.output_;
        PhysicProfiling PhysicProfiling113 = stmt.physicPlan_.LocateNode("113") as PhysicProfiling;
        LogicJoin       LogicJoin113       = PhysicProfiling113.logic_ as LogicJoin;
        var             filter113          = LogicJoin113.filter_;
        var             output113          = LogicJoin113.output_;
        PhysicHashJoin  PhysicHashJoin114  = stmt.physicPlan_.LocateNode("114") as PhysicHashJoin;
        LogicJoin       LogicJoin114       = PhysicHashJoin114.logic_ as LogicJoin;
        var             filter114          = LogicJoin114.filter_;
        var             output114          = LogicJoin114.output_;
        PhysicProfiling PhysicProfiling115 = stmt.physicPlan_.LocateNode("115") as PhysicProfiling;
        LogicJoin       LogicJoin115       = PhysicProfiling115.logic_ as LogicJoin;
        var             filter115          = LogicJoin115.filter_;
        var             output115          = LogicJoin115.output_;
        PhysicHashJoin  PhysicHashJoin116  = stmt.physicPlan_.LocateNode("116") as PhysicHashJoin;
        LogicJoin       LogicJoin116       = PhysicHashJoin116.logic_ as LogicJoin;
        var             filter116          = LogicJoin116.filter_;
        var             output116          = LogicJoin116.output_;
        PhysicProfiling PhysicProfiling117 = stmt.physicPlan_.LocateNode("117") as PhysicProfiling;
        LogicScanTable  LogicScanTable117  = PhysicProfiling117.logic_ as LogicScanTable;
        var             filter117          = LogicScanTable117.filter_;
        var             output117          = LogicScanTable117.output_;
        PhysicScanTable PhysicScanTablea   = stmt.physicPlan_.LocateNode("a") as PhysicScanTable;
        LogicScanTable  LogicScanTablea    = PhysicScanTablea.logic_ as LogicScanTable;
        var             filtera            = LogicScanTablea.filter_;
        var             outputa            = LogicScanTablea.output_;
        PhysicProfiling PhysicProfiling118 = stmt.physicPlan_.LocateNode("118") as PhysicProfiling;
        LogicScanTable  LogicScanTable118  = PhysicProfiling118.logic_ as LogicScanTable;
        var             filter118          = LogicScanTable118.filter_;
        var             output118          = LogicScanTable118.output_;
        PhysicScanTable PhysicScanTablec   = stmt.physicPlan_.LocateNode("c") as PhysicScanTable;
        LogicScanTable  LogicScanTablec    = PhysicScanTablec.logic_ as LogicScanTable;
        var             filterc            = LogicScanTablec.filter_;
        var             outputc            = LogicScanTablec.output_;
        var             hm116 = new Dictionary <KeyList, List <TaggedRow> >();
        PhysicProfiling PhysicProfiling119 = stmt.physicPlan_.LocateNode("119") as PhysicProfiling;
        LogicScanTable  LogicScanTable119  = PhysicProfiling119.logic_ as LogicScanTable;
        var             filter119          = LogicScanTable119.filter_;
        var             output119          = LogicScanTable119.output_;
        PhysicScanTable PhysicScanTableb   = stmt.physicPlan_.LocateNode("b") as PhysicScanTable;
        LogicScanTable  LogicScanTableb    = PhysicScanTableb.logic_ as LogicScanTable;
        var             filterb            = LogicScanTableb.filter_;
        var             outputb            = LogicScanTableb.output_;
        var             hm114       = new Dictionary <KeyList, List <TaggedRow> >();
        var             aggrcore112 = LogicAgg112.aggrFns_;
        var             hm112       = new Dictionary <KeyList, Row>();
        var             nrows110    = 0;

        PhysicProfiling109.nloops_++;
        PhysicProfiling111.nloops_++;
        PhysicProfiling113.nloops_++;
        PhysicProfiling115.nloops_++;
        PhysicProfiling117.nloops_++;
        var heapa = (LogicScanTablea.tabref_).Table().heap_.GetEnumerator();

        for (;;)
        {
            Row ra = null;
            if (context.stop_)
            {
                break;
            }
            if (heapa.MoveNext())
            {
                ra = heapa.Current;
            }
            else
            {
                break;
            }
            {
                {
                    // projection on PhysicScanTablea: Output: a.a1[0],a.a2[1]
                    Row rproj = new Row(2);
                    rproj[0] = ra[0];
                    rproj[1] = ra[1];
                    ra       = rproj;
                }

                PhysicProfiling117.nrows_++;
                var r117    = ra;
                var keys116 = KeyList.ComputeKeys(context, LogicJoin116.leftKeys_, r117);
                if (hm116.TryGetValue(keys116, out List <TaggedRow> exist))
                {
                    exist.Add(new TaggedRow(r117));
                }
                else
                {
                    var rows = new List <TaggedRow>();
                    rows.Add(new TaggedRow(r117));
                    hm116.Add(keys116, rows);
                }
            }
        }

        if (hm116.Count == 0)
        {
            return;
        }
        PhysicProfiling118.nloops_++;
        var heapc = (LogicScanTablec.tabref_).Table().heap_.GetEnumerator();

        for (;;)
        {
            Row rc = null;
            if (context.stop_)
            {
                break;
            }
            if (heapc.MoveNext())
            {
                rc = heapc.Current;
            }
            else
            {
                break;
            }
            {
                {
                    // projection on PhysicScanTablec: Output: c.c2[1]
                    Row rproj = new Row(1);
                    rproj[0] = rc[1];
                    rc       = rproj;
                }

                PhysicProfiling118.nrows_++;
                var r118 = rc;
                if (context.stop_)
                {
                    return;
                }
                Row  fakel116         = new Row(2);
                Row  r116             = new Row(fakel116, r118);
                var  keys116          = KeyList.ComputeKeys(context, LogicJoin116.rightKeys_, r116);
                bool foundOneMatch116 = false;
                if (hm116.TryGetValue(keys116, out List <TaggedRow> exist116))
                {
                    foundOneMatch116 = true;
                    foreach (var v116 in exist116)
                    {
                        r116 = new Row(v116.row_, r118);
                        {
                            // projection on PhysicHashJoin116: Output: a.a1[0],a.a2[1]
                            Row rproj = new Row(2);
                            rproj[0] = r116[0];
                            rproj[1] = r116[1];
                            r116     = rproj;
                        }

                        PhysicProfiling115.nrows_++;
                        var r115    = r116;
                        var keys114 = KeyList.ComputeKeys(context, LogicJoin114.leftKeys_, r115);
                        if (hm114.TryGetValue(keys114, out List <TaggedRow> exist))
                        {
                            exist.Add(new TaggedRow(r115));
                        }
                        else
                        {
                            var rows = new List <TaggedRow>();
                            rows.Add(new TaggedRow(r115));
                            hm114.Add(keys114, rows);
                        }
                    }
                }
                else
                {
                    // no match for antisemi
                }
            }
        }

        if (hm114.Count == 0)
        {
            return;
        }
        PhysicProfiling119.nloops_++;
        var heapb = (LogicScanTableb.tabref_).Table().heap_.GetEnumerator();

        for (;;)
        {
            Row rb = null;
            if (context.stop_)
            {
                break;
            }
            if (heapb.MoveNext())
            {
                rb = heapb.Current;
            }
            else
            {
                break;
            }
            {
                {
                    // projection on PhysicScanTableb: Output: b.b1[0]
                    Row rproj = new Row(1);
                    rproj[0] = rb[0];
                    rb       = rproj;
                }

                PhysicProfiling119.nrows_++;
                var r119 = rb;
                if (context.stop_)
                {
                    return;
                }
                Row  fakel114         = new Row(2);
                Row  r114             = new Row(fakel114, r119);
                var  keys114          = KeyList.ComputeKeys(context, LogicJoin114.rightKeys_, r114);
                bool foundOneMatch114 = false;
                if (hm114.TryGetValue(keys114, out List <TaggedRow> exist114))
                {
                    foundOneMatch114 = true;
                    foreach (var v114 in exist114)
                    {
                        r114 = new Row(v114.row_, r119);
                        {
                            // projection on PhysicHashJoin114: Output: a.a1[0],a.a2[1]
                            Row rproj = new Row(2);
                            rproj[0] = r114[0];
                            rproj[1] = r114[1];
                            r114     = rproj;
                        }

                        PhysicProfiling113.nrows_++;
                        var r113 = r114;
                        var keys = KeyList.ComputeKeys(context, LogicAgg112.keys_, r113);
                        if (hm112.TryGetValue(keys, out Row exist))
                        {
                            for (int i = 0; i < 1; i++)
                            {
                                var old = exist[i];
                                exist[i] = aggrcore112[i].Accum(context, old, r113);
                            }
                        }
                        else
                        {
                            hm112.Add(keys, PhysicHashAgg112.AggrCoreToRow(r113));
                            exist = hm112[keys];
                            for (int i = 0; i < 1; i++)
                            {
                                exist[i] = aggrcore112[i].Init(context, r113);
                            }
                        }
                    }
                }
                else
                {
                    // no match for antisemi
                }
            }
        }

        foreach (var v112 in hm112)
        {
            if (context.stop_)
            {
                break;
            }
            var keys112    = v112.Key;
            Row aggvals112 = v112.Value;
            for (int i = 0; i < 1; i++)
            {
                aggvals112[i] = aggrcore112[i].Finalize(context, aggvals112[i]);
            }
            var r112 = new Row(keys112, aggvals112);
            if (true || LogicAgg112.having_.Exec(context, r112) is true)
            {
                {
                    // projection on PhysicHashAgg112: Output: {a.a2}[0]*2,{count(a.a1)}[1],repeat('a',{a.a2}[0])
                    Row rproj = new Row(3);
                    rproj[0] = ((dynamic)r112[0] * (dynamic)2);
                    rproj[1] = r112[1];
                    rproj[2] = ExprSearch.Locate("74").Exec(context, r112) /*repeat('a',{a.a2}[0])*/;
                    r112     = rproj;
                }

                PhysicProfiling111.nrows_++;
                var r111 = r112;
                nrows110++;
                Debug.Assert(nrows110 <= 2);
                if (nrows110 == 2)
                {
                    context.stop_ = true;
                }
                var r110 = r111;
                PhysicProfiling109.nrows_++;
                var r109 = r110;
                Row newr = new Row(3);
                newr[0] = r109[0];
                newr[1] = r109[1];
                newr[2] = r109[2];
                PhysicCollect108.rows_.Add(newr);
                Console.WriteLine(newr);
            }
        }
    }
Esempio n. 8
0
        // from clause -
        //  pair each from item with cross join, their join conditions will be handled
        //  with where clauss processing.
        //
        LogicNode transformFromClause()
        {
            LogicNode transformOneFrom(TableRef tab)
            {
                LogicNode from;

                switch (tab)
                {
                case BaseTableRef bref:
                    from = new LogicScanTable(bref);
                    break;

                case ExternalTableRef eref:
                    from = new LogicScanFile(eref);
                    break;

                case QueryRef qref:
                    var plan = qref.query_.CreatePlan();
                    if (qref is FromQueryRef && queryOpt_.optimize_.remove_from_)
                    {
                        from = plan;
                    }
                    else
                    {
                        string alias = null;
                        if (qref is CTEQueryRef cq)
                        {
                            alias = cq.alias_;
                        }
                        else if (qref is FromQueryRef fq)
                        {
                            alias = fq.alias_;
                        }
                        var key = new NamedQuery(qref.query_, alias);
                        from = new LogicFromQuery(qref, plan);
                        subQueries_.Add(key);

                        // if from CTE, then it could be duplicates
                        if (!fromQueries_.ContainsKey(key))
                        {
                            fromQueries_.Add(key, from as LogicFromQuery);
                        }
                    }
                    break;

                case JoinQueryRef jref:
                    // We will form join group on all tables and put a filter on top
                    // of the joins as a normalized form for later processing.
                    //
                    //      from a join b on a1=b1 or a3=b3 join c on a2=c2;
                    //   => from a , b, c where  (a1=b1 or a3=b3) and a2=c2;
                    //
                    LogicJoin subjoin    = new LogicJoin(null, null);
                    Expr      filterexpr = null;
                    for (int i = 0; i < jref.tables_.Count; i++)
                    {
                        LogicNode t        = transformOneFrom(jref.tables_[i]);
                        var       children = subjoin.children_;
                        if (children[0] is null)
                        {
                            children[0] = t;
                        }
                        else
                        {
                            if (children[1] is null)
                            {
                                children[1] = t;
                            }
                            else
                            {
                                subjoin = new LogicJoin(t, subjoin);
                            }
                            subjoin.type_ = jref.joinops_[i - 1];
                            filterexpr    = filterexpr.AddAndFilter(jref.constraints_[i - 1]);
                        }
                    }
                    Debug.Assert(filterexpr != null);
                    from = new LogicFilter(subjoin, filterexpr);
                    break;

                default:
                    throw new Exception();
                }

                return(from);
            }

            LogicNode root;

            if (from_.Count >= 2)
            {
                var join     = new LogicJoin(null, null);
                var children = join.children_;
                from_.ForEach(x =>
                {
                    LogicNode from = transformOneFrom(x);
                    if (children[0] is null)
                    {
                        children[0] = from;
                    }
                    else
                    {
                        children[1] = (children[1] is null) ? from :
                                      new LogicJoin(from, children[1]);
                    }
                });
                root = join;
            }
            else if (from_.Count == 1)
            {
                root = transformOneFrom(from_[0]);
            }
            else
            {
                root = new LogicResult(selection_);
            }

            return(root);
        }