go.temporal.io/server@v1.23.0/common/persistence/visibility/store/standard/converter.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2021 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package standard
    26  
    27  import (
    28  	"errors"
    29  	"time"
    30  
    31  	"github.com/temporalio/sqlparser"
    32  	enumspb "go.temporal.io/api/enums/v1"
    33  
    34  	"go.temporal.io/server/common/namespace"
    35  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    36  	"go.temporal.io/server/common/persistence/visibility/store/query"
    37  	"go.temporal.io/server/common/searchattribute"
    38  )
    39  
    40  var allowedComparisonOperators = map[string]struct{}{
    41  	sqlparser.EqualStr: {},
    42  }
    43  
    44  type (
    45  	converter struct {
    46  		*query.Converter
    47  		fvInterceptor *valuesInterceptor
    48  	}
    49  )
    50  
    51  func newQueryConverter(
    52  	namespaceName namespace.Name,
    53  	searchAttributesTypeMap searchattribute.NameTypeMap,
    54  	searchAttributesMapperProvider searchattribute.MapperProvider,
    55  ) *converter {
    56  	fnInterceptor := newNameInterceptor()
    57  	fvInterceptor := newValuesInterceptor(
    58  		namespaceName,
    59  		searchAttributesTypeMap,
    60  		searchAttributesMapperProvider,
    61  	)
    62  
    63  	rangeCond := query.NewRangeCondConverter(fnInterceptor, fvInterceptor, false)
    64  	comparisonExpr := query.NewComparisonExprConverter(fnInterceptor, fvInterceptor, allowedComparisonOperators)
    65  
    66  	whereConverter := &query.WhereConverter{
    67  		Or:             query.NewNotSupportedExprConverter(),
    68  		RangeCond:      rangeCond,
    69  		ComparisonExpr: comparisonExpr,
    70  		Is:             query.NewNotSupportedExprConverter(),
    71  	}
    72  	whereConverter.And = query.NewAndConverter(whereConverter)
    73  
    74  	return &converter{
    75  		Converter:     query.NewConverter(fnInterceptor, whereConverter),
    76  		fvInterceptor: fvInterceptor,
    77  	}
    78  }
    79  
    80  func (c *converter) GetFilter(whereOrderBy string) (*sqlplugin.VisibilitySelectFilter, error) {
    81  	_, err := c.ConvertWhereOrderBy(whereOrderBy)
    82  	if err != nil {
    83  		// Convert ConverterError to InvalidArgument and pass through all other errors.
    84  		var converterErr *query.ConverterError
    85  		if errors.As(err, &converterErr) {
    86  			return nil, converterErr.ToInvalidArgument()
    87  		}
    88  		return nil, err
    89  	}
    90  
    91  	filter := c.fvInterceptor.filter
    92  	numPredicates := 0
    93  
    94  	if filter.WorkflowID != nil {
    95  		numPredicates++
    96  	}
    97  
    98  	if filter.WorkflowTypeName != nil {
    99  		numPredicates++
   100  	}
   101  
   102  	if filter.Status != int32(enumspb.WORKFLOW_EXECUTION_STATUS_UNSPECIFIED) {
   103  		numPredicates++
   104  	}
   105  
   106  	if numPredicates > 1 {
   107  		return nil, query.NewConverterError("too many filter conditions specified")
   108  	}
   109  
   110  	if filter.MinTime == nil {
   111  		minTime := time.Unix(0, 0)
   112  		filter.MinTime = &minTime
   113  	}
   114  
   115  	if filter.MaxTime == nil {
   116  		maxTime := time.Now().Add(24 * time.Hour)
   117  		filter.MaxTime = &maxTime
   118  	}
   119  
   120  	return filter, nil
   121  }