public override WTableReference ToTableReference()
        {
            WSelectQueryBlock queryBlock = this.SubqueryContext.ToSelectQueryBlock();

            queryBlock.SelectElements.Clear();

            List <WValueExpression> columnListExpr = new List <WValueExpression>();

            columnListExpr.Add(SqlUtil.GetValueExpr(this.SubqueryContext.PivotVariable.DefaultProperty()));
            columnListExpr.AddRange(this.ProjectedProperties.Select(SqlUtil.GetValueExpr));

            List <WScalarExpression> capParameters = new List <WScalarExpression>();

            foreach (var sideEffectKey in SideEffectKeys)
            {
                capParameters.Add(new WColumnNameList(columnListExpr));
                capParameters.Add(SqlUtil.GetValueExpr(sideEffectKey));
            }

            queryBlock.SelectElements.Add(
                SqlUtil.GetSelectScalarExpr(SqlUtil.GetFunctionCall(GremlinKeyword.func.Cap, capParameters),
                                            GremlinKeyword.TableDefaultColumnName));

            return(SqlUtil.GetDerivedTable(queryBlock, GetVariableName()));
        }
Пример #2
0
        public List <WSelectScalarExpression> GetConditionPathOuterVariableList(ref Dictionary <GremlinVariableProperty, string> map)
        {
            List <WSelectScalarExpression> pathOuterVariableList = new List <WSelectScalarExpression>();
            List <GremlinVariable>         variableList          = new List <GremlinVariable>();
            List <GremlinVariable>         temp = RepeatCondition.TerminationContext?.FetchVarsFromCurrAndChildContext();

            if (temp != null)
            {
                variableList.AddRange(temp);
            }
            temp = RepeatCondition.EmitContext?.FetchVarsFromCurrAndChildContext();
            if (temp != null)
            {
                variableList.AddRange(temp);
            }
            foreach (var variable in variableList)
            {
                if (variable is GremlinPathVariable)
                {
                    var pathVariable = (variable as GremlinPathVariable);
                    foreach (var stepVariable in pathVariable.PathList)
                    {
                        if (!variableList.Contains(stepVariable.GremlinVariable))
                        {
                            var aliasName = GenerateKey();
                            pathOuterVariableList.Add(SqlUtil.GetSelectScalarExpr(stepVariable.ToScalarExpression(), aliasName));
                            map[stepVariable] = aliasName;
                        }
                    }
                }
            }
            return(pathOuterVariableList);
        }
Пример #3
0
        public override WTableReference ToTableReference()
        {
            List <WScalarExpression> parameters = new List <WScalarExpression>();

            foreach (GremlinToSqlContext context in this.CoalesceContextList)
            {
                WSelectQueryBlock selectQueryBlock = context.ToSelectQueryBlock();
                Dictionary <string, WSelectElement> projectionMap = new Dictionary <string, WSelectElement>();
                WSelectElement value = selectQueryBlock.SelectElements[0];
                foreach (WSelectElement selectElement in selectQueryBlock.SelectElements)
                {
                    projectionMap[(selectElement as WSelectScalarExpression).ColumnName] = selectElement;
                }
                selectQueryBlock.SelectElements.Clear();

                selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr((value as WSelectScalarExpression).SelectExpr, this.DefaultProperty()));
                foreach (string property in this.ProjectedProperties)
                {
                    selectQueryBlock.SelectElements.Add(
                        projectionMap.TryGetValue(property, out value)
                            ? value : SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), property));
                }
                parameters.Add(SqlUtil.GetScalarSubquery(selectQueryBlock));
            }

            var tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Coalesce, parameters, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }
        public override WTableReference ToTableReference()
        {
            WSelectQueryBlock queryBlock = SubqueryContext.ToSelectQueryBlock();

            queryBlock.SelectElements.Clear();
            queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetFunctionCall(GremlinKeyword.func.Tree, PathVariable.GetDefaultProjection().ToScalarExpression()), GremlinKeyword.TableDefaultColumnName));
            return(SqlUtil.GetDerivedTable(queryBlock, GetVariableName()));
        }
        public override WTableReference ToTableReference()
        {
            WSelectQueryBlock queryBlock = SubqueryContext.ToSelectQueryBlock();

            queryBlock.SelectElements.Clear();
            queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetFunctionCall(GremlinKeyword.func.Count), GremlinKeyword.ScalarValue));
            return(SqlUtil.GetDerivedTable(queryBlock, GetVariableName()));
        }
Пример #6
0
        public override WTableReference ToTableReference()
        {
            WSelectQueryBlock firstQueryExpr = new WSelectQueryBlock();

            foreach (var projectProperty in ProjectedProperties)
            {
                if (projectProperty == GremlinKeyword.TableDefaultColumnName)
                {
                    firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(InputVariable.GetDefaultProjection().ToScalarExpression(),
                                                                                  GremlinKeyword.TableDefaultColumnName));
                }
                else if (InputVariable.ProjectedProperties.Contains(projectProperty))
                {
                    firstQueryExpr.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(
                            InputVariable.GetVariableProperty(projectProperty).ToScalarExpression(), projectProperty));
                }
                else
                {
                    firstQueryExpr.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), projectProperty));
                }
            }

            WSelectQueryBlock secondQueryExpr   = OptionalContext.ToSelectQueryBlock();
            bool HasAggregateFunctionAsChildren = false;

            foreach (var variable in OptionalContext.TableReferences)
            {
                if (variable is GremlinFoldVariable ||
                    variable is GremlinCountVariable ||
                    variable is GremlinMinVariable ||
                    variable is GremlinMaxVariable ||
                    variable is GremlinSumVariable ||
                    variable is GremlinMeanVariable ||
                    variable is GremlinTreeVariable)
                {
                    HasAggregateFunctionAsChildren = true;
                }
                var group = variable as GremlinGroupVariable;
                if (group != null && group.SideEffectKey == null)
                {
                    HasAggregateFunctionAsChildren = true;
                }
            }

            var WBinaryQueryExpression = SqlUtil.GetBinaryQueryExpr(firstQueryExpr, secondQueryExpr);

            List <WScalarExpression> parameters = new List <WScalarExpression>();

            parameters.Add(SqlUtil.GetScalarSubquery(WBinaryQueryExpression));
            var tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Optional, parameters, GetVariableName());

            ((WOptionalTableReference)tableRef).HasAggregateFunctionAsChildren = HasAggregateFunctionAsChildren;

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }
        public override WTableReference ToTableReference()
        {
            WSelectQueryBlock queryBlock = SubqueryContext.ToSelectQueryBlock();

            queryBlock.SelectElements.Clear();
            List <WScalarExpression> foldParameters = new List <WScalarExpression> {
                SubqueryContext.PivotVariable.ToCompose1()
            };

            queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetFunctionCall(GremlinKeyword.func.Fold, foldParameters), GremlinKeyword.TableDefaultColumnName));
            return(SqlUtil.GetDerivedTable(queryBlock, GetVariableName()));
        }
