hanahmily commented on a change in pull request #10: URL: https://github.com/apache/skywalking-banyandb/pull/10#discussion_r655868827
########## File path: pkg/logical/analyzer.go ########## @@ -0,0 +1,171 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package logical + +import ( + "context" + + flatbuffers "github.com/google/flatbuffers/go" + "github.com/pkg/errors" + + "github.com/apache/skywalking-banyandb/api/common" + apiv1 "github.com/apache/skywalking-banyandb/api/fbs/v1" + seriesSchema "github.com/apache/skywalking-banyandb/banyand/series/schema" + "github.com/apache/skywalking-banyandb/banyand/series/schema/sw" +) + +var ( + FieldNotDefinedErr = errors.New("field is not defined") + PairQueryInspectionErr = errors.New("pairQuery cannot be inspected") + InvalidConditionTypeErr = errors.New("invalid pair type") + TypePairInspectionErr = errors.New("pair cannot be inspected") + IncompatibleQueryConditionErr = errors.New("incompatible query condition type") + InvalidSchemaErr = errors.New("invalid schema") +) + +var ( + DefaultLimit uint32 = 20 +) + +type analyzer struct { + traceSeries seriesSchema.TraceSeries +} + +func DefaultAnalyzer() *analyzer { + return &analyzer{ + sw.NewTraceSeries(), + } +} + +func (a *analyzer) BuildTraceSchema(ctx context.Context, metadata common.Metadata) (Schema, error) { + traceSeries, err := a.traceSeries.Get(ctx, metadata) + + if err != nil { + return nil, err + } + + fs := &schema{ + fieldMap: make(map[string]*fieldSpec), + } + + // generate the schema of the fields for the traceSeries + for i := 0; i < traceSeries.Spec.FieldsLength(); i++ { + var fieldSpec apiv1.FieldSpec + if ok := traceSeries.Spec.Fields(&fieldSpec, i); ok { + fs.RegisterField(string(fieldSpec.Name()), i, &fieldSpec) + } else { + return nil, err + } + } + + return fs, nil +} + +func (a *analyzer) Analyze(ctx context.Context, criteria *apiv1.EntityCriteria, traceMetadata *common.Metadata, s Schema) (Plan, error) { + // parse scan + timeRange := criteria.TimestampNanoseconds(nil) + plan := Scan(timeRange.Begin(), timeRange.End(), traceMetadata) + + // parse selection + if criteria.FieldsLength() > 0 { + var fieldExprs []Expr + for i := 0; i < criteria.FieldsLength(); i++ { + var pairQuery apiv1.PairQuery + if criteria.Fields(&pairQuery, i) { + op := pairQuery.Op() + pair := pairQuery.Condition(nil) + unionPairTable := new(flatbuffers.Table) + if pair.Pair(unionPairTable) { + if pair.PairType() == apiv1.TypedPairStrPair { + unionStrPair := new(apiv1.StrPair) + unionStrPair.Init(unionPairTable.Bytes, unionPairTable.Pos) + lit := parseStrLiteral(unionStrPair) + fieldExprs = append(fieldExprs, binaryOpFactory[op](NewFieldRef(string(unionStrPair.Key())), lit)) + } else if pair.PairType() == apiv1.TypedPairIntPair { + unionIntPair := new(apiv1.IntPair) + unionIntPair.Init(unionPairTable.Bytes, unionPairTable.Pos) + lit := parseIntLiteral(unionIntPair) + fieldExprs = append(fieldExprs, binaryOpFactory[op](NewFieldRef(string(unionIntPair.Key())), lit)) + } else { + return nil, InvalidConditionTypeErr + } + } else { + return nil, TypePairInspectionErr + } + } else { + return nil, PairQueryInspectionErr + } + } Review comment: Would you pls refer to https://golang.org/doc/effective_go#if to remove redundant `else` statements? ########## File path: pkg/logical/analyzer_test.go ########## @@ -0,0 +1,170 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package logical_test + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/apache/skywalking-banyandb/api/common" + apiv1 "github.com/apache/skywalking-banyandb/api/fbs/v1" + apischema "github.com/apache/skywalking-banyandb/api/schema" + "github.com/apache/skywalking-banyandb/pkg/logical" +) + +func TestAnalyzer_SimpleTimeScan(t *testing.T) { + assert := require.New(t) + + ana := logical.DefaultAnalyzer() + + builder := NewCriteriaBuilder() + criteria := builder.Build( + AddLimit(0), + AddOffset(0), + builder.BuildMetaData("default", "trace"), + builder.BuildTimeStampNanoSeconds(time.Now().Add(-3*time.Hour), time.Now()), + ) + + metadata := &common.Metadata{ + KindVersion: apischema.SeriesKindVersion, + Spec: *criteria.Metadata(nil), + } + + schema, err := ana.BuildTraceSchema(context.TODO(), *metadata) + assert.NoError(err) + + plan, err := ana.Analyze(context.TODO(), criteria, metadata, schema) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) Review comment: Could you add an embedded literal to represent the expected plan? It will help me review the plan execution result ########## File path: pkg/logical/analyzer_test.go ########## @@ -0,0 +1,170 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package logical_test + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/apache/skywalking-banyandb/api/common" + apiv1 "github.com/apache/skywalking-banyandb/api/fbs/v1" + apischema "github.com/apache/skywalking-banyandb/api/schema" + "github.com/apache/skywalking-banyandb/pkg/logical" +) + +func TestAnalyzer_SimpleTimeScan(t *testing.T) { + assert := require.New(t) + + ana := logical.DefaultAnalyzer() + + builder := NewCriteriaBuilder() + criteria := builder.Build( + AddLimit(0), + AddOffset(0), + builder.BuildMetaData("default", "trace"), + builder.BuildTimeStampNanoSeconds(time.Now().Add(-3*time.Hour), time.Now()), + ) + + metadata := &common.Metadata{ + KindVersion: apischema.SeriesKindVersion, + Spec: *criteria.Metadata(nil), + } + + schema, err := ana.BuildTraceSchema(context.TODO(), *metadata) + assert.NoError(err) + + plan, err := ana.Analyze(context.TODO(), criteria, metadata, schema) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) +} + +func TestAnalyzer_ComplexQuery(t *testing.T) { + assert := require.New(t) + + ana := logical.DefaultAnalyzer() + + builder := NewCriteriaBuilder() + criteria := builder.Build( + AddLimit(5), + AddOffset(10), + builder.BuildMetaData("default", "trace"), + builder.BuildTimeStampNanoSeconds(time.Now().Add(-3*time.Hour), time.Now()), + builder.BuildFields("service_name", "=", "my_app", "http.method", "=", "GET"), + builder.BuildOrderBy("service_instance_id", apiv1.SortDESC), + builder.BuildProjection("trace_id", "service_id"), + ) + + metadata := &common.Metadata{ + KindVersion: apischema.SeriesKindVersion, + Spec: *criteria.Metadata(nil), + } + + schema, err := ana.BuildTraceSchema(context.TODO(), *metadata) + assert.NoError(err) + + plan, err := ana.Analyze(context.TODO(), criteria, metadata, schema) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) Review comment: Same as above, using an expected string instead of printing it to stdout. ########## File path: pkg/logical/optimizer_test.go ########## @@ -0,0 +1,81 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package logical_test + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/require" + + "github.com/apache/skywalking-banyandb/api/common" + apiv1 "github.com/apache/skywalking-banyandb/api/fbs/v1" + apischema "github.com/apache/skywalking-banyandb/api/schema" + "github.com/apache/skywalking-banyandb/pkg/logical" +) + +func TestOptimizer_ProjectionPushDown(t *testing.T) { + assert := require.New(t) + + ana := logical.DefaultAnalyzer() + + builder := NewCriteriaBuilder() + sT, eT := time.Now().Add(-3*time.Hour), time.Now() + criteria := builder.Build( + AddLimit(5), + AddOffset(10), + builder.BuildMetaData("default", "trace"), + builder.BuildTimeStampNanoSeconds(sT, eT), + builder.BuildFields("service_name", "=", "my_app", "http.method", "=", "GET"), + builder.BuildOrderBy("service_instance_id", apiv1.SortDESC), + builder.BuildProjection("trace_id", "service_id"), + ) + + traceMetadata := &common.Metadata{ + KindVersion: apischema.SeriesKindVersion, + Spec: *criteria.Metadata(nil), + } + + schema, err := ana.BuildTraceSchema(context.TODO(), *traceMetadata) + assert.NoError(err) + + plan, err := ana.Analyze(context.TODO(), criteria, traceMetadata, schema) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) + + plan, err = logical.NewProjectionPushDown().Apply(plan) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) + + correctPlan, err := logical.Projection(logical.Limit( + logical.Offset(logical.OrderBy( + logical.Selection( + logical.Scan(uint64(sT.UnixNano()), uint64(eT.UnixNano()), traceMetadata, "service_id", "trace_id", "service_instance_id", "http.method", "service_name"), Review comment: Why pushing fields into the projection list since the input doesn't indicate they will be returned. ########## File path: pkg/logical/analyzer_test.go ########## @@ -0,0 +1,170 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package logical_test + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/apache/skywalking-banyandb/api/common" + apiv1 "github.com/apache/skywalking-banyandb/api/fbs/v1" + apischema "github.com/apache/skywalking-banyandb/api/schema" + "github.com/apache/skywalking-banyandb/pkg/logical" +) + +func TestAnalyzer_SimpleTimeScan(t *testing.T) { + assert := require.New(t) + + ana := logical.DefaultAnalyzer() + + builder := NewCriteriaBuilder() + criteria := builder.Build( + AddLimit(0), + AddOffset(0), + builder.BuildMetaData("default", "trace"), + builder.BuildTimeStampNanoSeconds(time.Now().Add(-3*time.Hour), time.Now()), + ) + + metadata := &common.Metadata{ + KindVersion: apischema.SeriesKindVersion, + Spec: *criteria.Metadata(nil), + } + + schema, err := ana.BuildTraceSchema(context.TODO(), *metadata) + assert.NoError(err) + + plan, err := ana.Analyze(context.TODO(), criteria, metadata, schema) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) +} + +func TestAnalyzer_ComplexQuery(t *testing.T) { + assert := require.New(t) + + ana := logical.DefaultAnalyzer() + + builder := NewCriteriaBuilder() + criteria := builder.Build( + AddLimit(5), + AddOffset(10), + builder.BuildMetaData("default", "trace"), + builder.BuildTimeStampNanoSeconds(time.Now().Add(-3*time.Hour), time.Now()), + builder.BuildFields("service_name", "=", "my_app", "http.method", "=", "GET"), + builder.BuildOrderBy("service_instance_id", apiv1.SortDESC), + builder.BuildProjection("trace_id", "service_id"), + ) + + metadata := &common.Metadata{ + KindVersion: apischema.SeriesKindVersion, + Spec: *criteria.Metadata(nil), + } + + schema, err := ana.BuildTraceSchema(context.TODO(), *metadata) + assert.NoError(err) + + plan, err := ana.Analyze(context.TODO(), criteria, metadata, schema) + assert.NoError(err) + assert.NotNil(plan) + fmt.Print(logical.Format(plan)) +} + +func TestAnalyzer_Fields_FieldNotDefined(t *testing.T) { Review comment: Is not the series undefined? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
