Exemplo n.º 1
0
        public static Dictionary<string, FNode> BuildParameters(ExpressionVisitor Evaluator, HScriptParser.Bind_element_setContext context)
        {

            Dictionary<string, FNode> parameters = new Dictionary<string, FNode>();
            if (context == null)
                return parameters;
            
            foreach (HScriptParser.Bind_elementContext ctx in context.bind_element())
            {

                string key = ctx.SCALAR().GetText();
                bool is_dynamic = 
                    (ctx.K_STATIC() == null) 
                    ? true 
                    : false;
                FNode value = 
                    (is_dynamic) 
                    ? Evaluator.ToNode(ctx.expression()) 
                    : new FNodeValue(null, new Cell(ctx.expression().GetText(), false));
                parameters.Add(key, value);

            }
            return parameters;

        }
Exemplo n.º 2
0
        // Partitions //
        public static int GetPartitions(ExpressionVisitor Evaluator, HScriptParser.PartitionsContext context)
        {

            // Null then assume 1 partition //
            if (context == null)
                return 1;

            // If the expression is null, then max out cores //
            if (context.expression() == null)
                return Environment.ProcessorCount;

            // Otherwise, get the value //
            int cnt = (int)Evaluator.ToNode(context.expression()).Evaluate().valueINT;

            // Bound it //
            cnt = Math.Min(cnt, Environment.ProcessorCount * 2);
            cnt = Math.Max(cnt, 1);

            return cnt;

        }
Exemplo n.º 3
0
        // Expression or wildcard handelers //
        private static void AppendSet(ExpressionVisitor Evaluator, FNodeSet Fields, HScriptParser.EOW_expressionContext context)
        {

            FNode node = Evaluator.ToNode(context.expression_alias().expression());
            string alias = ("F" + Fields.Count.ToString());
            if (node.Name != null)
                alias = node.Name;
            if (context.expression_alias().K_AS() != null)
                alias = context.expression_alias().IDENTIFIER().GetText();
            Fields.Add(alias, node);

        }
Exemplo n.º 4
0
        public static void AllocateMemory(Workspace Home, MemoryStruct Heap, ExpressionVisitor Evaluator, HScriptParser.DeclareMatrix2DContext context)
        {

            string name = context.IDENTIFIER().GetText();
            CellAffinity type = GetAffinity(context.type());
            int rows = (int)Evaluator.ToNode(context.expression()[0]).Evaluate().valueINT;
            int cols = (int)Evaluator.ToNode(context.expression()[1]).Evaluate().valueINT;
            CellMatrix mat = new CellMatrix(rows, cols, type);
            Heap.Arrays.Reallocate(name, mat);

        }
Exemplo n.º 5
0
        // Heap allocations //
        public static void AllocateMemory(Workspace Home, MemoryStruct Heap, ExpressionVisitor Evaluator, HScriptParser.DeclareScalarContext context)
        {

            string name = context.IDENTIFIER().GetText();
            CellAffinity type = GetAffinity(context.type());
            Cell value = (context.ASSIGN() != null) ? Evaluator.ToNode(context.expression()).Evaluate() : new Cell(type);

            Heap.Scalars.Reallocate(name, value);

        }
Exemplo n.º 6
0
 internal static DeclareMatrixNode RenderDeclareNode(Workspace Home, ExpressionVisitor Evaluator, HScriptParser.DeclareMatrix2DContext context)
 {
     string name = context.IDENTIFIER().GetText();
     int row = (int)Evaluator.ToNode(context.expression()[0]).Evaluate().valueINT;
     int col = (int)Evaluator.ToNode(context.expression()[1]).Evaluate().valueINT;
     CellAffinity affinity = VisitorHelper.GetAffinity(context.type());
     MNodeLiteral m = new MNodeLiteral(null, new Gidran.CellMatrix(row, 1, affinity));
     return new DeclareMatrixNode(Home.GlobalHeap, name, m);
 }
Exemplo n.º 7
0
        internal static DeclareScalarNode RenderDeclareNode(Workspace Home, ExpressionVisitor Evaluator, HScriptParser.DeclareScalarContext context)
        {

            string name = context.IDENTIFIER().GetText();
            FNode node = Evaluator.ToNode(context.expression());
            return new DeclareScalarNode(Home.GlobalHeap, name, node);

        }
Exemplo n.º 8
0
        public static CreateChunkPlan RenderCreateChunk(Workspace Home, HScriptParser.Crudam_create_tableContext context)
        {

            ExpressionVisitor exp_vis = new ExpressionVisitor(null, Home);

            // Build the schema //
            Schema columns = new Schema();
            foreach (HScriptParser.Create_table_unitContext ctx in context.create_table_unit())
            {
                columns.Add(
                    ctx.IDENTIFIER().GetText(),
                    VisitorHelper.GetAffinity(ctx.type()),
                    (ctx.expression() == null) ? true : exp_vis.ToNode(ctx.expression()).Evaluate().valueBOOL,
                    VisitorHelper.GetSize(ctx.type(), true));
            }

            string name = context.full_table_name().table_name().IDENTIFIER().GetText();

            return new CreateChunkPlan(name, columns, Home);

        }
Exemplo n.º 9
0
        // Create //
        public static CreateTablePlan RenderCreatePlan(Workspace Home, HScriptParser.Crudam_create_tableContext context)
        {

            // Build visitor //
            ExpressionVisitor exp_vis = new ExpressionVisitor(null, Home);
            
            // Build the schema //
            Schema columns = new Schema();
            foreach (HScriptParser.Create_table_unitContext ctx in context.create_table_unit())
            {
                columns.Add(
                    ctx.IDENTIFIER().GetText(),
                    VisitorHelper.GetAffinity(ctx.type()),
                    (ctx.expression() == null) ? true : exp_vis.ToNode(ctx.expression()).Evaluate().valueBOOL,
                    VisitorHelper.GetSize(ctx.type(), true));
            }

            string name = context.full_table_name().table_name().GetText();
            string db = context.full_table_name().database_name().GetText();
            string db_path = Home.Connections[db];
            long chunk_size =
                (context.create_table_size() == null)
                ? RecordSet.EstimateMaxRecords(columns)
                : exp_vis.ToNode(context.create_table_size().expression()).Evaluate().valueINT;

            return new CreateTablePlan(db_path, name, columns, (int)chunk_size);

        }
Exemplo n.º 10
0
        // 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);

        }
Exemplo n.º 11
0
        // 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);

        }