Пример #8
0
        public List <WSelectScalarExpression> GetInputSelectList(ref Dictionary <GremlinVariableProperty, string> map)
        {
            List <WSelectScalarExpression> inputSelectList = new List <WSelectScalarExpression>();

            foreach (var projectProperty in FirstVariable.ProjectedProperties)
            {
                var aliasName = GenerateKey();
                inputSelectList.Add(SqlUtil.GetSelectScalarExpr(RepeatContext.PivotVariable.GetVariableProperty(projectProperty).ToScalarExpression(), aliasName));
                map[FirstVariable.GetVariableProperty(projectProperty)] = aliasName;
            }

            return(inputSelectList);
        }
Пример #9
0
 private WSelectQueryBlock GetSelectQueryBlock(GremlinToSqlContext context)
 {
     if (context == null)
     {
         var queryBlock = new WSelectQueryBlock();
         queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(InputVariable.GetDefaultProjection().ToScalarExpression()));
         return(queryBlock);
     }
     else
     {
         return(context.ToSelectQueryBlock());
     }
 }
Пример #10
0
        public override WTableReference ToTableReference()
        {
            List <WScalarExpression> parameters = new List <WScalarExpression>();
            //TODO: refactor
            WSelectQueryBlock selectQueryBlock = ProjectContext.ToSelectQueryBlock();

            selectQueryBlock.SelectElements.Clear();
            selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(ProjectContext.PivotVariable.ToCompose1(), GremlinKeyword.TableDefaultColumnName));
            parameters.Add(SqlUtil.GetScalarSubquery(selectQueryBlock));
            parameters.Add(SqlUtil.GetValueExpr(SideEffectKey));
            var secondTableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Store, parameters, this, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(null, secondTableRef));
        }
Пример #11
0
        internal List <WSelectElement> GetSelectElement(bool isToCompose1)
        {
            var selectElements = new List <WSelectElement>();

            if (PivotVariable.GetVariableType() == GremlinVariableType.Null)
            {
                selectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetStarColumnReferenceExpr(), GremlinKeyword.TableDefaultColumnName));
                return(selectElements);
            }

            if (isToCompose1)
            {
                selectElements.Add(SqlUtil.GetSelectScalarExpr(PivotVariable.ToCompose1(), GremlinKeyword.TableDefaultColumnName));
                return(selectElements);
            }

            foreach (var projectProperty in ProjectedProperties)
            {
                WSelectScalarExpression selectScalarExpr;
                if (projectProperty == GremlinKeyword.Path)
                {
                    selectScalarExpr = SqlUtil.GetSelectScalarExpr(ContextLocalPath.GetDefaultProjection().ToScalarExpression(), GremlinKeyword.Path);
                }
                else if (projectProperty == GremlinKeyword.TableDefaultColumnName)
                {
                    GremlinVariableProperty defaultProjection = PivotVariable.GetDefaultProjection();
                    selectScalarExpr = SqlUtil.GetSelectScalarExpr(defaultProjection.ToScalarExpression(), GremlinKeyword.TableDefaultColumnName);
                }
                else if (PivotVariable.ProjectedProperties.Contains(projectProperty))
                {
                    WScalarExpression columnExpr = PivotVariable.GetVariableProperty(projectProperty).ToScalarExpression();
                    selectScalarExpr = SqlUtil.GetSelectScalarExpr(columnExpr, projectProperty);
                }
                else
                {
                    selectScalarExpr = SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), projectProperty);
                }
                selectElements.Add(selectScalarExpr);
            }

            if (selectElements.Count == 0)
            {
                GremlinVariableProperty defaultProjection = PivotVariable.GetDefaultProjection();
                selectElements.Add(SqlUtil.GetSelectScalarExpr(defaultProjection.ToScalarExpression(), GremlinKeyword.TableDefaultColumnName));
            }

            return(selectElements);
        }
        public override WTableReference ToTableReference()
        {
            WSelectQueryBlock queryBlock = SubqueryContext.ToSelectQueryBlock();

            queryBlock.SelectElements.Clear();
            if (SubqueryContext.PivotVariable.ProjectedProperties.Count == 0)
            {
                SubqueryContext.PivotVariable.ProjectedProperties.Add(GetProjectKey());
            }

            List <WScalarExpression> foldParameters = new List <WScalarExpression> {
                SubqueryContext.PivotVariable.ToCompose1()
            };

            queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetFunctionCall(GremlinKeyword.func.Fold, foldParameters), GremlinKeyword.ScalarValue));
            return(SqlUtil.GetDerivedTable(queryBlock, GetVariableName()));
        }
