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