Пример #1
0
        public ColumnReference(TableReference tableReference, Column columnDescription)
        {
            InitializeMembers();

            this.tableReference = tableReference;
            this.columnName = columnDescription.Name;
            this.dataType = columnDescription.DataType;
        }
Пример #2
0
        public static SelectList Create(TableReference tableReference)
        {
            SelectList root = null;
            SelectList last = null;

            Create(ref root, ref last, tableReference);

            return root;
        }
Пример #3
0
 private void CopyMembers(TableStatistics old)
 {
     this.table = old.table;
     this.binCount = old.binCount;
     this.keyColumn = old.keyColumn;
     this.keyValue = new List<double>(old.keyValue);
     this.keyCount = new List<long>(old.keyCount);
     this.rowCount = old.rowCount;
 }
Пример #4
0
        public override Node Interpret()
        {
            if (IsUdf)
            {
                this.tableReference = new TableReference(this);
            }

            return base.Interpret();
        }
Пример #5
0
        public override Node Interpret()
        {
            // Subqueries already initialize this
            if (this.tableReference == null)
            {
                this.tableReference = new TableReference(this);
            }

            return base.Interpret();
        }
Пример #6
0
        public bool IsSpecificToTable(TableReference table)
        {
            foreach (var tr in NodeExtensions.EnumerateTableReferences(this))
            {
                if (tr != null && !tr.Compare(table))
                {
                    return false;
                }
            }

            return true;
        }
        public WhereClause GenerateWhereClauseSpecificToTable(TableReference table)
        {
            SearchCondition sc = null;

            // Loop over all conditions (JOIN ONs and WHERE conditions)
            // Result will be a combinations of the table specific conditions terms
            // of all these

            // Chain up search conditions with AND operator
            foreach (var condition in conditions)
            {
                foreach (var ex in EnumerateCnfTermsSpecificToTable(condition, table))
                {
                    var nsc = ex.GetParsingTree();

                    if (sc != null)
                    {
                        nsc.Stack.AddLast(Whitespace.Create());
                        nsc.Stack.AddLast(LogicalOperator.CreateAnd());
                        nsc.Stack.AddLast(Whitespace.Create());
                        nsc.Stack.AddLast(sc);
                    }

                    sc = nsc;
                }
            }

            // Prefix with the WHERE keyword
            if (sc != null)
            {
                var where = new WhereClause();
                where.Stack.AddLast(Keyword.Create("WHERE"));
                where.Stack.AddLast(Whitespace.Create());
                where.Stack.AddLast(sc);

                return where;
            }
            else
            {
                return null;
            }
        }
Пример #8
0
 private void InitializeMembers()
 {
     this.sourceTableReferences = new Dictionary<string, TableReference>(Schema.SchemaManager.Comparer);
     this.resultsTableReference = new TableReference(this);
 }
Пример #9
0
        private string GetEscapedUniqueName(TableReference table)
        {
            if (table.IsSubquery || table.IsComputed)
                {
                    return EscapeIdentifierName(table.Alias);
                }
                else
                {
                    string res = String.Empty;

                    if (table.DatasetName != null)
                    {
                        res += String.Format("{0}_", EscapeIdentifierName(table.DatasetName));
                    }

                    if (table.DatabaseName != null)
                    {
                        res += String.Format("{0}_", EscapeIdentifierName(table.DatabaseName));
                    }

                    if (table.SchemaName != null)
                    {
                        res += String.Format("{0}_", EscapeIdentifierName(table.SchemaName));
                    }

                    if (table.DatabaseObjectName != null)
                    {
                        res += String.Format("{0}", EscapeIdentifierName(table.DatabaseObjectName));
                    }

                    return res;
                }
        }