Пример #13
0
        internal List <WSelectElement> GetSelectElement(bool isToCompose1)
        {
            var selectElements = new List <WSelectElement>();

            if (this.PivotVariable.GetVariableType() == GremlinVariableType.NULL)
            {
                selectElements.Add(
                    SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null),
                                                GremlinKeyword.TableDefaultColumnName));
                return(selectElements);
            }

            if (isToCompose1)
            {
                selectElements.Add(
                    SqlUtil.GetSelectScalarExpr(this.PivotVariable.ToCompose1(), GremlinKeyword.TableDefaultColumnName));
                return(selectElements);
            }

            GremlinVariableProperty defaultProjection = this.PivotVariable.DefaultProjection();

            selectElements.Add(
                SqlUtil.GetSelectScalarExpr(defaultProjection.ToScalarExpression(), this.PivotVariable.DefaultProperty()));

            foreach (var property in this.ProjectedProperties)
            {
                WScalarExpression selectScalarExpr;
                if (property == GremlinKeyword.Path)
                {
                    selectScalarExpr = ContextLocalPath.DefaultProjection().ToScalarExpression();
                }
                else
                {
                    selectScalarExpr = this.PivotVariable.ProjectedProperties.Contains(property)
                        ? this.PivotVariable.GetVariableProperty(property).ToScalarExpression()
                        : SqlUtil.GetValueExpr(null);
                }
                selectElements.Add(SqlUtil.GetSelectScalarExpr(selectScalarExpr, property));
            }

            return(selectElements);
        }
        public override WTableReference ToTableReference()
        {
            List <WScalarExpression> parameters  = new List <WScalarExpression>();
            List <WSelectQueryBlock> queryBlocks = new List <WSelectQueryBlock>();

            //Must toSelectQueryBlock before toCompose1 of variableList in order to populate needed columns
            foreach (var byContext in ByContexts)
            {
                //TODO: select compose1
                WSelectQueryBlock queryBlock = byContext.ToSelectQueryBlock();
                queryBlock.SelectElements.Clear();
                queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(byContext.PivotVariable.ToCompose1(), GremlinKeyword.TableDefaultColumnName));
                queryBlocks.Add(queryBlock);
            }

            if (IsInRepeatContext)
            {
                //Must add as the first parameter
                parameters.Add(SqlUtil.GetColumnReferenceExpr(GremlinKeyword.RepeatInitalTableName, GremlinKeyword.Path));
            }
            foreach (var path in PathList)
            {
                if (path.AttachedVariable != null)
                {
                    parameters.Add(SqlUtil.GetColumnReferenceExpr(path.AttachedVariable.GetVariableName(), GremlinKeyword.Path));
                }
                else
                {
                    parameters.Add(path.StepVariable.First().ToCompose1());
                }
            }

            foreach (var block in queryBlocks)
            {
                parameters.Add(SqlUtil.GetScalarSubquery(block));
            }

            var tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Path2, parameters, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }
Пример #15
0
        public override WTableReference ToTableReference()
        {
            List <WSelectQueryBlock> selectQueryBlocks = new List <WSelectQueryBlock>();

            selectQueryBlocks.Add(new WSelectQueryBlock());
            selectQueryBlocks.Add(this.OptionalContext.ToSelectQueryBlock());

            Dictionary <string, WSelectElement> projectionMap = new Dictionary <string, WSelectElement>();
            WSelectElement value = selectQueryBlocks[1].SelectElements[0];

            foreach (WSelectElement selectElement in selectQueryBlocks[1].SelectElements)
            {
                projectionMap[(selectElement as WSelectScalarExpression).ColumnName] = selectElement;
            }
            selectQueryBlocks[1].SelectElements.Clear();

            selectQueryBlocks[0].SelectElements.Add(SqlUtil.GetSelectScalarExpr(this.InputVariable.DefaultProjection().ToScalarExpression(), this.DefaultProperty()));
            selectQueryBlocks[1].SelectElements.Add(SqlUtil.GetSelectScalarExpr((value as WSelectScalarExpression).SelectExpr, this.DefaultProperty()));
            foreach (string property in this.ProjectedProperties)
            {
                selectQueryBlocks[0].SelectElements.Add(
                    SqlUtil.GetSelectScalarExpr(
                        this.InputVariable.RealVariable.ProjectedProperties.Contains(property)
                            ? this.InputVariable.RealVariable.GetVariableProperty(property).ToScalarExpression()
                            : SqlUtil.GetValueExpr(null), property));

                selectQueryBlocks[1].SelectElements.Add(
                    projectionMap.TryGetValue(property, out value)
                        ? value : SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), property));
            }


            WBinaryQueryExpression binaryQueryExpression = SqlUtil.GetBinaryQueryExpr(selectQueryBlocks[0], selectQueryBlocks[1]);

            List <WScalarExpression> parameters = new List <WScalarExpression>();

            parameters.Add(SqlUtil.GetScalarSubquery(binaryQueryExpression));
            var tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Optional, parameters, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }
Пример #16
0
        public List <WSelectScalarExpression> GetOuterSelectList(ref Dictionary <GremlinVariableProperty, string> map)
        {
            List <WSelectScalarExpression> outerSelectList = new List <WSelectScalarExpression>();
            var allVariablesInRepeatContext = RepeatContext.FetchVarsFromCurrAndChildContext();

            foreach (var variable in allVariablesInRepeatContext)
            {
                if (variable is GremlinSelectedVariable)
                {
                    var repeatInnerVar = (variable as GremlinSelectedVariable);
                    if (repeatInnerVar.IsFromSelect &&
                        !allVariablesInRepeatContext.Contains(repeatInnerVar.RealVariable))
                    {
                        List <GremlinVariable> outerVarList = RepeatContext.Select(repeatInnerVar.SelectKey);
                        GremlinVariable        outerVar     = null;
                        switch (repeatInnerVar.Pop)
                        {
                        case GremlinKeyword.Pop.last:
                            outerVar = outerVarList.Last();
                            break;

                        case GremlinKeyword.Pop.first:
                            outerVar = outerVarList.First();
                            break;
                        }
                        if (repeatInnerVar != outerVar)
                        {
                            foreach (var property in repeatInnerVar.ProjectedProperties)
                            {
                                var aliasName = GenerateKey();
                                outerSelectList.Add(
                                    SqlUtil.GetSelectScalarExpr(
                                        outerVar.GetVariableProperty(property).ToScalarExpression(), aliasName));
                                map[repeatInnerVar.GetVariableProperty(property)] = aliasName;
                            }
                        }
                    }
                }
            }
            return(outerSelectList);
        }
Пример #17
0
        public override WTableReference ToTableReference()
        {
            List <WScalarExpression> parameters = new List <WScalarExpression>();

            foreach (var row in rows)
            {
                if (row is string || row is int)
                {
                    var queryBlock = new WSelectQueryBlock();
                    queryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(row), "_value"));
                    parameters.Add(SqlUtil.GetScalarSubquery(queryBlock));
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            var secondTableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Inject, parameters, this, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(null, secondTableRef));
        }
        public override WTableReference ToTableReference()
        {
            WSelectQueryBlock firstQueryExpr = new WSelectQueryBlock();

            foreach (var projectProperty in ProjectedProperties)
            {
                if (projectProperty == GremlinKeyword.TableDefaultColumnName)
                {
                    firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(InputVariable.DefaultProjection().ToScalarExpression(),
                                                                                  GremlinKeyword.TableDefaultColumnName));
                }
                else
                if (InputVariable.ProjectedProperties.Contains(projectProperty))
                {
                    firstQueryExpr.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(
                            InputVariable.GetVariableProperty(projectProperty).ToScalarExpression(), projectProperty));
                }
                else
                {
                    firstQueryExpr.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), projectProperty));
                }
            }

            if (OptionalContext.IsPopulateGremlinPath)
            {
                firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), GremlinKeyword.Path));
            }

            WSelectQueryBlock secondQueryExpr = OptionalContext.ToSelectQueryBlock(ProjectedProperties);
            var WBinaryQueryExpression        = SqlUtil.GetBinaryQueryExpr(firstQueryExpr, secondQueryExpr);

            List <WScalarExpression> parameters = new List <WScalarExpression>();

            parameters.Add(SqlUtil.GetScalarSubquery(WBinaryQueryExpression));
            var tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Optional, parameters, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }
