public WSqlTableContext Invoke(WSqlFragment node)
 {
     _results    = new WSqlTableContext();
     _nodeTables = null;
     node.Accept(this);
     return(_results);
 }
 public WSqlTableContext Invoke(WSqlFragment node, IEnumerable<Tuple<string, string>> nodeTables )
 {
     _results = new WSqlTableContext();
     _nodeTables = nodeTables;
     node.Accept(this);
     return _results;
 }
 public WSqlTableContext Invoke(WSqlFragment node, IEnumerable <Tuple <string, string> > nodeTables)
 {
     _results    = new WSqlTableContext();
     _nodeTables = nodeTables;
     node.Accept(this);
     return(_results);
 }
 public WSqlTableContext Invoke(WSqlFragment node)
 {
     _results = new WSqlTableContext();
     _nodeTables = null;
     node.Accept(this);
     return _results;
 }
 /// <summary>
 /// Returns true if there are columns of a specific table referenced in a statement
 /// </summary>
 /// <param name="node">The query statement</param>
 /// <param name="tableName">the table for query</param>
 /// <param name="context">Sql context with table alias mapping</param>
 /// <param name="columnsOfNodeTables"></param>
 /// <param name="conn">A open Sql connection</param>
 /// <returns></returns>
 public bool Invoke(WSelectQueryBlock node, string tableName, WSqlTableContext context,
     Dictionary<Tuple<string, string>, Dictionary<string, NodeColumns>> columnsOfNodeTables)
 {
     _tableExists = false;
     _tableName = tableName;
     _tableRef = context[tableName] as WNamedTableReference;
     _columnTableMapping = context.GetColumnToAliasMapping(columnsOfNodeTables);
     node.Accept(this);
     return _tableExists;
 }
 /// <summary>
 /// Returns true if there are columns of a specific table referenced in a statement
 /// </summary>
 /// <param name="node">The query statement</param>
 /// <param name="tableName">the table for query</param>
 /// <param name="context">Sql context with table alias mapping</param>
 /// <param name="columnsOfNodeTables"></param>
 /// <param name="conn">A open Sql connection</param>
 /// <returns></returns>
 public bool Invoke(WSelectQueryBlock node, string tableName, WSqlTableContext context,
                    Dictionary <Tuple <string, string>, Dictionary <string, NodeColumns> > columnsOfNodeTables)
 {
     _tableExists        = false;
     _tableName          = tableName;
     _tableRef           = context[tableName] as WNamedTableReference;
     _columnTableMapping = context.GetColumnTableMapping(columnsOfNodeTables);
     node.Accept(this);
     return(_tableExists);
 }
Example #7
0
 public MatchComponent(MatchNode node, List <MatchEdge> populatedEdges, WSqlTableContext context) : this(node)
 {
     Context = context;
     foreach (var edge in populatedEdges)
     {
         TableRef = SpanTableRef(TableRef, edge, node.RefAlias);
         EdgeMaterilizedDict[edge]     = true;
         StatisticsDict[edge.SinkNode] = Context.GetEdgeStatistics(edge);
         var edgeList = UnmaterializedNodeMapping.GetOrCreate(edge.SinkNode);
         edgeList.Add(edge);
         Nodes.Add(edge.SinkNode);
         Size         *= edge.AverageDegree;
         EstimateSize *= 1000;
     }
 }
Example #8
0
 /// <summary>
 /// Deep Copy
 /// </summary>
 /// <param name="component"></param>
 public MatchComponent(MatchComponent component)
 {
     Nodes = new List <MatchNode>(component.Nodes);
     EdgeMaterilizedDict        = new Dictionary <MatchEdge, bool>(component.EdgeMaterilizedDict);
     MaterializedNodeSplitCount = new Dictionary <MatchNode, int>(component.MaterializedNodeSplitCount);
     UnmaterializedNodeMapping  = new Dictionary <MatchNode, List <MatchEdge> >();
     foreach (var nodeMapping in component.UnmaterializedNodeMapping)
     {
         UnmaterializedNodeMapping[nodeMapping.Key] = new List <MatchEdge>(nodeMapping.Value);
     }
     StatisticsDict            = new Dictionary <MatchNode, ColumnStatistics>(component.StatisticsDict);
     TableRef                  = component.TableRef;
     Size                      = component.Size;
     Cost                      = component.Cost;
     DeltaMemory               = component.DeltaMemory;
     TotalMemory               = component.TotalMemory;
     EstimateDeltaMemory       = component.EstimateDeltaMemory;
     EstimateTotalMemory       = component.EstimateTotalMemory;
     EstimateSize              = component.EstimateSize;
     FatherOfRightestTableRef  = component.FatherOfRightestTableRef;
     RightestTableRefSize      = component.RightestTableRefSize;
     FatherListofDownSizeTable = new List <Tuple <WQualifiedJoin, String> >(component.FatherListofDownSizeTable);
     Context                   = component.Context;
 }
