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

Optimizes dynamic dispatch & casts. Fixes arithmetic operations, casts, and more. #1533

Merged
merged 2 commits into from
Aug 5, 2024

Conversation

johnedquinn
Copy link
Member

@johnedquinn johnedquinn commented Jul 31, 2024

Preface

  • This PR improves conformance by 2.66%. We now sit at over 97% conformance (legacy is at 92.49%). See the latest report.

Description

  • The biggest change in this PR is that we are fixing dynamic dispatch (while also maintaining high performance). Before, when there were dynamic arguments, we sometimes didn't invoke dynamic dispatch. It depended on whether there was a single function that had the highest number of exact matches at compile time. This was wrong. Please see FnResolver to see how we pass along the invocable matches when we have any dynamic argument (so we can see which has the most exact matches at runtime). Also, see ExprCallDynamic -- it is now simplified to run through the sorted candidates. See the KDocs there.
  • I've also simplified and optimized ExprCast. It now internally uses a CastTable which is a two-dimensional array (indexed by the PType.Kind ordinals) to receive a cast function. This speeds up the casts, and it's also much easier to read. It retains the same functionality as before while simplifying it and speeding it up.
  • To make these changes, I've been using the profiler to keep track of the performance changes. This PR tackles several performance problems (casts, dynamic dispatch, and unnecessary conversions to/from PartiQLValue).

What's Next?

  • I'm going to work on converting the Fns to use Datum instead. With this, our performance should be substantially better.

Other Information

License Information

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@johnedquinn johnedquinn force-pushed the v1-conformance-ops-perf branch 2 times, most recently from e74ae4a to 935fcfa Compare August 1, 2024 17:00
@johnedquinn johnedquinn changed the title [DRAFT] Optimizes dynamic dispatch & casts. Fixes arithmetic operations, casts, and more. Optimizes dynamic dispatch & casts. Fixes arithmetic operations, casts, and more. Aug 1, 2024
@johnedquinn johnedquinn requested a review from rchowell August 1, 2024 17:11
@johnedquinn johnedquinn marked this pull request as ready for review August 1, 2024 17:11
@@ -226,7 +225,6 @@ internal class MainCommand : Runnable {
}
val catalog = MemoryCatalog.builder()
.name("default")
.info(InfoSchema.ext())
Copy link
Member Author

Choose a reason for hiding this comment

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

This is just a fix from merging in recent changes from the CLI and SPI changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is added because there is one conformance test that requires IS TUPLE that passes in the old evaluator. We previously didn't have this function.

Copy link
Contributor

Choose a reason for hiding this comment

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

You should change the RexConverter to have IS TUPLE lower to is_struct since these are the same function.

@NotNull
static Datum intArbitrary(@NotNull BigInteger value) {
return new DatumBigInteger(value);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is mixing C-style names with SQL names. I say go for all SQL names for symmetry with PType. So update update int64Value to bigint.

Or visa-versa! No strong preference, just that right now they're mixed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was tempted to change all of them to SQL as I ran through this PR, though I held back due to the diff. The intention is to use the SQL names as it's the public API. If you don't mind, my follow-up PRs aim to clean up some of our APIs.

private val paramIndices: IntRange = args.indices
private val paramTypes: List<List<PType>> = this.candidates.map { candidate -> candidate.fn.signature.parameters.map { it.type } }
private val paramFamilies: List<List<CoercionFamily>> = this.candidates.map { candidate -> candidate.fn.signature.parameters.map { family(it.type.kind) } }
private val cachedMatches: MutableMap<List<PType>, Int> = mutableMapOf()
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting that this is a MutableMap<List<PType>> rather than something like MutableMap<List<PType.Kind>>.

Copy link
Member Author

Choose a reason for hiding this comment

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

It has to do with our function resolution. We follow PostgreSQL for exact matches -- which take into account parameters. So, in one dispatch (say argument of type DECIMAL(2,0)) could be matched to one candidate, while another dispatch (say argument DECIMAL(3,0)) could be matched to another candidate.

If we change our modelling of function signatures, then this can change as well.

Fixes arithmetic operations, casts, and more.
@johnedquinn johnedquinn force-pushed the v1-conformance-ops-perf branch from 935fcfa to d1a0506 Compare August 2, 2024 20:02
@@ -251,7 +251,7 @@ internal class Compiler(
error("Dynamic call candidate had different name than others; found $fnName but expected $name")
}
}
ExprCallDynamic.Candidate(fn)
fn
Copy link
Member Author

Choose a reason for hiding this comment

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

This is due to making ExprCallDynamic.Candidate private.

@johnedquinn johnedquinn requested a review from rchowell August 2, 2024 20:03
@johnedquinn johnedquinn merged commit 6c87a1a into partiql:v1 Aug 5, 2024
7 checks passed
@johnedquinn johnedquinn deleted the v1-conformance-ops-perf branch August 5, 2024 21:40
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.

2 participants