github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/operators.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package tree 12 13 import ( 14 "fmt" 15 16 "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" 17 ) 18 19 // This file implements the generation of unique names for every 20 // operator overload. 21 // 22 // The historical first purpose of generating these names is to be used 23 // as telemetry keys, for feature usage reporting. 24 25 // Detailed counter name generation follows. 26 // 27 // We pre-allocate the counter objects upfront here and later use 28 // Inc(), to avoid the hash map lookup in telemetry.Count upon type 29 // checking every scalar operator node. 30 31 // The logic that follows is also associated with a related feature in 32 // PostgreSQL, which may be implemented by CockroachDB in the future: 33 // exposing all the operators as unambiguous, non-overloaded built-in 34 // functions. For example, in PostgreSQL, one can use `SELECT 35 // int8um(123)` to apply the int8-specific unary minus operator. 36 // This feature can be considered in the future for two reasons: 37 // 38 // 1. some pg applications may simply require the ability to use the 39 // pg native operator built-ins. If/when this compatibility is 40 // considered, care should be taken to tweak the string maps below 41 // to ensure that the operator names generated here coincide with 42 // those used in the postgres library. 43 // 44 // 2. since the operator built-in functions are non-overloaded, they 45 // remove the requirement to disambiguate the type of operands 46 // with the ::: (annotate_type) operator. This may be useful 47 // to simplify/accelerate the serialization of scalar expressions 48 // in distsql. 49 // 50 51 func init() { 52 // Label the unary operators. 53 for op, overloads := range UnaryOps { 54 if int(op) >= len(unaryOpName) || unaryOpName[op] == "" { 55 panic(fmt.Sprintf("missing name for operator %q", op.String())) 56 } 57 opName := unaryOpName[op] 58 for _, impl := range overloads { 59 o := impl.(*UnaryOp) 60 o.counter = sqltelemetry.UnaryOpCounter(opName, o.Typ.String()) 61 } 62 } 63 64 // Label the comparison operators. 65 for op, overloads := range CmpOps { 66 if int(op) >= len(comparisonOpName) || comparisonOpName[op] == "" { 67 panic(fmt.Sprintf("missing name for operator %q", op.String())) 68 } 69 opName := comparisonOpName[op] 70 for _, impl := range overloads { 71 o := impl.(*CmpOp) 72 lname := o.LeftType.String() 73 rname := o.RightType.String() 74 o.counter = sqltelemetry.CmpOpCounter(opName, lname, rname) 75 } 76 } 77 78 // Label the binary operators. 79 for op, overloads := range BinOps { 80 if int(op) >= len(binaryOpName) || binaryOpName[op] == "" { 81 panic(fmt.Sprintf("missing name for operator %q", op.String())) 82 } 83 opName := binaryOpName[op] 84 for _, impl := range overloads { 85 o := impl.(*BinOp) 86 lname := o.LeftType.String() 87 rname := o.RightType.String() 88 o.counter = sqltelemetry.BinOpCounter(opName, lname, rname) 89 } 90 } 91 }