Skip to content

Make vertex-centric ranges of floats hold what their comparisons hold - #5123

Merged
porunov merged 1 commit into
JanusGraph:masterfrom
porunov:vertex-centric-float-ranges-as-compared
Oct 23, 2026
Merged

porunov merged 1 commit into
JanusGraph:masterfrom
porunov:vertex-centric-float-ranges-as-compared

Conversation

@porunov

@porunov porunov commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Fixes #5122

A range of a Float or Double key of the edges or properties of a vertex should return the relations whose values its comparisons hold: Cmp's ranges compare through AttributeUtils.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 by score:

  • outE('scored').has('score', lt(0.0d)) returned the edge scored -0.0, and lte(-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 did between(0.0d, 1.0d), which the slice answers alone;
  • lt(Double.NaN) returned every scored edge but NaN, and lte(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)) and has('score', -0.0d).has('score', gte(0.0d)) returned nothing, as did such conditions of Float or Double values on a key of type Object.

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), which intersectConstraints calls for lt, lte, gt and gte, gives the interval of the values which the range holds, in the order of the sort keys. For a Float or Double value it moves the bounds as LuceneIndex.numericQuery does:

    • a bound at either zero holds both zeros where it is inclusive, from -0.0 (gte) or up to +0.0 (lte), and neither where it is strict, after +0.0 (gt) or before -0.0 (lt);
    • a bound of NaN is the range from NaN to NaN, which is the point NaN where it is inclusive (lte, gte) and empty where it is strict (lt, gt);
    • a range from a start ends before NaN, exclusive.

    The bounds it adds are of the value's own type, Float or Double (decimal, new). A value of another type gives the same interval as before.

    • So the intersections of the intervals of several conditions on the key (compileConstraints), the check which returns an empty query where an interval is empty, and the slices which EdgeSerializer.getQuery writes from the intervals hold what the conditions hold. A range which is empty after the move, such as gt(-0.0) with lt(0.0), (+0.0, -0.0), makes the query empty before a slice is written, so it doesn't reach getQuery's sliceStart <= sliceEnd assertion.
    • 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 a within() are points as before, which Cmp.EQUAL compares by equals and 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.constructSliceQueries drops Leave out the elements which lack the order key, as TinkerPop does #5108's clause which marked the slice of a Float or Double range 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.doubleToSortableLong and floatToSortableInt take Double.doubleToLongBits and Float.floatToIntBits, which give every NaN the same bits, 0x7ff8000000000000 and 0x7fc00000, so no value comes between +∞ and NaN, and the slice of NaN holds a NaN of any bits. A new SerializerTest holds 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 a Float or Double key 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 a Double key ascending, descending, and after an Integer key group (ascending), of three such labels for a Float key (the one after group descending), and of a label which no index sorts; and properties of a key which an index sorts by the Double key descending, and of one which an index sorts by the Float key ascending. Their values of both keys, and of a key of type Object, are NaN, -0.0, +0.0, -1.5, 1.5, -∞ and +∞, and one of each has no value.
    • The conditions: lt, lte, gt and gte at each of these values, eq of -0.0, +0.0 and NaN, within(-0.0, 0.0), within(NaN, 1.5) and has(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 the Double key and of the Float key, and on the label without an index for the Object key.
    • Each runs in a transaction of its own, so that its slices are read from the storage backend rather than cut from a wider slice in the vertex's cache, and each condition alone runs in a JanusGraphMultiVertexQuery too, 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, or executeMultiSliceMultiQuery for the two slices of a within()) 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's JanusGraphPredicate.test.
    • On each index, gt(1.5), gte(0.0), lt(-0.0), lte(0.0), has(key) and lte(NaN) are a single fitted slice and return 1, 4, 2, 4, 7 and 1 relations, and within(-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)) and gt(-0.0) on its label sorted by a Double score are fitted, and return the edge scored 1.5 alone.
  • JanusGraphTest.testRelationTypeIndexes: the four has('weight', gt(3.6)) of the properties which an index sorts by a Float weight are fitted, where Leave out the elements which lack the order key, as TinkerPop does #5108 had them checked in memory.
  • SerializerTest.testEveryNaNIsWrittenInByteOrderAsOneValueAfterPositiveInfinity (new): a Double or Float NaN of other bits, signalling, negative, or of another payload, is written in byte order as the bytes of Double.NaN or Float.NaN, which come after those of +∞.

