Skip to content

Commit a7fe6cf

Browse files
committed
builtins: refactor bit shifting logic in mapToUnorderedUniqueInt
This patch refactors the bit shifting logic in `mapToUnorderedUniqueInt` to use constants instead of magic numbers. Release note: None
1 parent 56b8c1c commit a7fe6cf

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

pkg/sql/sem/builtins/builtins.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9796,11 +9796,13 @@ func GenerateUniqueUnorderedID(instanceID base.SQLInstanceID) tree.DInt {
97969796
// serial unique uint64 to an unordered unique int64. It accomplishes this by
97979797
// reversing the timestamp portion of the unique ID. This bit manipulation
97989798
// should preserve the number of 1-bits.
9799-
func mapToUnorderedUniqueInt(val uint64) uint64 {
9799+
func mapToUnorderedUniqueInt(uniqueInt uint64) uint64 {
98009800
// val is [0][48 bits of ts][15 bits of node id]
9801-
ts := (val & ((uint64(math.MaxUint64) >> 16) << 15)) >> 15
9802-
v := (bits.Reverse64(ts) >> 1) | (val & (1<<15 - 1))
9803-
return v
9801+
ts := uniqueInt & builtinconstants.UniqueIntTimestampMask
9802+
nodeID := uniqueInt & builtinconstants.UniqueIntNodeIDMask
9803+
reversedTS := bits.Reverse64(ts<<builtinconstants.UniqueIntLeadingZeroBits) << builtinconstants.UniqueIntNodeIDBits
9804+
unorderedUniqueInt := reversedTS | nodeID
9805+
return unorderedUniqueInt
98049806
}
98059807

98069808
// ProcessUniqueID is an ID which is unique to this process in the cluster.

pkg/sql/sem/builtins/builtins_test.go

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,54 @@ func TestGenerateUniqueIDOrder(t *testing.T) {
8080
}
8181
}
8282

83-
// TestMapToUniqueUnorderedID verifies that the mapping preserves the ones count.
84-
func TestMapToUniqueUnorderedID(t *testing.T) {
83+
func TestMapToUnorderedUniqueInt(t *testing.T) {
8584
defer leaktest.AfterTest(t)()
86-
for i := 0; i < 30; i++ {
87-
// RandInput is [0][63 random bits].
88-
randInput := uint64(rand.Int63())
89-
output := mapToUnorderedUniqueInt(randInput)
90-
91-
inputOnesCount := bits.OnesCount64(randInput)
92-
outputOnesCount := bits.OnesCount64(output)
93-
require.Equalf(t, inputOnesCount, outputOnesCount, "input: %b, output: "+
94-
"%b\nExpected: %d, got: %d", randInput, output, inputOnesCount,
95-
outputOnesCount)
96-
}
85+
defer log.Scope(t).Close(t)
86+
87+
t.Run("preserves number of one bits", func(t *testing.T) {
88+
for i := 0; i < 30; i++ {
89+
// RandInput is [0][63 random bits].
90+
randInput := uint64(rand.Int63())
91+
output := mapToUnorderedUniqueInt(randInput)
92+
93+
inputOnesCount := bits.OnesCount64(randInput)
94+
outputOnesCount := bits.OnesCount64(output)
95+
require.Equalf(t, inputOnesCount, outputOnesCount, "input: %b, output: "+
96+
"%b\nExpected: %d, got: %d", randInput, output, inputOnesCount,
97+
outputOnesCount)
98+
}
99+
})
100+
101+
t.Run("correctly reverses timestamp", func(t *testing.T) {
102+
for name, tc := range map[string]struct {
103+
input uint64
104+
expected uint64
105+
}{
106+
"asymmetrical timestamp": {
107+
input: 0b0101100000000000000000000000000000000000000000000000000000000001,
108+
expected: 0b0000000000000000000000000000000000000000000001101000000000000001,
109+
},
110+
"symmetrical timestamp": {
111+
input: 0b0100000000000000000000000000000000000000000000001000000000000101,
112+
expected: 0b0100000000000000000000000000000000000000000000001000000000000101,
113+
},
114+
"max timestamp": {
115+
input: 0b0111111111111111111111111111111111111111111111111000000000000001,
116+
expected: 0b0111111111111111111111111111111111111111111111111000000000000001,
117+
},
118+
} {
119+
t.Run(name, func(t *testing.T) {
120+
actual := mapToUnorderedUniqueInt(tc.input)
121+
require.Equalf(t,
122+
tc.expected, actual,
123+
"actual unordered unique int does not match expected:\n"+
124+
"%064b (expected)\n"+
125+
"%064b (actual)",
126+
tc.expected, actual,
127+
)
128+
})
129+
}
130+
})
97131
}
98132

99133
// TestSerialNormalizationWithUniqueUnorderedID makes sure that serial

0 commit comments

Comments
 (0)