Created
November 22, 2016 22:39
-
-
Save simon-weber/e9ac8147b5daaf1ca08fdae0ec55cad2 to your computer and use it in GitHub Desktop.
lovefield query/index/order bug example
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
document.write = function (s) { | |
// jsfiddle doesn't allow document.write. | |
document.body.insertAdjacentHTML("beforeend", s); | |
} | |
const schemaBuilder = lf.schema.create('schema', 1); | |
schemaBuilder.createTable('Item'). | |
addColumn('id', lf.Type.INTEGER). | |
addColumn('num', lf.Type.STRING). | |
addPrimaryKey(['id']). | |
// Removing this index fixes the ordering. | |
addIndex('idxDesc', ['num'], false, lf.Order.ASC); | |
let db; | |
let item; | |
schemaBuilder.connect({storeType: lf.schema.DataStoreType.MEMORY}).then(function(_db) { | |
db = _db; | |
item = db.getSchema().table('Item'); | |
const rows = [1, 2, 3].map(i => item.createRow({id: i, num: parseInt(i, 10)})); | |
return db.insertOrReplace().into(item).values(rows).exec(); | |
}).then(function() { | |
// Show this working as expected without the or+match query. | |
return db.select().from(item).orderBy(item.num, lf.Order.ASC).exec(); | |
}).then(function(results) { | |
document.write(`ordered as expected: ${JSON.stringify(results.map(i => i.num))}`); | |
}).then(function() { | |
// Show it breaking. | |
return db.select().from(item).where( | |
lf.op.or( | |
// When the index exists, these arguments determine the order of the results. | |
item.num.match('3'), | |
item.num.match('1'), | |
item.num.match('2') | |
)) | |
.orderBy(item.num, lf.Order.ASC) | |
.exec(); | |
}).then(function(results) { | |
document.write(`<br>ordered by args instead: ${JSON.stringify(results.map(i => i.num))}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment