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

chore(kf): query composed contracts more efficiently #295

Merged
merged 2 commits into from
Jun 5, 2024

Conversation

MicBun
Copy link
Contributor

@MicBun MicBun commented Jun 3, 2024

Description

Applying the suggestion from Brennan to make the query more efficient despite current kwil limitation.
Notes:
The original suggestion is modified:

    return select r.date_value as date_value, sum(r.value) as value from get_record_internal($date_from, $date_to, $frozen_at) as r
    group by r.date_value;

into:

    for $row in SELECT date_value, sum(value) as value FROM get_index_internal($date_from, $date_to, $frozen_at) GROUP BY date_value {
        return next $row.date_value, $row.value;
    }

It will cause errors. It looks like there is a bug on return select ... group, since the for statements works

error calling action: call action: jsonrpc.Error: code = -300, message = ERROR: structure of query does not match function result type (SQLSTATE 42804), data = null
err code = -300, msg = ERROR: structure of query does not match function result type (SQLSTATE 42804)

image

Related Problem

resolves: #291

How Has This Been Tested?

  1. Kwil-cli configure and set private key to 0000000000000000000000000000000000000000000000000000000000000001
  2. Run the test get records with taxonomy on composed_stream_contract_test.md file
  3. image

@MicBun MicBun self-assigned this Jun 3, 2024
@MicBun MicBun linked an issue Jun 3, 2024 that may be closed by this pull request
@MicBun MicBun changed the title fix: efficient composed contract query chore: efficient composed contract query Jun 3, 2024
@MicBun MicBun changed the title chore: efficient composed contract query chore: make efficient query for composed contract Jun 3, 2024
@MicBun MicBun force-pushed the chore/efficient-query-composed branch from d725ee6 to 6ab3a57 Compare June 3, 2024 17:06
@MicBun MicBun requested review from brennanjl and a team June 3, 2024 17:14
@MicBun MicBun marked this pull request as ready for review June 3, 2024 17:14
@MicBun
Copy link
Contributor Author

MicBun commented Jun 3, 2024

tagging @brennanjl, you may want to take a look at note section. for loop works, but the return select ... group by returned structure of query does not match function result type (SQLSTATE 42804)

Copy link

pr-time-tracker bot commented Jun 3, 2024

@outerlook
@zolotokrylin
⚠️⚠️⚠️
You must submit the time spent on this PR.
⚠️⚠️⚠️

outerlook
outerlook previously approved these changes Jun 3, 2024
@brennanjl
Copy link
Collaborator

Just to confirm, using return next works, but returning the query does not?

@brennanjl
Copy link
Collaborator

Ah never mind, I have found the source of the bug. Thank you for flagging.

@MicBun
Copy link
Contributor Author

MicBun commented Jun 3, 2024

Just to confirm, using return next works, but returning the query does not?

@brennanjl. Yes, it is. Also which one is considered best practice between those two? I see no difference

@brennanjl
Copy link
Collaborator

Yes, it is. Also which one is considered best practice between those two? I see no difference

In general, if you can do it in SQL, you should. It will virtually always be more efficient. Therefore, I'd say the one without return next.

The bug stems from the fact that postgres returns a numeric data type when calling sum on int8. A fix should be up soon

@brennanjl
Copy link
Collaborator

brennanjl commented Jun 4, 2024

FYI, a fix is up here: kwilteam/kwil-db#790. You will need to typecast your sum(r.value) to be an int: sum(r.value)::int. Kwil will automatically treat it as a decimal(1000,0) to prevent overflow. If you are concerned that a sum might lead to overflow, you can keep it as a numeric.

@MicBun
Copy link
Contributor Author

MicBun commented Jun 4, 2024

In general, if you can do it in SQL, you should. It will virtually always be more efficient. Therefore, I'd say the one without return next.
You will need to typecast your sum(r.value) to be an int: sum(r.value)::int. Kwil will automatically treat it as a decimal(1000,0) to prevent overflow.

Alright, thanks for the note. 👍

converted to the draft:

  1. Need to change return next with SQL ones
  2. Waiting for added tests for decimal kwilteam/kwil-db#790 PR to be merged
  3. Re-test result with type assertion

@MicBun MicBun marked this pull request as draft June 4, 2024 00:31
@zolotokrylin zolotokrylin changed the title chore: make efficient query for composed contract chore: query composed contracts more efficiently Jun 4, 2024
@zolotokrylin zolotokrylin changed the title chore: query composed contracts more efficiently chore(kf): query composed contracts more efficiently Jun 4, 2024
@brennanjl
Copy link
Collaborator

@MicBun it just got merged

@MicBun
Copy link
Contributor Author

MicBun commented Jun 5, 2024

@MicBun it just got merged

Alright, thanks!

@MicBun MicBun marked this pull request as ready for review June 5, 2024 09:30
@MicBun MicBun requested a review from outerlook June 5, 2024 09:30
@MicBun MicBun merged commit 02ed8a4 into main Jun 5, 2024
6 of 8 checks passed
@MicBun MicBun deleted the chore/efficient-query-composed branch June 5, 2024 10:23
@MicBun MicBun linked an issue Jun 6, 2024 that may be closed by this pull request
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.

Bug: Schema references value incorrectly Problem: Inefficient query from composed contract
3 participants