[CALCITE-7808] Cache recently created and decorated SQL types - #5282
Open
FrankChen021 wants to merge 1 commit into
Open
FrankChen021 wants to merge 1 commit into
FrankChen021 wants to merge 1 commit into
Conversation
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes CALCITE-7808.
Why
RelDataTypeFactoryImplcanonicalizes equivalent types through a global weak interner, but the interner receives an already constructed type. Repeated requests for the same SQL type therefore create temporaryBasicSqlType, charset-wrapper, and type-digest objects before canonicalization discards them.Large literal ARRAYs frequently request the same type shape many times. In Druid's high-locality string-IN benchmark, avoiding construction on repeated requests reduced allocation by 45.74% at 100,000 literals and 44.70% at 1,000,000 literals.
What
Add two bounded front caches to each
SqlTypeFactoryImpl, with one entry perSqlTypeNamein each cache:For string literals, the entries form this pipeline:
A request with a different precision or decoration replaces the previous entry; this does not retain every type from
CHAR(1)throughCHAR(65536). Cache misses use the existing construction and canonicalization paths, and the global weak interner remains authoritative.Verification
SqlTypeFactoryTestcovers base-type reuse, replacement by a different precision, and decorated-type reuse with changed charset and source cases../gradlew :core:test --tests org.apache.calcite.sql.type.SqlTypeFactoryTest :core:autostyleJavaCheck :core:checkstyleMain :core:checkstyleTest(28 completed, 0 failed)InPlanningBenchmark.queryStringInSqlPlanOnlywith-prof gcDruid benchmark results
Configuration:
inSubQueryThreshold=2147483647,rowsPerSegment=500000, 2 forks, 2 one-second warmup iterations, and 5 one-second measurement iterations. Allocation is cumulative bytes per operation, not retained or peak heap.The performance measurement currently comes from Druid; a Calcite-local
ubenchmarkis not yet included.Scope
The benefit depends on locality. The benchmark uses ordered numeric strings, so requests of the same character length are consecutive. Fixed-length values also have high locality, while frequently alternating lengths may have a lower hit rate. These results should not be generalized to arbitrary string workloads.