Пример #19
0
        public List <WSelectScalarExpression> GetRepeatPathOuterVariableList(ref Dictionary <GremlinVariableProperty, string> map)
        {
            List <WSelectScalarExpression> pathOuterVariableList = new List <WSelectScalarExpression>();
            var allVariablesInRepeatContext = RepeatContext.FetchVarsFromCurrAndChildContext();

            foreach (var variable in allVariablesInRepeatContext)
            {
                if (variable is GremlinPathVariable)
                {
                    var pathVariable = (variable as GremlinPathVariable);
                    foreach (var stepVariable in pathVariable.PathList)
                    {
                        if (!allVariablesInRepeatContext.Contains(stepVariable.GremlinVariable))
                        {
                            var aliasName = GenerateKey();
                            pathOuterVariableList.Add(SqlUtil.GetSelectScalarExpr(stepVariable.ToScalarExpression(), aliasName));
                            map[stepVariable] = aliasName;
                        }
                    }
                }
            }
            return(pathOuterVariableList);
        }
Пример #20
0
        public List <WSelectScalarExpression> GetConditionSelectList(ref Dictionary <GremlinVariableProperty, string> map)
        {
            List <WSelectScalarExpression> terminateSelectList = new List <WSelectScalarExpression>();
            List <GremlinVariable>         variableList        = new List <GremlinVariable>();
            List <GremlinVariable>         temp = RepeatCondition.TerminationContext?.FetchVarsFromCurrAndChildContext();

            if (temp != null)
            {
                variableList.AddRange(temp);
            }
            temp = RepeatCondition.EmitContext?.FetchVarsFromCurrAndChildContext();
            if (temp != null)
            {
                variableList.AddRange(temp);
            }
            List <Tuple <string, string> > slot = new List <Tuple <string, string> >();

            foreach (var variable in variableList)
            {
                if (variable is GremlinSelectedVariable)
                {
                    var repeatInnerVar = (variable as GremlinSelectedVariable);
                    foreach (var property in repeatInnerVar.ProjectedProperties)
                    {
                        if (slot.FindIndex(p => p.Item1 == variable.GetVariableName() && p.Item2 == property) == -1)
                        {
                            var aliasName = GenerateKey();
                            terminateSelectList.Add(SqlUtil.GetSelectScalarExpr((variable as GremlinSelectedVariable).RealVariable.GetVariableProperty(property).ToScalarExpression(), aliasName));
                            map[repeatInnerVar.GetVariableProperty(property)] = aliasName;
                            slot.Add(new Tuple <string, string>(variable.GetVariableName(), property));
                        }
                    }
                }
            }
            return(terminateSelectList);
        }
Пример #21
0
        internal List <WSelectElement> GetSelectElement(List <string> ProjectedProperties)
        {
            var selectElements = new List <WSelectElement>();

            if ((PivotVariable is GremlinUnionVariable && HomeVariable is GremlinSideEffectVariable) ||
                PivotVariable.GetVariableType() == GremlinVariableType.NULL)
            {
                selectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetStarColumnReferenceExpr()));
                return(selectElements);
            }
            if (ProjectedProperties != null && ProjectedProperties.Count != 0)
            {
                foreach (var projectProperty in ProjectedProperties)
                {
                    WSelectScalarExpression selectScalarExpr;
                    if (projectProperty == GremlinKeyword.TableDefaultColumnName)
                    {
                        GremlinVariableProperty defaultProjection = PivotVariable.DefaultProjection();
                        selectScalarExpr = SqlUtil.GetSelectScalarExpr(defaultProjection.ToScalarExpression(), GremlinKeyword.TableDefaultColumnName);
                        selectElements.Add(selectScalarExpr);
                    }
                    else
                    if (ProjectVariablePropertiesList.All(p => p.Item2 != projectProperty))
                    {
                        if (PivotVariable.ProjectedProperties.Contains(projectProperty))
                        {
                            WScalarExpression columnExpr =
                                PivotVariable.GetVariableProperty(projectProperty).ToScalarExpression();
                            selectScalarExpr = SqlUtil.GetSelectScalarExpr(columnExpr, projectProperty);
                            selectElements.Add(selectScalarExpr);
                        }
                        else
                        {
                            selectScalarExpr = SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), projectProperty);
                            selectElements.Add(selectScalarExpr);
                        }
                    }
                }
            }
            else
            {
                selectElements.Add(SqlUtil.GetSelectScalarExpr(PivotVariable.DefaultProjection().ToScalarExpression(), GremlinKeyword.TableDefaultColumnName));
            }

            if (IsPopulateGremlinPath)
            {
                selectElements.Add(SqlUtil.GetSelectScalarExpr(CurrentContextPath.DefaultProjection().ToScalarExpression(), GremlinKeyword.Path));
            }
            foreach (var item in ProjectVariablePropertiesList)
            {
                selectElements.Add(SqlUtil.GetSelectScalarExpr(item.Item1.ToScalarExpression(), item.Item2));
            }
            if (selectElements.Count == 0)
            {
                if (PivotVariable is GremlinTableVariable ||
                    (PivotVariable is GremlinUnionVariable && HomeVariable is GremlinSideEffectVariable))
                {
                    selectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetStarColumnReferenceExpr()));
                }
                else if (PivotVariable.GetVariableType() == GremlinVariableType.Table)
                {
                    throw new Exception("Can't process table type");
                }
                else
                {
                    GremlinVariableProperty defaultProjection = PivotVariable.DefaultProjection();
                    selectElements.Add(SqlUtil.GetSelectScalarExpr(defaultProjection.ToScalarExpression()));
                }
            }

            return(selectElements);
        }
Пример #22
0
        public override WTableReference ToTableReference()
        {
            List <WScalarExpression> parameters = new List <WScalarExpression>();
            WTableReference          tableReference;

            if (this.PredicateContext != null)
            {
                parameters.Add(SqlUtil.GetScalarSubquery(this.PredicateContext.ToSelectQueryBlock()));

                // Align
                List <WSelectQueryBlock> selectQueryBlocks = new List <WSelectQueryBlock>();
                selectQueryBlocks.Add(this.TrueChoiceContext.ToSelectQueryBlock());
                selectQueryBlocks.Add(this.FalseChocieContext.ToSelectQueryBlock());

                foreach (WSelectQueryBlock selectQueryBlock in selectQueryBlocks)
                {
                    Dictionary <string, WSelectElement> projectionMap = new Dictionary <string, WSelectElement>();
                    WSelectElement value = selectQueryBlock.SelectElements[0];
                    foreach (WSelectElement selectElement in selectQueryBlock.SelectElements)
                    {
                        projectionMap[(selectElement as WSelectScalarExpression).ColumnName] = selectElement;
                    }
                    selectQueryBlock.SelectElements.Clear();

                    selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr((value as WSelectScalarExpression).SelectExpr, this.DefaultProperty()));
                    foreach (string property in this.ProjectedProperties)
                    {
                        selectQueryBlock.SelectElements.Add(
                            projectionMap.TryGetValue(property, out value)
                                ? value : SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), property));
                    }
                    parameters.Add(SqlUtil.GetScalarSubquery(selectQueryBlock));
                }
                parameters.AddRange(selectQueryBlocks.Select(SqlUtil.GetScalarSubquery));

                tableReference = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Choose, parameters, GetVariableName());
            }
            else
            {
                parameters.Add(SqlUtil.GetScalarSubquery(this.ChoiceContext.ToSelectQueryBlock()));

                foreach (var option in this.Options)
                {
                    if (option.Key is GremlinKeyword.Pick && (GremlinKeyword.Pick)option.Key == GremlinKeyword.Pick.None)
                    {
                        parameters.Add(SqlUtil.GetValueExpr(null));
                    }
                    else
                    {
                        parameters.Add(SqlUtil.GetValueExpr(option.Key));
                    }

                    //Align
                    WSelectQueryBlock selectQueryBlock = option.Value.ToSelectQueryBlock();
                    Dictionary <string, WSelectElement> projectionMap = new Dictionary <string, WSelectElement>();
                    WSelectElement value = selectQueryBlock.SelectElements[0];
                    foreach (WSelectElement selectElement in selectQueryBlock.SelectElements)
                    {
                        projectionMap[(selectElement as WSelectScalarExpression).ColumnName] = selectElement;
                    }
                    selectQueryBlock.SelectElements.Clear();

                    selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr((value as WSelectScalarExpression).SelectExpr, this.DefaultProperty()));
                    foreach (string property in this.ProjectedProperties)
                    {
                        selectQueryBlock.SelectElements.Add(
                            projectionMap.TryGetValue(property, out value)
                                ? value : SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), property));
                    }
                    parameters.Add(SqlUtil.GetScalarSubquery(selectQueryBlock));
                }
                tableReference = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.ChooseWithOptions, parameters, GetVariableName());
            }
            return(SqlUtil.GetCrossApplyTableReference(tableReference));
        }
