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

feat(prefix sort): Support string type key in prefix sort #11527

Closed

Conversation

zhli1142015
Copy link
Contributor

@zhli1142015 zhli1142015 commented Nov 13, 2024

Support string type key in PrefixSort.
We introduce the configuration parameter prefixsort_max_string_length,
which sets the maximum prefix length for strings. The implementation dynamically
determines the prefix length by comparing the configured maximum with the
actual maximum column length from RowContainer, using the smaller of the two.
This ensures efficient and flexible prefix sorting for variable-length string types.

Default value of prefixsort_max_string_length is 16.

Perf result:

StdSort_no-payloads_1_varchar_1k                          158.23ns     6.32M
PrefixSort                                      415.89%    38.05ns    26.28M
StdSort_no-payloads_2_varchar_1k                          186.19ns     5.37M
PrefixSort                                      197.21%    94.41ns    10.59M
StdSort_no-payloads_3_varchar_1k                          197.90ns     5.05M
PrefixSort                                      148.93%   132.89ns     7.53M
StdSort_no-payloads_4_varchar_1k                          211.35ns     4.73M
PrefixSort                                      126.75%   166.75ns     6.00M
StdSort_no-payloads_1_varchar_10k                         257.23ns     3.89M
PrefixSort                                      358.08%    71.84ns    13.92M
StdSort_no-payloads_2_varchar_10k                         272.61ns     3.67M
PrefixSort                                      227.46%   119.85ns     8.34M
StdSort_no-payloads_3_varchar_10k                         295.37ns     3.39M
PrefixSort                                      170.20%   173.55ns     5.76M
StdSort_no-payloads_4_varchar_10k                         319.42ns     3.13M
PrefixSort                                      152.60%   209.31ns     4.78M
StdSort_no-payloads_1_varchar_100k                        348.19ns     2.87M
PrefixSort                                      403.18%    86.36ns    11.58M
StdSort_no-payloads_2_varchar_100k                        409.94ns     2.44M
PrefixSort                                      261.56%   156.73ns     6.38M
StdSort_no-payloads_3_varchar_100k                        469.93ns     2.13M
PrefixSort                                      206.32%   227.76ns     4.39M
StdSort_no-payloads_4_varchar_100k                        526.94ns     1.90M
PrefixSort                                      186.66%   282.29ns     3.54M
StdSort_no-payloads_1_varchar_1000k                       780.28ns     1.28M
PrefixSort                                      627.88%   124.27ns     8.05M
StdSort_no-payloads_2_varchar_1000k                       976.32ns     1.02M
PrefixSort                                      491.56%   198.62ns     5.03M
StdSort_no-payloads_3_varchar_1000k                         1.08us   928.51K
PrefixSort                                      376.44%   286.10ns     3.50M
StdSort_no-payloads_4_varchar_1000k                         1.12us   889.85K
PrefixSort                                      321.50%   349.54ns     2.86M

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 13, 2024
Copy link

netlify bot commented Nov 13, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit f35b4da
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/675b7c3ab5b2ce000861b950

@zhli1142015
Copy link
Contributor Author

cc @jinchengchenghh , @skadilover , @xiaoxmeng , could you help to review this PR? Thanks.

@Yuhta Yuhta requested a review from xiaoxmeng November 13, 2024 16:29
velox/exec/PrefixSort.cpp Outdated Show resolved Hide resolved
velox/exec/PrefixSort.cpp Outdated Show resolved Hide resolved
velox/exec/benchmarks/PrefixSortBenchmark.cpp Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/docs/configs.rst Outdated Show resolved Hide resolved
velox/exec/tests/PrefixSortTest.cpp Show resolved Hide resolved
@jinchengchenghh
Copy link
Contributor

And we can do the further optimization after we add the RowContainer stats, which may record the maxSize of each row, if encodeSize is more than maxSize, we can say it can be fully encoded.

@zhli1142015
Copy link
Contributor Author

And we can do the further optimization after we add the RowContainer stats, which may record the maxSize of each row, if encodeSize is more than maxSize, we can say it can be fully encoded.

If maxSize < prefixsort_string_prefix_length, the encoded size should be maxSize + 1, indicating the column is fully encoded. This allows inclusion of following keys in the prefix and less memory allocation for prefix.
Planning to implement this in the next PR.

Copy link
Contributor

@jinchengchenghh jinchengchenghh left a comment

Choose a reason for hiding this comment

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

Looks good, some minors.

velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/tests/PrefixSortTest.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@jinchengchenghh jinchengchenghh left a comment

Choose a reason for hiding this comment

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

Much appreciate for your contribution.

@jinchengchenghh
Copy link
Contributor

Can you rerun the benchmark to measure the effect that avoid copy string?

@zhli1142015
Copy link
Contributor Author

Cases where strings are stored separately in different blocks should be rare, so the results won’t differ much.

StdSort_no-payloads_1_varchar_1k                          154.76ns     6.46M
PrefixSort                                      293.38%    52.75ns    18.96M
StdSort_no-payloads_2_varchar_1k                          161.67ns     6.19M
PrefixSort                                      209.76%    77.07ns    12.98M
StdSort_no-payloads_3_varchar_1k                          161.56ns     6.19M
PrefixSort                                      194.60%    83.02ns    12.04M
StdSort_no-payloads_4_varchar_1k                          164.47ns     6.08M
PrefixSort                                      191.84%    85.73ns    11.66M
StdSort_no-payloads_1_varchar_10k                         241.94ns     4.13M
PrefixSort                                      278.68%    86.81ns    11.52M
StdSort_no-payloads_2_varchar_10k                         239.05ns     4.18M
PrefixSort                                      234.25%   102.05ns     9.80M
StdSort_no-payloads_3_varchar_10k                         245.33ns     4.08M
PrefixSort                                      218.46%   112.30ns     8.90M
StdSort_no-payloads_4_varchar_10k                         247.89ns     4.03M
PrefixSort                                      217.00%   114.24ns     8.75M
StdSort_no-payloads_1_varchar_100k                        345.14ns     2.90M
PrefixSort                                      323.30%   106.76ns     9.37M
StdSort_no-payloads_2_varchar_100k                        340.79ns     2.93M
PrefixSort                                      219.84%   155.01ns     6.45M
StdSort_no-payloads_3_varchar_100k                        388.60ns     2.57M
PrefixSort                                      200.12%   194.18ns     5.15M
StdSort_no-payloads_4_varchar_100k                        421.94ns     2.37M
PrefixSort                                      184.94%   228.16ns     4.38M
StdSort_no-payloads_1_varchar_1000k                       709.13ns     1.41M
PrefixSort                                      571.14%   124.16ns     8.05M
StdSort_no-payloads_2_varchar_1000k                       808.04ns     1.24M
PrefixSort                                      191.03%   423.00ns     2.36M
StdSort_no-payloads_3_varchar_1000k                       937.25ns     1.07M
PrefixSort                                      174.17%   538.12ns     1.86M
StdSort_no-payloads_4_varchar_1000k                         1.07us   933.94K
PrefixSort                                      183.02%   585.04ns     1.71M

@zhouyuan
Copy link
Contributor

Hi @zhli1142015 I assume the extra prefix will introduce higher memory footprint, do you happen to have some metrics on comparing the memory usage?

thanks,
-yuan

@zhli1142015
Copy link
Contributor Author

Hi @zhli1142015 I assume the extra prefix will introduce higher memory footprint, do you happen to have some metrics on comparing the memory usage?

thanks, -yuan

Please check: #11272 (comment)

@zhli1142015 zhli1142015 changed the title Support string type key in prefix sort feat: Support string type key in prefix sort Nov 15, 2024
@zhli1142015 zhli1142015 changed the title feat: Support string type key in prefix sort feat(perfix sort): Support string type key in prefix sort Nov 19, 2024
@zhli1142015 zhli1142015 changed the title feat(perfix sort): Support string type key in prefix sort feat(prefix sort): Support string type key in prefix sort Nov 19, 2024
@zhli1142015 zhli1142015 force-pushed the string_type_prefix branch 2 times, most recently from 6fc1dff to 23e5d78 Compare December 6, 2024 06:05
@zhli1142015
Copy link
Contributor Author

Hello @xiaoxmeng, I’ve updated the PR to leverage the maxBytes from the RowContainer stats collection. Could you please help review it?
Thanks.

@zhli1142015 zhli1142015 force-pushed the string_type_prefix branch 2 times, most recently from 477152c to 0a54657 Compare December 7, 2024 02:55
Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

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

@zhli1142015 thanks for adding string support % minors.

velox/common/base/PrefixSortConfig.h Outdated Show resolved Hide resolved
velox/core/QueryConfig.h Show resolved Hide resolved
velox/core/QueryConfig.h Outdated Show resolved Hide resolved
velox/docs/configs.rst Show resolved Hide resolved
velox/exec/PrefixSort.h Outdated Show resolved Hide resolved
velox/exec/PrefixSort.cpp Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
address comments

address comments

address comments

address comments

address comments

fix build

address comments

get string column max length from row container

fix ut

collect stats for orderBy op
Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

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

@zhli1142015 LGTM % minors. Thanks!

velox/exec/PrefixSort.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Outdated Show resolved Hide resolved
velox/exec/prefixsort/PrefixSortEncoder.h Show resolved Hide resolved
velox/exec/PrefixSort.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@xiaoxmeng xiaoxmeng left a comment

Choose a reason for hiding this comment

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

@zhli1142015 LGTM. Thanks for the iterations!

velox/exec/PrefixSort.cpp Outdated Show resolved Hide resolved
@facebook-github-bot
Copy link
Contributor

@xiaoxmeng has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@xiaoxmeng has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@jinchengchenghh
Copy link
Contributor

Please also update here, https://github.com/facebookincubator/velox/blob/main/velox/exec/PrefixSort.cpp#L190

Move the partial normalized string key to the last.

@zhli1142015
Copy link
Contributor Author

zhli1142015 commented Dec 13, 2024

Please also update here, https://github.com/facebookincubator/velox/blob/main/velox/exec/PrefixSort.cpp#L190

Move the partial normalized string key to the last.

Now VARCHAR columns' encode size is assigned a large value UINT_MAX in optimizeSortKeysOrder. so VARCHAR columns are placed after other supported types columns, but before unsupported types columns. I think we don't need to update comparison logic.

@jinchengchenghh
Copy link
Contributor

Yes, thanks for your clarification. No problem. Good change.

@facebook-github-bot
Copy link
Contributor

@xiaoxmeng merged this pull request in 960c2af.

athmaja-n pushed a commit to athmaja-n/velox that referenced this pull request Jan 10, 2025
…cubator#11527)

Summary:
Support string type key in PrefixSort.
We introduce the configuration parameter `prefixsort_max_string_length`,
which sets the maximum prefix length for strings. The implementation dynamically
determines the prefix length by comparing the configured maximum with the
actual maximum column length from RowContainer, using the smaller of the two.
This ensures efficient and flexible prefix sorting for variable-length string types.

Default value of `prefixsort_max_string_length` is 16.

Perf result:
```
StdSort_no-payloads_1_varchar_1k                          158.23ns     6.32M
PrefixSort                                      415.89%    38.05ns    26.28M
StdSort_no-payloads_2_varchar_1k                          186.19ns     5.37M
PrefixSort                                      197.21%    94.41ns    10.59M
StdSort_no-payloads_3_varchar_1k                          197.90ns     5.05M
PrefixSort                                      148.93%   132.89ns     7.53M
StdSort_no-payloads_4_varchar_1k                          211.35ns     4.73M
PrefixSort                                      126.75%   166.75ns     6.00M
StdSort_no-payloads_1_varchar_10k                         257.23ns     3.89M
PrefixSort                                      358.08%    71.84ns    13.92M
StdSort_no-payloads_2_varchar_10k                         272.61ns     3.67M
PrefixSort                                      227.46%   119.85ns     8.34M
StdSort_no-payloads_3_varchar_10k                         295.37ns     3.39M
PrefixSort                                      170.20%   173.55ns     5.76M
StdSort_no-payloads_4_varchar_10k                         319.42ns     3.13M
PrefixSort                                      152.60%   209.31ns     4.78M
StdSort_no-payloads_1_varchar_100k                        348.19ns     2.87M
PrefixSort                                      403.18%    86.36ns    11.58M
StdSort_no-payloads_2_varchar_100k                        409.94ns     2.44M
PrefixSort                                      261.56%   156.73ns     6.38M
StdSort_no-payloads_3_varchar_100k                        469.93ns     2.13M
PrefixSort                                      206.32%   227.76ns     4.39M
StdSort_no-payloads_4_varchar_100k                        526.94ns     1.90M
PrefixSort                                      186.66%   282.29ns     3.54M
StdSort_no-payloads_1_varchar_1000k                       780.28ns     1.28M
PrefixSort                                      627.88%   124.27ns     8.05M
StdSort_no-payloads_2_varchar_1000k                       976.32ns     1.02M
PrefixSort                                      491.56%   198.62ns     5.03M
StdSort_no-payloads_3_varchar_1000k                         1.08us   928.51K
PrefixSort                                      376.44%   286.10ns     3.50M
StdSort_no-payloads_4_varchar_1000k                         1.12us   889.85K
PrefixSort                                      321.50%   349.54ns     2.86M
```

Pull Request resolved: facebookincubator#11527

Reviewed By: Yuhta

Differential Revision: D67149095

Pulled By: xiaoxmeng

fbshipit-source-id: 79f02c81165a873aa8068260b5580850f30a4fc5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants