Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntFieldIndex LowerBound and ReverseLowerBound iterating incorrectly #96

Open
ronerizon opened this issue Apr 19, 2021 · 4 comments
Open

Comments

@ronerizon
Copy link

ronerizon commented Apr 19, 2021

When using large numbers in int field the following behavior is occuring

`

// Create a sample struct
type Person struct {
	Email string
	Name  string
	Age   int
}

// Create the DB schema
schema := &memdb.DBSchema{
	Tables: map[string]*memdb.TableSchema{
		"person": &memdb.TableSchema{
			Name: "person",
			Indexes: map[string]*memdb.IndexSchema{
				"id": &memdb.IndexSchema{
					Name:    "id",
					Unique:  true,
					Indexer: &memdb.StringFieldIndex{Field: "Email"},
				},
				"age": &memdb.IndexSchema{
					Name:    "age",
					Unique:  false,
					Indexer: &memdb.IntFieldIndex{Field: "Age"},
				},
			},
		},
	},
}

// Create a new data base
db, err := memdb.NewMemDB(schema)
if err != nil {
	panic(err)
}

// Create a write transaction
txn := db.Txn(true)

// Insert some people
people := []*Person{
	&Person{"[email protected]", "Joe", 30 * 10000000000},
	&Person{"[email protected]", "Lucy", 35* 10000000000},
	&Person{"[email protected]", "Tariq", 21* 10000000000},
	&Person{"[email protected]", "Dorothy", 53* 10000000000},
	&Person{"[email protected]", "Dorothy", 18* 10000000000},
}
for _, p := range people {
	if err := txn.Insert("person", p); err != nil {
		panic(err)
	}
}

// Commit the transaction
txn.Commit()

// Create read-only transaction
txn = db.Txn(false)
defer txn.Abort()

// Range scan over people with ages between 25 and 35 inclusive
it, err := txn.ReverseLowerBound("person", "age", 29 * 10000000000)
if err != nil {
	panic(err)
}

fmt.Println("People aged 25 - 35:")
for obj := it.Next(); obj != nil; obj = it.Next() {
	p := obj.(*Person)
	fmt.Printf("  %s is aged %d\n", p.Name, p.Age)
}

`

People aged 25 - 35:
Dorothy is aged 530000000000
Lucy is aged 350000000000
Dorothy is aged 180000000000

If we will remove * 10000000000 from the code we will get the following ouptut

People aged 25 - 35:
Tariq is aged 21
Dorothy is aged 18

Which is correct

@dnephin
Copy link
Contributor

dnephin commented Apr 19, 2021

Thank you for the bug report! Which version of go-memdb are you using?

We recently merged PR #93 which fixed a sorting issue with uint. I wonder if the same bug exists for IntFieldIndex. It seems likely that the bug is in the IntFieldIndex if the same problem is not observed with other indexers.

@ronerizon
Copy link
Author

I don't know exactly but in goplayground (latest package if I don't mistake) it has the same behaviour
https://play.golang.org/p/VdCfcgM8sAs

@wardn
Copy link

wardn commented Jun 25, 2021

This bug appears to be in the IntFieldIndex as well. I'm indexing int64 fields but they don't sort correctly with IntFieldIndex. Converting the fields to uint64 and using the UintFieldIndex works as intended.

@lthibault
Copy link

I'm facing a similar issue where ReverseLowerBound on a primary key works as expected (Exhibit A), but the same operation on anything other than id only returns entries that are strictly less than the provided argument (Exhibit B).

hackerwins pushed a commit to yorkie-team/yorkie that referenced this issue Nov 21, 2024
This change resolves a test failure in the js-sdk caused by an unexpected
MemDB search behavior. The issue stems from the `ReverseLowerBound` method
behaving differently for unique and non-unique indexes.

Key changes:
- Set `doc_id_actor_id_server_seq` index as unique
- Ensures correct record retrieval in `FindMinSyncedSeqInfo`
- Resolves `ConnectError: [internal] change not found` during DetachDocument

Addressed a known MemDB limitation where index uniqueness affects search
behavior, referencing hashicorp/go-memdb#96.
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

No branches or pull requests

4 participants