Example #1
0
func TestSumAccumulators(t *testing.T) {
	// int64 sum accumulator.
	var (
		iv interface{}
		ia = NewSumInt64LiteralAccumulator(0)
	)
	for i := int64(0); i < 5; i++ {
		l, _ := literal.DefaultBuilder().Build(literal.Int64, i)
		iv, _ = ia.Accumulate(l)
	}
	if got, want := iv.(int64), int64(10); got != want {
		t.Errorf("Int64 sum accumulator failed; got %d, want %d", got, want)
	}
	// float64 sum accumulator.
	var (
		fv interface{}
		fa = NewSumFloat64LiteralAccumulator(0)
	)
	for i := float64(0); i < 5; i += 1.0 {
		l, _ := literal.DefaultBuilder().Build(literal.Float64, i)
		fv, _ = fa.Accumulate(l)
	}
	if got, want := fv.(float64), float64(10); got != want {
		t.Errorf("Int64 sum accumulator failed; got %f, want %f", got, want)
	}
}
Example #2
0
// groupRangeReduce takes a sorted range and generates a new row containing
// the aggregated columns and the non aggregated ones.
func (t *Table) groupRangeReduce(i, j int, alias map[string]string, acc map[string]Accumulator) (Row, error) {
	if i > j {
		return nil, fmt.Errorf("cannot aggregate empty ranges [%d, %d)", i, j)
	}
	// Initialize the range and accumulator results.
	rng := t.data[i:j]
	vaccs := make(map[string]interface{})
	// Reset the accumulators.
	for _, a := range acc {
		a.Reset()
	}
	// Aggregate the range using the provided aggregators.
	for _, r := range rng {
		for b, a := range acc {
			av, err := a.Accumulate(r[b])
			if err != nil {
				return nil, err
			}
			vaccs[b] = av
		}
	}
	// Create a new row based on the resulting aggregations with the proper
	// binding aliasing and the non aggregated values.
	newRow := Row{}
	for b, v := range rng[0] {
		acc, ok := vaccs[b]
		if !ok {
			if a, ok := alias[b]; ok {
				newRow[a] = v
			} else {
				newRow[b] = v
			}
		} else {
			a, ok := alias[b]
			if !ok {
				return nil, fmt.Errorf("aggregated bindings require and alias; binding %s missing alias", b)
			}
			// Accumulators currently only can return numeric literals.
			switch acc.(type) {
			case int64:
				l, err := literal.DefaultBuilder().Build(literal.Int64, acc)
				if err != nil {
					return nil, err
				}
				newRow[a] = &Cell{L: l}
			case float64:
				l, err := literal.DefaultBuilder().Build(literal.Float64, acc)
				if err != nil {
					return nil, err
				}
				newRow[a] = &Cell{L: l}
			default:
				return nil, fmt.Errorf("aggregation of binding %s returned unknown value %v or type", b, acc)
			}
		}
	}
	return newRow, nil
}
Example #3
0
func TestCellString(t *testing.T) {
	now := time.Now()
	n := node.NewBlankNode()
	p, err := predicate.NewImmutable("foo")
	if err != nil {
		t.Fatalf("failed to create predicate with error %v", err)
	}
	l, err := literal.DefaultBuilder().Parse(`"true"^^type:bool`)
	if err != nil {
		t.Fatalf("failed to create literal with error %v", err)
	}
	testTable := []struct {
		c    *Cell
		want string
	}{
		{c: &Cell{S: "foo"}, want: `foo`},
		{c: &Cell{N: n}, want: n.String()},
		{c: &Cell{P: p}, want: p.String()},
		{c: &Cell{L: l}, want: l.String()},
		{c: &Cell{T: &now}, want: now.Format(time.RFC3339Nano)},
	}
	for _, entry := range testTable {
		if got := entry.c.String(); got != entry.want {
			t.Errorf("Cell.String failed to return the right string; got %q, want %q", got, entry.want)
		}
	}
}
Example #4
0
func TestCountAccumulators(t *testing.T) {
	// Count accumulator.
	var (
		cv interface{}
		ca = NewCountAccumulator()
	)
	for i := int64(0); i < 5; i++ {
		cv, _ = ca.Accumulate(i)
	}
	if got, want := cv.(int64), int64(5); got != want {
		t.Errorf("Count accumulator failed; got %d, want %d", got, want)
	}
	// Count distint accumulator
	var (
		dv interface{}
		da = NewCountDistinctAccumulator()
	)
	for i := int64(0); i < 10; i++ {
		l, _ := literal.DefaultBuilder().Build(literal.Int64, i%2)
		dv, _ = da.Accumulate(l)
	}
	if got, want := dv.(int64), int64(2); got != want {
		t.Errorf("Count distinct accumulator failed; got %d, want %d", got, want)
	}
}
Example #5
0
// limitCollection collects the limit of rows to return as indicated by the
// LIMIT clause.
func limitCollection() ElementHook {
	var f func(st *Statement, ce ConsumedElement) (ElementHook, error)
	f = func(st *Statement, ce ConsumedElement) (ElementHook, error) {
		if ce.IsSymbol() || ce.token.Type == lexer.ItemLimit {
			return f, nil
		}
		if ce.token.Type != lexer.ItemLiteral {
			return nil, fmt.Errorf("limit clause required an int64 literal; found %v instead", ce.token)
		}
		l, err := literal.DefaultBuilder().Parse(ce.token.Text)
		if err != nil {
			return nil, fmt.Errorf("failed to parse limit literal %q with error %v", ce.token.Text, err)
		}
		if l.Type() != literal.Int64 {
			return nil, fmt.Errorf("limit required an int64 value; found %s instead", l)
		}
		lv, err := l.Int64()
		if err != nil {
			return nil, fmt.Errorf("failed to retrieve the int64 value for literal %v with error %v", l, err)
		}
		st.limitSet, st.limit = true, lv
		return f, nil
	}
	return f
}
Example #6
0
func populateBenchmarkStore(b *testing.B) storage.Store {
	s, ctx := memory.NewStore(), context.Background()
	g, err := s.NewGraph(ctx, "?test")
	if err != nil {
		b.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", err)
	}
	buf := bytes.NewBufferString(testTriples)
	if _, err := io.ReadIntoGraph(ctx, g, buf, literal.DefaultBuilder()); err != nil {
		b.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	trpls := make(chan *triple.Triple)
	go func() {
		if err := g.Triples(ctx, trpls); err != nil {
			b.Fatal(err)
		}
	}()
	cnt := 0
	for _ = range trpls {
		cnt++
	}
	if got, want := cnt, len(strings.Split(testTriples, "\n"))-1; got != want {
		b.Fatalf("Failed to import all test triples; got %v, want %v", got, want)
	}
	return s
}
Example #7
0
func TestDataAccessTripleToRowObjectBindingsDroping(t *testing.T) {
	n, p, _ := testNodeTemporalPredicateLiteral(t)
	ts, err := p.TimeAnchor()
	if err != nil {
		t.Fatal(err)
	}
	testTable := []struct {
		t   string
		cls *semantic.GraphClause
		bc  *table.Cell
		ac  *table.Cell
		tc  *table.Cell
		ic  *table.Cell
		tsc *table.Cell
		atc *table.Cell
	}{
		{
			t: n.String() + "\t" + p.String() + "\t" + n.String(),
			cls: &semantic.GraphClause{
				OBinding:   "?o",
				OAlias:     "?alias",
				OTypeAlias: "?o",
				OIDAlias:   "?id",
			},
			bc: &table.Cell{N: n},
			ac: &table.Cell{N: n},
			tc: &table.Cell{S: table.CellString(n.Type().String())},
			ic: &table.Cell{S: table.CellString(n.ID().String())},
		},
		{
			t: n.String() + "\t" + p.String() + "\t" + p.String(),
			cls: &semantic.GraphClause{
				OBinding:       "?o",
				OAlias:         "?alias",
				OIDAlias:       "?id",
				OAnchorBinding: "?ts",
				OAnchorAlias:   "?o",
			},
			bc:  &table.Cell{P: p},
			ac:  &table.Cell{P: p},
			ic:  &table.Cell{S: table.CellString(string(p.ID()))},
			tsc: &table.Cell{T: ts},
			atc: &table.Cell{T: ts},
		},
	}
	for _, entry := range testTable {
		tpl, err := triple.Parse(entry.t, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %q with error %v", entry.t, err)
		}
		r, err := tripleToRow(tpl, entry.cls)
		if err != nil {
			t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, entry.cls, err)
		}
		if r != nil {
			t.Errorf("tripleToRow for triple %q and clasuse %v, failed to drop triple and returned %v", tpl, entry.cls, r)
		}
	}
}
Example #8
0
func TestDataAccessTripleToRowPredicateBindings(t *testing.T) {
	n, p, _ := testNodeTemporalPredicateLiteral(t)
	ts, err := p.TimeAnchor()
	if err != nil {
		t.Fatal(err)
	}
	testTable := []struct {
		t   string
		cls *semantic.GraphClause
		bc  *table.Cell
		ac  *table.Cell
		ic  *table.Cell
		tc  *table.Cell
		atc *table.Cell
	}{
		{
			t: n.String() + "\t" + p.String() + "\t" + n.String(),
			cls: &semantic.GraphClause{
				PBinding:       "?p",
				PAlias:         "?alias",
				PIDAlias:       "?id",
				PAnchorBinding: "?ts",
				PAnchorAlias:   "?tsa",
			},
			bc:  &table.Cell{P: p},
			ac:  &table.Cell{P: p},
			ic:  &table.Cell{S: table.CellString(string(p.ID()))},
			tc:  &table.Cell{T: ts},
			atc: &table.Cell{T: ts},
		},
	}
	for _, entry := range testTable {
		tpl, err := triple.Parse(entry.t, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %q with error %v", entry.t, err)
		}
		r, err := tripleToRow(tpl, entry.cls)
		if err != nil {
			t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, entry.cls, err)
		}
		if got, want := r["?p"], entry.bc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?p\"; got %q, want %q", got, want)
		}
		if got, want := r["?alias"], entry.ac; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding alias \"?alias\"; got %q, want %q", got, want)
		}
		if got, want := r["?id"], entry.ic; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?id\"; got %q, want %q", got, want)
		}
		if got, want := r["?ts"], entry.tc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?ts\"; got %q, want %q", got, want)
		}
		if got, want := r["?tsa"], entry.tc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?tsa\"; got %q, want %q", got, want)
		}
	}
}
Example #9
0
func TestSerializationContents(t *testing.T) {
	var buffer bytes.Buffer
	ts, ctx := getTestTriples(t), context.Background()

	g, err := memory.NewStore().NewGraph(ctx, "test")
	if err != nil {
		t.Fatalf("memory.NewStore().NewGraph should have never failed to create a graph")
	}
	if err := g.AddTriples(ctx, ts); err != nil {
		t.Errorf("storage.AddTriples should have not fail to add triples %v with error %v", ts, err)
	}
	// Serialize to a buffer.
	cnt, err := WriteGraph(ctx, &buffer, g)
	if err != nil {
		t.Errorf("io.WriteGraph failed to read %s with error %v", buffer.String(), err)
	}
	if cnt != 6 {
		t.Errorf("io.WriteGraph should have been able to write 6 triples not %d", cnt)
	}
	// Deserialize from a buffer.
	g2, err := memory.DefaultStore.NewGraph(ctx, "test2")
	if err != nil {
		t.Fatalf("memory.DefaultStore.NewGraph should have never failed to create a graph")
	}
	cnt2, err := ReadIntoGraph(ctx, g2, &buffer, literal.DefaultBuilder())
	if err != nil {
		t.Errorf("io.readIntoGraph failed to read %s with error %v", buffer.String(), err)
	}
	if cnt2 != 6 {
		t.Errorf("io.readIntoGraph should have been able to read 6 triples not %d", cnt2)
	}
	// Check the graphs are equal
	m := make(map[string]bool)
	gs := 0
	gtpls, err := g.Triples(ctx)
	if err != nil {
		t.Errorf("g.Triples failed to retrieve triples with error %v", err)
	}
	for trpl := range gtpls {
		m[trpl.GUID()] = true
		gs++
	}
	gos := 0
	g2tpls, err := g2.Triples(ctx)
	if err != nil {
		t.Errorf("g2.Triples failed to retrieve triples with error %v", err)
	}
	for trpl := range g2tpls {
		if _, ok := m[trpl.GUID()]; !ok {
			t.Errorf("Failed to unmarshal marshaled triple; could not find triple %s", trpl.String())
		}
		gos++
	}
	if gs != gos || gs != 6 || gos != 6 {
		t.Errorf("Failed to unmarshal marshaled the right number of triples, %d != %d != 6", gs, gos)
	}
}
Example #10
0
func TestReify(t *testing.T) {
	tr, err := ParseTriple("/some/type<some id>\t\"foo\"@[]\t\"bar\"@[]", literal.DefaultBuilder())
	if err != nil {
		t.Fatalf("triple.ParseTriple failed to parse valid triple with error %v", err)
	}
	rts, bn := tr.Reify()
	if len(rts) != 4 || bn == nil {
		t.Errorf("triple.Reify failed to create 4 valid triples and a valid blank node; returned %v, %s instead", rts, bn)
	}
}
Example #11
0
// ToLiteral converts the node found by the lexer and converts it into a
// BadWolf literal.
func ToLiteral(ce ConsumedElement) (*literal.Literal, error) {
	if ce.IsSymbol() {
		return nil, fmt.Errorf("semantic.ToLiteral cannot convert symbol %v to a literal", ce)
	}
	tkn := ce.Token()
	if tkn.Type != lexer.ItemLiteral {
		return nil, fmt.Errorf("semantic.ToLiteral cannot convert token type %s to a literal", tkn.Type)
	}
	return literal.DefaultBuilder().Parse(tkn.Text)
}
Example #12
0
func TestParsetriple(t *testing.T) {
	ss := []string{
		"/some/type<some id>\t\"foo\"@[]\t/some/type<some id>",
		"/some/type<some id>\t\"foo\"@[]\t\"bar\"@[]",
	}
	for _, s := range ss {
		if _, err := ParseTriple(s, literal.DefaultBuilder()); err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %s with error %v", s, err)
		}
	}
}
Example #13
0
// InitializeCommands initializes the available commands with the given storage
// instance.
func InitializeCommands(driver storage.Store, chanSize, bulkTripleOpSize, builderSize int, rl repl.ReadLiner) []*command.Command {
	return []*command.Command{
		assert.New(driver, literal.DefaultBuilder(), chanSize),
		benchmark.New(driver, chanSize),
		export.New(driver, bulkTripleOpSize),
		load.New(driver, bulkTripleOpSize, builderSize),
		run.New(driver, chanSize),
		repl.New(driver, chanSize, bulkTripleOpSize, builderSize, rl),
		version.New(),
	}
}
Example #14
0
func TestStatementAddData(t *testing.T) {
	tr, err := triple.ParseTriple(`/_<foo> "foo"@[] /_<bar>`, literal.DefaultBuilder())
	if err != nil {
		t.Fatalf("triple.ParseTriple failed to parse valid triple with error %v", err)
	}
	st := &Statement{}
	st.BindType(Query)
	st.AddData(tr)
	if got, want := st.Data(), []*triple.Triple{tr}; !reflect.DeepEqual(got, want) {
		t.Errorf("semantic.AddData returned the wrong data avaiable; got %v, want %v", got, want)
	}
}
Example #15
0
func init() {
	dach = dataAccumulator(literal.DefaultBuilder())
	gach = graphAccumulator()
	wnch = whereNextWorkingClause()
	wich = whereInitWorkingClause()
	wsch = whereSubjectClause()
	wpch = wherePredicateClause()
	woch = whereObjectClause()

	predicateRegexp = regexp.MustCompile(`^"(.+)"@\["?([^\]"]*)"?\]$`)
	boundRegexp = regexp.MustCompile(`^"(.+)"@\["?([^\]"]*)"?,"?([^\]"]*)"?\]$`)
}
Example #16
0
func populateTestStore(t *testing.T) storage.Store {
	s := memory.NewStore()
	g, err := s.NewGraph("?test_graph")
	if err != nil {
		t.Fatalf("memory.NewGraph failed to create \"?test_graph\" with error %v", err)
	}
	b := bytes.NewBufferString(testTriples)
	if _, err := io.ReadIntoGraph(g, b, literal.DefaultBuilder()); err != nil {
		t.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	return s
}
Example #17
0
func createTriples(t *testing.T, ss []string) []*triple.Triple {
	ts := []*triple.Triple{}
	for _, s := range ss {
		trpl, err := triple.Parse(s, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %s with error %v", s, err)
			continue
		}
		ts = append(ts, trpl)
	}
	return ts
}
Example #18
0
// assertCommand runs all the BQL statements available in the file.
func assertCommand(cmd *Command, args []string) int {
	if len(args) < 3 {
		fmt.Fprintf(os.Stderr, "Missing required folder path. ")
		cmd.Usage()
		return 2
	}
	// Open the folder.
	folder := strings.TrimSpace(args[2])
	f, err := os.Open(folder)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open folder %s\n\n\t%v\n\n", folder, err)
		return 2
	}
	fis, err := f.Readdir(0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read folder %s\n\n\t%v\n\n", folder, err)
		return 2
	}
	fmt.Printf("Processing folder %q...\n\n", folder)
	for _, fi := range fis {
		if !strings.Contains(fi.Name(), "json") {
			continue
		}
		fmt.Println("-------------------------------------------------------------")
		fmt.Printf("Processing file %q...\n\n", fi.Name())
		lns, err := readLines(path.Join(folder, fi.Name()))
		if err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to read file content with error %v\n\n", err)
			return 2
		}
		rawStory := strings.Join(lns, "\n")
		s := &compliance.Story{}
		if err := s.Unmarshal(rawStory); err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to unmarshal story with error %v\n\n", err)
			return 2
		}
		m, err := s.Run(memory.NewStore(), literal.DefaultBuilder())
		if err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to run story %q with error %v\n\n", s.Name, err)
			return 2
		}
		for aid, aido := range m {
			if aido.Equal {
				fmt.Printf("%s [TRUE]\n", aid)
			} else {
				fmt.Printf("%s [FALSE]\n\nGot:\n\n%s\nWant:\n\n%s\n", aid, aido.Got, aido.Want)
			}
		}
	}
	fmt.Println("-------------------------------------------------------------")
	fmt.Println("\ndone")
	return 0
}
Example #19
0
func getTestTriples(t *testing.T, trpls []string) []*triple.Triple {
	var ts []*triple.Triple
	for _, s := range trpls {
		trpl, err := triple.Parse(s, literal.DefaultBuilder())
		if err != nil {
			t.Fatalf("triple.Parse failed to parse valid triple %s with error %v", s, err)
			continue
		}
		ts = append(ts, trpl)
	}
	return ts
}
Example #20
0
func TestDataAccessBasicBindings(t *testing.T) {
	n, p, l := testNodePredicateLiteral(t)
	cls := &semantic.GraphClause{
		SBinding: "?s",
		PBinding: "?p",
		OBinding: "?o",
	}
	testTable := []struct {
		t  string
		sc *table.Cell
		pc *table.Cell
		oc *table.Cell
	}{
		{
			t:  n.String() + "\t" + p.String() + "\t" + n.String(),
			sc: &table.Cell{N: n},
			pc: &table.Cell{P: p},
			oc: &table.Cell{N: n},
		},
		{
			t:  n.String() + "\t" + p.String() + "\t" + p.String(),
			sc: &table.Cell{N: n},
			pc: &table.Cell{P: p},
			oc: &table.Cell{P: p},
		},
		{
			t:  n.String() + "\t" + p.String() + "\t" + l.String(),
			sc: &table.Cell{N: n},
			pc: &table.Cell{P: p},
			oc: &table.Cell{L: l},
		},
	}
	for _, entry := range testTable {
		tpl, err := triple.Parse(entry.t, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %q with error %v", entry.t, err)
		}
		r, err := tripleToRow(tpl, cls)
		if err != nil {
			t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, cls, err)
		}
		if got, want := r["?s"], entry.sc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?s\"; got %q, want %q", got, want)
		}
		if got, want := r["?p"], entry.pc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?p\"; got %q, want %q", got, want)
		}
		if got, want := r["?o"], entry.oc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?o\"; got %q, want %q", got, want)
		}
	}
}
Example #21
0
func testNodePredicateLiteral(t *testing.T) (*node.Node, *predicate.Predicate, *literal.Literal) {
	n, err := node.Parse(`/foo<bar>`)
	if err != nil {
		t.Fatal(err)
	}
	p, err := predicate.Parse(`"foo"@[]`)
	if err != nil {
		t.Fatal(err)
	}
	l, err := literal.DefaultBuilder().Parse(`"true"^^type:bool`)
	if err != nil {
		t.Fatal(err)
	}
	return n, p, l
}
Example #22
0
func testNodeTemporalPredicateLiteral(t *testing.T) (*node.Node, *predicate.Predicate, *literal.Literal) {
	n, err := node.Parse(`/foo<bar>`)
	if err != nil {
		t.Fatal(err)
	}
	p, err := predicate.Parse(`"bar"@[1975-01-01T00:01:01.999999999Z]`)
	if err != nil {
		t.Fatal(err)
	}
	l, err := literal.DefaultBuilder().Parse(`"true"^^type:bool`)
	if err != nil {
		t.Fatal(err)
	}
	return n, p, l
}
Example #23
0
func TestTreeTraversalToRoot(t *testing.T) {
	// Graph traversal data.
	traversalTriples := `/person<Gavin Belson>  "born in"@[]    /city<Springfield>
		/person<Gavin Belson>  "parent of"@[]  /person<Peter Belson>
		/person<Gavin Belson>  "parent of"@[]  /person<Mary Belson>
		/person<Mary Belson>   "parent of"@[]  /person<Amy Schumer>
		/person<Mary Belson>   "parent of"@[]  /person<Joe Schumer>`

	traversalQuery := `SELECT ?grandparent
		                 FROM ?test
										 WHERE {
										   ?s "parent of"@[] /person<Amy Schumer> .
											 ?grandparent "parent of"@[] ?s
										 };`

	// Load traversing data
	s, ctx := memory.NewStore(), context.Background()
	g, gErr := s.NewGraph(ctx, "?test")
	if gErr != nil {
		t.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", gErr)
	}
	b := bytes.NewBufferString(traversalTriples)
	if _, err := io.ReadIntoGraph(ctx, g, b, literal.DefaultBuilder()); err != nil {
		t.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	p, pErr := grammar.NewParser(grammar.SemanticBQL())
	if pErr != nil {
		t.Fatalf("grammar.NewParser: should have produced a valid BQL parser with error %v", pErr)
	}
	st := &semantic.Statement{}
	if err := p.Parse(grammar.NewLLk(traversalQuery, 1), st); err != nil {
		t.Errorf("Parser.consume: failed to parse query %q with error %v", traversalQuery, err)
	}
	plnr, err := New(ctx, s, st, 0)
	if err != nil {
		t.Errorf("planner.New failed to create a valid query plan with error %v", err)
	}
	tbl, err := plnr.Execute(ctx)
	if err != nil {
		t.Errorf("planner.Excecute failed for query %q with error %v", traversalQuery, err)
	}
	if got, want := len(tbl.Bindings()), 1; got != want {
		t.Errorf("tbl.Bindings returned the wrong number of bindings for %q; got %d, want %d", traversalQuery, got, want)
	}
	if got, want := len(tbl.Rows()), 1; got != want {
		t.Errorf("planner.Excecute failed to return the expected number of rows for query %q; got %d want %d\nGot:\n%v\n", traversalQuery, got, want, tbl)
	}
}
Example #24
0
// inferCell builds a Cell out of the provided string.
func inferCell(s string) *table.Cell {
	if n, err := node.Parse(s); err == nil {
		return &table.Cell{N: n}
	}
	if p, err := predicate.Parse(s); err == nil {
		return &table.Cell{P: p}
	}
	if l, err := literal.DefaultBuilder().Parse(s); err == nil {
		return &table.Cell{L: l}
	}
	t, err := time.Parse(time.RFC3339Nano, s)
	if err == nil {
		return &table.Cell{T: &t}
	}
	return &table.Cell{S: table.CellString(s)}
}
Example #25
0
func TestDataAccessTripleToRowSubjectBindings(t *testing.T) {
	n, p, _ := testNodePredicateLiteral(t)
	testTable := []struct {
		t   string
		cls *semantic.GraphClause
		sc  *table.Cell
		ac  *table.Cell
		tc  *table.Cell
		ic  *table.Cell
	}{
		{
			t: n.String() + "\t" + p.String() + "\t" + n.String(),
			cls: &semantic.GraphClause{
				SBinding:   "?s",
				SAlias:     "?alias",
				STypeAlias: "?type",
				SIDAlias:   "?id",
			},
			sc: &table.Cell{N: n},
			ac: &table.Cell{N: n},
			tc: &table.Cell{S: table.CellString(n.Type().String())},
			ic: &table.Cell{S: table.CellString(n.ID().String())},
		},
	}
	for _, entry := range testTable {
		tpl, err := triple.Parse(entry.t, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %q with error %v", entry.t, err)
		}
		r, err := tripleToRow(tpl, entry.cls)
		if err != nil {
			t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, entry.cls, err)
		}
		if got, want := r["?s"], entry.sc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?s\"; got %q, want %q", got, want)
		}
		if got, want := r["?alias"], entry.ac; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding alias \"?alias\"; got %q, want %q", got, want)
		}
		if got, want := r["?type"], entry.tc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?type\"; got %q, want %q", got, want)
		}
		if got, want := r["?id"], entry.ic; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?ud\"; got %q, want %q", got, want)
		}
	}
}
Example #26
0
func TestReifyImmutable(t *testing.T) {
	tr, err := Parse("/some/type<some id>\t\"foo\"@[]\t\"bar\"@[]", literal.DefaultBuilder())
	if err != nil {
		t.Fatalf("triple.Parse failed to parse valid triple with error %v", err)
	}
	rts, bn, err := tr.Reify()
	if err != nil {
		t.Errorf("triple.Reify failed to reify %v with error %v", tr, err)
	}
	if len(rts) != 4 || bn == nil {
		t.Errorf("triple.Reify failed to create 4 valid triples and a valid blank node; returned %v, %s instead", rts, bn)
	}
	for _, trpl := range rts[1:] {
		ps := string(trpl.Predicate().ID())
		if ps != "_subject" && ps != "_predicate" && ps != "_object" {
			t.Errorf("Inalid reification predicate; found %q", ps)
		}
	}
}
Example #27
0
func getTestTriples(t *testing.T) []*triple.Triple {
	ts := []*triple.Triple{}
	ss := []string{
		"/u<john>\t\"knows\"@[]\t/u<mary>",
		"/u<john>\t\"knows\"@[]\t/u<peter>",
		"/u<john>\t\"knows\"@[]\t/u<alice>",
		"/u<mary>\t\"knows\"@[]\t/u<andrew>",
		"/u<mary>\t\"knows\"@[]\t/u<kim>",
		"/u<mary>\t\"knows\"@[]\t/u<alice>",
	}
	for _, s := range ss {
		trpl, err := triple.Parse(s, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %s with error %v", s, err)
			continue
		}
		ts = append(ts, trpl)
	}
	return ts
}
Example #28
0
func init() {
	dach = dataAccumulator(literal.DefaultBuilder())
	gach = graphAccumulator()
	wnch = whereNextWorkingClause()
	wich = whereInitWorkingClause()
	wsch = whereSubjectClause()
	wpch = wherePredicateClause()
	woch = whereObjectClause()
	vach = varAccumulator()
	bgch = bindingsGraphChecker()
	gbch = groupByBindings()
	gcch = groupByBindingsChecker()
	obch = orderByBindings()
	occh = orderByBindingsChecker()
	hech = havingExpression()
	hebl = havingExpressionBuilder()
	licl = limitCollection()
	gbcl = collectGlobalBounds()

	predicateRegexp = regexp.MustCompile(`^"(.+)"@\["?([^\]"]*)"?\]$`)
	boundRegexp = regexp.MustCompile(`^"(.+)"@\["?([^\]"]*)"?,"?([^\]"]*)"?\]$`)
}
Example #29
0
// cellToObject returns an object for the given cell.
func cellToObject(c *table.Cell) (*triple.Object, error) {
	if c == nil {
		return nil, errors.New("cannot create an object out of and empty cell")
	}
	if c.N != nil {
		return triple.NewNodeObject(c.N), nil
	}
	if c.P != nil {
		return triple.NewPredicateObject(c.P), nil
	}
	if c.L != nil {
		return triple.NewLiteralObject(c.L), nil
	}
	if c.S != "" {
		l, err := literal.DefaultBuilder().Parse(fmt.Sprintf(`"%s"^^type:string`, c.S))
		if err != nil {
			return nil, err
		}
		return triple.NewLiteralObject(l), nil
	}
	return nil, fmt.Errorf("invalid cell %v", c)
}
Example #30
0
func populateTestStore(t *testing.T) storage.Store {
	s := memory.NewStore()
	g, err := s.NewGraph("?test")
	if err != nil {
		t.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", err)
	}
	b := bytes.NewBufferString(testTriples)
	if _, err := io.ReadIntoGraph(g, b, literal.DefaultBuilder()); err != nil {
		t.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	tpls, err := g.Triples()
	if err != nil {
		t.Fatal(err)
	}
	cnt := 0
	for _ = range tpls {
		cnt++
	}
	if got, want := cnt, len(strings.Split(testTriples, "\n"))-2; got != want {
		t.Fatalf("Failed to import all test triples; got %v, want %v", got, want)
	}
	return s
}