Mutations, each breaking one part of the change, are all caught (15, run on inmemory and in SerializerTest):

  • A gte at a zero from +0.0: scoreAsc: score >= -0.0 returns [2, 4, 6] for [1, 2, 4, 6], the edge scored -0.0 missing.
  • A gt at a zero after -0.0: score > -0.0 returns [2, 4, 6] for [4, 6], and Leave out the elements which lack the order key, as TinkerPop does #5108's gt(-0.0) 2 edges for 1.
  • An lte at a zero up to -0.0: score <= -0.0 returns [1, 3, 5] for [1, 2, 3, 5].
  • An lt at a zero before +0.0: score < -0.0 returns [1, 3, 5] for [3, 5].
  • No zero moved at all: score <= -0.0 returns [1, 3, 5], and Leave out the elements which lack the order key, as TinkerPop does #5108's gt(-0.0) 2 edges for 1.
  • A range from a start without the NaN end, or with an inclusive one: score > -Infinity returns NaN as well, and Leave out the elements which lack the order key, as TinkerPop does #5108's gt(1.0) 2 edges for 1.
  • A NaN bound taken as any other: score < NaN returns [1, 2, 3, 4, 5, 6] for []; as a point whatever the comparison: score < NaN returns [0]; as empty whatever the comparison: score <= NaN returns [] for [0], on the label without an index too.
  • The slice of a range which ends before NaN marked unfitted, as Leave out the elements which lack the order key, as TinkerPop does #5108 had a Float or Double range from a start: the fitted checks of all three JanusGraphTest tests.
  • The bounds which range adds for a Float key made Doubles: a ClassCastException in testRelationTypeIndexes and the new test.
  • The bounds of any number moved, not only of a Float or Double: a ClassCastException from Integer and Long keys in testRelationTypeIndexes and Leave out the elements which lack the order key, as TinkerPop does #5108's test.
  • A key of type Object given the intervals of master: plain: any <= -0.0, any >= 0.0 returns [] for [1, 2].
  • NumericUtils.doubleToSortableLong taking Double.doubleToRawLongBits: the new SerializerTest (the JanusGraph tests store NaNs of the usual bits only).

A 16th, restoring #5108's isDecimal clause alone, changes nothing, as no interval reaches the clause.

On master 3148803d4, with these tests:

  • testVertexCentricRangesOfFloatsAndDoublesHoldWhatTheirComparisonsHold fails on inmemory at scoreAsc: 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 as score >= 0.0, score <= -0.0, [] for [1, 2]. Alone, lt(0.0), lte(-0.0), gte(0.0), lt(NaN) and lte(NaN) are wrong on every index.
  • testVertexCentricOrderLeavesOutTheRelationsWithoutTheKey and testRelationTypeIndexes fail at the fitted checks of the ranges from a start.

Locally, Java 11, with checkstyle (no violations):

  • On an earlier state of this change, which has its main code: janusgraph-driver (132), janusgraph-core (243), janusgraph-inmemory (717, 85 skipped), janusgraph-test (1,133), janusgraph-berkeleyje (399, 12 skipped) and janusgraph-lucene (464, 148 skipped) pass. Since then only the new test has changed, its Javadoc and its multi-query, which now reads a second vertex, and the changelog's wording; on this change as it stands, checkstyle and the three JanusGraphTest tests above pass on inmemory and BerkeleyJE.
  • The TinkerPop process suites of inmemory (-Dtest.skip.tp=false, InMemoryJanusGraphProcessTest and InMemoryMultiQueryJanusGraphProcessTest, 1,860 tests, 80 skipped) pass on an earlier state of this change, which has its main code too, but for one test, GroupCountTest's g_withSideEffectXa__linkedhashmapX_V_out_groupCountXaX_byXlabelX_out_out_capXaX, which surefire filed under LambdaStepTest$Traversals: its setup took 728 s to load its graph and failed to commit it with an ExpiredLockException, as the lock outlived storage.lock.expiry-time (5 min) while the shared machine stalled. GroupCountTest$Traversals and LambdaStepTest$Traversals pass on both suites when run again (92 tests).
  • CQL, HBase and the other storage backends are left to CI.

