public override void Visit(WSchemaObjectFunctionTableReference node) { if (node is WAggregateTableReference || node is WBarrierTableReference || node is WBoundNodeTableReference || node is WCoinTableReference || node is WConstantReference || node is WDedupGlobalTableReference || node is WGroupTableReference || node is WInjectTableReference || node is WOrderGlobalTableReference || node is WRangeGlobalTableReference || node is WSampleGlobalTableReference || node is WSelectOneTableReference || node is WSelectColumnTableReference || node is WStoreTableReference || node is WSubgraphTableReference || node is WTreeTableReference) { blocks.Add(new AggregationBlock(node)); } else if (new ModificationVisitor().Invoke(node)) { blocks.Add(new AggregationBlock(node)); } else { blocks.Last().AddTable(node); } }
/// <summary> /// Generate the Table-Valued Function by the edge given the node alias /// </summary> /// <param name="edge"></param> /// <param name="nodeAlias"></param> /// <returns></returns> private static WTableReference EdgeToTableReference(MatchEdge edge, string nodeAlias) { var edgeColumn = edge.EdgeColumn; var edgeIdentifiers = edge.EdgeColumn.MultiPartIdentifier.Identifiers; if (nodeAlias != edgeIdentifiers.First().Value) { var identifiers = new List <Identifier>(edgeIdentifiers); identifiers.RemoveAt(0); identifiers.Insert(0, new Identifier { Value = nodeAlias }); edgeColumn = new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier { Identifiers = identifiers } }; } var columnName = edgeIdentifiers.Last().Value; var deColIdentifiers = new Identifier[] { edgeColumn.MultiPartIdentifier.Identifiers.First(), new Identifier { Value = columnName + "DeleteCol" } }; var decoderFunction = new Identifier { Value = edge.SourceNode.TableObjectName.SchemaIdentifier.Value + '_' + edge.SourceNode.TableObjectName.BaseIdentifier.Value + '_' + columnName + '_' + "Decoder", }; var tableRef = new WSchemaObjectFunctionTableReference { SchemaObject = new WSchemaObjectName( new Identifier { Value = "dbo" }, decoderFunction), Parameters = new List <WScalarExpression> { edgeColumn, new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier(deColIdentifiers) } }, Alias = new Identifier { Value = edge.EdgeAlias, } }; return(tableRef); }
internal string AddTable(WSchemaObjectFunctionTableReference table) { string alias = table.Alias.Value; this.NonFreeTables[alias] = new NonFreeTable(table); this.TableInputDependency[alias] = new HashSet <string>(); this.NonFreeTables[alias].Position = this.TableInputDependency.Count; return(alias); }
public override void Visit(WSchemaObjectFunctionTableReference tableReference) { if (tableReference is WGroupTableReference) { aggregateFunctionCount++; } tableReference.AcceptChildren(this); }
public AggregationBlock(WSchemaObjectFunctionTableReference table) { this.RootTableAlias = table.Alias.Value; this.GraphPattern = new MatchGraph(); this.NonFreeTables = new Dictionary <string, NonFreeTable>(); this.NonFreeTables[RootTableAlias] = new NonFreeTable(table); this.TableInputDependency = new Dictionary <string, HashSet <string> >(); this.TableInputDependency[RootTableAlias] = new HashSet <string>(); this.NonFreeTables[RootTableAlias].Position = this.TableInputDependency.Count; }
public override void Visit(WSchemaObjectFunctionTableReference tableRef) { if (tableRef is WPathTableReference) { return; } else { tableRef.AcceptChildren(this); } }
public override void Visit(WSchemaObjectFunctionTableReference node) { if (node is WAddETableReference || node is WAddVTableReference || node is WCommitTableReference || node is WDropTableReference || node is WUpdatePropertiesTableReference) { hasModification = true; } else { base.Visit(node); } }
public override WTableReference ToTableReference() { List <WScalarExpression> parameters = new List <WScalarExpression> { SqlUtil.GetValueExpr(this.AmountToSample) }; if (this.ProbabilityContext != null) { parameters.Add(SqlUtil.GetScalarSubquery(this.ProbabilityContext.ToSelectQueryBlock())); } WSchemaObjectFunctionTableReference tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.SampleGlobal, parameters, this.GetVariableName()); return(SqlUtil.GetCrossApplyTableReference(tableRef)); }
public override WTableReference ToTableReference() { List <WScalarExpression> parameters = new List <WScalarExpression> { this.InputVariable.GetDefaultProjection().ToScalarExpression(), SqlUtil.GetValueExpr(this.AmountToSample) }; foreach (string property in this.ProjectedProperties) { parameters.Add(SqlUtil.GetValueExpr(property)); } WSchemaObjectFunctionTableReference tableRef = SqlUtil.GetFunctionTableReference(GremlinKeyword.func.SampleLocal, parameters, this.GetVariableName()); return(SqlUtil.GetCrossApplyTableReference(tableRef)); }
private WTableReference ParseTableReference(TableReference tabRef) { if (tabRef == null) { return null; } var tabRefWithAlias = tabRef as TableReferenceWithAlias; if (tabRefWithAlias!=null && tabRefWithAlias.Alias!=null && GraphViewKeywords._keywords.Contains(tabRefWithAlias.Alias.Value)) { var token = _tokens[tabRefWithAlias.Alias.FirstTokenIndex]; throw new SyntaxErrorException(token.Line, tabRefWithAlias.Alias.Value, "System restricted Name cannot be used"); } switch (tabRef.GetType().Name) { case "NamedTableReference": { var oref = tabRef as NamedTableReference; if (oref.SchemaObject.BaseIdentifier.QuoteType == QuoteType.NotQuoted && (oref.SchemaObject.BaseIdentifier.Value[0] == '@' || oref.SchemaObject.BaseIdentifier.Value[0] == '#')) { var pref = new WSpecialNamedTableReference { Alias = oref.Alias, TableHints = new List<WTableHint>(), FirstTokenIndex = oref.FirstTokenIndex, LastTokenIndex = oref.LastTokenIndex, TableObjectName = ParseSchemaObjectName(oref.SchemaObject), }; if (oref.TableHints != null) { foreach (var hint in oref.TableHints) pref.TableHints.Add(ParseTableHint(hint)); } return pref; } else { var pref = new WNamedTableReference { Alias = oref.Alias, TableHints = new List<WTableHint>(), FirstTokenIndex = oref.FirstTokenIndex, LastTokenIndex = oref.LastTokenIndex, TableObjectName = ParseSchemaObjectName(oref.SchemaObject), }; if (oref.TableHints != null) { foreach (var hint in oref.TableHints) pref.TableHints.Add(ParseTableHint(hint)); } return pref; } } case "QueryDerivedTable": { var oref = tabRef as QueryDerivedTable; var pref = new WQueryDerivedTable { QueryExpr = ParseSelectQueryStatement(oref.QueryExpression), Alias = oref.Alias, Columns = oref.Columns, FirstTokenIndex = oref.FirstTokenIndex, LastTokenIndex = oref.LastTokenIndex, }; return pref; } case "SchemaObjectFunctionTableReference": { var oref = tabRef as SchemaObjectFunctionTableReference; var pref = new WSchemaObjectFunctionTableReference { Alias = oref.Alias, Columns = oref.Columns, SchemaObject = ParseSchemaObjectName(oref.SchemaObject), FirstTokenIndex = oref.FirstTokenIndex, LastTokenIndex = oref.LastTokenIndex }; if (oref.Parameters == null) return pref; pref.Parameters = new List<WScalarExpression>(); foreach (var param in oref.Parameters) pref.Parameters.Add(ParseScalarExpression(param)); return pref; } case "QualifiedJoin": { var oref = tabRef as QualifiedJoin; var pref = new WQualifiedJoin { FirstTableRef = ParseTableReference(oref.FirstTableReference), SecondTableRef = ParseTableReference(oref.SecondTableReference), QualifiedJoinType = oref.QualifiedJoinType, JoinHint = oref.JoinHint, JoinCondition = ParseBooleanExpression(oref.SearchCondition), FirstTokenIndex = oref.FirstTokenIndex, LastTokenIndex = oref.LastTokenIndex, }; return pref; } case "UnqualifiedJoin": { var oref = tabRef as UnqualifiedJoin; var pref = new WUnqualifiedJoin { FirstTableRef = ParseTableReference(oref.FirstTableReference), SecondTableRef = ParseTableReference(oref.SecondTableReference), UnqualifiedJoinType = oref.UnqualifiedJoinType, FirstTokenIndex = oref.FirstTokenIndex, LastTokenIndex = oref.LastTokenIndex, }; return pref; } case "JoinParenthesisTableReference": { var ptab = tabRef as JoinParenthesisTableReference; var wptab = new WParenthesisTableReference { Table = ParseTableReference(ptab.Join), FirstTokenIndex = ptab.FirstTokenIndex, LastTokenIndex = ptab.LastTokenIndex, }; return wptab; } default: return null; } }
// 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)); }
/// <summary> /// Generate the Table-Valued Function by the edge given the node alias /// </summary> /// <param name="edge"></param> /// <param name="nodeAlias"></param> /// <returns></returns> private static WTableReference EdgeToTableReference(MatchEdge edge, string nodeAlias) { var edgeColumn = edge.EdgeColumn; var edgeIdentifiers = edge.EdgeColumn.MultiPartIdentifier.Identifiers; if (nodeAlias != edgeIdentifiers.First().Value) { var identifiers = new List<Identifier>(edgeIdentifiers); identifiers.RemoveAt(0); identifiers.Insert(0, new Identifier { Value = nodeAlias }); edgeColumn = new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier { Identifiers = identifiers } }; } var columnName = edgeIdentifiers.Last().Value; var deColIdentifiers = new Identifier[] { edgeColumn.MultiPartIdentifier.Identifiers.First(), new Identifier {Value = columnName + "DeleteCol"} }; var decoderFunction = new Identifier { Value = edge.SourceNode.TableObjectName.SchemaIdentifier.Value + '_' + edge.SourceNode.TableObjectName.BaseIdentifier.Value + '_' + columnName + '_' + "Decoder", }; var tableRef = new WSchemaObjectFunctionTableReference { SchemaObject = new WSchemaObjectName( new Identifier {Value = "dbo"}, decoderFunction), Parameters = new List<WScalarExpression> { edgeColumn, new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier(deColIdentifiers) } }, Alias = new Identifier { Value = edge.EdgeAlias, } }; return tableRef; }
public override void Visit(WSchemaObjectFunctionTableReference node) { nonVertexTableList.Add(node); }
public virtual void Visit(WSchemaObjectFunctionTableReference node) { node.AcceptChildren(this); }