Skip to content

Commit fce8059

Browse files
committed
Update relation_id_from_string to use the 128-bits of the Sha256 has rather than
just 64-bits.
1 parent 2f78895 commit fce8059

88 files changed

Lines changed: 898 additions & 759 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

meta/src/meta/templates/parser.go.template

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package lqp
1010

1111
import (
1212
"crypto/sha256"
13+
"encoding/binary"
1314
"fmt"
1415
"math"
1516
"math/big"
@@ -362,16 +363,9 @@ func (p *Parser) startFragment(fragmentID *pb.FragmentId) *pb.FragmentId {{
362363
}}
363364

364365
func (p *Parser) relationIdFromString(name string) *pb.RelationId {{
365-
// Create RelationId from string hash (matching Python implementation)
366-
// Python uses: int(hashlib.sha256(name.encode()).hexdigest()[:16], 16)
367-
// This takes only first 8 bytes (16 hex chars) as id_low, id_high is always 0
368-
// Python interprets the hex as big-endian, so we read bytes in big-endian order
369366
hash := sha256.Sum256([]byte(name))
370-
var low uint64
371-
for i := 0; i < 8; i++ {{
372-
low = (low << 8) | uint64(hash[i])
373-
}}
374-
high := uint64(0)
367+
low := binary.LittleEndian.Uint64(hash[:8])
368+
high := binary.LittleEndian.Uint64(hash[8:16])
375369
relationId := &pb.RelationId{{IdLow: low, IdHigh: high}}
376370

377371
// Store the mapping for the current fragment if we're inside one

meta/src/meta/templates/parser.jl.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ end
228228
function relation_id_from_string(parser::ParserState, name::String)
229229
# Create RelationId from string and track mapping for debug info
230230
hash_bytes = sha256(name)
231-
id_low = Base.parse(UInt64, bytes2hex(hash_bytes[1:8]), base=16)
232-
id_high = UInt64(0)
231+
# Match the convention in backir-to-lqp.jl
232+
id_low = reinterpret(UInt64, hash_bytes[1:8])[1]
233+
id_high = reinterpret(UInt64, hash_bytes[9:16])[1]
233234
relation_id = Proto.RelationId(id_low, id_high)
234235

235236
# Store the mapping for the current fragment if we're inside one

meta/src/meta/templates/parser.py.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ class Parser:
213213

214214
def relation_id_from_string(self, name: str) -> Any:
215215
"""Create RelationId from string and track mapping for debug info."""
216-
id_low = int(hashlib.sha256(name.encode()).hexdigest()[:16], 16)
217-
id_high = 0
216+
hash_bytes = hashlib.sha256(name.encode()).digest()
217+
id_low = int.from_bytes(hash_bytes[:8], byteorder='little')
218+
id_high = int.from_bytes(hash_bytes[8:16], byteorder='little')
218219
relation_id = logic_pb2.RelationId(id_low=id_low, id_high=id_high)
219220

220221
# Store the mapping for the current fragment if we're inside one

sdks/go/src/parser.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package lqp
1010

1111
import (
1212
"crypto/sha256"
13+
"encoding/binary"
1314
"fmt"
1415
"math"
1516
"math/big"
@@ -388,16 +389,9 @@ func (p *Parser) startFragment(fragmentID *pb.FragmentId) *pb.FragmentId {
388389
}
389390

390391
func (p *Parser) relationIdFromString(name string) *pb.RelationId {
391-
// Create RelationId from string hash (matching Python implementation)
392-
// Python uses: int(hashlib.sha256(name.encode()).hexdigest()[:16], 16)
393-
// This takes only first 8 bytes (16 hex chars) as id_low, id_high is always 0
394-
// Python interprets the hex as big-endian, so we read bytes in big-endian order
395392
hash := sha256.Sum256([]byte(name))
396-
var low uint64
397-
for i := 0; i < 8; i++ {
398-
low = (low << 8) | uint64(hash[i])
399-
}
400-
high := uint64(0)
393+
low := binary.LittleEndian.Uint64(hash[:8])
394+
high := binary.LittleEndian.Uint64(hash[8:16])
401395
relationId := &pb.RelationId{IdLow: low, IdHigh: high}
402396

403397
// Store the mapping for the current fragment if we're inside one

sdks/julia/LogicalQueryProtocol.jl/src/parser.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ end
254254
function relation_id_from_string(parser::ParserState, name::String)
255255
# Create RelationId from string and track mapping for debug info
256256
hash_bytes = sha256(name)
257-
id_low = Base.parse(UInt64, bytes2hex(hash_bytes[1:8]), base=16)
258-
id_high = UInt64(0)
257+
# Match the convention in backir-to-lqp.jl
258+
id_low = reinterpret(UInt64, hash_bytes[1:8])[1]
259+
id_high = reinterpret(UInt64, hash_bytes[9:16])[1]
259260
relation_id = Proto.RelationId(id_low, id_high)
260261

261262
# Store the mapping for the current fragment if we're inside one

sdks/python/src/lqp/gen/parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ def start_fragment(
241241

242242
def relation_id_from_string(self, name: str) -> Any:
243243
"""Create RelationId from string and track mapping for debug info."""
244-
id_low = int(hashlib.sha256(name.encode()).hexdigest()[:16], 16)
245-
id_high = 0
244+
hash_bytes = hashlib.sha256(name.encode()).digest()
245+
id_low = int.from_bytes(hash_bytes[:8], byteorder='little')
246+
id_high = int.from_bytes(hash_bytes[8:16], byteorder='little')
246247
relation_id = logic_pb2.RelationId(id_low=id_low, id_high=id_high)
247248

248249
# Store the mapping for the current fragment if we're inside one

tests/bin/arithmetic.bin

54 Bytes
Binary file not shown.

tests/bin/attributes.bin

27 Bytes
Binary file not shown.

tests/bin/comparisons.bin

83 Bytes
Binary file not shown.

tests/bin/config_flags.bin

30 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)