Example #9
0
        private void ConstructSelectGraph()
        {
            QueryGraph = GraphViewDocDBCommand.DocDB_ConstructGraph(SelectQueryBlock);
            NodeTable  = QueryGraph.ConnectedSubGraphs[0].Nodes;
            var AttachPredicateVisitor = new AttachWhereClauseVisitor();
            var TableContext           = new WSqlTableContext();
            var GraphMeta          = new GraphMetaData();
            var columnTableMapping = TableContext.GetColumnToAliasMapping(GraphMeta.ColumnsOfNodeTables);

            if (SelectQueryBlock != null)
            {
                AttachPredicateVisitor.Invoke(SelectQueryBlock.WhereClause, QueryGraph, columnTableMapping);
            }
            int GroupNumber = 0;

            foreach (var node in NodeTable)
            {
                GraphViewDocDBCommand.GetQuery(node.Value);
                if (!GraphDescription.ContainsKey(node.Value.NodeAlias))
                {
                    GraphDescription[node.Value.NodeAlias] = ++GroupNumber;
                }
            }
        }
        /// <summary>
        /// The entry point of the optimizer, activated when visting each SELECT query block.
        /// </summary>
        /// <param name="node">The SELECT query block</param>
        public override void Visit(WSelectQueryBlock node)
        {
            var checkVarVisitor = new CollectVariableVisitor();
            var currentContext = checkVarVisitor.Invoke(node.FromClause, _graphMetaData.ColumnsOfNodeTables.Keys);
            currentContext.ParentContext = _context;
            _context = currentContext;

            base.Visit(node);

            CheckValidity(node);
            var graph = ConstructGraph(node);
            //ChangeSelectStarExpression(node, graph);

            if (graph != null)
            {
                //OptimizeTail(node, graph);
                AttachPredicates(node.WhereClause,graph);
                EstimateRows(graph);
                RetrieveStatistics(graph);

                var components = new List<MatchComponent>();
                foreach (var subGraph in graph.ConnectedSubGraphs)
                {

                    components.Add(ConstructComponent(subGraph, graph.ReversedEdgeDict, graph.SourceNodeStatisticsDict));
            #if DEBUG
                    foreach (var matchNode in subGraph.Nodes.Values)
                    {
                        Trace.WriteLine(matchNode.NodeAlias);
                        Trace.WriteLine(string.Format("  RowCount:{0}", matchNode.TableRowCount));
                        Trace.WriteLine(string.Format("  EstiRow:{0}", matchNode.EstimatedRows));
                    }
            #endif

                }

                UpdateQuery(node, components);

            #if DEBUG
                Trace.WriteLine(string.Format("Rows:{0}", components[0].Cardinality));
                Trace.WriteLine(string.Format("Cost:{0}", components[0].Cost));
                Trace.WriteLine(string.Format("Estimated Rows:{0}", components[0].SqlEstimatedSize));

            #endif
                node.MatchClause = null;
            }

            _context = _context.ParentContext;
        }
Example #11
0
        public MatchComponent(MatchNode node, List<MatchEdge> populatedEdges, WSqlTableContext context) : this(node)
        {
            Context = context;
            foreach (var edge in populatedEdges)
            {
                TableRef = SpanTableRef(TableRef, edge, node.RefAlias);
                EdgeMaterilizedDict[edge] = true;
                StatisticsDict[edge.SinkNode] = Context.GetEdgeStatistics(edge);
                var edgeList = UnmaterializedNodeMapping.GetOrCreate(edge.SinkNode);
                edgeList.Add(edge);
                Nodes.Add(edge.SinkNode);
                Size *= edge.AverageDegree;
                EstimateSize *= 1000;

            }
        }
