Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion bql/grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ func SemanticBQL() *Grammar {
}
setElementHook(semanticBQL, varSymbols, semantic.VarAccumulatorHook(), nil)

// Collect and valiadate group by bindinds.
// Collect and validate group by bindings.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx!

grpSymbols := []semantic.Symbol{"GROUP_BY", "GROUP_BY_BINDINGS"}
setElementHook(semanticBQL, grpSymbols, semantic.GroupByBindings(), nil)
setClauseHook(semanticBQL, []semantic.Symbol{"GROUP_BY"}, nil, semantic.GroupByBindingsChecker())
Expand Down Expand Up @@ -952,5 +952,12 @@ func SemanticBQL() *Grammar {
})
setClauseHook(semanticBQL, []semantic.Symbol{"START"}, nil, semantic.GroupByBindingsChecker())

// CONSTRUCT clause semantic hooks.
setClauseHook(semanticBQL, []semantic.Symbol{"CONSTRUCT"}, nil, semantic.TypeBindingClauseHook(semantic.Construct))
setClauseHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_FACTS"}, semantic.InitWorkingConstructClauseHook(), nil)
constructTriplesSymbols := []semantic.Symbol{"CONSTRUCT_TRIPLES", "MORE_CONSTRUCT_TRIPLES"}
setClauseHook(semanticBQL, constructTriplesSymbols, semantic.NextWorkingConstructClauseHook(), semantic.NextWorkingConstructClauseHook())


return semanticBQL
}
35 changes: 34 additions & 1 deletion bql/semantic/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ func CollectGlobalBounds() ElementHook {
return collectGlobalBounds()
}

// InitWorkingConstructClauseHook returns the singleton for clause accumulation within the construct statement.
func InitWorkingConstructClauseHook() ClauseHook {
return InitWorkingConstructClause()
}

// NextWorkingConstructClauseHook returns the singleton for clause accumulation within the construct statement.
func NextWorkingConstructClauseHook() ClauseHook {
return NextWorkingConstructClause()
}

// TypeBindingClauseHook returns a ClauseHook that sets the binding type.
func TypeBindingClauseHook(t StatementType) ClauseHook {
var f ClauseHook
Expand Down Expand Up @@ -567,7 +577,7 @@ func whereObjectClause() ElementHook {
return f
}

// whereObjectClause returns an element hook that updates the object
// varAccumulator returns an element hook that updates the object
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx!

// modifiers on the working graph clause.
func varAccumulator() ElementHook {
var (
Expand Down Expand Up @@ -862,3 +872,26 @@ func collectGlobalBounds() ElementHook {
}
return f
}

// InitWorkingConstructClause returns a clause hook to initialize a new working
// construct clause.
func InitWorkingConstructClause() ClauseHook {
var f ClauseHook
f = func(s *Statement, _ Symbol) (ClauseHook, error) {
s.ResetWorkingConstructClause()
return f, nil
}
return f
}


// NextWorkingConstructClause returns a clause hook to close the current working
// construct clause and start a new working construct clause.
func NextWorkingConstructClause() ClauseHook {
var f ClauseHook
f = func(s *Statement, _ Symbol) (ClauseHook, error) {
s.AddWorkingConstructClause()
return f, nil
}
return f
}
37 changes: 33 additions & 4 deletions bql/semantic/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestTypeBindingClauseHook(t *testing.T) {
st := &Statement{}
f(st, Symbol("FOO"))
if got, want := st.Type(), Insert; got != want {
t.Errorf("semantic.TypeBidingHook failed to set the right type; got %s, want %s", got, want)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow...

t.Errorf("semantic.TypeBindingHook failed to set the right type; got %s, want %s", got, want)
}
}

Expand All @@ -153,9 +153,14 @@ func TestWhereWorkingClauseHook(t *testing.T) {
f := whereNextWorkingClause()
st := &Statement{}
st.ResetWorkingGraphClause()
wcs := st.WorkingClause()
wcs.SBinding = "?a"
f(st, Symbol("FOO"))
wcs = st.WorkingClause()
wcs.SBinding = "?b"
f(st, Symbol("FOO"))
if got, want := len(st.GraphPatternClauses()), 0; got != want {

if got, want := len(st.GraphPatternClauses()), 2; got != want {
t.Errorf("semantic.whereNextWorkingClause should have returned two clauses for statement %v; got %d, want %d", st, got, want)
}
}
Expand Down Expand Up @@ -1169,7 +1174,7 @@ func TestBindingsGraphChecker(t *testing.T) {
want bool
}{
{
id: "missing biding",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:'(

id: "missing binding",
s: &Statement{
pattern: []*GraphClause{
{},
Expand Down Expand Up @@ -1199,7 +1204,7 @@ func TestBindingsGraphChecker(t *testing.T) {
want: false,
},
{
id: "all bidings available",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:'(

id: "all bindings available",
s: &Statement{
pattern: []*GraphClause{
{},
Expand Down Expand Up @@ -1913,3 +1918,27 @@ func TestCollectGlobalBounds(t *testing.T) {
}
}
}

func TestInitWorkingConstructClauseHook(t *testing.T) {
f := InitWorkingConstructClause()
st := &Statement{}
f(st, Symbol("FOO"))
if st.WorkingConstructClause() == nil {
t.Errorf("semantic.InitConstructWorkingClause should have returned a valid working clause for statement %v", st)
}
}

func TestNextWorkingConstructClauseHook(t *testing.T) {
f := NextWorkingConstructClause()
st := &Statement{}
st.ResetWorkingConstructClause()
wcs := st.WorkingConstructClause()
wcs.SBinding = "?a"
f(st, Symbol("FOO"))
wcs = st.WorkingConstructClause()
wcs.SBinding = "?b"
f(st, Symbol("FOO"))
if got, want := len(st.ConstructClauses()), 2; got != want {
t.Errorf("semantic.NextConstructWorkingClause should have returned two clauses for statement %v; got %d, want %d", st, got, want)
}
}
11 changes: 10 additions & 1 deletion bql/semantic/semantic.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,20 @@ func (s *Statement) GlobalLookupOptions() *storage.LookupOptions {
return &lo
}

// ConstructClauses returns the list of construct clauses in the statement.
func (s *Statement) ConstructClauses() []*ConstructClause {
return s.constructClauses
}

// ResetWorkingConstructClause resets the current working construct clause.
func (s *Statement) ResetWorkingConstructClause() {
s.workingConstructClause = &ConstructClause{}
s.workingConstructClause = &ConstructClause{}
}

// WorkingConstructClause returns the current working clause.
func (s *Statement) WorkingConstructClause() *ConstructClause {
return s.workingConstructClause
}

// AddWorkingConstructClause adds the current working construct clause to the set
// of construct clauses that form the construct statement.
Expand Down