AttachWhereClauseVisitor traverses the WHERE clause and attachs predicates into nodes and edges of constructed graph.
Inheritance: WSqlFragmentVisitor
Ejemplo n.º 1
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;
                }
            }
        }
 private void AttachPredicates(WWhereClause whereClause, MatchGraph graph)
 {
     // Attaches proper parts of the where clause into the Estimiation Query
     var attachPredicateVisitor = new AttachWhereClauseVisitor();
     var columnTableMapping = _context.GetColumnToAliasMapping(_graphMetaData.ColumnsOfNodeTables);
     attachPredicateVisitor.Invoke(whereClause, graph, columnTableMapping);
 }
        /// <summary>
        /// Estimate number of rows of node table in graph.
        /// </summary>
        /// <param name="graph">Constructed node graph</param>
        private void EstimateRows(WSelectQueryBlock query, MatchGraph graph)
        {
            var declareParameter = "";
            if (_variables != null)
            {
                foreach (var parameter in _variables)
                {
                    declareParameter += "DECLARE " + parameter.VariableName.Value + " " +
                                        TsqlFragmentToString.DataType(parameter.DataType) + "\r\n";
                }
            }
            var estimator = new TableSizeEstimator(Conn);
            bool first = true;
            var fromQuerySb = new StringBuilder(1024);
            foreach (var subGraph in graph.ConnectedSubGraphs)
            {
                foreach (var node in subGraph.Nodes)
                {
                    if (first)
                        first = false;
                    else
                        fromQuerySb.Append(", ");
                    var currentNode = node.Value;
                    fromQuerySb.AppendFormat("{0} AS [{1}] WITH (ForceScan)", currentNode.TableObjectName,
                        currentNode.NodeAlias);
                }
            }
            // Attach proper parts of the where clause into the Estimiation Query
            var columnTableMapping = _context.GetColumnTableMapping(_columnsOfNodeTables);
            var attachWhereClauseVisiter = new AttachNodeEdgePredictesVisitor();
            var nodeEstimationWhereClause = attachWhereClauseVisiter.Invoke(query.WhereClause, graph, columnTableMapping);
            string nodeEstimationQuery = string.Format("{0}\r\n SELECT * FROM {1}\r\n", declareParameter,
                fromQuerySb);
            if (nodeEstimationWhereClause.SearchCondition != null)
                nodeEstimationQuery += nodeEstimationWhereClause.ToString();
            var estimateRows = estimator.GetQueryTableEstimatedRows(nodeEstimationQuery);

            foreach (var subGraph in graph.ConnectedSubGraphs)
            {
                // Update Row Estimation for nodes
                foreach (var node in subGraph.Nodes)
                {
                    var currentNode = node.Value;
                    currentNode.EstimatedRows = estimateRows[node.Key];
                    var tableSchema = currentNode.TableObjectName.SchemaIdentifier.Value;
                    var tableName = currentNode.TableObjectName.BaseIdentifier.Value;
                    currentNode.TableRowCount = estimator.GetTableRowCount(tableSchema, tableName);
                }
                }

            // Attach predicates to nodes and edges
            var attachPredicateVisitor = new AttachWhereClauseVisitor();
            attachPredicateVisitor.Invoke(query.WhereClause, graph, columnTableMapping);

        }