diff --git a/query/expression.go b/query/expression.go index 2abaf9b..011ac96 100644 --- a/query/expression.go +++ b/query/expression.go @@ -1,6 +1,10 @@ package query -import "fmt" +import ( + "fmt" + + recordmanager "github.com/JunNishimura/GoSQL/record_manager" +) // Expression is one side of a term of a predicate: either the name of a field, // which stands for whatever the record being looked at holds in it, or a @@ -15,6 +19,13 @@ type Expression interface { // Evaluate returns the value the expression comes to for the record the // scan is on. Evaluate(s Scan) (Constant, error) + // AppliesTo reports whether the expression can be evaluated against a + // record of schema. + // + // This is what a planner sorts the terms of a predicate by: a term both of + // whose sides apply to one table's schema can be tested as that table is + // read, before it is joined to anything. + AppliesTo(schema *recordmanager.Schema) bool fmt.Stringer } @@ -56,6 +67,11 @@ func (e FieldExpression) Evaluate(s Scan) (Constant, error) { return s.GetValue(e.fieldName) } +// AppliesTo reports whether schema has the field the expression stands for. +func (e FieldExpression) AppliesTo(schema *recordmanager.Schema) bool { + return schema.HasField(e.fieldName) +} + // FieldName is the name of the field the expression stands for. func (e FieldExpression) FieldName() string { return e.fieldName @@ -90,6 +106,13 @@ func (e ConstantExpression) Evaluate(_ Scan) (Constant, error) { return e.val, nil } +// AppliesTo reports that the constant can be evaluated against a record of +// any schema, an empty one included, since evaluating it reads nothing from +// the record. +func (e ConstantExpression) AppliesTo(_ *recordmanager.Schema) bool { + return true +} + // Value is the constant the expression stands for. func (e ConstantExpression) Value() Constant { return e.val diff --git a/query/expression_test.go b/query/expression_test.go index 93a2cde..d03ee3b 100644 --- a/query/expression_test.go +++ b/query/expression_test.go @@ -203,3 +203,44 @@ func TestConstantExpressionValue(t *testing.T) { } }) } + +func TestFieldExpressionAppliesTo(t *testing.T) { + tests := []struct { + name string + fieldName string + want bool + }{ + { + name: "given a schema with an id field, when an expression naming id is checked against it, then it applies", + fieldName: "id", + want: true, + }, + { + name: "given a schema with an id field, when an expression naming a field it does not have is checked against it, then it does not apply", + fieldName: "missing", + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + schema := newTestLayout(t).Schema() + + if got := NewFieldExpression(tt.fieldName).AppliesTo(schema); got != tt.want { + t.Errorf("AppliesTo() = %t, want %t", got, tt.want) + } + }) + } +} + +// A literal reads nothing from a record, so there is no schema it cannot be +// evaluated against, an empty one included. +func TestConstantExpressionAppliesTo(t *testing.T) { + t.Run("given a schema with no fields, when a constant expression is checked against it, then it applies", func(t *testing.T) { + schema := recordmanager.NewSchema() + + if got := NewConstantExpression(NewIntConstant(7)).AppliesTo(schema); !got { + t.Errorf("AppliesTo() = %t, want true", got) + } + }) +} diff --git a/query/predicate.go b/query/predicate.go index 95dc2a8..a591e89 100644 --- a/query/predicate.go +++ b/query/predicate.go @@ -3,6 +3,8 @@ package query import ( "slices" "strings" + + recordmanager "github.com/JunNishimura/GoSQL/record_manager" ) // Predicate is the condition a query keeps records by: terms that all have to @@ -74,6 +76,67 @@ func (p Predicate) Terms() []Term { return slices.Clone(p.terms) } +// SelectSubPred returns the predicate of the terms that can be evaluated +// against a record of schema alone, in the order they were given, and whether +// there are any. +// +// These are the terms a planner can test as a table is read, before it is +// joined to anything, so that the join is given fewer records. That is safe +// because the terms are joined by "and": a record one of them rules out is +// ruled out of the whole predicate, wherever in the plan it is tested. +// +// Having no such terms is reported apart from a predicate of no terms. That +// one holds everywhere, and a planner given it would put a select over the +// table that keeps every record, rather than putting none. +func (p Predicate) SelectSubPred(schema *recordmanager.Schema) (Predicate, bool) { + return p.filter(func(term Term) bool { + return term.AppliesTo(schema) + }) +} + +// JoinSubPred returns the predicate of the terms that can be evaluated against +// a record of the two schemas joined but not against a record of either +// alone, in the order they were given, and whether there are any. +// +// These are the terms a planner tests where it joins a table of one schema to +// a plan of the other: they could not be tested any earlier, and testing them +// there keeps the join from handing on pairs of records they rule out. A term +// either schema answers alone is left out, since SelectSubPred has already +// put it below the join. +// +// The two schemas are not put together into one to check a term against. +// They can share a field name, which a schema refuses, so each side of the +// term is checked against one schema and then the other instead. +func (p Predicate) JoinSubPred(schema1, schema2 *recordmanager.Schema) (Predicate, bool) { + appliesToEither := func(e Expression) bool { + return e.AppliesTo(schema1) || e.AppliesTo(schema2) + } + + return p.filter(func(term Term) bool { + if term.AppliesTo(schema1) || term.AppliesTo(schema2) { + return false + } + + return appliesToEither(term.lhs) && appliesToEither(term.rhs) + }) +} + +// filter returns the predicate of the terms keep holds for, in the order they +// were given, and whether there are any. +func (p Predicate) filter(keep func(Term) bool) (Predicate, bool) { + var terms []Term + for _, term := range p.terms { + if keep(term) { + terms = append(terms, term) + } + } + if len(terms) == 0 { + return Predicate{}, false + } + + return Predicate{terms: terms}, true +} + // String writes the terms in the order they were given, with "and" between // them. // diff --git a/query/predicate_test.go b/query/predicate_test.go index 45ad8ff..7178ee3 100644 --- a/query/predicate_test.go +++ b/query/predicate_test.go @@ -268,3 +268,141 @@ func TestPredicateTerms(t *testing.T) { } }) } + +func TestPredicateSelectSubPred(t *testing.T) { + tests := []struct { + name string + pred Predicate + wantTerms []Term + wantOK bool + }{ + { + name: "given a schema with id and name fields, when the sub-predicate of a predicate whose terms all name its fields is taken, then it is every term, in order", + pred: NewPredicate(termThatHolds, termThatAlsoHolds), + wantTerms: []Term{termThatHolds, termThatAlsoHolds}, + wantOK: true, + }, + // The term the schema cannot answer sits between the two it can, so + // leaving it out has to keep the order of those on either side. + { + name: "given a schema with id and name fields, when the sub-predicate of a predicate with a term naming a field it does not have is taken, then that term is left out and the rest keep their order", + pred: NewPredicate(termThatHolds, termOnAMissingField, termThatAlsoHolds), + wantTerms: []Term{termThatHolds, termThatAlsoHolds}, + wantOK: true, + }, + // No term to test is told apart from a predicate of no terms, which + // holds everywhere: a planner reads it as nothing to select on, not as + // a select that keeps every record. + { + name: "given a schema with id and name fields, when the sub-predicate of a predicate whose only term names a field it does not have is taken, then there is none", + pred: NewPredicate(termOnAMissingField), + wantOK: false, + }, + { + name: "given a schema with id and name fields, when the sub-predicate of a predicate of no terms is taken, then there is none", + pred: NewPredicate(), + wantOK: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + schema := newTestLayout(t).Schema() + + got, ok := tt.pred.SelectSubPred(schema) + if ok != tt.wantOK { + t.Fatalf("SelectSubPred() ok = %t, want %t", ok, tt.wantOK) + } + if !ok { + return + } + if !slices.Equal(got.Terms(), tt.wantTerms) { + t.Errorf("SelectSubPred() = %s, want %s", got, NewPredicate(tt.wantTerms...)) + } + }) + } +} + +// newTestDeptSchema builds the schema a table is joined to the one of +// newTestLayout on: "dept_id" is equated with "id" by a join term, and +// "dept_name" is there so the schema holds more than the field it is joined on. +func newTestDeptSchema(t *testing.T) *recordmanager.Schema { + t.Helper() + + s := recordmanager.NewSchema() + if err := s.AddIntField("dept_id"); err != nil { + t.Fatalf("AddIntField(%q) error = %v", "dept_id", err) + } + if err := s.AddStringField("dept_name", testStringFieldLength); err != nil { + t.Fatalf("AddStringField(%q) error = %v", "dept_name", err) + } + + return s +} + +func TestPredicateJoinSubPred(t *testing.T) { + var ( + joinTerm = NewTerm(NewFieldExpression("id"), NewFieldExpression("dept_id")) + reversedJoinTerm = NewTerm(NewFieldExpression("dept_name"), NewFieldExpression("name")) + deptTerm = NewTerm(NewFieldExpression("dept_id"), NewConstantExpression(NewIntConstant(1))) + termOnAThirdTable = NewTerm(NewFieldExpression("id"), NewFieldExpression("missing")) + ) + + tests := []struct { + name string + pred Predicate + wantTerms []Term + wantOK bool + }{ + { + name: "given a schema with id and name fields and one with dept_id and dept_name, when the sub-predicate of a predicate equating id with dept_id is taken, then it is that term", + pred: NewPredicate(joinTerm), + wantTerms: []Term{joinTerm}, + wantOK: true, + }, + // Either schema alone answers the terms mixed in here, so they are + // tested as each table is read and are no business of the join. + { + name: "given a schema with id and name fields and one with dept_id and dept_name, when the sub-predicate of a predicate mixing join terms with terms on one schema is taken, then it is the join terms in order, with a field of the second schema on either side", + pred: NewPredicate(termThatHolds, joinTerm, deptTerm, reversedJoinTerm), + wantTerms: []Term{joinTerm, reversedJoinTerm}, + wantOK: true, + }, + { + name: "given a schema with id and name fields and one with dept_id and dept_name, when the sub-predicate of a predicate whose terms each name fields of one schema is taken, then there is none", + pred: NewPredicate(termThatHolds, deptTerm), + wantOK: false, + }, + // Neither schema answers this term alone, but the two joined do not + // either, since one of its fields is in neither. It belongs to a join + // with some third table, not this one. + { + name: "given a schema with id and name fields and one with dept_id and dept_name, when the sub-predicate of a predicate equating id with a field neither has is taken, then there is none", + pred: NewPredicate(termOnAThirdTable), + wantOK: false, + }, + { + name: "given a schema with id and name fields and one with dept_id and dept_name, when the sub-predicate of a predicate of no terms is taken, then there is none", + pred: NewPredicate(), + wantOK: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + schema1 := newTestLayout(t).Schema() + schema2 := newTestDeptSchema(t) + + got, ok := tt.pred.JoinSubPred(schema1, schema2) + if ok != tt.wantOK { + t.Fatalf("JoinSubPred() ok = %t, want %t", ok, tt.wantOK) + } + if !ok { + return + } + if !slices.Equal(got.Terms(), tt.wantTerms) { + t.Errorf("JoinSubPred() = %s, want %s", got, NewPredicate(tt.wantTerms...)) + } + }) + } +} diff --git a/query/term.go b/query/term.go index 434ea94..75c5f81 100644 --- a/query/term.go +++ b/query/term.go @@ -1,6 +1,10 @@ package query -import "fmt" +import ( + "fmt" + + recordmanager "github.com/JunNishimura/GoSQL/record_manager" +) // Term is one comparison of a predicate: two expressions that a record has to // bring to the same value. @@ -61,6 +65,16 @@ func (t Term) IsSatisfied(s Scan) (bool, error) { return lhsVal == rhsVal, nil } +// AppliesTo reports whether both sides of the term can be evaluated against a +// record of schema. +// +// One side is not enough. A term of a field of this table and a field of +// another cannot be tested until the two are joined, however much of it this +// table holds. +func (t Term) AppliesTo(schema *recordmanager.Schema) bool { + return t.lhs.AppliesTo(schema) && t.rhs.AppliesTo(schema) +} + // String writes the two sides with an equals sign between them. // // The sign is written in rather than carried, because equality is the only diff --git a/query/term_test.go b/query/term_test.go index 579f49c..dc631de 100644 --- a/query/term_test.go +++ b/query/term_test.go @@ -163,3 +163,55 @@ func TestTermRHS(t *testing.T) { } }) } + +func TestTermAppliesTo(t *testing.T) { + tests := []struct { + name string + lhs Expression + rhs Expression + want bool + }{ + { + name: "given a schema with id and name fields, when a term of id and a constant is checked against it, then it applies", + lhs: NewFieldExpression("id"), + rhs: NewConstantExpression(NewIntConstant(testRecordID)), + want: true, + }, + { + name: "given a schema with id and name fields, when a term of id and name is checked against it, then it applies", + lhs: NewFieldExpression("id"), + rhs: NewFieldExpression("name"), + want: true, + }, + { + name: "given a schema with id and name fields, when a term of two constants is checked against it, then it applies", + lhs: NewConstantExpression(NewIntConstant(1)), + rhs: NewConstantExpression(NewIntConstant(1)), + want: true, + }, + // The two cases below each leave one side out of the schema, so a + // term that checked only one of its sides would get one of them wrong. + { + name: "given a schema with id and name fields, when a term of id and a field it does not have is checked against it, then it does not apply", + lhs: NewFieldExpression("id"), + rhs: NewFieldExpression("missing"), + want: false, + }, + { + name: "given a schema with id and name fields, when a term of a field it does not have and id is checked against it, then it does not apply", + lhs: NewFieldExpression("missing"), + rhs: NewFieldExpression("id"), + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + schema := newTestLayout(t).Schema() + + if got := NewTerm(tt.lhs, tt.rhs).AppliesTo(schema); got != tt.want { + t.Errorf("AppliesTo() = %t, want %t", got, tt.want) + } + }) + } +}