Пример #23
0
        public override WTableReference ToTableReference()
        {
            if (RepeatContext.ContextLocalPath != null)
            {
                RepeatContext.ContextLocalPath.PathList.Insert(0, null);
                RepeatContext.ContextLocalPath.IsInRepeatContext = true;
            }

            //The following two variables are used for manually creating SelectScalarExpression of repeat
            List <WSelectScalarExpression> repeatFirstSelect  = new List <WSelectScalarExpression>();
            List <WSelectScalarExpression> repeatSecondSelect = new List <WSelectScalarExpression>();

            // The following two variables are used for Generating a Map
            // such as N_0.id -> key_0
            // Then we will use this map to replace ColumnRefernceExpression in the syntax tree which matchs n_0.id to R_0.key_0
            Dictionary <WColumnReferenceExpression, string> repeatVistorMap    = new Dictionary <WColumnReferenceExpression, string>();
            Dictionary <WColumnReferenceExpression, string> conditionVistorMap = new Dictionary <WColumnReferenceExpression, string>();

            //We should generate the syntax tree firstly
            //Some variables will populate ProjectProperty only when we call the ToTableReference function where they appear.
            WRepeatConditionExpression repeatConditionExpr = GetRepeatConditionExpression();
            WSelectQueryBlock          repeatQueryBlock    = RepeatContext.ToSelectQueryBlock();

            // TODO: explain this step in detail
            var repeatNewToOldSelectedVarMap = GetNewToOldSelectedVarMap(RepeatContext);

            if (!repeatNewToOldSelectedVarMap.ContainsKey(RepeatContext.PivotVariable))
            {
                repeatNewToOldSelectedVarMap[RepeatContext.PivotVariable] = RepeatContext.VariableList.First();
            }
            foreach (var pair in repeatNewToOldSelectedVarMap)
            {
                GremlinVariable newVariable = pair.Key;
                GremlinVariable oldVariable = pair.Value;
                foreach (var property in pair.Value.ProjectedProperties)
                {
                    var aliasName          = GenerateKey();
                    var firstSelectColumn  = oldVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                    var secondSelectColumn = newVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;

                    repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                    repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
                    repeatVistorMap[firstSelectColumn.Copy()] = aliasName;
                }
            }

            if (RepeatCondition.TerminationContext != null && RepeatCondition.TerminationContext.VariableList.Count > 0)
            {
                var terminatedNewToOldSelectedVarMap = GetNewToOldSelectedVarMap(RepeatCondition.TerminationContext);
                if (!terminatedNewToOldSelectedVarMap.ContainsKey(RepeatContext.PivotVariable))
                {
                    terminatedNewToOldSelectedVarMap[RepeatContext.PivotVariable] = RepeatCondition.TerminationContext.VariableList.First();
                }
                foreach (var pair in terminatedNewToOldSelectedVarMap)
                {
                    GremlinVariable newVariable = pair.Key;
                    GremlinVariable oldVariable = pair.Value;
                    foreach (var property in oldVariable.ProjectedProperties)
                    {
                        var aliasName         = GenerateKey();
                        var firstSelectColumn = RepeatCondition.StartFromContext
                            ? oldVariable.GetVariableProperty(property).ToScalarExpression()
                            : SqlUtil.GetValueExpr(null);
                        var secondSelectColumn = newVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;

                        repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                        repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));

                        if (RepeatCondition.StartFromContext)
                        {
                            conditionVistorMap[(firstSelectColumn as WColumnReferenceExpression).Copy()] = aliasName;
                        }
                        else
                        {
                            conditionVistorMap[secondSelectColumn.Copy()] = aliasName;
                        }
                    }
                }
            }

            if (RepeatCondition.EmitContext != null && RepeatCondition.EmitContext.VariableList.Count > 0)
            {
                var terminatedNewToOldSelectedVarMap = GetNewToOldSelectedVarMap(RepeatCondition.EmitContext);
                if (!terminatedNewToOldSelectedVarMap.ContainsKey(RepeatContext.PivotVariable))
                {
                    terminatedNewToOldSelectedVarMap[RepeatContext.PivotVariable] = RepeatCondition.EmitContext.VariableList.First();
                }
                foreach (var pair in terminatedNewToOldSelectedVarMap)
                {
                    GremlinVariable newVariable = pair.Key;
                    GremlinVariable oldVariable = pair.Value;
                    foreach (var property in pair.Value.ProjectedProperties)
                    {
                        var aliasName         = GenerateKey();
                        var firstSelectColumn = RepeatCondition.IsEmitContext
                            ? oldVariable.GetVariableProperty(property).ToScalarExpression()
                            : SqlUtil.GetValueExpr(null);
                        var secondSelectColumn = newVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;

                        repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                        repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));

                        if (RepeatCondition.IsEmitContext)
                        {
                            conditionVistorMap[(firstSelectColumn as WColumnReferenceExpression).Copy()] = aliasName;
                        }
                        else
                        {
                            conditionVistorMap[secondSelectColumn.Copy()] = aliasName;
                        }
                    }
                }
            }

            foreach (var property in ProjectedProperties)
            {
                if (property == GremlinKeyword.Path)
                {
                    repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), GremlinKeyword.Path));
                    repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(RepeatContext.ContextLocalPath.GetDefaultProjection().ToScalarExpression(), GremlinKeyword.Path));
                    continue;
                }
                WScalarExpression firstExpr = InputVariable.ProjectedProperties.Contains(property)
                    ? InputVariable.GetVariableProperty(property).ToScalarExpression()
                    : SqlUtil.GetValueExpr(null);

                WScalarExpression secondExpr = RepeatContext.PivotVariable.ProjectedProperties.Contains(property)
                    ? RepeatContext.PivotVariable.GetVariableProperty(property).ToScalarExpression()
                    : SqlUtil.GetValueExpr(null);

                repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(firstExpr, property));
                repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(secondExpr, property));
            }

            WSelectQueryBlock firstQueryExpr = new WSelectQueryBlock();

            foreach (var selectColumnExpr in repeatFirstSelect)
            {
                firstQueryExpr.SelectElements.Add(selectColumnExpr);
            }

            repeatQueryBlock = RepeatContext.ToSelectQueryBlock();
            repeatQueryBlock.SelectElements.Clear();
            foreach (var selectColumnExpr in repeatSecondSelect)
            {
                repeatQueryBlock.SelectElements.Add(selectColumnExpr);
            }

            //Replace N_0.id -> R_0.key_0, when N_0 is a outer variable
            new ModifyColumnNameVisitor().Invoke(repeatQueryBlock, repeatVistorMap);
            new ModifyColumnNameVisitor().Invoke(repeatConditionExpr, conditionVistorMap);

            List <WScalarExpression> repeatParameters = new List <WScalarExpression>()
            {
                SqlUtil.GetScalarSubquery(SqlUtil.GetBinaryQueryExpr(firstQueryExpr, repeatQueryBlock)),
                repeatConditionExpr
            };
            var tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Repeat, repeatParameters, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }
Пример #24
0
        public override WTableReference ToTableReference()
        {
            Dictionary <GremlinVariableProperty, string> map  = new Dictionary <GremlinVariableProperty, string>();
            Dictionary <GremlinVariableProperty, string> map2 = new Dictionary <GremlinVariableProperty, string>();
            Dictionary <GremlinVariableProperty, string> map3 = new Dictionary <GremlinVariableProperty, string>();
            Dictionary <GremlinVariableProperty, string> map4 = new Dictionary <GremlinVariableProperty, string>();

            WRepeatConditionExpression conditionExpr = GetRepeatConditionExpression();

            List <WSelectScalarExpression> inputSelectList        = GetInputSelectList(ref map);
            List <WSelectScalarExpression> outerSelectList        = GetOuterSelectList(ref map);
            List <WSelectScalarExpression> terminateSelectList    = GetConditionSelectList(ref map2);
            List <WSelectScalarExpression> repeatPathOuterList    = GetRepeatPathOuterVariableList(ref map3);
            List <WSelectScalarExpression> conditionPathOuterList = GetConditionPathOuterVariableList(ref map4);

            WSelectQueryBlock selectQueryBlock = RepeatContext.ToSelectQueryBlock();

            selectQueryBlock.SelectElements.Clear();

            foreach (var selectElement in inputSelectList)
            {
                selectQueryBlock.SelectElements.Add(selectElement);
            }
            foreach (var selectElement in outerSelectList)
            {
                selectQueryBlock.SelectElements.Add(selectElement);
            }
            foreach (var selectElement in terminateSelectList)
            {
                selectQueryBlock.SelectElements.Add(selectElement);
            }
            foreach (var selectElement in repeatPathOuterList)
            {
                selectQueryBlock.SelectElements.Add(selectElement);
            }
            foreach (var selectElement in conditionPathOuterList)
            {
                selectQueryBlock.SelectElements.Add(selectElement);
            }

            WSelectQueryBlock firstQueryExpr = new WSelectQueryBlock();

            foreach (var item in map)
            {
                firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(item.Key.ToScalarExpression(), item.Value));
            }
            foreach (var item in map2)
            {
                firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), item.Value));
            }
            foreach (var item in map3)
            {
                firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(item.Key.ToScalarExpression(), item.Value));
            }
            foreach (var item in map4)
            {
                firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(item.Key.ToScalarExpression(), item.Value));
            }

            //firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(FirstVariable.DefaultProjection().ToScalarExpression(), GremlinKeyword.TableDefaultColumnName));
            //selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(RepeatContext.PivotVariable.DefaultProjection().ToScalarExpression(), GremlinKeyword.TableDefaultColumnName));
            foreach (var property in ProjectedProperties)
            {
                if (InputVariable.ProjectedProperties.Contains(property))
                {
                    firstQueryExpr.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(
                            InputVariable.GetVariableProperty(property).ToScalarExpression(), property));
                }
                else
                {
                    firstQueryExpr.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), property));
                }
                if (RepeatContext.PivotVariable.ProjectedProperties.Contains(property))
                {
                    selectQueryBlock.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(
                            RepeatContext.PivotVariable.GetVariableProperty(property).ToScalarExpression(), property));
                }
                else
                {
                    selectQueryBlock.SelectElements.Add(
                        SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), property));
                }
            }

            if (SelectedVariableList.Count != 0)
            {
                foreach (var selectedVariableTuple in SelectedVariableList)
                {
                    var columnName       = selectedVariableTuple.Item1;
                    var selectedVariable = selectedVariableTuple.Item2;
                    firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), columnName));

                    List <WScalarExpression> compose2Paramters = new List <WScalarExpression>();
                    compose2Paramters.Add(selectedVariable.RealVariable.ToCompose1());
                    compose2Paramters.Add(SqlUtil.GetColumnReferenceExpr("R", columnName));
                    WFunctionCall compose2 = SqlUtil.GetFunctionCall(GremlinKeyword.func.Compose2, compose2Paramters);
                    selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(compose2, columnName));
                }
            }

            if (RepeatContext.IsPopulateGremlinPath)
            {
                var columnName   = GremlinKeyword.Path;
                var pathVariable = RepeatContext.CurrentContextPath;
                firstQueryExpr.SelectElements.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), columnName));
                selectQueryBlock.SelectElements.Add(SqlUtil.GetSelectScalarExpr(pathVariable.DefaultProjection().ToScalarExpression(), columnName));
            }

            var WBinaryQueryExpression = SqlUtil.GetBinaryQueryExpr(firstQueryExpr, selectQueryBlock);


            ModifyColumnNameVisitor newVisitor = new ModifyColumnNameVisitor();

            newVisitor.Invoke(selectQueryBlock, map);
            newVisitor.Invoke(conditionExpr, map2);
            newVisitor.Invoke(selectQueryBlock, map3);
            newVisitor.Invoke(conditionExpr, map4);

            List <WScalarExpression> repeatParameters = new List <WScalarExpression>();

            repeatParameters.Add(SqlUtil.GetScalarSubquery(WBinaryQueryExpression));
            repeatParameters.Add(conditionExpr);
            var secondTableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Repeat, repeatParameters, this, GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(null, secondTableRef));
        }
Пример #25
0
        // Repeat algorithm
        // Firstly, we generate the repeatQueryBlock in order that we can get the initial query
        // Secondly, we generate the inputVariableVistorMap via repeatInputVariable.ProjectedProperties,
        // untilInputVariable.ProjectedProperties, emitInputVariable.ProjectedProperties and ProjectedProperties.
        // Generally, these properties are related to the input every time, but in repeatQueryBlock, these are
        // just related to the input of the first time. Therefore, we need to replace these after.
        // Thirdly, we need to generate the firstQueryExpr and the selectColumnExpr of repeatQueryBlock. Pay
        // attention, we need to repeatQueryBlock again because we need more properties about the output of
        // the last step in repeat-step. These properties are populated in the second step.
        // Fourthly, we use the inputVariableVistorMap to replace the columns in the repeatQueryBlock. But we
        // should not change the columns in path-step. Because if we generate the path in the repeat-step, the
        // path consists of
        //  1. the previous steps before the repeat-step
        //  2. the local path(_path) in the repeat-step
        // Keep in mind that the initial _path is null, the _path includes all steps as long as they are in
        // the repeat-step except for the first input. And the _path after the first pass includes the last step
        // in the repeat-step. So the path must include the two part. That means all columns in path-step should
        // not be replaced. Here, we use the ModifyRepeatInputVariablesVisitor to finish this work. If it visits
        // WPathTableReference, it does nothing, otherwise, it will replace the columns according to the
        // inputVariableVistorMap.
        public override WTableReference ToTableReference()
        {
            //The following two variables are used for manually creating SelectScalarExpression of repeat
            List <WSelectScalarExpression> firstSelectList = new List <WSelectScalarExpression>();
            List <WSelectScalarExpression> secondSelectList = new List <WSelectScalarExpression>();
            WScalarExpression firstSelectColumn, secondSelectColumn, firstExpr, secondExpr;

            // The following map is used to replace columns of the first input to columns of the repeat input
            // such as N_0.id -> R.key_0
            string aliasName;
            Tuple <string, string> key, value;
            Dictionary <Tuple <string, string>, Tuple <string, string> > inputVariableVistorMap =
                new Dictionary <Tuple <string, string>, Tuple <string, string> >();

            // We should generate the syntax tree firstly
            // Some variables will populate ProjectProperty only when we call the ToTableReference function where they appear.
            WRepeatConditionExpression repeatConditionExpr = this.GetRepeatConditionExpression();
            WSelectQueryBlock          repeatQueryBlock    = this.RepeatContext.ToSelectQueryBlock();

            GremlinVariable repeatInputVariable  = this.RepeatContext.VariableList.First();
            GremlinVariable repeatOutputVariable = this.RepeatContext.PivotVariable;
            GremlinVariable realInputVariable    = this.InputVariable.RealVariable;
            GremlinVariable repeatPivotVariable  = this.RepeatContext.PivotVariable;

            // this.DefaultProperty
            key   = new Tuple <string, string>(this.GetVariableName(), this.DefaultProperty());
            value = key;
            inputVariableVistorMap[key] = value;

            key   = new Tuple <string, string>(realInputVariable.GetVariableName(), realInputVariable.DefaultProperty());
            value = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, this.DefaultProperty());
            inputVariableVistorMap[key] = value;

            firstExpr  = realInputVariable.DefaultProjection().ToScalarExpression();
            secondExpr = repeatOutputVariable.DefaultProjection().ToScalarExpression();
            firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstExpr, this.DefaultProperty()));
            secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondExpr, this.DefaultProperty()));

            foreach (string property in this.ProjectedProperties)
            {
                if (property == GremlinKeyword.Path)
                {
                    firstSelectList.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), GremlinKeyword.Path));
                    secondSelectList.Add(
                        SqlUtil.GetSelectScalarExpr(
                            this.RepeatContext.ContextLocalPath.DefaultProjection().ToScalarExpression(),
                            GremlinKeyword.Path));
                }
                else
                {
                    key   = new Tuple <string, string>(this.GetVariableName(), property);
                    value = key;
                    inputVariableVistorMap[key] = value;

                    key   = new Tuple <string, string>(realInputVariable.GetVariableName(), property);
                    value = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, property);
                    inputVariableVistorMap[key] = value;

                    firstExpr = realInputVariable.ProjectedProperties.Contains(property)
                        ? realInputVariable.GetVariableProperty(property).ToScalarExpression()
                        : SqlUtil.GetValueExpr(null);
                    secondExpr = repeatOutputVariable.ProjectedProperties.Contains(property)
                        ? this.RepeatContext.PivotVariable.GetVariableProperty(property).ToScalarExpression()
                        : SqlUtil.GetValueExpr(null);

                    firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstExpr, property));
                    secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondExpr, property));
                }
            }

            // repeatInputVariable.DefaultProperty()
            key = new Tuple <string, string>(repeatInputVariable.GetVariableName(), repeatInputVariable.DefaultProperty());
            if (!inputVariableVistorMap.Keys.Contains(key))
            {
                aliasName = this.GenerateKey();
                value     = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, aliasName);
                inputVariableVistorMap[key] = value;

                firstSelectColumn  = repeatInputVariable.DefaultProjection().ToScalarExpression() as WColumnReferenceExpression;
                secondSelectColumn = repeatPivotVariable.DefaultProjection().ToScalarExpression() as WColumnReferenceExpression;
                firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
            }
            foreach (string property in repeatInputVariable.ProjectedProperties)
            {
                key = new Tuple <string, string>(repeatInputVariable.GetVariableName(), property);
                if (!inputVariableVistorMap.Keys.Contains(key))
                {
                    aliasName = this.GenerateKey();
                    value     = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, aliasName);
                    inputVariableVistorMap[key] = value;

                    firstSelectColumn  = repeatInputVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                    secondSelectColumn = repeatPivotVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                    firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                    secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
                }
            }

            if (this.RepeatCondition.TerminationContext != null && this.RepeatCondition.TerminationContext.VariableList.Count > 0)
            {
                GremlinVariable untilInputVariable = this.RepeatCondition.TerminationContext.VariableList.First();

                // untilInputVariable.DefaultProperty()
                key = new Tuple <string, string>(untilInputVariable.GetVariableName(), untilInputVariable.DefaultProperty());
                if (!inputVariableVistorMap.Keys.Contains(key))
                {
                    aliasName = this.GenerateKey();
                    value     = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, aliasName);
                    inputVariableVistorMap[key] = value;

                    firstSelectColumn =
                        untilInputVariable.DefaultProjection().ToScalarExpression() as WColumnReferenceExpression;
                    secondSelectColumn =
                        repeatPivotVariable.DefaultProjection().ToScalarExpression() as WColumnReferenceExpression;
                    firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                    secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
                }

                foreach (string property in untilInputVariable.ProjectedProperties)
                {
                    key = new Tuple <string, string>(untilInputVariable.GetVariableName(), property);
                    if (!inputVariableVistorMap.Keys.Contains(key))
                    {
                        aliasName = this.GenerateKey();
                        value     = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, aliasName);
                        inputVariableVistorMap[key] = value;

                        firstSelectColumn  = untilInputVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                        secondSelectColumn = repeatPivotVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                        firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                        secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
                    }
                }
            }

            if (this.RepeatCondition.EmitContext != null && this.RepeatCondition.EmitContext.VariableList.Count > 0)
            {
                GremlinVariable emitInputVariable = this.RepeatCondition.EmitContext.VariableList.First();

                // emitInputVariable.DefaultProperty()
                key = new Tuple <string, string>(emitInputVariable.GetVariableName(), emitInputVariable.DefaultProperty());
                if (!inputVariableVistorMap.Keys.Contains(key))
                {
                    aliasName = this.GenerateKey();
                    value     = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, aliasName);
                    inputVariableVistorMap[key] = value;

                    firstSelectColumn =
                        emitInputVariable.DefaultProjection().ToScalarExpression() as WColumnReferenceExpression;
                    secondSelectColumn =
                        repeatPivotVariable.DefaultProjection().ToScalarExpression() as WColumnReferenceExpression;
                    firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                    secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
                }

                foreach (string property in emitInputVariable.ProjectedProperties)
                {
                    key = new Tuple <string, string>(emitInputVariable.GetVariableName(), property);
                    if (!inputVariableVistorMap.Keys.Contains(key))
                    {
                        aliasName = this.GenerateKey();
                        value     = new Tuple <string, string>(GremlinKeyword.RepeatInitalTableName, aliasName);
                        inputVariableVistorMap[key] = value;

                        firstSelectColumn =
                            emitInputVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                        secondSelectColumn =
                            repeatPivotVariable.GetVariableProperty(property).ToScalarExpression() as WColumnReferenceExpression;
                        firstSelectList.Add(SqlUtil.GetSelectScalarExpr(firstSelectColumn, aliasName));
                        secondSelectList.Add(SqlUtil.GetSelectScalarExpr(secondSelectColumn, aliasName));
                    }
                }
            }

            WSelectQueryBlock firstQueryExpr = new WSelectQueryBlock();

            foreach (WSelectScalarExpression selectColumnExpr in firstSelectList)
            {
                firstQueryExpr.SelectElements.Add(selectColumnExpr);
            }

            repeatQueryBlock = this.RepeatContext.ToSelectQueryBlock();
            repeatQueryBlock.SelectElements.Clear();
            foreach (WSelectScalarExpression selectColumnExpr in secondSelectList)
            {
                repeatQueryBlock.SelectElements.Add(selectColumnExpr);
            }

            //Then we will use the inputVariableVistorMap to replace ColumnRefernceExpression in the syntax tree
            //which matchs n_0.id to R_0.key_0 except for WPathTableReference
            new ModifyRepeatInputVariablesVisitor().Invoke(repeatQueryBlock, inputVariableVistorMap);
            new ModifyRepeatInputVariablesVisitor().Invoke(repeatConditionExpr, inputVariableVistorMap);

            List <WScalarExpression> repeatParameters = new List <WScalarExpression>()
            {
                SqlUtil.GetScalarSubquery(SqlUtil.GetBinaryQueryExpr(firstQueryExpr, repeatQueryBlock)),
                repeatConditionExpr
            };
            WSchemaObjectFunctionTableReference tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.Repeat, repeatParameters, this.GetVariableName());

            return(SqlUtil.GetCrossApplyTableReference(tableRef));
        }