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())); }
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())); }
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(); 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())); }
internal virtual WFunctionCall ToCompose1() { List <WScalarExpression> parameters = new List <WScalarExpression>(); var projectKey = GetProjectKey(); parameters.Add(SqlUtil.GetValueExpr(projectKey)); foreach (var projectProperty in ProjectedProperties) { parameters.Add(GetVariableProperty(projectProperty).ToScalarExpression()); parameters.Add(SqlUtil.GetValueExpr(projectProperty)); } return(SqlUtil.GetFunctionCall(GremlinKeyword.func.Compose1, parameters)); }
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())); }
internal WScalarExpression ToScalarExpression() { List <WScalarExpression> parameters = new List <WScalarExpression>(); foreach (var variable in GremlinVariableList) { if (variable is GremlinGhostVariable && (variable as GremlinGhostVariable).RealVariable is GremlinRepeatSelectedVariable) { parameters.Add(variable.DefaultVariableProperty().ToScalarExpression()); } else { parameters.Add(variable.ToCompose1()); } } return(SqlUtil.GetFunctionCall(GremlinKeyword.func.Compose2, parameters)); }
internal virtual WFunctionCall ToCompose1() { List <WScalarExpression> parameters = new List <WScalarExpression> { SqlUtil.GetValueExpr(GremlinKeyword.TableDefaultColumnName), GetDefaultProjection().ToScalarExpression(), SqlUtil.GetValueExpr(GremlinKeyword.TableDefaultColumnName) }; foreach (var projectProperty in ProjectedProperties) { if (projectProperty == GremlinKeyword.TableDefaultColumnName) { continue; } parameters.Add(GetVariableProperty(projectProperty).ToScalarExpression()); parameters.Add(SqlUtil.GetValueExpr(projectProperty)); } return(SqlUtil.GetFunctionCall(GremlinKeyword.func.Compose1, parameters)); }
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)); }
public override WTableReference ToTableReference() { //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); 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] = aliasName; } } if (RepeatCondition.TerminationContext != null && RepeatCondition.TerminationContext.VariableList.Count > 0) { var terminatedNewToOldSelectedVarMap = GetNewToOldSelectedVarMap(RepeatCondition.TerminationContext); terminatedNewToOldSelectedVarMap[RepeatContext.PivotVariable] = RepeatCondition.TerminationContext.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.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] = aliasName; } else { conditionVistorMap[secondSelectColumn] = aliasName; } } } } if (RepeatCondition.EmitContext != null && RepeatCondition.EmitContext.VariableList.Count > 0) { var terminatedNewToOldSelectedVarMap = GetNewToOldSelectedVarMap(RepeatCondition.EmitContext); 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] = aliasName; } else { conditionVistorMap[secondSelectColumn] = aliasName; } } } } foreach (var property in ProjectedProperties) { 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)); } if (SelectedVariableList.Count != 0) { foreach (var selectedVariableTuple in SelectedVariableList) { var columnName = selectedVariableTuple.Item1; var selectedVariable = selectedVariableTuple.Item2; var compose2 = SqlUtil.GetFunctionCall(GremlinKeyword.func.Compose2, new List <WScalarExpression>() { SqlUtil.GetColumnReferenceExpr(GremlinKeyword.RepeatInitalTableName, columnName), selectedVariable.RealVariable.ToCompose1() }); repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), columnName)); repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(compose2, columnName)); } } if (RepeatContext.IsPopulateGremlinPath) { repeatFirstSelect.Add(SqlUtil.GetSelectScalarExpr(SqlUtil.GetValueExpr(null), GremlinKeyword.Path)); repeatSecondSelect.Add(SqlUtil.GetSelectScalarExpr(RepeatContext.CurrentContextPath.DefaultProjection().ToScalarExpression(), GremlinKeyword.Path)); } 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)); }