protected override void InternalOpen() { // TODO: Rewrite this... Schema.TableType tableType = new Schema.TableType(); Schema.BaseTableVar tableVar = new Schema.BaseTableVar(tableType); Schema.TableVarColumn newColumn; foreach (Schema.TableVarColumn column in Node.TableVar.Columns) { newColumn = (Schema.TableVarColumn)column.Copy(); tableType.Columns.Add(newColumn.Column); tableVar.Columns.Add(newColumn); } Schema.Order order = new Schema.Order(); Schema.OrderColumn newOrderColumn; Schema.OrderColumn orderColumn; for (int index = 0; index < Node.Order.Columns.Count; index++) { orderColumn = Node.Order.Columns[index]; newOrderColumn = new Schema.OrderColumn(tableVar.Columns[orderColumn.Column], orderColumn.Ascending, orderColumn.IncludeNils); newOrderColumn.Sort = orderColumn.Sort; newOrderColumn.IsDefaultSort = orderColumn.IsDefaultSort; order.Columns.Add(newOrderColumn); } tableVar.Orders.Add(order); _table = new NativeTable(Manager, tableVar); PopulateTable(); _scan = new Scan(Manager, _table, _table.ClusteredIndex, ScanDirection.Forward, null, null); _scan.Open(); }
private void DetermineOrder(Plan plan) { Order = null; if (SourceNode.Order != null) { Schema.Order newOrder = new Schema.Order(); Schema.OrderColumn orderColumn; Schema.OrderColumn newOrderColumn; newOrder.InheritMetaData(SourceNode.Order.MetaData); newOrder.IsInherited = true; for (int index = 0; index < SourceNode.Order.Columns.Count; index++) { orderColumn = SourceNode.Order.Columns[index]; newOrderColumn = new Schema.OrderColumn ( TableVar.Columns[SourceTableVar.Columns.IndexOfName(orderColumn.Column.Name)], orderColumn.Ascending, orderColumn.IncludeNils ); newOrderColumn.Sort = orderColumn.Sort; newOrderColumn.IsDefaultSort = orderColumn.IsDefaultSort; Error.AssertWarn(newOrderColumn.Sort != null, "Sort is null"); if (newOrderColumn.IsDefaultSort) { plan.AttachDependency(newOrderColumn.Sort); } else { if (newOrderColumn.Sort.HasDependencies()) { plan.AttachDependencies(newOrderColumn.Sort.Dependencies); } } newOrder.Columns.Add(newOrderColumn); } Order = newOrder; } }
// TODO: Compile row types for each index, saving column indexes to prevent the need for lookup during insert, update, and delete. private void InternalInitialize(ServerProcess AProcess) { Schema.RowType LKeyRowType; Schema.RowType LDataRowType; // Create the indexes required to store data as described by the given table type // Determine Fanout, Capacity, Clustering Key, KeyLength, DataLength FClusteringKey = TableVar.FindClusteringOrder(AProcess.Plan); //Schema.Key LClusteringKey = TableVar.FindClusteringKey(); //FClusteringKey = new Schema.Order(LClusteringKey, AProcess.Plan); LKeyRowType = new Schema.RowType(FClusteringKey.Columns); LDataRowType = new Schema.RowType(); foreach (Schema.Column LColumn in TableVar.DataType.Columns) { if (!FClusteringKey.Columns.Contains(LColumn.Name)) { LDataRowType.Columns.Add(new Schema.Column(LColumn.Name, LColumn.DataType)); } } // Add an internal identifier for uniqueness of keys in nonunique indexes #if USEINTERNALID FInternalIDColumn = new Schema.TableVarColumn(new Schema.Column(CInternalIDColumnName, AProcess.Plan.Catalog.DataTypes.SystemGuid), Schema.TableVarColumnType.InternalID); LDataRowType.Columns.Add(FInternalIDColumn.Column); #endif // Create the Clustered index FClusteredIndex = new TableBufferIndex ( AProcess, FClusteringKey, LKeyRowType, LDataRowType, true, FFanout, FCapacity ); Indexes.Add(FClusteredIndex); // DataLength and DataColumns for all non clustered indexes is the key length and columns of the clustered key LDataRowType = LKeyRowType; // Create non clustered indexes for each key and order (unique sets) Schema.Order LKey; foreach (Schema.Key LNonClusteredKey in TableVar.Keys) { if (!FClusteringKey.Includes(LNonClusteredKey)) { LKey = new Schema.Order(LNonClusteredKey, AProcess.Plan); if (!Indexes.Contains(LKey)) { LKeyRowType = new Schema.RowType(LKey.Columns); Indexes.Add ( new TableBufferIndex ( AProcess, LKey, LKeyRowType, LDataRowType, false, FFanout, FCapacity ) ); } } } foreach (Schema.Order LOrder in TableVar.Orders) { // This is a potentially non-unique index, so add a GUID to ensure uniqueness of the key in the BTree LKey = new Schema.Order(LOrder); #if USEINTERNALID if (!LKey.Includes(LClusteringKey)) { Schema.OrderColumn LUniqueColumn = new Schema.OrderColumn(FInternalIDColumn, true); LUniqueColumn.Sort = ((Schema.ScalarType)LUniqueColumn.Column.DataType).GetUniqueSort(AProcess.Plan); LKey.Columns.Add(LUniqueColumn); } #endif if (!Indexes.Contains(LKey)) { LKeyRowType = new Schema.RowType(LKey.Columns); Indexes.Add ( new TableBufferIndex ( AProcess, LKey, LKeyRowType, LDataRowType, false, FFanout, FCapacity ) ); } } }
//TODO: Add support as we can. protected override DevicePlanNode InternalPrepare(Schema.DevicePlan plan, PlanNode planNode) { if (planNode is BaseTableVarNode) { BaseTableVarNode node = (BaseTableVarNode)planNode; node.CursorType = CursorType.Dynamic; node.RequestedCursorType = plan.Plan.CursorContext.CursorType; node.CursorCapabilities = CursorCapability.Navigable | CursorCapability.BackwardsNavigable | CursorCapability.Searchable | CursorCapability.Updateable; node.CursorIsolation = plan.Plan.CursorContext.CursorIsolation; node.Order = Compiler.OrderFromKey(plan.Plan, Compiler.FindClusteringKey(plan.Plan, node.TableVar)); return(new DevicePlanNode(node)); } else if ((planNode is OrderNode) && (planNode.Nodes[0] is BaseTableVarNode) && (plan.Plan.CursorContext.CursorType != CursorType.Static)) { OrderNode node = (OrderNode)planNode; BaseTableVarNode tableVarNode = (BaseTableVarNode)planNode.Nodes[0]; bool isSupported = false; foreach (Schema.Key key in tableVarNode.TableVar.Keys) { var tableOrder = Compiler.OrderFromKey(plan.Plan, key); if (node.RequestedOrder.Equivalent(tableOrder)) { node.PhysicalOrder = tableOrder; node.ScanDirection = ScanDirection.Forward; isSupported = true; break; } else if (node.RequestedOrder.Equivalent(new Schema.Order(tableOrder, true))) { node.PhysicalOrder = tableOrder; node.ScanDirection = ScanDirection.Backward; isSupported = true; break; } } if (!isSupported) { foreach (Schema.Order order in tableVarNode.TableVar.Orders) { //We support one column (or one column plus a single-column key ordered in the same direction). if (order.Columns.Count > 1) { break; } if (node.RequestedOrder.Equivalent(order)) { node.PhysicalOrder = order; node.ScanDirection = ScanDirection.Forward; isSupported = true; break; } else if (node.RequestedOrder.Equivalent(new Schema.Order(order, true))) { node.PhysicalOrder = order; node.ScanDirection = ScanDirection.Backward; isSupported = true; break; } var rowIdKey = tableVarNode.TableVar.Keys.MinimumSubsetKey(tableVarNode.TableVar.Columns); var tableOrder = Compiler.OrderFromKey(plan.Plan, rowIdKey); //If we have a rowId key... Add it to the ordering and see if we match if (rowIdKey.Columns.Count == 1 && tableOrder.Columns.Count == 1) { Order newOrder = new Order(order); newOrder.Columns.Add(tableOrder.Columns[0]); if (node.RequestedOrder.Equivalent(newOrder)) { node.PhysicalOrder = newOrder; node.ScanDirection = ScanDirection.Forward; isSupported = true; break; } else if (node.RequestedOrder.Equivalent(new Schema.Order(newOrder, true))) { node.PhysicalOrder = newOrder; node.ScanDirection = ScanDirection.Backward; isSupported = true; break; } } } } if (!isSupported) { //Support every ordering... Use nestedFilterCursor to emulate support... node.PhysicalOrder = node.RequestedOrder; node.ScanDirection = ScanDirection.Forward; isSupported = true; } if (isSupported) { node.Order = new Schema.Order(); node.Order.MergeMetaData(node.RequestedOrder.MetaData); node.Order.IsInherited = false; Schema.OrderColumn orderColumn; Schema.OrderColumn newOrderColumn; for (int index = 0; index < node.PhysicalOrder.Columns.Count; index++) { orderColumn = node.PhysicalOrder.Columns[index]; newOrderColumn = new Schema.OrderColumn ( node.TableVar.Columns[orderColumn.Column], node.ScanDirection == ScanDirection.Forward ? orderColumn.Ascending : !orderColumn.Ascending ); newOrderColumn.Sort = orderColumn.Sort; newOrderColumn.IsDefaultSort = orderColumn.IsDefaultSort; Error.AssertWarn(newOrderColumn.Sort != null, "Sort is null"); node.Order.Columns.Add(newOrderColumn); } if (!node.TableVar.Orders.Contains(node.Order)) { node.TableVar.Orders.Add(node.Order); } node.CursorType = CursorType.Dynamic; node.RequestedCursorType = plan.Plan.CursorContext.CursorType; node.CursorCapabilities = CursorCapability.Navigable | CursorCapability.BackwardsNavigable | CursorCapability.Searchable | (plan.Plan.CursorContext.CursorCapabilities & CursorCapability.Updateable); node.CursorIsolation = plan.Plan.CursorContext.CursorIsolation; return(new DevicePlanNode(node)); } } else if (planNode is CreateTableVarBaseNode) { plan.Plan.CheckRight(GetRight(Schema.RightNames.CreateStore)); return(new DevicePlanNode(planNode)); } else if (planNode is AlterTableNode) { //Don't support altering tables in V1 //plan.Plan.CheckRight(GetRight(Schema.RightNames.AlterStore)); //AlterTableNode alterTableNode = (AlterTableNode)planNode; //if (alterTableNode.AlterTableStatement.CreateColumns.Count > 0) // throw new RuntimeException(RuntimeException.Codes.UnimplementedCreateCommand, "Columns in a memory device"); //if (alterTableNode.AlterTableStatement.DropColumns.Count > 0) // throw new RuntimeException(RuntimeException.Codes.UnimplementedDropCommand, "Columns in a memory device"); //return new DevicePlanNode(planNode); } else if (planNode is DropTableNode) { plan.Plan.CheckRight(GetRight(Schema.RightNames.DropStore)); return(new DevicePlanNode(planNode)); } plan.IsSupported = false; return(null); }
public override void DetermineDataType(Plan plan) { DetermineModifiers(plan); _dataType = new Schema.TableType(); _tableVar = new Schema.ResultTableVar(this); _tableVar.Owner = plan.User; _tableVar.InheritMetaData(SourceTableVar.MetaData); if (_expressions == null) { // This is a rename all expression, merge metadata and inherit columns _tableVar.MergeMetaData(_metaData); // Inherit columns Schema.TableVarColumn newColumn; foreach (Schema.TableVarColumn column in SourceTableVar.Columns) { newColumn = column.Inherit(_tableAlias); DataType.Columns.Add(newColumn.Column); TableVar.Columns.Add(newColumn); } } else { bool columnAdded; Schema.TableVarColumn column; int renameColumnIndex; for (int index = 0; index < SourceTableVar.Columns.Count; index++) { columnAdded = false; foreach (RenameColumnExpression renameColumn in _expressions) { renameColumnIndex = SourceTableVar.Columns.IndexOf(renameColumn.ColumnName); if (renameColumnIndex < 0) { throw new Schema.SchemaException(Schema.SchemaException.Codes.ObjectNotFound, renameColumn.ColumnName); } else if (renameColumnIndex == index) { if (columnAdded) { throw new CompilerException(CompilerException.Codes.DuplicateRenameColumn, renameColumn.ColumnName); } column = SourceTableVar.Columns[index].InheritAndRename(renameColumn.ColumnAlias); column.MergeMetaData(renameColumn.MetaData); DataType.Columns.Add(column.Column); TableVar.Columns.Add(column); columnAdded = true; } } if (!columnAdded) { column = SourceTableVar.Columns[index].Inherit(); DataType.Columns.Add(column.Column); TableVar.Columns.Add(column); } } } DetermineRemotable(plan); // Inherit keys Schema.Key newKey; foreach (Schema.Key key in SourceTableVar.Keys) { newKey = new Schema.Key(); newKey.InheritMetaData(key.MetaData); newKey.IsInherited = true; newKey.IsSparse = key.IsSparse; foreach (Schema.TableVarColumn keyColumn in key.Columns) { newKey.Columns.Add(TableVar.Columns[SourceTableVar.Columns.IndexOfName(keyColumn.Name)]); } TableVar.Keys.Add(newKey); } // Inherit orders Schema.Order newOrder; Schema.OrderColumn orderColumn; Schema.OrderColumn newOrderColumn; foreach (Schema.Order order in SourceTableVar.Orders) { newOrder = new Schema.Order(); newOrder.InheritMetaData(order.MetaData); newOrder.IsInherited = true; for (int index = 0; index < order.Columns.Count; index++) { orderColumn = order.Columns[index]; newOrderColumn = new Schema.OrderColumn ( TableVar.Columns[SourceTableVar.Columns.IndexOfName(orderColumn.Column.Name)], orderColumn.Ascending, orderColumn.IncludeNils ); newOrderColumn.Sort = orderColumn.Sort; newOrderColumn.IsDefaultSort = orderColumn.IsDefaultSort; Error.AssertWarn(newOrderColumn.Sort != null, "Sort is null"); if (newOrderColumn.IsDefaultSort) { plan.AttachDependency(newOrderColumn.Sort); } else { if (newOrderColumn.Sort.HasDependencies()) { plan.AttachDependencies(newOrderColumn.Sort.Dependencies); } } newOrder.Columns.Add(newOrderColumn); } TableVar.Orders.Add(newOrder); } DetermineOrder(plan); #if UseReferenceDerivation // Copy references #if UseElaborable if (plan.CursorContext.CursorCapabilities.HasFlag(CursorCapability.Elaborable)) #endif { if (SourceTableVar.HasReferences()) { foreach (Schema.ReferenceBase reference in SourceTableVar.References) { if (reference.SourceTable.Equals(SourceTableVar)) { Schema.JoinKey sourceKey = new Schema.JoinKey(); foreach (Schema.TableVarColumn column in reference.SourceKey.Columns) { sourceKey.Columns.Add(TableVar.Columns[SourceTableVar.Columns.IndexOfName(column.Name)]); } int newReferenceID = Schema.Object.GetNextObjectID(); string newReferenceName = DeriveSourceReferenceName(reference, newReferenceID, sourceKey); Schema.DerivedReference newReference = new Schema.DerivedReference(newReferenceID, newReferenceName, reference); newReference.IsExcluded = reference.IsExcluded; newReference.InheritMetaData(reference.MetaData); newReference.SourceTable = _tableVar; newReference.AddDependency(_tableVar); newReference.TargetTable = reference.TargetTable; newReference.AddDependency(reference.TargetTable); newReference.SourceKey.IsUnique = reference.SourceKey.IsUnique; foreach (Schema.TableVarColumn column in sourceKey.Columns) { newReference.SourceKey.Columns.Add(column); } newReference.TargetKey.IsUnique = reference.TargetKey.IsUnique; foreach (Schema.TableVarColumn column in reference.TargetKey.Columns) { newReference.TargetKey.Columns.Add(column); } //newReference.UpdateReferenceAction = reference.UpdateReferenceAction; //newReference.DeleteReferenceAction = reference.DeleteReferenceAction; _tableVar.References.Add(newReference); } else if (reference.TargetTable.Equals(SourceTableVar)) { Schema.JoinKey targetKey = new Schema.JoinKey(); foreach (Schema.TableVarColumn column in reference.TargetKey.Columns) { targetKey.Columns.Add(TableVar.Columns[SourceTableVar.Columns.IndexOfName(column.Name)]); } int newReferenceID = Schema.Object.GetNextObjectID(); string newReferenceName = DeriveTargetReferenceName(reference, newReferenceID, targetKey); Schema.DerivedReference newReference = new Schema.DerivedReference(newReferenceID, newReferenceName, reference); newReference.IsExcluded = reference.IsExcluded; newReference.InheritMetaData(reference.MetaData); newReference.SourceTable = reference.SourceTable; newReference.AddDependency(reference.SourceTable); newReference.TargetTable = _tableVar; newReference.AddDependency(_tableVar); newReference.SourceKey.IsUnique = reference.SourceKey.IsUnique; foreach (Schema.TableVarColumn column in reference.SourceKey.Columns) { newReference.SourceKey.Columns.Add(column); } newReference.TargetKey.IsUnique = reference.TargetKey.IsUnique; foreach (Schema.TableVarColumn column in targetKey.Columns) { newReference.TargetKey.Columns.Add(column); } //newReference.UpdateReferenceAction = reference.UpdateReferenceAction; //newReference.DeleteReferenceAction = reference.DeleteReferenceAction; _tableVar.References.Add(newReference); } } } } #endif }