Пример #10
0
        /// <summary>
        /// Composes a source query for a remote table
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public SourceTableQuery PrepareCopyRemoteTable(TableReference table)
        {
            // -- Load schema
            var sm = this.GetSchemaManager(false);
            var ds = sm.Datasets[table.DatasetName];

            // Graywulf dataset has to be converted to prevent registry access
            if (ds is GraywulfDataset)
            {
                ds = new SqlServerDataset(ds);
            }

            // --- Generate most restrictive query

            // Find the query specification this table belongs to
            var qs = ((TableSource)table.Node).QuerySpecification;

            // Run the normalizer to convert where clause to a normal form
            var cnr = new SearchConditionNormalizer();
            cnr.NormalizeQuerySpecification(qs);

            var cg = SqlCodeGeneratorFactory.CreateCodeGenerator(ds);
            var sql = cg.GenerateMostRestrictiveTableQuery(table, true, 0);

            return new SourceTableQuery()
            {
                Dataset = ds,
                Query = sql
            };
        }
Пример #11
0
        public void PrepareComputeTableStatistics(Context context, TableReference tr, out string connectionString, out string sql)
        {
            // Assign a database server to the query
            // TODO: maybe make this function generic
            // TODO: check this part to use appropriate server and database
            var sm = GetSchemaManager(false);
            var ds = sm.Datasets[tr.DatasetName];

            if (ds is GraywulfDataset && !((GraywulfDataset)ds).IsSpecificInstanceRequired)
            {
                var gds = (GraywulfDataset)ds;
                var dd = new DatabaseDefinition(context);
                dd.Guid = gds.DatabaseDefinition.Guid;
                dd.Load();

                // Get a server from the scheduler
                var si = new ServerInstance(Context);
                si.Guid = Scheduler.GetNextServerInstance(new Guid[] { dd.Guid }, StatDatabaseVersionName, null);
                si.Load();

                connectionString = si.GetConnectionString().ConnectionString;

                SubstituteDatabaseName(tr, si.Guid, StatDatabaseVersionName);
                tr.DatabaseObject = null;
            }
            else
            {
                // Run it on the specific database
                connectionString = ds.ConnectionString;
            }

            // Generate statistics query
            var cg = new SqlServerCodeGenerator();
            cg.ResolveNames = true;
            sql = cg.GenerateTableStatisticsQuery(tr);
        }
Пример #12
0
        /// <summary>
        /// Checks if table is a remote table cached locally and if so,
        /// substitutes corresponding temp table name
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public string SubstituteRemoteTableName(TableReference tr)
        {
            if (RemoteTableReferences.ContainsKey(tr.UniqueName))
            {
                /*return String.Format("[{0}].[{1}].[{2}]",
                    GetTemporaryDatabaseConnectionString().InitialCatalog,
                    Query.TemporarySchemaName,
                    TemporaryTables[table.UniqueName]);*/

                var table = TemporaryTables[tr.UniqueName];

                return String.Format("[{0}].[{1}].[{2}]",
                    !String.IsNullOrEmpty(table.DatabaseName) ? table.DatabaseName : table.Dataset.DatabaseName,
                    table.SchemaName,
                    table.TableName);
            }
            else
            {
                return tr.GetFullyResolvedName();
            }
        }
Пример #13
0
        public void CopyRemoteTable(TableReference table, SourceQueryParameters source)
        {
            // Temp table name
            var temptable = GetTemporaryTable(table.EscapedUniqueName);
            TemporaryTables.TryAdd(table.UniqueName, temptable);

            var destination = new DestinationTableParameters()
            {
                Table = temptable,
                Operation = DestinationTableOperation.Drop | DestinationTableOperation.Create,
            };

            var bcp = CreateQueryImporter(source, destination, false);
            bcp.Source = source;
            bcp.Destination = destination;

            bcp.CreateDestinationTable();

            var guid = Guid.NewGuid();
            RegisterCancelable(guid, bcp);

            bcp.Execute();

            UnregisterCancelable(guid);
        }
Пример #14
0
        private static void Create(ref SelectList root, ref SelectList last, TableReference tableReference)
        {
            foreach (var cr in tableReference.ColumnReferences)
            {
                // Create new expression
                var nsl = SelectList.Create(cr);

                // If root is uninitialized, initialize now
                if (root == null)
                {
                    root = nsl;
                }

                // Append to list if not the first
                if (last != null)
                {
                    last.Append(nsl);
                }

                last = nsl;
            }

            if (root == null)
            {
                throw new InvalidOperationException();
            }
        }