Example #12
0
 /// <summary>
 /// Deep Copy
 /// </summary>
 /// <param name="component"></param>
 public MatchComponent(MatchComponent component)
 {
     Nodes = new List<MatchNode>(component.Nodes);
     EdgeMaterilizedDict = new Dictionary<MatchEdge, bool>(component.EdgeMaterilizedDict);
     MaterializedNodeSplitCount = new Dictionary<MatchNode, int>(component.MaterializedNodeSplitCount);
     UnmaterializedNodeMapping = new Dictionary<MatchNode, List<MatchEdge>>();
     foreach (var nodeMapping in component.UnmaterializedNodeMapping)
     {
         UnmaterializedNodeMapping[nodeMapping.Key] = new List<MatchEdge>(nodeMapping.Value);
     }
     StatisticsDict = new Dictionary<MatchNode, ColumnStatistics>(component.StatisticsDict);
     TableRef = component.TableRef;
     Size = component.Size;
     Cost = component.Cost;
     DeltaMemory = component.DeltaMemory;
     TotalMemory = component.TotalMemory;
     EstimateDeltaMemory = component.EstimateDeltaMemory;
     EstimateTotalMemory = component.EstimateTotalMemory;
     EstimateSize = component.EstimateSize;
     FatherOfRightestTableRef = component.FatherOfRightestTableRef;
     RightestTableRefSize = component.RightestTableRefSize;
     FatherListofDownSizeTable = new List<Tuple<WQualifiedJoin, String>>(component.FatherListofDownSizeTable);
     Context = component.Context;
 }
        private WMatchClause ConstructReversedMatchClause(WSelectQueryBlock node, WSqlTableContext context)
        {
            var result = new WMatchClause()
            {
                Paths = new List<WMatchPath>(),
            };

            foreach (var path in node.MatchClause.Paths)
            {
                var reversedEdgeNameList = new List<string>();
                var nodeList = new List<Tuple<WSchemaObjectName, WEdgeColumnReferenceExpression>>();
                var edgeList = path.PathEdgeList;

                for (var i = 0; i < edgeList.Count; ++i)
                {
                    var sourceTableName = edgeList[i].Item1.Identifiers.Last().Value;
                    var edgeName = edgeList[i].Item2.MultiPartIdentifier.Identifiers.Last().Value;
                    WTableReference sourceTable = context[sourceTableName];
                    var sourceTableObjectName =
                        (sourceTable as WNamedTableReference).TableObjectName.Identifiers.Last().Value;

                    reversedEdgeNameList.Add(sourceTableObjectName + "_" + edgeName + "Reversed");
                }

                var sinkIdentifier = path.Tail.Identifiers.Last();

                for (var i = edgeList.Count - 1; i >= 0; --i)
                {
                    var nodeName = new WSchemaObjectName()
                    {
                        Identifiers = new List<Identifier>
                        {
                            new Identifier()
                            {
                                Value = sinkIdentifier.Value,
                                QuoteType = sinkIdentifier.QuoteType,
                            }
                        },
                    };

                    var originalPath = edgeList[i].Item2;
                    var edgeCol = new WEdgeColumnReferenceExpression()
                    {
                        ColumnType = ColumnType.Regular,
                        Alias = originalPath.Alias,
                        MaxLength = originalPath.MaxLength,
                        MinLength = originalPath.MinLength,
                        AttributeValueDict = originalPath.AttributeValueDict,
                        MultiPartIdentifier = new WMultiPartIdentifier(new Identifier()
                        {
                            QuoteType = originalPath.MultiPartIdentifier.Identifiers.Last().QuoteType,
                            Value = reversedEdgeNameList[i],
                        }),
                    };

                    nodeList.Add(new Tuple<WSchemaObjectName, WEdgeColumnReferenceExpression>(nodeName, edgeCol));
                    sinkIdentifier = edgeList[i].Item1.Identifiers.Last();
                }

                result.Paths.Add(new WMatchPath
                                 {
                                     PathEdgeList = nodeList,
                                     Tail = edgeList[0].Item1,
                                     IsReversed = true,
                                 });
            }

            return result;
        }
        /// <summary>
        /// The entry of the optimizer, activated when visting each Select Query Block
        /// </summary>
        /// <param name="node"></param>
        public override void Visit(WSelectQueryBlock node)
        {
            var checkVarVisitor = new CollectVariableVisitor();
            var currentContext = checkVarVisitor.Invoke(node.FromClause, _columnsOfNodeTables.Keys);
            currentContext.UpperLevel = _context;
            _context = currentContext;

            base.Visit(node);

            _statisticsCalculator.Context = _context;
            CheckValidity(node);
            var graph = ConstructGraph(node);
            //ChangeSelectStarExpression(node, graph);

            if (graph != null)
            {
                OptimizeTail(node, graph);
                EstimateRows(node, graph);
                EstimateAverageDegree(graph);

                var components = new List<MatchComponent>();
                foreach (var subGraph in graph.ConnectedSubGraphs)
                {
                    

                    components.Add(ConstructComponent(subGraph));
#if DEBUG
                    foreach (var matchNode in subGraph.Nodes.Values)
                    {
                        Trace.WriteLine(matchNode.NodeAlias);
                        Trace.WriteLine(string.Format("  RowCount:{0}", matchNode.TableRowCount));
                        Trace.WriteLine(string.Format("  EstiRow:{0}", matchNode.EstimatedRows));
                    }
#endif

                }

                UpdateQuery(node, components);

#if DEBUG
                Trace.WriteLine(string.Format("Rows:{0}", components[0].Size));
                Trace.WriteLine(string.Format("Cost:{0}", components[0].Cost));
                Trace.WriteLine(string.Format("Estimated Rows:{0}", components[0].EstimateSize));

#endif
                node.MatchClause = null;
            }

            _context = _context.UpperLevel;
        }