Repository navigation
Make vertex-centric ranges of floats hold what their comparisons hold - #5123
Merged
porunov merged 1 commit intoOct 23, 2026
Merged
Conversation
Fixes JanusGraph#5122 A query of the edges or properties of a vertex turns each condition on a key into an interval, in the order in which the storage backend holds the key's values as sort keys: for a Float or Double key, the order of their sortable encoding, which puts -0.0 before +0.0 and NaN after +Infinity. The comparisons of a range (AttributeUtils.compare) take -0.0 and +0.0 as equal instead, and NaN as equal to NaN alone and comparable with no other value. Where a vertex-centric index sorts the relations by the key, a range with an end read the slice between its bounds and returned it without checking it in memory, so has("score", lt(0.0)) returned an edge scored -0.0, lte(-0.0) missed one scored +0.0, lt(NaN) returned every score but NaN and lte(NaN) every score, and gte(0.0), whose slice started at +0.0, missed -0.0. The intervals of several conditions on the key were intersected and checked for emptiness in that order as well, also where no index sorts by the key, and so were those of Float and Double values on a key of type Object: gte(0.0) with lte(-0.0), or an equality of -0.0 with gte(0.0), returned nothing, and an equality of NaN with gt(1.0) returned the edge scored NaN where an index sorts by the score. A range of a Float or Double value is now the interval of the values which its comparison holds in that order, as LuceneIndex.numericQuery has it: a bound at either zero holds both zeros where it is inclusive, from -0.0 or up to +0.0, and neither where it is strict, after +0.0 or before -0.0; a bound of NaN holds NaN alone where it is inclusive and nothing where it is strict; and a range from a start ends before NaN. The bounds move where the conditions become intervals rather than where EdgeSerializer writes the slices, as the intervals are intersected and checked for emptiness before. The intersections, the check and the slices then hold what the conditions hold, and a range which is empty after the move, such as gt(-0.0) with lt(0.0), returns nothing without a read. The slice of a range from a start now leaves out NaN, which the serializer writes as one value whatever its bits, after +Infinity, and with it the relations without a value of the key, so the query is answered by the slice alone, where JanusGraph checked it in memory. A has(key) still reads up to the relations without a value and holds NaN, and an equality still tells -0.0 from +0.0, as equals does. Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com> Co-authored-by: Oleksandr Porunov <alexandr.porunov@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
No blocking issues were identified, and the regression tests cover the changed bound normalization, interval intersections, slice fitting, and serialization behavior.
No open issues 🎉
What changed in this PR
Fixes #5122 by aligning vertex-centric Float and Double ranges with JanusGraph’s comparisons at signed zero and NaN, without changing stored data or requiring reindexing.
Changes:
- Normalizes floating-point bounds before interval intersection and slice construction.
- Allows indexed ranges with a lower bound to exclude NaN without additional in-memory filtering.
- Adds regression coverage for query results, fitted slices, and NaN serialization, and updates the changelog.
| File | Description |
|---|---|
| janusgraph-test/src/test/java/org/janusgraph/graphdb/serializer/SerializerTest.java | Verifies canonical NaN serialization and ordering after positive infinity. |
| janusgraph-core/src/main/java/org/janusgraph/graphdb/query/vertex/BasicVertexCentricQueryBuilder.java | Normalizes decimal bounds and updates slice-fitting logic. |
| janusgraph-backend-testutils/src/main/java/org/janusgraph/graphdb/JanusGraphTest.java | Tests indexed, unindexed, and multi-vertex ranges and fitted query plans. |
| docs/changelog.md | Documents corrected range behavior and removal of unnecessary filtering. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5122
A range of a
FloatorDoublekey of the edges or properties of a vertex should return the relations whose values its comparisons hold:Cmp's ranges compare throughAttributeUtils.compare, which takes -0.0 and +0.0 as equal, and NaN as equal to NaN alone and comparable with no other value. A query of a vertex's relations turns each condition into an interval of the order in which the storage backend holds the sort keys instead, that of their encoding, which puts -0.0 before +0.0 and NaN after positive infinity, and reads its slices, intersects its intervals and decides whether it is empty in that order. So where a vertex-centric index sorts the edges byscore:outE('scored').has('score', lt(0.0d))returned the edge scored -0.0, andlte(-0.0d)missed the one scored +0.0, as these ranges with an end are answered by the slice alone;gte(0.0d), which JanusGraph checks in memory, missed the edge scored -0.0, as its slice starts at +0.0, and so didbetween(0.0d, 1.0d), which the slice answers alone;lt(Double.NaN)returned every scored edge but NaN, andlte(Double.NaN)every scored edge;has('score', Double.NaN).has('score', gt(1.0d))returned the edge scored NaN.And on any label, whether an index sorts it or not,
has('score', gte(0.0d)).has('score', lte(-0.0d))andhas('score', -0.0d).has('score', gte(0.0d))returned nothing, as did such conditions ofFloatorDoublevalues on a key of typeObject.These queries now return the relations whose values the comparisons hold, with an index or without, as a Lucene mixed index finds them since #5030. And the slice of a range from a start, such as
outE('scored').has('score', gt(1.0d)), ends before NaN, so the index answers it alone, where JanusGraph checked it in memory, as for a range of a key of another type since #5108.Changes
BasicVertexCentricQueryBuilder.range(new), whichintersectConstraintscalls forlt,lte,gtandgte, gives the interval of the values which the range holds, in the order of the sort keys. For aFloatorDoublevalue it moves the bounds asLuceneIndex.numericQuerydoes:gte) or up to +0.0 (lte), and neither where it is strict, after +0.0 (gt) or before -0.0 (lt);lte,gte) and empty where it is strict (lt,gt);The bounds it adds are of the value's own type,
FloatorDouble(decimal, new). A value of another type gives the same interval as before.compileConstraints), the check which returns an empty query where an interval is empty, and the slices whichEdgeSerializer.getQuerywrites from the intervals hold what the conditions hold. A range which is empty after the move, such asgt(-0.0)withlt(0.0),(+0.0, -0.0), makes the query empty before a slice is written, so it doesn't reachgetQuery'ssliceStart <= sliceEndassertion.has(key)is still the range without ends, whose slice ends before the relations without a value of the key (Leave out the elements which lack the order key, as TinkerPop does #5108) and so holds NaN. An equality and awithin()are points as before, whichCmp.EQUALcompares byequalsand the slice of each point holds exactly: -0.0 and +0.0 apart, and every NaN as one. They returned the wrong relations only next to a range.BasicVertexCentricQueryBuilder.constructSliceQueriesdrops Leave out the elements which lack the order key, as TinkerPop does #5108's clause which marked the slice of aFloatorDoublerange from a start unfitted (AttributeUtils.isDecimal). The interval of such a range now ends before NaN, so the clause could no longer be reached, and the slice holds what the range holds.The slice which ends before NaN relies on the serializers writing every NaN as one value, after +∞:
NumericUtils.doubleToSortableLongandfloatToSortableInttakeDouble.doubleToLongBitsandFloat.floatToIntBits, which give every NaN the same bits,0x7ff8000000000000and0x7fc00000, so no value comes between +∞ and NaN, and the slice of NaN holds a NaN of any bits. A newSerializerTestholds that.Changelog: a section after Lucene range queries reach zero, negative decimals, the infinities and the ends of
long. Leave out the elements which lack the order key, as TinkerPop does #5108's section said that JanusGraph still checks a range of aFloatorDoublekey from a start in memory, and now says that its slice ends before NaN.Tests
JanusGraphTest.testVertexCentricRangesOfFloatsAndDoublesHoldWhatTheirComparisonsHold(new, every storage backend). A vertex has edges of labels which an index sorts by aDoublekey ascending, descending, and after anIntegerkeygroup(ascending), of three such labels for aFloatkey (the one aftergroupdescending), and of a label which no index sorts; and properties of a key which an index sorts by theDoublekey descending, and of one which an index sorts by theFloatkey ascending. Their values of both keys, and of a key of typeObject, are NaN, -0.0, +0.0, -1.5, 1.5, -∞ and +∞, and one of each has no value.lt,lte,gtandgteat each of these values,eqof -0.0, +0.0 and NaN,within(-0.0, 0.0),within(NaN, 1.5)andhas(key); each alone, and each ordered pair of two of them but those at the infinities: 684 per key, on the five relation types of theDoublekey and of theFloatkey, and on the label without an index for theObjectkey.JanusGraphMultiVertexQuerytoo, of the vertex and of the one which its edges go to, which has no such edges out nor properties: it reads the slices of both at once (executeMultiQuery, orexecuteMultiSliceMultiQueryfor the two slices of awithin()) and returns their relations from the vertices' caches (loadRelationsFromCache), none for the second vertex. The 7,898 queries return the relations whose values pass each condition'sJanusGraphPredicate.test.gt(1.5),gte(0.0),lt(-0.0),lte(0.0),has(key)andlte(NaN)are a single fitted slice and return 1, 4, 2, 4, 7 and 1 relations, andwithin(-0.0, 0.0)two fitted slices with 2 relations; on the label without an index,gt(1.5)is checked in memory.JanusGraphTest.testVertexCentricOrderLeavesOutTheRelationsWithoutTheKey(from Leave out the elements which lack the order key, as TinkerPop does #5108):has('score', gt(1.0))andgt(-0.0)on its label sorted by aDoublescore are fitted, and return the edge scored 1.5 alone.JanusGraphTest.testRelationTypeIndexes: the fourhas('weight', gt(3.6))of the properties which an index sorts by aFloatweight are fitted, where Leave out the elements which lack the order key, as TinkerPop does #5108 had them checked in memory.SerializerTest.testEveryNaNIsWrittenInByteOrderAsOneValueAfterPositiveInfinity(new): aDoubleorFloatNaN of other bits, signalling, negative, or of another payload, is written in byte order as the bytes ofDouble.NaNorFloat.NaN, which come after those of +∞.Mutations, each breaking one part of the change, are all caught (15, run on inmemory and in
SerializerTest):gteat a zero from +0.0:scoreAsc: score >= -0.0returns[2, 4, 6]for[1, 2, 4, 6], the edge scored -0.0 missing.gtat a zero after -0.0:score > -0.0returns[2, 4, 6]for[4, 6], and Leave out the elements which lack the order key, as TinkerPop does #5108'sgt(-0.0)2 edges for 1.lteat a zero up to -0.0:score <= -0.0returns[1, 3, 5]for[1, 2, 3, 5].ltat a zero before +0.0:score < -0.0returns[1, 3, 5]for[3, 5].score <= -0.0returns[1, 3, 5], and Leave out the elements which lack the order key, as TinkerPop does #5108'sgt(-0.0)2 edges for 1.score > -Infinityreturns NaN as well, and Leave out the elements which lack the order key, as TinkerPop does #5108'sgt(1.0)2 edges for 1.score < NaNreturns[1, 2, 3, 4, 5, 6]for[]; as a point whatever the comparison:score < NaNreturns[0]; as empty whatever the comparison:score <= NaNreturns[]for[0], on the label without an index too.FloatorDoublerange from a start: the fitted checks of all threeJanusGraphTesttests.rangeadds for aFloatkey madeDoubles: aClassCastExceptionintestRelationTypeIndexesand the new test.FloatorDouble: aClassCastExceptionfromIntegerandLongkeys intestRelationTypeIndexesand Leave out the elements which lack the order key, as TinkerPop does #5108's test.Objectgiven the intervals of master:plain: any <= -0.0, any >= 0.0returns[]for[1, 2].NumericUtils.doubleToSortableLongtakingDouble.doubleToRawLongBits: the newSerializerTest(the JanusGraph tests store NaNs of the usual bits only).A 16th, restoring #5108's
isDecimalclause alone, changes nothing, as no interval reaches the clause.On master
3148803d4, with these tests:testVertexCentricRangesOfFloatsAndDoublesHoldWhatTheirComparisonsHoldfails on inmemory atscoreAsc: score <= -0.0,[1, 3, 5]for[1, 2, 3, 5]. Collecting every mismatch instead, 1,586 of its 7,898 queries return other relations than the conditions hold: 191 on each of the eight relation types which an index sorts by the key, 40 of their single conditions in the multi-query, and 18 on the label without an index, 6 for each of the three keys, all of them two conditions at the two zeros, such asscore >= 0.0, score <= -0.0,[]for[1, 2]. Alone,lt(0.0),lte(-0.0),gte(0.0),lt(NaN)andlte(NaN)are wrong on every index.testVertexCentricOrderLeavesOutTheRelationsWithoutTheKeyandtestRelationTypeIndexesfail at the fitted checks of the ranges from a start.Locally, Java 11, with checkstyle (no violations):
JanusGraphTesttests above pass on inmemory and BerkeleyJE.-Dtest.skip.tp=false,InMemoryJanusGraphProcessTestandInMemoryMultiQueryJanusGraphProcessTest, 1,860 tests, 80 skipped) pass on an earlier state of this change, which has its main code too, but for one test,GroupCountTest'sg_withSideEffectXa__linkedhashmapX_V_out_groupCountXaX_byXlabelX_out_out_capXaX, which surefire filed underLambdaStepTest$Traversals: its setup took 728 s to load its graph and failed to commit it with anExpiredLockException, as the lock outlivedstorage.lock.expiry-time(5 min) while the shared machine stalled.GroupCountTest$TraversalsandLambdaStepTest$Traversalspass on both suites when run again (92 tests).Notes
Where the bounds move. Moving them where
EdgeSerializer.getQuerywrites the slices wouldn't do: before that,compileConstraintsintersects the intervals of several conditions on the key, andconstructQueryWithoutProfiledecides that one is empty, comparing their bounds in the encoding's order: master returns nothing forhas('score', gte(0.0d)).has('score', lte(-0.0d))andhas('score', -0.0d).has('score', gte(0.0d))on any label, without a slice, keeps the point NaN withingt(1.0d), and turnsgte(0.0d)withgt(-0.0d)into[0.0, …), which a move ingetQuerywould start at -0.0, holding both zeros, whichgt(-0.0d)rejects. So the bounds move where each condition becomes an interval, andgetQueryis unchanged; an interval which is empty after the move never reaches it.A key of type
Object, which can't be a sort key, gets the same intervals for itsFloatandDoublevalues, which only decide whether the query is empty:has('x', gte(0.0d)).has('x', lte(-0.0d))now returns the relations with a zero value, of any numeric type, asCmpcompares them, where it returned nothing.TinkerGraph answers otherwise. TinkerPop's
P(gremlin-core 3.8.2) puts -0.0 below +0.0 and holds NaN equal to nothing:lt(0.0d).test(-0.0d)andgt(-0.0d).test(0.0d)are true, andlte(-0.0d).test(0.0d),gte(0.0d).test(-0.0d)andeq,lteandgte(Double.NaN).test(Double.NaN)false. So TinkerGraph answerslt(0.0d),lte(-0.0d)andgte(0.0d)as master's indexes did, returns nothing forhas('score', gte(0.0d)).has('score', lte(-0.0d)), as master did on any label, and nothing forlte(Double.NaN), where this change returns NaN, as JanusGraph returns NaN forhas('score', Double.NaN), with an index or without, before and after this change. But JanusGraph convertsPtoCmpand checks byCmpthe conditions which no index answers, as on the label without an index, and Lucene finds whatCmpholds since Let Lucene range queries reach zero, negative decimals and the ends of their type #5030, so an index mustn't change that answer; this change makes the vertex-centric indexes give it.Cost. A range of a
FloatorDoublekey from a start is now answered by the slice alone, where JanusGraph checked each relation of the slice against the condition, as it answers a range of another type since Leave out the elements which lack the order key, as TinkerPop does #5108. The slices are the same as before, except that a range from a start now ends before NaN, and that a bound at a zero moves to the other zero where that zero is needed to hold both zeros or neither. An A/B ofjanusgraph-coreagainst master3148803d4, on an inmemory graph: a vertex with 1,000 edges of each of three labels,plain, which no index sorts, and two which a vertex-centric index sorts by aDoublescore, ascending and descending; a fifth of the edges have noscore, and ten each have NaN, -0.0 and +0.0. One JVM per pass, three passes a side in alternating order, each figure the median of the passes' medians of three timed windows of 2 s after a warm-up:outE(l).has('score', gt(250.0d)).count(),lsorted ascendingoutE(l).has('score', gt(250.0d)).count(), sorted descendingoutE(l).has('score', gte(-250.0d)).count(), sorted ascendingoutE(l).has('score', gte(0.0d)).count(), sorted ascendinglocal(outE(l).has('score', gt(250.0d)).order().by('score', desc).limit(10)), sorted descendinglocal(outE(l).has('score', gt(250.0d)).limit(10)), sorted ascendingoutE(l).has('score', lt(0.0d)).count(), sorted ascendingoutE(l).has('score', between(-250.0d, 250.0d)).count(), sorted ascendingoutE(l).has('score').count(), sorted ascendingoutE('plain').has('score', gt(250.0d)).count()The ranges from a start, which master checked relation by relation, take 31–36% less time, and 16% less where an order and a limit follow them; the other traversals move by under 4%. The counts differ where the ten edges with -0.0 move from
lt(0.0d)togte(0.0d), asCmpcompares them.🤖 Generated with Claude Code