Notes

  • Where the bounds move. Moving them where EdgeSerializer.getQuery writes the slices wouldn't do: before that, compileConstraints intersects the intervals of several conditions on the key, and constructQueryWithoutProfile decides that one is empty, comparing their bounds in the encoding's order: master returns nothing for has('score', gte(0.0d)).has('score', lte(-0.0d)) and has('score', -0.0d).has('score', gte(0.0d)) on any label, without a slice, keeps the point NaN within gt(1.0d), and turns gte(0.0d) with gt(-0.0d) into [0.0, …), which a move in getQuery would start at -0.0, holding both zeros, which gt(-0.0d) rejects. So the bounds move where each condition becomes an interval, and getQuery is 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 its Float and Double values, 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, as Cmp compares 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) and gt(-0.0d).test(0.0d) are true, and lte(-0.0d).test(0.0d), gte(0.0d).test(-0.0d) and eq, lte and gte(Double.NaN).test(Double.NaN) false. So TinkerGraph answers lt(0.0d), lte(-0.0d) and gte(0.0d) as master's indexes did, returns nothing for has('score', gte(0.0d)).has('score', lte(-0.0d)), as master did on any label, and nothing for lte(Double.NaN), where this change returns NaN, as JanusGraph returns NaN for has('score', Double.NaN), with an index or without, before and after this change. But JanusGraph converts P to Cmp and checks by Cmp the conditions which no index answers, as on the label without an index, and Lucene finds what Cmp holds 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 Float or Double key 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 of janusgraph-core against master 3148803d4, 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 a Double score, ascending and descending; a fifth of the edges have no score, 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:

    Traversal of the vertex master this change
    outE(l).has('score', gt(250.0d)).count(), l sorted ascending 77.9 µs 54.1 µs −30.6%
    outE(l).has('score', gt(250.0d)).count(), sorted descending 83.7 µs 54.0 µs −35.5%
    outE(l).has('score', gte(-250.0d)).count(), sorted ascending 248.9 µs 164.1 µs −34.1%
    outE(l).has('score', gte(0.0d)).count(), sorted ascending 164.8 µs, 394 edges 113.9 µs, 404 −30.9%
    local(outE(l).has('score', gt(250.0d)).order().by('score', desc).limit(10)), sorted descending 41.1 µs 34.6 µs −15.8%
    local(outE(l).has('score', gt(250.0d)).limit(10)), sorted ascending 28.5 µs 28.0 µs −1.8%
    outE(l).has('score', lt(0.0d)).count(), sorted ascending 119.0 µs, 413 edges 115.2 µs, 403 −3.2%
    outE(l).has('score', between(-250.0d, 250.0d)).count(), sorted ascending 126.2 µs 123.2 µs −2.4%
    outE(l).has('score').count(), sorted ascending 240.2 µs 238.4 µs −0.7%
    outE('plain').has('score', gt(250.0d)).count() 345.5 µs 337.5 µs −2.3%

    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) to gte(0.0d), as Cmp compares them.

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@porunov
porunov marked this pull request as ready for review October 23, 2026 02:12
@porunov porunov added this to the 1.2.0 milestone Oct 23, 2026
@porunov
porunov merged commit c29f6f3 into JanusGraph:master Oct 23, 2026
216 of 324 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vertex-centric ranges of Float and Double keys disagree with JanusGraph's comparisons at -0.0, +0.0 and NaN

2 participants