// Reads // public static StagedReadName RenderStagedReadPlan(Workspace Home, HScriptParser.Crudam_readContext context) { // Get the data source // DataSet data = VisitorHelper.GetData(Home, context.full_table_name()); string alias = (context.K_AS() != null) ? context.IDENTIFIER().GetText() : data.Name; // Create a local heap to work off of // MemoryStruct local_heap = new MemoryStruct(true); // Create a record register // StreamRegister memory = new StreamRegister(null); // Create expression visitor // ExpressionVisitor exp_vis = new ExpressionVisitor(local_heap, Home, alias, data.Columns, memory); // Where clause // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); // Create a reader // RecordReader reader = data.OpenReader(where); // Attach the reader to the register // memory.BaseStream = reader; // Create the action visitor // ActionVisitor act_vis = new ActionVisitor(Home, local_heap, exp_vis); // Get the declarations // if (context.crudam_declare_many() != null) { VisitorHelper.AllocateMemory(Home, local_heap, exp_vis, context.crudam_declare_many()); } // Get the initial actionset // TNode pre_run = (context.init_action() != null) ? act_vis.ToNode(context.init_action().query_action()) : new TNodeNothing(null); // Get the main actionset // TNode run = act_vis.ToNode(context.main_action().query_action()); // Get the final actionset // TNode post_run = (context.final_action() != null) ? act_vis.ToNode(context.final_action().query_action()) : new TNodeNothing(null); return new StagedReadName(reader, pre_run, run, post_run); }
public static FastReadPlan RenderFastReadPlan(Workspace Home, HScriptParser.Crudam_read_fastContext context) { // Get the data source // DataSet data = VisitorHelper.GetData(Home, context.full_table_name()); string alias = (context.K_AS() != null) ? context.IDENTIFIER().GetText() : data.Name; // Create a record register // StreamRegister memory = new StreamRegister(null); // Create expression visitor // ExpressionVisitor exp_vis = new ExpressionVisitor(new MemoryStruct(true), Home, alias, data.Columns, memory); // Where clause // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); // Create a reader // RecordReader reader = data.OpenReader(where); // Attach the reader to the register // memory.BaseStream = reader; // Get the fields being returned // FNodeSet nodes = VisitorHelper.GetReturnStatement(exp_vis, context.return_action().expression_or_wildcard_set()); // Get the output cursor from the return statement // RecordWriter writer = VisitorHelper.GetWriter(Home, nodes.Columns, context.return_action()); return new FastReadPlan(data, where, nodes, writer); }
// Update // public static UpdatePlan RenderUpdatePlan(Workspace Home, HScriptParser.Crudam_updateContext context) { // Get the data source // DataSet data = VisitorHelper.GetData(Home, context.full_table_name()); // Create expression visitor // ExpressionVisitor exp_vis = new ExpressionVisitor(null, Home, data.Name, data.Columns, null); // Get where // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); // Create the key and fnodeset // Key keys = new Key(); FNodeSet expressions = new FNodeSet(); foreach (HScriptParser.Update_unitContext ctx in context.update_unit()) { keys.Add(data.Columns.ColumnIndex(ctx.IDENTIFIER().GetText())); expressions.Add(exp_vis.ToNode(ctx.expression())); } return new UpdatePlan(data, keys, expressions, where); }
// Delete // public static DeletePlan RenderDeletePlan(Workspace Home, HScriptParser.Crudam_deleteContext context) { // Get the data source // DataSet data = VisitorHelper.GetData(Home, context.full_table_name()); // Create expression visitor // ExpressionVisitor exp_vis = new ExpressionVisitor(null, Home, data.Name, data.Columns, null); // Get where // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); return new DeletePlan(data, where); }
// Merge // public static MergePlan RenderMergePlan(Workspace Home, HScriptParser.Crudam_mergeContext context) { // Get the data sources // DataSet data1 = VisitorHelper.GetData(Home, context.merge_source()[0].full_table_name()); DataSet data2 = VisitorHelper.GetData(Home, context.merge_source()[1].full_table_name()); // Get the aliases // string alias1 = (context.merge_source()[0].IDENTIFIER() ?? context.merge_source()[0].full_table_name().table_name().IDENTIFIER()).GetText(); string alias2 = (context.merge_source()[1].IDENTIFIER() ?? context.merge_source()[1].full_table_name().table_name().IDENTIFIER()).GetText(); // Build the registers; the join functions only use static registers // StaticRegister mem1 = new StaticRegister(null); StaticRegister mem2 = new StaticRegister(null); // Create our expression builder // ExpressionVisitor exp_vis = new ExpressionVisitor(null, Home); exp_vis.AddSchema(alias1, data1.Columns, mem1); exp_vis.AddSchema(alias2, data2.Columns, mem2); // Get the equality keys // Key eq1 = new Key(); Key eq2 = new Key(); foreach (HScriptParser.Merge_equi_predicateContext ctx in context.merge_equi_predicate()) { string a1 = ctx.table_variable()[0].IDENTIFIER()[0].GetText(); string a2 = ctx.table_variable()[1].IDENTIFIER()[0].GetText(); string c1 = ctx.table_variable()[0].IDENTIFIER()[1].GetText(); string c2 = ctx.table_variable()[1].IDENTIFIER()[1].GetText(); int idx1 = -1; int idx2 = -1; if (a1 == alias1 && a2 == alias2) { // Look up indicides // idx1 = data1.Columns.ColumnIndex(c1); idx2 = data2.Columns.ColumnIndex(c2); // Check for invalid keys // if (idx1 == -1) throw new Exception(string.Format("Column '{0}' does not exist in '{1}'", c1, alias1)); if (idx2 == -1) throw new Exception(string.Format("Column '{0}' does not exist in '{1}'", c2, alias2)); } else if (a1 == alias2 && a2 == alias1) { // Look up indicides // idx1 = data1.Columns.ColumnIndex(c2); idx2 = data2.Columns.ColumnIndex(c1); // Check for invalid keys // if (idx1 == -1) throw new Exception(string.Format("Column '{0}' does not exist in '{1}'", c2, idx1)); if (idx2 == -1) throw new Exception(string.Format("Column '{0}' does not exist in '{1}'", c1, idx2)); } else throw new Exception("Aliases passed are invalid"); // add the keys // eq1.Add(idx1); eq2.Add(idx2); } // Get the predicate // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); // Get the list of expressions // FNodeSet nodes = VisitorHelper.GetReturnStatement(exp_vis, context.return_action().expression_or_wildcard_set()); // Get the output cursor // RecordWriter out_data = VisitorHelper.GetWriter(Home, nodes.Columns, context.return_action()); // Get the join method // MergeMethod method = VisitorHelper.GetMergeMethod(context.merge_type()); // Find the best algorithm // MergeAlgorithm alg = MergeAlgorithm.SortMerge; if (context.merge_algorithm() != null) { string suggest_alg = exp_vis.ToNode(context.merge_algorithm().expression()).Evaluate().valueSTRING.ToUpper(); if (suggest_alg == "NL") alg = MergeAlgorithm.NestedLoop; else if (suggest_alg == "SM") alg = MergeAlgorithm.SortMerge; else if (suggest_alg == "HT") alg = MergeAlgorithm.HashTable; } if (eq1.Count == 0) alg = MergeAlgorithm.NestedLoop; return new MergePlan(method, alg, out_data, nodes, where, data1, data2, eq1, eq2, mem1, mem2); }
public static PartitionedAggregatePlan RenderPartitionedAggregatePlan(Workspace Home, HScriptParser.Crudam_aggregateContext context) { // Get the data source // DataSet data = VisitorHelper.GetData(Home, context.full_table_name()); string alias = (context.K_AS() != null) ? context.IDENTIFIER().GetText() : data.Name; // Create a register // StaticRegister memory = new StaticRegister(null); // Create expression visitor // ExpressionVisitor exp_vis = new ExpressionVisitor(null, Home, alias, data.Columns, memory); // Get where // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); // Get the reader // //RecordReader reader = data.OpenReader(where); // Get the keys // FNodeSet keys = (context.K_BY() != null) ? exp_vis.ToNodes(context.expression_alias_list()) : new FNodeSet(); // Get the reducers // AggregateSet values = (context.K_OVER() != null) ? exp_vis.ToReducers(context.beta_reduction_list()) : new AggregateSet(); // Create a second register for the return memory // StaticRegister return_memory = new StaticRegister(null); // Need to build a visitor off of the aggregator schema // ExpressionVisitor agg_vis = new ExpressionVisitor(null, Home, "agg", AggregatePlan.GetInterimSchema(keys, values), return_memory); // Get the output // FNodeSet return_vars = VisitorHelper.GetReturnStatement(agg_vis, context.return_action().expression_or_wildcard_set()); // Get the output cursor // RecordWriter out_put_writter = VisitorHelper.GetWriter(Home, return_vars.Columns, context.return_action()); // Get the partitioner // int Partitions = VisitorHelper.GetPartitions(exp_vis, context.partitions()); return new PartitionedAggregatePlan(out_put_writter, data, where, keys, values, return_vars, Home.TempSpace, Partitions); }
internal static ReadMapNode RenderMapNode(Workspace Home, int PartitionID, HScriptParser.Crudam_read_maprContext context) { // Get the data source // DataSet data = VisitorHelper.GetData(Home, context.full_table_name()); string alias = (context.K_AS() != null) ? context.IDENTIFIER().GetText() : data.Name; // Create a local heap to work off of // MemoryStruct local_heap = new MemoryStruct(true); // Create a record register // StaticRegister memory = new StaticRegister(null); // Create expression visitor // ExpressionVisitor exp_vis = new ExpressionVisitor(local_heap, Home, alias, data.Columns, memory); // Where clause // Predicate where = VisitorHelper.GetWhere(exp_vis, context.where_clause()); // Create a reader // RecordReader reader = data.OpenReader(where); // Get the declarations // if (context.crudam_declare_many() != null) { VisitorHelper.AllocateMemory(Home, local_heap, exp_vis, context.crudam_declare_many()); } // Get the map actions // ActionVisitor act_vis = new ActionVisitor(Home, local_heap, exp_vis); act_vis.IsAsync = true; TNode map = act_vis.ToNode(context.map_action().query_action()); // Get the reduce actions // act_vis = new ActionVisitor(Home, local_heap, exp_vis); act_vis.IsAsync = false; TNode red = act_vis.ToNode(context.reduce_action().query_action()); ReadMapNode node = new ReadMapNode(PartitionID, map, red, memory, where); return node; }