Skip to content
Source
Sign inSign up
TanStack/db

feat: add support for dates to max and min functions. #428

MergedMAST1999(GitHub profile, opens in a new tab)merged 10 commits intomain fromrefs/pull/428/headmerged by samwillis

I tried not to change too many things. If something doesn't look right, or we need some tests that are missing, I'm happy to try and make those changes.

BTW, I tried adding date support to eq as well but, I couldn't figure out a way to make it work. I made a change in packages/db/src/query/compiler/evaluators.ts and, after checking it, I can see that it's converting date to number and comparing it correctly.

After I added a test to confirm it, the test kept failing :( So if it's something that needs more work I can take it out and follow up later.

Feel free to make any necessary changes :)

    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: dc6a2a838c7964f76079d68c35139b1b6cd7cb71

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 13 packages
      NameType
      @tanstack/db-ivmPatch
      @tanstack/dbPatch
      @tanstack/angular-dbPatch
      @tanstack/electric-db-collectionPatch
      @tanstack/query-db-collectionPatch
      @tanstack/react-dbPatch
      @tanstack/rxdb-db-collectionPatch
      @tanstack/solid-dbPatch
      @tanstack/svelte-dbPatch
      @tanstack/trailbase-db-collectionPatch
      @tanstack/vue-dbPatch
      todosPatch
      @tanstack/db-example-react-todoPatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. I also checked with lt, gt and the whole family and I think it's working. Though I recommend someone double check.

    3. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-db

      npm i https://pkg.pr.new/@tanstack/angular-db@428

      @tanstack/db

      npm i https://pkg.pr.new/@tanstack/db@428

      @tanstack/db-ivm

      npm i https://pkg.pr.new/@tanstack/db-ivm@428

      @tanstack/electric-db-collection

      npm i https://pkg.pr.new/@tanstack/electric-db-collection@428

      @tanstack/query-db-collection

      npm i https://pkg.pr.new/@tanstack/query-db-collection@428

      @tanstack/react-db

      npm i https://pkg.pr.new/@tanstack/react-db@428

      @tanstack/rxdb-db-collection

      npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@428

      @tanstack/solid-db

      npm i https://pkg.pr.new/@tanstack/solid-db@428

      @tanstack/svelte-db

      npm i https://pkg.pr.new/@tanstack/svelte-db@428

      @tanstack/trailbase-db-collection

      npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@428

      @tanstack/vue-db

      npm i https://pkg.pr.new/@tanstack/vue-db@428

      commit: dc6a2a8

    4. OK, managed to make it work.

      The PR is ready for review.

    5. Hey @MAST1999 just letting you know this hasn't dropped off my radar. I've asked @kevin-dp to review it as he worked on the groupBy operator recently.

    6. I left some comments with regard to the code changes. I believe the changes here are indeed enough for Dates to work with aggregation functions.

      On the longer term, i'm not sure if this is the approach we want to take. Turning the value into a number with Number seems brittle and works for Dates but may not work for other data types.

      We may want a bigger refactor such that min and max work on any data type that has a total ordering. This means we need a custom comparator function as well as a bottom value and a top value for that specific data type. For example, for numbers the comparator is <, the bottom value is -Inf and the top value is +Inf.

    7. I think in future a larger refactor may be good to support a wider range of types in min/max, but in the sort term this looks like a good route to getting them working for Date. Numbers and dates are by far the most common use for these functions, so lets concentrate on getting that in soon.

    8. samwilliscommented

      Hi @MAST1999, I'm finally catching up on this! I hope you don't mind that I have made some tweaks and pushed to your branch. You had identified much of what needed to happen, and while looking through this it's become apparent we actually have a small bug with range lookups on Date properties.

      @kevin-dp could you take a look at this PR, I think its all good now but I want you to cast your eyes over the normalisation of keys for the btree index. Even though we had a comparison function that supported dates they would still be inserted using the Date object ref, and so range queries would not work. This PR changes it to normalise dates to a number as the key in the index so that the same date is inserted under the same key. As you did this initial work I want to check that we are making the correct changes. May be worth discussing on Monday.

      @MAST1999 I removed the normalisation of other objects to JSON - this is an edge case that I don't think we need to support, and stringifying to json is very slow. It would be better to just go on object ref for them (essentually random order).

    9. Overall the changes look good to me. Left some comments for small improvements.

      packages/db/src/query/compiler/evaluators.ts line 147OutdatedResolvedby samwillis
      packages/db/src/indexes/btree-index.ts line 307OutdatedResolvedby samwillis
      packages/db/src/query/compiler/group-by.ts line 358OutdatedResolvedby samwillis
      packages/db/src/utils/comparison.ts line 54OutdatedResolvedby samwillis
      packages/db-ivm/src/operators/groupBy.ts line 218OutdatedResolvedby samwillis
      packages/db-ivm/src/operators/groupBy.ts line 225OutdatedResolvedby samwillis
      packages/db-ivm/src/operators/groupBy.ts line 231OutdatedResolvedby samwillis
      kevin-dp(GitHub profile, opens in a new tab)replied on packages/db-ivm/src/operators/groupBy.ts line 225

      This comment also applies to max

      kevin-dp(GitHub profile, opens in a new tab)replied on packages/db-ivm/src/operators/groupBy.ts line 231

      This comment also applies to max

    10. Thanks for addressing my feedback. I left 1 comment that is important to double check whether it is correct or not before we merge.

      kevin-dp(GitHub profile, opens in a new tab)replied on packages/db-ivm/src/operators/groupBy.ts line 237

      Should it be if (!minValue || (value && value < minValue)) ?

      kevin-dp(GitHub profile, opens in a new tab)replied on packages/db-ivm/src/operators/groupBy.ts line 237

      These comments also apply to max

    11. samwillis approved these changes

    12. samwillis merged this51c6bc5

    13. samwillis closed this

Sign in to join this conversation.