Пример #15
0
 private void InitializeMembers()
 {
     this.tableReference = null;
 }
Пример #16
0
 private void CopyMembers(QuerySpecification old)
 {
     this.sourceTableReferences = new Dictionary <string, TableReference>(old.sourceTableReferences);
     this.resultsTableReference = new TableReference(old.resultsTableReference);
 }
Пример #17
0
 private void OnAsyncExecute(Guid workflowInstanceGuid, string activityInstanceId, QueryBase query, TableReference tableReference, string connectionString, string sql)
 {
     RegisterCancelable(workflowInstanceGuid, activityInstanceId, query);
     query.ComputeTableStatistics(tableReference, connectionString, sql);
     UnregisterCancelable(workflowInstanceGuid, activityInstanceId, query);
 }
Пример #18
0
        public SourceQueryParameters PrepareCopyRemoteTable(TableReference table)
        {
            // Load schema
            var sm = this.GetSchemaManager(false);
            var ds = sm.Datasets[table.DatasetName];

            // Graywulf dataset is to be converted to prevent registry access
            if (ds is GraywulfDataset)
            {
                ds = new SqlServerDataset(ds);
            }

            var source = new SourceQueryParameters();

            source.Dataset = ds;

            // Find the query specification this table belongs to
            var qs = ((TableSource)table.Node).QuerySpecification;

            // Run the normalizer
            var cnr = new SearchConditionNormalizer();
            cnr.NormalizeQuerySpecification(qs);

            var cg = SqlCodeGeneratorFactory.CreateCodeGenerator(ds);
            source.Query = cg.GenerateMostRestrictiveTableQuery(table, true, 0);

            return source;
        }
Пример #19
0
 private void OnAsyncExecute(Guid workflowInstanceGuid, string activityInstanceId, QueryPartitionBase querypartition, TableReference remotetable, SourceQueryParameters source)
 {
     RegisterCancelable(workflowInstanceGuid, activityInstanceId, querypartition);
     querypartition.CopyRemoteTable(remotetable, source);
     UnregisterCancelable(workflowInstanceGuid, activityInstanceId, querypartition);
 }
Пример #20
0
        /// <summary>
        /// Gather statistics for the table with the specified bin size
        /// </summary>
        /// <param name="tr"></param>
        /// <param name="binSize"></param>
        public virtual void ComputeTableStatistics(TableReference tr, string connectionString, string sql)
        {
            using (SqlConnection cn = new SqlConnection(connectionString))
            {
                cn.Open();

                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    cmd.Parameters.Add("@BinCount", SqlDbType.Decimal).Value = tr.Statistics.BinCount;
                    cmd.CommandTimeout = queryTimeout;

                    ExecuteLongCommandReader(cmd, dr =>
                    {
                        long rc = 0;
                        while (dr.Read())
                        {
                            tr.Statistics.KeyValue.Add(dr.GetDouble(0));
                            tr.Statistics.KeyCount.Add(dr.GetInt64(1));

                            rc += dr.GetInt64(1);
                        }
                        tr.Statistics.RowCount = rc;
                    });
                }
            }
        }
Пример #21
0
        private void CopyMembers(ColumnReference old)
        {
            this.columnExpression = old.columnExpression;
            this.columnIdentifier = old.columnIdentifier;
            this.tableReference = old.tableReference;

            this.columnName = old.columnName;
            this.dataType = old.dataType;
            this.columnAlias = old.columnAlias;

            this.isStar = old.isStar;
            this.isComplexExpression = old.isComplexExpression;
            this.selectListIndex = old.selectListIndex;
            this.columnContext = old.columnContext;
        }
Пример #22
0
        /// <summary>
        /// Copies a table from a remote data source by creating and
        /// executing a table copy task.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="source"></param>
        public void CopyRemoteTable(TableReference table, SourceTableQuery source)
        {
            // Create a target table name
            var temptable = GetTemporaryTable(GetEscapedUniqueName(table));
            TemporaryTables.TryAdd(table.UniqueName, temptable);

            var dest = new DestinationTable(temptable)
            {
                Options = TableInitializationOptions.Drop | TableInitializationOptions.Create
            };

            var tc = CreateTableCopyTask(source, dest, false);

            var guid = Guid.NewGuid();
            RegisterCancelable(guid, tc);

            tc.Execute();

            UnregisterCancelable(guid);
        }
Пример #23
0
        private void InitializeMembers()
        {
            this.columnExpression = null;
            this.columnIdentifier = null;
            this.tableReference = null;

            this.columnName = null;
            this.dataType = DataType.Unknown;
            this.columnAlias = null;

            this.isStar = false;
            this.isComplexExpression = false;
            this.selectListIndex = -1;
            this.columnContext = Graywulf.SqlParser.ColumnContext.None;
        }
Пример #24
0
 /// <summary>
 /// Checks if table is a remote table cached locally and if so,
 /// substitutes corresponding temp table name
 /// </summary>
 /// <param name="table"></param>
 /// <returns></returns>
 public string SubstituteRemoteTableName(TableReference tr)
 {
     if (RemoteTableReferences.ContainsKey(tr.UniqueName))
     {
         return CodeGenerator.GetResolvedTableName(TemporaryTables[tr.UniqueName]);
     }
     else
     {
         return CodeGenerator.GetResolvedTableName(tr);
     }
 }
Пример #25
0
        /// <summary>
        /// Collect list of table sources and load columns from the schema.
        /// </summary>
        /// <remarks>
        /// Source tables are put into a dictionary that is keyed by table alias
        /// or table name.
        /// </remarks>
        /// <param name="qs"></param>
        private void CollectSourceTableReferences(QuerySpecification qs)
        {
            // --- Collect column references from subqueries or load from the database schema

            foreach (var tr in qs.EnumerateSourceTableReferences(false))
            {
                string tablekey;
                if (tr.IsSubquery || tr.IsComputed || tr.IsUdf || tr.Alias != null)
                {
                    tablekey = tr.Alias;
                }
                else
                {
                    // If no alias is used then use table name
                    tablekey = tr.DatabaseObjectName;
                }

                // Make sure that table key is used only once
                if (qs.SourceTableReferences.ContainsKey(tablekey))
                {
                    throw CreateException(ExceptionMessages.DuplicateTableAlias, null, tablekey, tr.Node);
                }
                else
                {
                    var ntr = new TableReference(tr);

                    if (!ntr.IsSubquery && !ntr.IsComputed)
                    {
                        // Load table description from underlying schema
                        // Attempt to load dataset and throw exception of name cannot be resolved
                        DatasetBase ds;

                        try
                        {
                            ds = schemaManager.Datasets[ntr.DatasetName];
                        }
                        catch (KeyNotFoundException ex)
                        {
                            throw CreateException(ExceptionMessages.UnresolvableDatasetReference, ex, ntr.DatasetName, ntr.Node);
                        }
                        catch (SchemaException ex)
                        {
                            throw CreateException(ExceptionMessages.UnresolvableDatasetReference, ex, ntr.DatasetName, ntr.Node);
                        }

                        ntr.DatabaseObject = ds.GetObject(ntr.DatabaseName, ntr.SchemaName, ntr.DatabaseObjectName);

                        // Load column descriptions for the table
                        ntr.LoadColumnReferences(schemaManager);
                    }

                    qs.SourceTableReferences.Add(tablekey, ntr);
                }
            }
        }
Пример #26
0
        /// <summary>
        /// Enumerates the terms of an expression tree that are specific to a table.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        /// <remarks>The expression must be in CNF</remarks>
        private static IEnumerable<LogicalExpressions.Expression> EnumerateCnfTermsSpecificToTable(LogicalExpressions.Expression node, TableReference table)
        {
            foreach (var term in EnumerateCnfTerms(node))
            {
                if (term is LogicalExpressions.OperatorOr)
                {
                    // A term is only specific to a table if it contains predicates
                    // only specific to the particular table
                    var specifictotable = true;
                    foreach (var exp in EnumerateCnfTermPredicates(term))
                    {
                        if (!GetCnfLiteralPredicate(exp).IsSpecificToTable(table))
                        {
                            specifictotable = false;
                            break;
                        }
                    }

                    if (specifictotable)
                    {
                        yield return term;
                    }
                }
                else
                {
                    if (GetCnfLiteralPredicate(term).IsSpecificToTable(table))
                    {
                        yield return term;
                    }
                }
            }
        }
Пример #27
0
        public bool Compare(TableReference other)
        {
            bool res = true;

            // **** verify this or delete
            //res &= (!this.isUdf && !other.isUdf) ||
            //       (this.isUdf && StringComparer.CurrentCultureIgnoreCase.Compare(this.alias, other.tableName) == 0) ||
            //       (other.isUdf && StringComparer.CurrentCultureIgnoreCase.Compare(other.alias, this.tableName) == 0) ||
            //       (this.isUdf && other.isUdf && StringComparer.CurrentCultureIgnoreCase.Compare(this.alias, other.alias) == 0);

            res &= (this.datasetName == null || other.datasetName == null ||
                    SchemaManager.Comparer.Compare(this.datasetName, other.datasetName) == 0);

            res &= (this.databaseName == null || other.databaseName == null ||
                    SchemaManager.Comparer.Compare(this.databaseName, other.databaseName) == 0);

            res &= (this.schemaName == null || other.schemaName == null ||
                    SchemaManager.Comparer.Compare(this.schemaName, other.schemaName) == 0);

            res &= (this.databaseObjectName == null || other.databaseObjectName == null ||
                    SchemaManager.Comparer.Compare(this.databaseObjectName, other.databaseObjectName) == 0);

            res &= (this.alias == null || other.alias == null ||
                    SchemaManager.Comparer.Compare(this.alias, other.alias) == 0);

            return res;
        }
Пример #28
0
 private void CopyMembers(QuerySpecification old)
 {
     this.sourceTableReferences = new Dictionary<string, TableReference>(old.sourceTableReferences);
     this.resultsTableReference = new TableReference(old.resultsTableReference);
 }
Пример #29
0
 public TableReference(TableReference old)
 {
     CopyMembers(old);
 }
Пример #30
0
 public static TableOrViewName Create(TableReference tr)
 {
     var res = new TableOrViewName();
     res.tableReference = tr;
     return res;
 }
Пример #31
0
        private void CopyMembers(TableReference old)
        {
            this.node = old.node;

            this.databaseObject = old.databaseObject;

            this.datasetName = old.datasetName;
            this.databaseName = old.databaseName;
            this.schemaName = old.schemaName;
            this.databaseObjectName = old.databaseObjectName;
            this.alias = old.alias;

            this.isTableOrView = old.isTableOrView;
            this.isUdf = old.isUdf;
            this.isSubquery = old.isSubquery;
            this.isComputed = old.isComputed;

            // Deep copy of column references
            this.columnReferences = new List<ColumnReference>();
            foreach (var cr in old.columnReferences)
            {
                var ncr = new ColumnReference(cr)
                {
                    TableReference = this
                };
                this.columnReferences.Add(ncr);
            }
            //this.conditionReferences = new List<SearchConditionReference>(old.conditionReferences);

            this.statistics = old.statistics == null ? null : new TableStatistics(old.statistics);
        }
Пример #32
0
 private void InitializeMembers()
 {
     this.tableReference = null;
 }
Пример #33
0
 private void InitializeMembers()
 {
     this.sourceTableReferences = new Dictionary <string, TableReference>(Schema.SchemaManager.Comparer);
     this.resultsTableReference = new TableReference(this);
 }