├── .asf.yaml ├── .github └── workflows │ └── c-cpp.yml ├── .gitignore ├── Dockerfile ├── LICENSE ├── META.json ├── Makefile ├── NOTICE ├── README.md ├── datasketches.control ├── docker-entrypoint-initdb.d └── 01_init_datasketches.sql ├── package.sh ├── sql ├── datasketches--1.3.0--1.4.0.sql ├── datasketches--1.3.0--1.6.0.sql ├── datasketches--1.4.0--1.5.0.sql ├── datasketches--1.5.0--1.6.0.sql ├── datasketches--1.6.0--1.7.0.sql ├── datasketches_aod_sketch.sql ├── datasketches_cpc_sketch.sql ├── datasketches_frequent_strings_sketch.sql ├── datasketches_hll_sketch.sql ├── datasketches_kll_double_sketch.sql ├── datasketches_kll_float_sketch.sql ├── datasketches_quantiles_double_sketch.sql ├── datasketches_req_float_sketch.sql └── datasketches_theta_sketch.sql ├── src ├── agg_state.h ├── allocator.h ├── aod_sketch_c_adapter.cpp ├── aod_sketch_c_adapter.h ├── aod_sketch_pg_functions.c ├── base64.c ├── base64.h ├── common.c ├── cpc_sketch_c_adapter.cpp ├── cpc_sketch_c_adapter.h ├── cpc_sketch_pg_functions.c ├── frequent_strings_sketch_c_adapter.cpp ├── frequent_strings_sketch_c_adapter.h ├── frequent_strings_sketch_pg_functions.c ├── global_hooks.c ├── hll_sketch_c_adapter.cpp ├── hll_sketch_c_adapter.h ├── hll_sketch_pg_functions.c ├── kll_double_sketch_c_adapter.cpp ├── kll_double_sketch_c_adapter.h ├── kll_double_sketch_pg_functions.c ├── kll_float_sketch_c_adapter.cpp ├── kll_float_sketch_c_adapter.h ├── kll_float_sketch_pg_functions.c ├── postgres_h_substitute.h ├── ptr_with_size.h ├── quantiles_double_sketch_c_adapter.cpp ├── quantiles_double_sketch_c_adapter.h ├── quantiles_double_sketch_pg_functions.c ├── req_float_sketch_c_adapter.cpp ├── req_float_sketch_c_adapter.h ├── req_float_sketch_pg_functions.c ├── theta_sketch_c_adapter.cpp ├── theta_sketch_c_adapter.h └── theta_sketch_pg_functions.c └── test ├── aod_sketch_test.sql ├── cpc_sketch_test.sql ├── fi_sketch_test.sql ├── hll_sketch_test.sql ├── kll_double_sketch_test.sql ├── kll_float_sketch_test.sql ├── quantiles_sketch_test.sql ├── req_sketch_test.sql └── theta_sketch_test.sql /.asf.yaml: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | github: 17 | homepage: https://datasketches.apache.org 18 | description: "PostgreSQL extension providing approximate algorithms based on apache/datasketches-cpp" 19 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: checkout 16 | uses: actions/checkout@v4 17 | - name: checkout datasketches-cpp 18 | uses: actions/checkout@v4 19 | with: 20 | repository: apache/datasketches-cpp 21 | path: datasketches-cpp 22 | - name: download Boost 23 | run: make boost 24 | - name: install PostgreSQL 25 | run: | 26 | sudo apt update 27 | sudo apt -y install postgresql-16 postgresql-client-16 postgresql-server-dev-16 libpq-dev 28 | - name: make 29 | run: make 30 | - name: install 31 | run: sudo make install 32 | - name: start server 33 | run: | 34 | sudo systemctl start postgresql.service 35 | pg_isready 36 | - name: create test database 37 | run: sudo -u postgres createdb test 38 | - name: copy tests 39 | run: cp -r test /tmp 40 | - name: run tests 41 | run: sudo -u postgres bash -c "cd; for t in /tmp/test/*.sql; do psql test -f \$t; done" 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse project files 2 | .cproject 3 | .project 4 | .settings/ 5 | 6 | # CLion project files 7 | .idea 8 | 9 | # Visual Studio Code 10 | .vscode/ 11 | 12 | # OSX files 13 | .DS_Store 14 | 15 | # Compiler output, build specific 16 | *.a 17 | *.o 18 | *.so 19 | *.dll 20 | *.dylib 21 | bin/ 22 | build/ 23 | lib/ 24 | 25 | # Log file 26 | 27 | # Package Files # 28 | *.zip 29 | *.tar.gz 30 | 31 | # Other 32 | local/ 33 | tmp/ 34 | _site/ 35 | _* 36 | _*/ 37 | 38 | # exceptions 39 | !__init__.py 40 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | ARG BASE_IMAGE_VERSION=latest 19 | 20 | ARG DATASKETCHES_CPP_HASH=8135b65408947694e13bd131038889e439847aa2 21 | ARG DATASKETCHES_CPP_VERSION=5.2.0 22 | 23 | FROM postgres:$BASE_IMAGE_VERSION 24 | 25 | MAINTAINER dev@datasketches.apache.org 26 | 27 | ENV APACHE_DIST_URLS \ 28 | https://www.apache.org/dyn/closer.cgi?action=download&filename= \ 29 | https://www-us.apache.org/dist/ \ 30 | https://www.apache.org/dist/ \ 31 | https://archive.apache.org/dist/ 32 | 33 | ARG DATASKETCHES_CPP_VERSION 34 | ARG DATASKETCHES_CPP_HASH 35 | 36 | ENV DS_CPP_VER=$DATASKETCHES_CPP_VERSION 37 | ENV DS_CPP_HASH=$DATASKETCHES_CPP_HASH 38 | 39 | 40 | ADD . /datasketches-postgresql 41 | WORKDIR /datasketches-postgresql 42 | 43 | RUN echo "===> Adding prerequisites..." && \ 44 | export PG_MAJOR=`apt list --installed 2>&1 | sed -n "s/^postgresql-\([0-9.]*\)\/.*/\1/p"` && \ 45 | export PG_MINOR=`apt list --installed 2>&1 | sed -n "s/^postgresql-$PG_MAJOR\/\S*\s\(\S*\)\s.*/\1/p"` && \ 46 | apt-get update -y && \ 47 | DEBIAN_FRONTEND=noninteractive \ 48 | apt-get install --no-install-recommends --allow-downgrades -y -q \ 49 | ca-certificates \ 50 | build-essential wget unzip \ 51 | libboost-all-dev \ 52 | postgresql-server-dev-$PG_MAJOR \ 53 | libpq-dev && \ 54 | \ 55 | \ 56 | echo "===> Building datasketches..." && \ 57 | set -eux; \ 58 | download_bin() { \ 59 | local f="$1"; shift; \ 60 | local hash="$1"; shift; \ 61 | local distFile="$1"; shift; \ 62 | local success=; \ 63 | local distUrl=; \ 64 | for distUrl in $APACHE_DIST_URLS; do \ 65 | if wget -nv -O "$f" "$distUrl$distFile"; then \ 66 | success=1; \ 67 | # Checksum the download \ 68 | echo "$hash" "*$f" | sha1sum -c -; \ 69 | break; \ 70 | fi; \ 71 | done; \ 72 | [ -n "$success" ]; \ 73 | } && \ 74 | download_bin "datasketches-cpp.zip" "$DS_CPP_HASH" "datasketches/cpp/$DS_CPP_VER/apache-datasketches-cpp-$DS_CPP_VER-src.zip" && \ 75 | unzip datasketches-cpp.zip && \ 76 | mv apache-datasketches-cpp-$DS_CPP_VER-src datasketches-cpp && \ 77 | make && \ 78 | make install && \ 79 | \ 80 | \ 81 | echo "===> Clean up..." && \ 82 | apt-mark hold postgresql-$PG_MAJOR postgresql-client-$PG_MAJOR && \ 83 | apt-get -y remove --purge --auto-remove \ 84 | ca-certificates \ 85 | build-essential wget unzip \ 86 | postgresql-server-dev-$PG_MAJOR libpq-dev && \ 87 | apt-get clean && \ 88 | rm -rf /datasketches-postgresql /var/lib/apt/lists/* /tmp/* /var/tmp/* 89 | 90 | ADD /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d 91 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | 180 | 181 | APPENDIX A: How to apply the Apache License to your work. 182 | 183 | To apply the Apache License to your work, attach the following 184 | boilerplate notice, with the fields enclosed by brackets "[]" 185 | replaced with your own identifying information. (Don't include 186 | the brackets!) The text should be enclosed in the appropriate 187 | comment syntax for the file format. We also recommend that a 188 | file or class name and description of purpose be included on the 189 | same "printed page" as the copyright notice for easier 190 | identification within third-party archives. 191 | ------------------------------------------------------------- 192 | Copyright [yyyy] [name of copyright owner] 193 | 194 | Licensed under the Apache License, Version 2.0 (the "License"); 195 | you may not use this file except in compliance with the License. 196 | You may obtain a copy of the License at 197 | 198 | http://www.apache.org/licenses/LICENSE-2.0 199 | 200 | Unless required by applicable law or agreed to in writing, software 201 | distributed under the License is distributed on an "AS IS" BASIS, 202 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 203 | See the License for the specific language governing permissions and 204 | limitations under the License. 205 | ------------------------------------------------------------- 206 | 207 | 208 | -------------------------------------------------------------------------------- /META.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "datasketches", 3 | "abstract": "approximate algorithms for big data analysis", 4 | "version": "1.8.0-SNAPSHOT", 5 | "maintainer": [ 6 | "Apache DataSketches Dev List ", 7 | "Apache DataSketches Users List " 8 | ], 9 | "license": "postgresql", 10 | "provides": { 11 | "cpc_sketch": { 12 | "abstract": "CPC sketch for approximate distinct counting", 13 | "file": "sql/datasketches_cpc_sketch.sql", 14 | "version": "1.8.0-SNAPSHOT" 15 | }, 16 | "theta_sketch": { 17 | "abstract": "Theta sketch for approximate distinct counting with set operations", 18 | "file": "sql/datasketches_theta_sketch.sql", 19 | "version": "1.8.0-SNAPSHOT" 20 | }, 21 | "aod_sketch": { 22 | "abstract": "Specialized Tuple sketch with an array of double values associated with each key", 23 | "file": "sql/datasketches_aod_sketch.sql", 24 | "version": "1.8.0-SNAPSHOT" 25 | }, 26 | "hll_sketch": { 27 | "abstract": "HLL sketch for approximate distinct counting", 28 | "file": "sql/datasketches_hll_sketch.sql", 29 | "version": "1.8.0-SNAPSHOT" 30 | }, 31 | "kll_float_sketch": { 32 | "abstract": "KLL quantiles sketch for approximating distributions of float values (quanitles, ranks, histograms)", 33 | "file": "sql/datasketches_kll_float_sketch.sql", 34 | "version": "1.8.0-SNAPSHOT" 35 | }, 36 | "kll_double_sketch": { 37 | "abstract": "KLL quantiles sketch for approximating distributions of double values (quanitles, ranks, histograms)", 38 | "file": "sql/datasketches_kll_double_sketch.sql", 39 | "version": "1.8.0-SNAPSHOT" 40 | }, 41 | "req_float_sketch": { 42 | "abstract": "REQ (Relative Error Quantiles) sketch for approximating distributions of float values (quanitles, ranks, histograms)", 43 | "file": "sql/datasketches_req_float_sketch.sql", 44 | "version": "1.8.0-SNAPSHOT" 45 | }, 46 | "frequent_strings_sketch": { 47 | "abstract": "frequent items sketch for approximate computation of the most frequent strings", 48 | "file": "sql/datasketches_frequent_strings_sketch.sql", 49 | "version": "1.8.0-SNAPSHOT" 50 | }, 51 | "quantiles_double_sketch": { 52 | "abstract": "Quantiles sketch for approximating distributions of double values (quanitles, ranks, histograms), superseded by KLL sketch, included to support legacy sketch data from other platforms", 53 | "file": "sql/datasketches_quantiles_double_sketch.sql", 54 | "version": "1.8.0-SNAPSHOT" 55 | } 56 | }, 57 | "resources": { 58 | "bugtracker": { 59 | "web": "https://github.com/apache/datasketches-postgresql/issues" 60 | }, 61 | "repository": { 62 | "url": "https://github.com/apache/datasketches-postgresql.git", 63 | "web": "https://github.com/apache/datasketches-postgresql", 64 | "type": "git" 65 | } 66 | }, 67 | "meta-spec": { 68 | "version": "1.0.0", 69 | "url": "http://pgxn.org/meta/spec.txt" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | EXTENSION = datasketches 19 | EXTVERSION = $(shell grep default_version $(EXTENSION).control | sed -e "s/default_version[[:space:]]*=[[:space:]]*'\([^']*\)'/\1/") 20 | MODULE_big = datasketches 21 | 22 | SQL_MODULES = sql/datasketches_cpc_sketch.sql \ 23 | sql/datasketches_kll_float_sketch.sql \ 24 | sql/datasketches_kll_double_sketch.sql \ 25 | sql/datasketches_theta_sketch.sql \ 26 | sql/datasketches_frequent_strings_sketch.sql \ 27 | sql/datasketches_hll_sketch.sql \ 28 | sql/datasketches_aod_sketch.sql \ 29 | sql/datasketches_req_float_sketch.sql \ 30 | sql/datasketches_quantiles_double_sketch.sql 31 | SQL_INSTALL = sql/$(EXTENSION)--$(EXTVERSION).sql 32 | DATA = $(SQL_INSTALL) \ 33 | sql/datasketches--1.3.0--1.4.0.sql \ 34 | sql/datasketches--1.4.0--1.5.0.sql \ 35 | sql/datasketches--1.5.0--1.6.0.sql \ 36 | sql/datasketches--1.3.0--1.6.0.sql 37 | 38 | EXTRA_CLEAN = $(SQL_INSTALL) 39 | 40 | OBJS = src/global_hooks.o src/base64.o src/common.o \ 41 | src/kll_float_sketch_pg_functions.o src/kll_float_sketch_c_adapter.o \ 42 | src/kll_double_sketch_pg_functions.o src/kll_double_sketch_c_adapter.o \ 43 | src/cpc_sketch_pg_functions.o src/cpc_sketch_c_adapter.o \ 44 | src/theta_sketch_pg_functions.o src/theta_sketch_c_adapter.o \ 45 | src/frequent_strings_sketch_pg_functions.o src/frequent_strings_sketch_c_adapter.o \ 46 | src/hll_sketch_pg_functions.o src/hll_sketch_c_adapter.o \ 47 | src/aod_sketch_pg_functions.o src/aod_sketch_c_adapter.o \ 48 | src/req_float_sketch_pg_functions.o src/req_float_sketch_c_adapter.o \ 49 | src/quantiles_double_sketch_pg_functions.o src/quantiles_double_sketch_c_adapter.o 50 | 51 | # assume a dir or link named "datasketches-cpp" in the current dir 52 | CORE = datasketches-cpp 53 | 54 | # assume a dir or link named "boost" in the current dir 55 | BOOST = boost 56 | BOOST_VER = 1.75.0 57 | BOOST_FILE = boost_$(subst .,_,$(BOOST_VER)) 58 | BOOST_URL = https://archives.boost.io/release/$(BOOST_VER)/source/$(BOOST_FILE).zip 59 | 60 | PG_CPPFLAGS = -fPIC -I/usr/local/include -I$(BOOST) -I$(CORE)/common/include \ 61 | -I$(CORE)/kll/include \ 62 | -I$(CORE)/cpc/include \ 63 | -I$(CORE)/theta/include \ 64 | -I$(CORE)/fi/include \ 65 | -I$(CORE)/hll/include \ 66 | -I$(CORE)/tuple/include \ 67 | -I$(CORE)/req/include \ 68 | -I$(CORE)/quantiles/include 69 | PG_CXXFLAGS = -std=c++11 70 | SHLIB_LINK = -lstdc++ -L/usr/local/lib 71 | 72 | PG_CONFIG = pg_config 73 | PGXS := $(shell $(PG_CONFIG) --pgxs) 74 | include $(PGXS) 75 | 76 | # fix LLVM JIT compilation error 77 | ifeq ($(with_llvm), yes) 78 | COMPILE.cxx.bc = $(CLANG) -xc++ -Wno-ignored-attributes $(BITCODE_CXXFLAGS) $(CPPFLAGS) -emit-llvm -c 79 | endif 80 | 81 | %.bc : %.cpp 82 | $(COMPILE.cxx.bc) -o $@ $< 83 | if [ "$(with_llvm)" = "yes" ]; then $(LLVM_BINPATH)/opt -module-summary -f $@ -o $@; fi 84 | 85 | # generate combined sql 86 | $(SQL_INSTALL): $(SQL_MODULES) 87 | cat $^ > $@ 88 | 89 | install: $(SQL_INSTALL) 90 | 91 | boost: 92 | wget $(BOOST_URL) 93 | unzip $(BOOST_FILE).zip 94 | ln -s $(BOOST_FILE) boost 95 | 96 | tests: 97 | @for t in test/*.sql; do psql test -f $$t; done 98 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Apache DataSketches-postgresql 2 | Copyright 2023 The Apache Software Foundation 3 | 4 | Copyright 2015-2018 Yahoo Inc. 5 | Copyright 2019-2020 Verizon Media 6 | Copyright 2021 Yahoo Inc. 7 | 8 | This product includes software developed at 9 | The Apache Software Foundation (http://www.apache.org/). 10 | 11 | Prior to moving to ASF, the software for this project was developed at 12 | Yahoo Inc. (https://developer.yahoo.com). 13 | -------------------------------------------------------------------------------- /datasketches.control: -------------------------------------------------------------------------------- 1 | # DataSketches extension 2 | comment = 'Aggregation functions and data types for approximate algorithms' 3 | default_version = '1.8.0-SNAPSHOT' 4 | relocatable = true 5 | module_pathname = '$libdir/datasketches' 6 | -------------------------------------------------------------------------------- /docker-entrypoint-initdb.d/01_init_datasketches.sql: -------------------------------------------------------------------------------- 1 | CREATE EXTENSION IF NOT EXISTS datasketches -------------------------------------------------------------------------------- /package.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | 20 | # This schipt is to prepare a package for pgxn.org 21 | 22 | VER=$1 23 | if [ -z $1 ]; then 24 | echo target PGXN package version must be specified: x.y.z 25 | exit; 26 | fi 27 | 28 | TAG=$2 29 | if [ -z $2 ]; then 30 | echo datasketches-postgresql git tag must be specified 31 | exit; 32 | fi 33 | 34 | # version of datasketches-cpp core library to include 35 | CORETAG=5.2.0 36 | 37 | DST=datasketches-$VER 38 | 39 | PGARCH=postgresql-$VER.zip 40 | COREARCH=core-$CORETAG.zip 41 | 42 | rm -rf $DST 43 | rm -f $DST.zip 44 | 45 | git archive --format zip --prefix=$DST/ --output $PGARCH $TAG 46 | cd ../datasketches-cpp 47 | git archive --format zip --output ../datasketches-postgresql/$COREARCH $CORETAG 48 | cd ../datasketches-postgresql 49 | 50 | unzip $PGARCH 51 | COREDIR=$DST/datasketches-cpp 52 | mkdir $COREDIR 53 | unzip $COREARCH -d $COREDIR 54 | 55 | # extra readme files confuse pgxn.org 56 | rm $COREDIR/README.md 57 | 58 | zip -r $DST.zip $DST 59 | 60 | rm -f $PGARCH $COREARCH 61 | rm -rf $DST 62 | -------------------------------------------------------------------------------- /sql/datasketches--1.4.0--1.5.0.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | -- no changes 19 | -------------------------------------------------------------------------------- /sql/datasketches--1.6.0--1.7.0.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | -- no changes 19 | -------------------------------------------------------------------------------- /sql/datasketches_aod_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE aod_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION aod_sketch_in(cstring) RETURNS aod_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION aod_sketch_out(aod_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE aod_sketch ( 29 | INPUT = aod_sketch_in, 30 | OUTPUT = aod_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as aod_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (aod_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION aod_sketch_build_agg(internal, anyelement, double precision[]) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_aod_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION aod_sketch_build_agg(internal, anyelement, double precision[], int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_aod_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION aod_sketch_build_agg(internal, anyelement, double precision[], int, real) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_aod_sketch_build_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION aod_sketch_from_internal(internal) RETURNS aod_sketch 50 | AS '$libdir/datasketches', 'pg_aod_sketch_from_internal' 51 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION aod_sketch_union_agg(internal, aod_sketch) RETURNS internal 54 | AS '$libdir/datasketches', 'pg_aod_sketch_union_agg' 55 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION aod_sketch_union_agg(internal, aod_sketch, int) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_aod_sketch_union_agg' 59 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION aod_sketch_union_agg(internal, aod_sketch, int, int) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_aod_sketch_union_agg' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION aod_sketch_intersection_agg(internal, aod_sketch) RETURNS internal 66 | AS '$libdir/datasketches', 'pg_aod_sketch_intersection_agg' 67 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE FUNCTION aod_sketch_intersection_agg(internal, aod_sketch, int) RETURNS internal 70 | AS '$libdir/datasketches', 'pg_aod_sketch_intersection_agg' 71 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 72 | 73 | CREATE OR REPLACE FUNCTION aod_sketch_union_combine(internal, internal) RETURNS internal 74 | AS '$libdir/datasketches', 'pg_aod_sketch_union_combine' 75 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 76 | 77 | CREATE OR REPLACE FUNCTION aod_sketch_intersection_combine(internal, internal) RETURNS internal 78 | AS '$libdir/datasketches', 'pg_aod_sketch_intersection_combine' 79 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 80 | 81 | CREATE OR REPLACE FUNCTION aod_sketch_serialize_state(internal) RETURNS bytea 82 | AS '$libdir/datasketches', 'pg_aod_sketch_serialize_state' 83 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 84 | 85 | CREATE OR REPLACE FUNCTION aod_sketch_deserialize_state(bytea, internal) RETURNS internal 86 | AS '$libdir/datasketches', 'pg_aod_sketch_deserialize_state' 87 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 88 | 89 | CREATE OR REPLACE AGGREGATE aod_sketch_build(anyelement, double precision[]) ( 90 | STYPE = internal, 91 | SFUNC = aod_sketch_build_agg, 92 | COMBINEFUNC = aod_sketch_union_combine, 93 | SERIALFUNC = aod_sketch_serialize_state, 94 | DESERIALFUNC = aod_sketch_deserialize_state, 95 | FINALFUNC = aod_sketch_from_internal, 96 | PARALLEL = SAFE 97 | ); 98 | 99 | CREATE OR REPLACE AGGREGATE aod_sketch_build(anyelement, double precision[], int) ( 100 | STYPE = internal, 101 | SFUNC = aod_sketch_build_agg, 102 | COMBINEFUNC = aod_sketch_union_combine, 103 | SERIALFUNC = aod_sketch_serialize_state, 104 | DESERIALFUNC = aod_sketch_deserialize_state, 105 | FINALFUNC = aod_sketch_from_internal, 106 | PARALLEL = SAFE 107 | ); 108 | 109 | CREATE OR REPLACE AGGREGATE aod_sketch_build(anyelement, double precision[], int, real) ( 110 | STYPE = internal, 111 | SFUNC = aod_sketch_build_agg, 112 | COMBINEFUNC = aod_sketch_union_combine, 113 | SERIALFUNC = aod_sketch_serialize_state, 114 | DESERIALFUNC = aod_sketch_deserialize_state, 115 | FINALFUNC = aod_sketch_from_internal, 116 | PARALLEL = SAFE 117 | ); 118 | 119 | CREATE OR REPLACE AGGREGATE aod_sketch_union(aod_sketch) ( 120 | STYPE = internal, 121 | SFUNC = aod_sketch_union_agg, 122 | COMBINEFUNC = aod_sketch_union_combine, 123 | SERIALFUNC = aod_sketch_serialize_state, 124 | DESERIALFUNC = aod_sketch_deserialize_state, 125 | FINALFUNC = aod_sketch_from_internal, 126 | PARALLEL = SAFE 127 | ); 128 | 129 | CREATE OR REPLACE AGGREGATE aod_sketch_union(aod_sketch, int) ( 130 | STYPE = internal, 131 | SFUNC = aod_sketch_union_agg, 132 | COMBINEFUNC = aod_sketch_union_combine, 133 | SERIALFUNC = aod_sketch_serialize_state, 134 | DESERIALFUNC = aod_sketch_deserialize_state, 135 | FINALFUNC = aod_sketch_from_internal, 136 | PARALLEL = SAFE 137 | ); 138 | 139 | CREATE OR REPLACE AGGREGATE aod_sketch_union(aod_sketch, int, int) ( 140 | STYPE = internal, 141 | SFUNC = aod_sketch_union_agg, 142 | COMBINEFUNC = aod_sketch_union_combine, 143 | SERIALFUNC = aod_sketch_serialize_state, 144 | DESERIALFUNC = aod_sketch_deserialize_state, 145 | FINALFUNC = aod_sketch_from_internal, 146 | PARALLEL = SAFE 147 | ); 148 | 149 | CREATE OR REPLACE AGGREGATE aod_sketch_intersection(aod_sketch) ( 150 | STYPE = internal, 151 | SFUNC = aod_sketch_intersection_agg, 152 | COMBINEFUNC = aod_sketch_intersection_combine, 153 | SERIALFUNC = aod_sketch_serialize_state, 154 | DESERIALFUNC = aod_sketch_deserialize_state, 155 | FINALFUNC = aod_sketch_from_internal, 156 | PARALLEL = SAFE 157 | ); 158 | 159 | CREATE OR REPLACE AGGREGATE aod_sketch_intersection(aod_sketch, int) ( 160 | STYPE = internal, 161 | SFUNC = aod_sketch_intersection_agg, 162 | COMBINEFUNC = aod_sketch_intersection_combine, 163 | SERIALFUNC = aod_sketch_serialize_state, 164 | DESERIALFUNC = aod_sketch_deserialize_state, 165 | FINALFUNC = aod_sketch_from_internal, 166 | PARALLEL = SAFE 167 | ); 168 | 169 | CREATE OR REPLACE FUNCTION aod_sketch_get_estimate(aod_sketch) RETURNS double precision 170 | AS '$libdir/datasketches', 'pg_aod_sketch_get_estimate' 171 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 172 | 173 | CREATE OR REPLACE FUNCTION aod_sketch_get_estimate_and_bounds(aod_sketch) RETURNS double precision[] 174 | AS '$libdir/datasketches', 'pg_aod_sketch_get_estimate_and_bounds' 175 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 176 | 177 | CREATE OR REPLACE FUNCTION aod_sketch_get_estimate_and_bounds(aod_sketch, int) RETURNS double precision[] 178 | AS '$libdir/datasketches', 'pg_aod_sketch_get_estimate_and_bounds' 179 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 180 | 181 | CREATE OR REPLACE FUNCTION aod_sketch_to_string(aod_sketch) RETURNS TEXT 182 | AS '$libdir/datasketches', 'pg_aod_sketch_to_string' 183 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 184 | 185 | CREATE OR REPLACE FUNCTION aod_sketch_to_string(aod_sketch, boolean) RETURNS TEXT 186 | AS '$libdir/datasketches', 'pg_aod_sketch_to_string' 187 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 188 | 189 | CREATE OR REPLACE FUNCTION aod_sketch_union(aod_sketch, aod_sketch) RETURNS aod_sketch 190 | AS '$libdir/datasketches', 'pg_aod_sketch_union' 191 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 192 | 193 | CREATE OR REPLACE FUNCTION aod_sketch_union(aod_sketch, aod_sketch, int) RETURNS aod_sketch 194 | AS '$libdir/datasketches', 'pg_aod_sketch_union' 195 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 196 | 197 | CREATE OR REPLACE FUNCTION aod_sketch_intersection(aod_sketch, aod_sketch) RETURNS aod_sketch 198 | AS '$libdir/datasketches', 'pg_aod_sketch_intersection' 199 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 200 | 201 | CREATE OR REPLACE FUNCTION aod_sketch_intersection(aod_sketch, aod_sketch, int) RETURNS aod_sketch 202 | AS '$libdir/datasketches', 'pg_aod_sketch_intersection' 203 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 204 | 205 | CREATE OR REPLACE FUNCTION aod_sketch_a_not_b(aod_sketch, aod_sketch) RETURNS aod_sketch 206 | AS '$libdir/datasketches', 'pg_aod_sketch_a_not_b' 207 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 208 | 209 | CREATE OR REPLACE FUNCTION aod_sketch_a_not_b(aod_sketch, aod_sketch, int) RETURNS aod_sketch 210 | AS '$libdir/datasketches', 'pg_aod_sketch_a_not_b' 211 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 212 | 213 | CREATE OR REPLACE FUNCTION aod_sketch_to_kll_float_sketch(aod_sketch, int) RETURNS kll_float_sketch 214 | AS '$libdir/datasketches', 'pg_aod_sketch_to_kll_float_sketch' 215 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 216 | 217 | CREATE OR REPLACE FUNCTION aod_sketch_to_kll_float_sketch(aod_sketch, int, int) RETURNS kll_float_sketch 218 | AS '$libdir/datasketches', 'pg_aod_sketch_to_kll_float_sketch' 219 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 220 | 221 | CREATE OR REPLACE FUNCTION aod_sketch_students_t_test(aod_sketch, aod_sketch) RETURNS double precision[] 222 | AS '$libdir/datasketches', 'pg_aod_sketch_students_t_test' 223 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 224 | 225 | CREATE OR REPLACE FUNCTION aod_sketch_to_means(aod_sketch) RETURNS double precision[] 226 | AS '$libdir/datasketches', 'pg_aod_sketch_to_means' 227 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 228 | 229 | CREATE OR REPLACE FUNCTION aod_sketch_to_variances(aod_sketch) RETURNS double precision[] 230 | AS '$libdir/datasketches', 'pg_aod_sketch_to_variances' 231 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 232 | -------------------------------------------------------------------------------- /sql/datasketches_cpc_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE cpc_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION cpc_sketch_in(cstring) RETURNS cpc_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION cpc_sketch_out(cpc_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE cpc_sketch ( 29 | INPUT = cpc_sketch_in, 30 | OUTPUT = cpc_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as cpc_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (cpc_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION cpc_sketch_build_agg(internal, anyelement) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_cpc_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION cpc_sketch_build_agg(internal, anyelement, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_cpc_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION cpc_sketch_from_internal(internal) RETURNS cpc_sketch 46 | AS '$libdir/datasketches', 'pg_cpc_sketch_from_internal' 47 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION cpc_sketch_get_estimate_from_internal(internal) RETURNS double precision 50 | AS '$libdir/datasketches', 'pg_cpc_sketch_get_estimate_from_internal' 51 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION cpc_sketch_union_agg(internal, cpc_sketch) RETURNS internal 54 | AS '$libdir/datasketches', 'pg_cpc_sketch_union_agg' 55 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION cpc_sketch_union_agg(internal, cpc_sketch, int) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_cpc_sketch_union_agg' 59 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION cpc_sketch_combine(internal, internal) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_cpc_sketch_combine' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION cpc_sketch_serialize_state(internal) RETURNS bytea 66 | AS '$libdir/datasketches', 'pg_cpc_sketch_serialize_state' 67 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE FUNCTION cpc_sketch_deserialize_state(bytea, internal) RETURNS internal 70 | AS '$libdir/datasketches', 'pg_cpc_sketch_deserialize_state' 71 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 72 | 73 | CREATE OR REPLACE AGGREGATE cpc_sketch_distinct(anyelement) ( 74 | STYPE = internal, 75 | SFUNC = cpc_sketch_build_agg, 76 | COMBINEFUNC = cpc_sketch_combine, 77 | SERIALFUNC = cpc_sketch_serialize_state, 78 | DESERIALFUNC = cpc_sketch_deserialize_state, 79 | FINALFUNC = cpc_sketch_get_estimate_from_internal, 80 | PARALLEL = SAFE 81 | ); 82 | 83 | CREATE OR REPLACE AGGREGATE cpc_sketch_distinct(anyelement, int) ( 84 | STYPE = internal, 85 | SFUNC = cpc_sketch_build_agg, 86 | COMBINEFUNC = cpc_sketch_combine, 87 | SERIALFUNC = cpc_sketch_serialize_state, 88 | DESERIALFUNC = cpc_sketch_deserialize_state, 89 | FINALFUNC = cpc_sketch_get_estimate_from_internal, 90 | PARALLEL = SAFE 91 | ); 92 | 93 | CREATE OR REPLACE AGGREGATE cpc_sketch_build(anyelement) ( 94 | STYPE = internal, 95 | SFUNC = cpc_sketch_build_agg, 96 | COMBINEFUNC = cpc_sketch_combine, 97 | SERIALFUNC = cpc_sketch_serialize_state, 98 | DESERIALFUNC = cpc_sketch_deserialize_state, 99 | FINALFUNC = cpc_sketch_from_internal, 100 | PARALLEL = SAFE 101 | ); 102 | 103 | CREATE OR REPLACE AGGREGATE cpc_sketch_build(anyelement, int) ( 104 | STYPE = internal, 105 | SFUNC = cpc_sketch_build_agg, 106 | COMBINEFUNC = cpc_sketch_combine, 107 | SERIALFUNC = cpc_sketch_serialize_state, 108 | DESERIALFUNC = cpc_sketch_deserialize_state, 109 | FINALFUNC = cpc_sketch_from_internal, 110 | PARALLEL = SAFE 111 | ); 112 | 113 | CREATE OR REPLACE AGGREGATE cpc_sketch_union(cpc_sketch) ( 114 | STYPE = internal, 115 | SFUNC = cpc_sketch_union_agg, 116 | COMBINEFUNC = cpc_sketch_combine, 117 | SERIALFUNC = cpc_sketch_serialize_state, 118 | DESERIALFUNC = cpc_sketch_deserialize_state, 119 | FINALFUNC = cpc_sketch_from_internal, 120 | PARALLEL = SAFE 121 | ); 122 | 123 | CREATE OR REPLACE AGGREGATE cpc_sketch_union(cpc_sketch, int) ( 124 | STYPE = internal, 125 | SFUNC = cpc_sketch_union_agg, 126 | COMBINEFUNC = cpc_sketch_combine, 127 | SERIALFUNC = cpc_sketch_serialize_state, 128 | DESERIALFUNC = cpc_sketch_deserialize_state, 129 | FINALFUNC = cpc_sketch_from_internal, 130 | PARALLEL = SAFE 131 | ); 132 | 133 | CREATE OR REPLACE FUNCTION cpc_sketch_get_estimate(cpc_sketch) RETURNS double precision 134 | AS '$libdir/datasketches', 'pg_cpc_sketch_get_estimate' 135 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 136 | 137 | CREATE OR REPLACE FUNCTION cpc_sketch_get_estimate_and_bounds(cpc_sketch) RETURNS double precision[] 138 | AS '$libdir/datasketches', 'pg_cpc_sketch_get_estimate_and_bounds' 139 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 140 | 141 | CREATE OR REPLACE FUNCTION cpc_sketch_get_estimate_and_bounds(cpc_sketch, int) RETURNS double precision[] 142 | AS '$libdir/datasketches', 'pg_cpc_sketch_get_estimate_and_bounds' 143 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 144 | 145 | CREATE OR REPLACE FUNCTION cpc_sketch_to_string(cpc_sketch) RETURNS TEXT 146 | AS '$libdir/datasketches', 'pg_cpc_sketch_to_string' 147 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 148 | 149 | CREATE OR REPLACE FUNCTION cpc_sketch_union(cpc_sketch, cpc_sketch) RETURNS cpc_sketch 150 | AS '$libdir/datasketches', 'pg_cpc_sketch_union' 151 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 152 | 153 | CREATE OR REPLACE FUNCTION cpc_sketch_union(cpc_sketch, cpc_sketch, int) RETURNS cpc_sketch 154 | AS '$libdir/datasketches', 'pg_cpc_sketch_union' 155 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 156 | -------------------------------------------------------------------------------- /sql/datasketches_frequent_strings_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE frequent_strings_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_in(cstring) RETURNS frequent_strings_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_out(frequent_strings_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE frequent_strings_sketch ( 29 | INPUT = frequent_strings_sketch_in, 30 | OUTPUT = frequent_strings_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as frequent_strings_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (frequent_strings_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_build_agg(internal, int, varchar) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_build_agg(internal, int, varchar, bigint) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_merge_agg(internal, int, frequent_strings_sketch) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_merge_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_serialize(internal) RETURNS bytea 50 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_serialize' 51 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_deserialize(bytea, internal) RETURNS internal 54 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_deserialize' 55 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_combine(internal, internal) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_combine' 59 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_finalize(internal) RETURNS frequent_strings_sketch 62 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_serialize' 63 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE AGGREGATE frequent_strings_sketch_build(int, varchar) ( 66 | STYPE = internal, 67 | SFUNC = frequent_strings_sketch_build_agg, 68 | COMBINEFUNC = frequent_strings_sketch_combine, 69 | SERIALFUNC = frequent_strings_sketch_serialize, 70 | DESERIALFUNC = frequent_strings_sketch_deserialize, 71 | FINALFUNC = frequent_strings_sketch_finalize, 72 | PARALLEL = SAFE 73 | ); 74 | 75 | CREATE OR REPLACE AGGREGATE frequent_strings_sketch_build(int, varchar, bigint) ( 76 | STYPE = internal, 77 | SFUNC = frequent_strings_sketch_build_agg, 78 | COMBINEFUNC = frequent_strings_sketch_combine, 79 | SERIALFUNC = frequent_strings_sketch_serialize, 80 | DESERIALFUNC = frequent_strings_sketch_deserialize, 81 | FINALFUNC = frequent_strings_sketch_finalize, 82 | PARALLEL = SAFE 83 | ); 84 | 85 | CREATE OR REPLACE AGGREGATE frequent_strings_sketch_merge(int, frequent_strings_sketch) ( 86 | STYPE = internal, 87 | SFUNC = frequent_strings_sketch_merge_agg, 88 | COMBINEFUNC = frequent_strings_sketch_combine, 89 | SERIALFUNC = frequent_strings_sketch_serialize, 90 | DESERIALFUNC = frequent_strings_sketch_deserialize, 91 | FINALFUNC = frequent_strings_sketch_finalize, 92 | PARALLEL = SAFE 93 | ); 94 | 95 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_to_string(frequent_strings_sketch) RETURNS TEXT 96 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_to_string' 97 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 98 | 99 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_to_string(frequent_strings_sketch, boolean) RETURNS TEXT 100 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_to_string' 101 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 102 | 103 | CREATE TYPE frequent_strings_sketch_row AS (str varchar, estimate bigint, lower_bound bigint, upper_bound bigint); 104 | 105 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_result_no_false_positives(frequent_strings_sketch) 106 | RETURNS setof frequent_strings_sketch_row 107 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_result_no_false_positives' 108 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 109 | 110 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_result_no_false_positives(frequent_strings_sketch, bigint) 111 | RETURNS setof frequent_strings_sketch_row 112 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_result_no_false_positives' 113 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 114 | 115 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_result_no_false_negatives(frequent_strings_sketch) 116 | RETURNS setof frequent_strings_sketch_row 117 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_result_no_false_negatives' 118 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 119 | 120 | CREATE OR REPLACE FUNCTION frequent_strings_sketch_result_no_false_negatives(frequent_strings_sketch, bigint) 121 | RETURNS setof frequent_strings_sketch_row 122 | AS '$libdir/datasketches', 'pg_frequent_strings_sketch_result_no_false_negatives' 123 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 124 | 125 | -------------------------------------------------------------------------------- /sql/datasketches_hll_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE hll_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION hll_sketch_in(cstring) RETURNS hll_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION hll_sketch_out(hll_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE hll_sketch ( 29 | INPUT = hll_sketch_in, 30 | OUTPUT = hll_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as hll_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (hll_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION hll_sketch_build_agg(internal, anyelement) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_hll_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION hll_sketch_build_agg(internal, anyelement, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_hll_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION hll_sketch_build_agg(internal, anyelement, int, int) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_hll_sketch_build_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION hll_sketch_from_internal(internal) RETURNS hll_sketch 50 | AS '$libdir/datasketches', 'pg_hll_sketch_from_internal' 51 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION hll_sketch_get_estimate_from_internal(internal) RETURNS double precision 54 | AS '$libdir/datasketches', 'pg_hll_sketch_get_estimate_from_internal' 55 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION hll_sketch_union_agg(internal, hll_sketch) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_hll_sketch_union_agg' 59 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION hll_sketch_union_agg(internal, hll_sketch, int) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_hll_sketch_union_agg' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION hll_sketch_union_agg(internal, hll_sketch, int, int) RETURNS internal 66 | AS '$libdir/datasketches', 'pg_hll_sketch_union_agg' 67 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE FUNCTION hll_sketch_combine(internal, internal) RETURNS internal 70 | AS '$libdir/datasketches', 'pg_hll_sketch_combine' 71 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 72 | 73 | CREATE OR REPLACE FUNCTION hll_sketch_serialize_state(internal) RETURNS bytea 74 | AS '$libdir/datasketches', 'pg_hll_sketch_serialize_state' 75 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 76 | 77 | CREATE OR REPLACE FUNCTION hll_sketch_deserialize_state(bytea, internal) RETURNS internal 78 | AS '$libdir/datasketches', 'pg_hll_sketch_deserialize_state' 79 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 80 | 81 | CREATE OR REPLACE AGGREGATE hll_sketch_distinct(anyelement) ( 82 | STYPE = internal, 83 | SFUNC = hll_sketch_build_agg, 84 | COMBINEFUNC = hll_sketch_combine, 85 | SERIALFUNC = hll_sketch_serialize_state, 86 | DESERIALFUNC = hll_sketch_deserialize_state, 87 | FINALFUNC = hll_sketch_get_estimate_from_internal, 88 | PARALLEL = SAFE 89 | ); 90 | 91 | CREATE OR REPLACE AGGREGATE hll_sketch_distinct(anyelement, int) ( 92 | STYPE = internal, 93 | SFUNC = hll_sketch_build_agg, 94 | COMBINEFUNC = hll_sketch_combine, 95 | SERIALFUNC = hll_sketch_serialize_state, 96 | DESERIALFUNC = hll_sketch_deserialize_state, 97 | FINALFUNC = hll_sketch_get_estimate_from_internal, 98 | PARALLEL = SAFE 99 | ); 100 | 101 | CREATE OR REPLACE AGGREGATE hll_sketch_build(anyelement) ( 102 | STYPE = internal, 103 | SFUNC = hll_sketch_build_agg, 104 | COMBINEFUNC = hll_sketch_combine, 105 | SERIALFUNC = hll_sketch_serialize_state, 106 | DESERIALFUNC = hll_sketch_deserialize_state, 107 | FINALFUNC = hll_sketch_from_internal, 108 | PARALLEL = SAFE 109 | ); 110 | 111 | CREATE OR REPLACE AGGREGATE hll_sketch_build(anyelement, int) ( 112 | STYPE = internal, 113 | SFUNC = hll_sketch_build_agg, 114 | COMBINEFUNC = hll_sketch_combine, 115 | SERIALFUNC = hll_sketch_serialize_state, 116 | DESERIALFUNC = hll_sketch_deserialize_state, 117 | FINALFUNC = hll_sketch_from_internal, 118 | PARALLEL = SAFE 119 | ); 120 | 121 | CREATE OR REPLACE AGGREGATE hll_sketch_build(anyelement, int, int) ( 122 | STYPE = internal, 123 | SFUNC = hll_sketch_build_agg, 124 | COMBINEFUNC = hll_sketch_combine, 125 | SERIALFUNC = hll_sketch_serialize_state, 126 | DESERIALFUNC = hll_sketch_deserialize_state, 127 | FINALFUNC = hll_sketch_from_internal, 128 | PARALLEL = SAFE 129 | ); 130 | 131 | CREATE OR REPLACE AGGREGATE hll_sketch_union(hll_sketch) ( 132 | STYPE = internal, 133 | SFUNC = hll_sketch_union_agg, 134 | COMBINEFUNC = hll_sketch_combine, 135 | SERIALFUNC = hll_sketch_serialize_state, 136 | DESERIALFUNC = hll_sketch_deserialize_state, 137 | FINALFUNC = hll_sketch_from_internal, 138 | PARALLEL = SAFE 139 | ); 140 | 141 | CREATE OR REPLACE AGGREGATE hll_sketch_union(hll_sketch, int) ( 142 | STYPE = internal, 143 | SFUNC = hll_sketch_union_agg, 144 | COMBINEFUNC = hll_sketch_combine, 145 | SERIALFUNC = hll_sketch_serialize_state, 146 | DESERIALFUNC = hll_sketch_deserialize_state, 147 | FINALFUNC = hll_sketch_from_internal, 148 | PARALLEL = SAFE 149 | ); 150 | 151 | CREATE OR REPLACE AGGREGATE hll_sketch_union(hll_sketch, int, int) ( 152 | STYPE = internal, 153 | SFUNC = hll_sketch_union_agg, 154 | COMBINEFUNC = hll_sketch_combine, 155 | SERIALFUNC = hll_sketch_serialize_state, 156 | DESERIALFUNC = hll_sketch_deserialize_state, 157 | FINALFUNC = hll_sketch_from_internal, 158 | PARALLEL = SAFE 159 | ); 160 | 161 | CREATE OR REPLACE FUNCTION hll_sketch_get_estimate(hll_sketch) RETURNS double precision 162 | AS '$libdir/datasketches', 'pg_hll_sketch_get_estimate' 163 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 164 | 165 | CREATE OR REPLACE FUNCTION hll_sketch_get_estimate_and_bounds(hll_sketch) RETURNS double precision[] 166 | AS '$libdir/datasketches', 'pg_hll_sketch_get_estimate_and_bounds' 167 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 168 | 169 | CREATE OR REPLACE FUNCTION hll_sketch_get_estimate_and_bounds(hll_sketch, int) RETURNS double precision[] 170 | AS '$libdir/datasketches', 'pg_hll_sketch_get_estimate_and_bounds' 171 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 172 | 173 | CREATE OR REPLACE FUNCTION hll_sketch_to_string(hll_sketch) RETURNS TEXT 174 | AS '$libdir/datasketches', 'pg_hll_sketch_to_string' 175 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 176 | 177 | CREATE OR REPLACE FUNCTION hll_sketch_union(hll_sketch, hll_sketch) RETURNS hll_sketch 178 | AS '$libdir/datasketches', 'pg_hll_sketch_union' 179 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 180 | 181 | CREATE OR REPLACE FUNCTION hll_sketch_union(hll_sketch, hll_sketch, int) RETURNS hll_sketch 182 | AS '$libdir/datasketches', 'pg_hll_sketch_union' 183 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 184 | 185 | CREATE OR REPLACE FUNCTION hll_sketch_union(hll_sketch, hll_sketch, int, int) RETURNS hll_sketch 186 | AS '$libdir/datasketches', 'pg_hll_sketch_union' 187 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 188 | -------------------------------------------------------------------------------- /sql/datasketches_kll_double_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE kll_double_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION kll_double_sketch_in(cstring) RETURNS kll_double_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION kll_double_sketch_out(kll_double_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE kll_double_sketch ( 29 | INPUT = kll_double_sketch_in, 30 | OUTPUT = kll_double_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as kll_double_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (kll_double_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION kll_double_sketch_build_agg(internal, double precision) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_kll_double_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION kll_double_sketch_build_agg(internal, double precision, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_kll_double_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION kll_double_sketch_merge_agg(internal, kll_double_sketch) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_kll_double_sketch_merge_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION kll_double_sketch_merge_agg(internal, kll_double_sketch, int) RETURNS internal 50 | AS '$libdir/datasketches', 'pg_kll_double_sketch_merge_agg' 51 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION kll_double_sketch_serialize(internal) RETURNS bytea 54 | AS '$libdir/datasketches', 'pg_kll_double_sketch_serialize' 55 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION kll_double_sketch_deserialize(bytea, internal) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_kll_double_sketch_deserialize' 59 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION kll_double_sketch_combine(internal, internal) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_kll_double_sketch_combine' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION kll_double_sketch_finalize(internal) RETURNS kll_double_sketch 66 | AS '$libdir/datasketches', 'pg_kll_double_sketch_serialize' 67 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE AGGREGATE kll_double_sketch_build(double precision) ( 70 | STYPE = internal, 71 | SFUNC = kll_double_sketch_build_agg, 72 | COMBINEFUNC = kll_double_sketch_combine, 73 | SERIALFUNC = kll_double_sketch_serialize, 74 | DESERIALFUNC = kll_double_sketch_deserialize, 75 | FINALFUNC = kll_double_sketch_finalize, 76 | PARALLEL = SAFE 77 | ); 78 | 79 | CREATE OR REPLACE AGGREGATE kll_double_sketch_build(double precision, int) ( 80 | STYPE = internal, 81 | SFUNC = kll_double_sketch_build_agg, 82 | COMBINEFUNC = kll_double_sketch_combine, 83 | SERIALFUNC = kll_double_sketch_serialize, 84 | DESERIALFUNC = kll_double_sketch_deserialize, 85 | FINALFUNC = kll_double_sketch_finalize, 86 | PARALLEL = SAFE 87 | ); 88 | 89 | CREATE OR REPLACE AGGREGATE kll_double_sketch_merge(kll_double_sketch) ( 90 | STYPE = internal, 91 | SFUNC = kll_double_sketch_merge_agg, 92 | COMBINEFUNC = kll_double_sketch_combine, 93 | SERIALFUNC = kll_double_sketch_serialize, 94 | DESERIALFUNC = kll_double_sketch_deserialize, 95 | FINALFUNC = kll_double_sketch_finalize, 96 | PARALLEL = SAFE 97 | ); 98 | 99 | CREATE OR REPLACE AGGREGATE kll_double_sketch_merge(kll_double_sketch, int) ( 100 | STYPE = internal, 101 | SFUNC = kll_double_sketch_merge_agg, 102 | COMBINEFUNC = kll_double_sketch_combine, 103 | SERIALFUNC = kll_double_sketch_serialize, 104 | DESERIALFUNC = kll_double_sketch_deserialize, 105 | FINALFUNC = kll_double_sketch_finalize, 106 | PARALLEL = SAFE 107 | ); 108 | 109 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_rank(kll_double_sketch, double precision) RETURNS double precision 110 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_rank' 111 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 112 | 113 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_quantile(kll_double_sketch, double precision) RETURNS double precision 114 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_quantile' 115 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 116 | 117 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_n(kll_double_sketch) RETURNS bigint 118 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_n' 119 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 120 | 121 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_max_item(kll_double_sketch) RETURNS double precision 122 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_max_item' 123 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 124 | 125 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_min_item(kll_double_sketch) RETURNS double precision 126 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_min_item' 127 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 128 | 129 | CREATE OR REPLACE FUNCTION kll_double_sketch_to_string(kll_double_sketch) RETURNS TEXT 130 | AS '$libdir/datasketches', 'pg_kll_double_sketch_to_string' 131 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 132 | 133 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_pmf(kll_double_sketch, double precision[]) RETURNS double precision[] 134 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_pmf' 135 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 136 | 137 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_cdf(kll_double_sketch, double precision[]) RETURNS double precision[] 138 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_cdf' 139 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 140 | 141 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_quantiles(kll_double_sketch, double precision[]) RETURNS double precision[] 142 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_quantiles' 143 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 144 | 145 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_histogram(kll_double_sketch) RETURNS double precision[] 146 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_histogram' 147 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 148 | 149 | CREATE OR REPLACE FUNCTION kll_double_sketch_get_histogram(kll_double_sketch, int) RETURNS double precision[] 150 | AS '$libdir/datasketches', 'pg_kll_double_sketch_get_histogram' 151 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 152 | -------------------------------------------------------------------------------- /sql/datasketches_kll_float_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE kll_float_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION kll_float_sketch_in(cstring) RETURNS kll_float_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION kll_float_sketch_out(kll_float_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE kll_float_sketch ( 29 | INPUT = kll_float_sketch_in, 30 | OUTPUT = kll_float_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as kll_float_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (kll_float_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION kll_float_sketch_build_agg(internal, real) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_kll_float_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION kll_float_sketch_build_agg(internal, real, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_kll_float_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION kll_float_sketch_merge_agg(internal, kll_float_sketch) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_kll_float_sketch_merge_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION kll_float_sketch_merge_agg(internal, kll_float_sketch, int) RETURNS internal 50 | AS '$libdir/datasketches', 'pg_kll_float_sketch_merge_agg' 51 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION kll_float_sketch_serialize(internal) RETURNS bytea 54 | AS '$libdir/datasketches', 'pg_kll_float_sketch_serialize' 55 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION kll_float_sketch_deserialize(bytea, internal) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_kll_float_sketch_deserialize' 59 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION kll_float_sketch_combine(internal, internal) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_kll_float_sketch_combine' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION kll_float_sketch_finalize(internal) RETURNS kll_float_sketch 66 | AS '$libdir/datasketches', 'pg_kll_float_sketch_serialize' 67 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE AGGREGATE kll_float_sketch_build(real) ( 70 | STYPE = internal, 71 | SFUNC = kll_float_sketch_build_agg, 72 | COMBINEFUNC = kll_float_sketch_combine, 73 | SERIALFUNC = kll_float_sketch_serialize, 74 | DESERIALFUNC = kll_float_sketch_deserialize, 75 | FINALFUNC = kll_float_sketch_finalize, 76 | PARALLEL = SAFE 77 | ); 78 | 79 | CREATE OR REPLACE AGGREGATE kll_float_sketch_build(real, int) ( 80 | STYPE = internal, 81 | SFUNC = kll_float_sketch_build_agg, 82 | COMBINEFUNC = kll_float_sketch_combine, 83 | SERIALFUNC = kll_float_sketch_serialize, 84 | DESERIALFUNC = kll_float_sketch_deserialize, 85 | FINALFUNC = kll_float_sketch_finalize, 86 | PARALLEL = SAFE 87 | ); 88 | 89 | CREATE OR REPLACE AGGREGATE kll_float_sketch_merge(kll_float_sketch) ( 90 | STYPE = internal, 91 | SFUNC = kll_float_sketch_merge_agg, 92 | COMBINEFUNC = kll_float_sketch_combine, 93 | SERIALFUNC = kll_float_sketch_serialize, 94 | DESERIALFUNC = kll_float_sketch_deserialize, 95 | FINALFUNC = kll_float_sketch_finalize, 96 | PARALLEL = SAFE 97 | ); 98 | 99 | CREATE OR REPLACE AGGREGATE kll_float_sketch_merge(kll_float_sketch, int) ( 100 | STYPE = internal, 101 | SFUNC = kll_float_sketch_merge_agg, 102 | COMBINEFUNC = kll_float_sketch_combine, 103 | SERIALFUNC = kll_float_sketch_serialize, 104 | DESERIALFUNC = kll_float_sketch_deserialize, 105 | FINALFUNC = kll_float_sketch_finalize, 106 | PARALLEL = SAFE 107 | ); 108 | 109 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_rank(kll_float_sketch, real) RETURNS double precision 110 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_rank' 111 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 112 | 113 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_quantile(kll_float_sketch, double precision) RETURNS real 114 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_quantile' 115 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 116 | 117 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_n(kll_float_sketch) RETURNS bigint 118 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_n' 119 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 120 | 121 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_max_item(kll_float_sketch) RETURNS real 122 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_max_item' 123 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 124 | 125 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_min_item(kll_float_sketch) RETURNS real 126 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_min_item' 127 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 128 | 129 | CREATE OR REPLACE FUNCTION kll_float_sketch_to_string(kll_float_sketch) RETURNS TEXT 130 | AS '$libdir/datasketches', 'pg_kll_float_sketch_to_string' 131 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 132 | 133 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_pmf(kll_float_sketch, real[]) RETURNS double precision[] 134 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_pmf' 135 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 136 | 137 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_cdf(kll_float_sketch, real[]) RETURNS double precision[] 138 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_cdf' 139 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 140 | 141 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_quantiles(kll_float_sketch, double precision[]) RETURNS real[] 142 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_quantiles' 143 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 144 | 145 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_histogram(kll_float_sketch) RETURNS double precision[] 146 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_histogram' 147 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 148 | 149 | CREATE OR REPLACE FUNCTION kll_float_sketch_get_histogram(kll_float_sketch, int) RETURNS double precision[] 150 | AS '$libdir/datasketches', 'pg_kll_float_sketch_get_histogram' 151 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 152 | -------------------------------------------------------------------------------- /sql/datasketches_quantiles_double_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE quantiles_double_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_in(cstring) RETURNS quantiles_double_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_out(quantiles_double_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE quantiles_double_sketch ( 29 | INPUT = quantiles_double_sketch_in, 30 | OUTPUT = quantiles_double_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as quantiles_double_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (quantiles_double_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_build_agg(internal, double precision) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_build_agg(internal, double precision, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_merge_agg(internal, quantiles_double_sketch) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_merge_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_merge_agg(internal, quantiles_double_sketch, int) RETURNS internal 50 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_merge_agg' 51 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_serialize(internal) RETURNS bytea 54 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_serialize' 55 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_deserialize(bytea, internal) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_deserialize' 59 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_combine(internal, internal) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_combine' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_finalize(internal) RETURNS quantiles_double_sketch 66 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_serialize' 67 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE AGGREGATE quantiles_double_sketch_build(double precision) ( 70 | STYPE = internal, 71 | SFUNC = quantiles_double_sketch_build_agg, 72 | COMBINEFUNC = quantiles_double_sketch_combine, 73 | SERIALFUNC = quantiles_double_sketch_serialize, 74 | DESERIALFUNC = quantiles_double_sketch_deserialize, 75 | FINALFUNC = quantiles_double_sketch_finalize, 76 | PARALLEL = SAFE 77 | ); 78 | 79 | CREATE OR REPLACE AGGREGATE quantiles_double_sketch_build(double precision, int) ( 80 | STYPE = internal, 81 | SFUNC = quantiles_double_sketch_build_agg, 82 | COMBINEFUNC = quantiles_double_sketch_combine, 83 | SERIALFUNC = quantiles_double_sketch_serialize, 84 | DESERIALFUNC = quantiles_double_sketch_deserialize, 85 | FINALFUNC = quantiles_double_sketch_finalize, 86 | PARALLEL = SAFE 87 | ); 88 | 89 | CREATE OR REPLACE AGGREGATE quantiles_double_sketch_merge(quantiles_double_sketch) ( 90 | STYPE = internal, 91 | SFUNC = quantiles_double_sketch_merge_agg, 92 | COMBINEFUNC = quantiles_double_sketch_combine, 93 | SERIALFUNC = quantiles_double_sketch_serialize, 94 | DESERIALFUNC = quantiles_double_sketch_deserialize, 95 | FINALFUNC = quantiles_double_sketch_finalize, 96 | PARALLEL = SAFE 97 | ); 98 | 99 | CREATE OR REPLACE AGGREGATE quantiles_double_sketch_merge(quantiles_double_sketch, int) ( 100 | STYPE = internal, 101 | SFUNC = quantiles_double_sketch_merge_agg, 102 | COMBINEFUNC = quantiles_double_sketch_combine, 103 | SERIALFUNC = quantiles_double_sketch_serialize, 104 | DESERIALFUNC = quantiles_double_sketch_deserialize, 105 | FINALFUNC = quantiles_double_sketch_finalize, 106 | PARALLEL = SAFE 107 | ); 108 | 109 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_rank(quantiles_double_sketch, double precision) RETURNS double precision 110 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_rank' 111 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 112 | 113 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_quantile(quantiles_double_sketch, double precision) RETURNS double precision 114 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_quantile' 115 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 116 | 117 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_n(quantiles_double_sketch) RETURNS bigint 118 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_n' 119 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 120 | 121 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_to_string(quantiles_double_sketch) RETURNS TEXT 122 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_to_string' 123 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 124 | 125 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_pmf(quantiles_double_sketch, double precision[]) RETURNS double precision[] 126 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_pmf' 127 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 128 | 129 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_cdf(quantiles_double_sketch, double precision[]) RETURNS double precision[] 130 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_cdf' 131 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 132 | 133 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_quantiles(quantiles_double_sketch, double precision[]) RETURNS double precision[] 134 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_quantiles' 135 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 136 | 137 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_histogram(quantiles_double_sketch) RETURNS double precision[] 138 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_histogram' 139 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 140 | 141 | CREATE OR REPLACE FUNCTION quantiles_double_sketch_get_histogram(quantiles_double_sketch, int) RETURNS double precision[] 142 | AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_histogram' 143 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 144 | -------------------------------------------------------------------------------- /sql/datasketches_req_float_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE req_float_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION req_float_sketch_in(cstring) RETURNS req_float_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION req_float_sketch_out(req_float_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE req_float_sketch ( 29 | INPUT = req_float_sketch_in, 30 | OUTPUT = req_float_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as req_float_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (req_float_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION req_float_sketch_build_agg(internal, real) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_req_float_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION req_float_sketch_build_agg(internal, real, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_req_float_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION req_float_sketch_build_agg(internal, real, int, boolean) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_req_float_sketch_build_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION req_float_sketch_merge_agg(internal, req_float_sketch) RETURNS internal 50 | AS '$libdir/datasketches', 'pg_req_float_sketch_merge_agg' 51 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION req_float_sketch_merge_agg(internal, req_float_sketch, int) RETURNS internal 54 | AS '$libdir/datasketches', 'pg_req_float_sketch_merge_agg' 55 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION req_float_sketch_merge_agg(internal, req_float_sketch, int, boolean) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_req_float_sketch_merge_agg' 59 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION req_float_sketch_serialize(internal) RETURNS bytea 62 | AS '$libdir/datasketches', 'pg_req_float_sketch_serialize' 63 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION req_float_sketch_deserialize(bytea, internal) RETURNS internal 66 | AS '$libdir/datasketches', 'pg_req_float_sketch_deserialize' 67 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE FUNCTION req_float_sketch_combine(internal, internal) RETURNS internal 70 | AS '$libdir/datasketches', 'pg_req_float_sketch_combine' 71 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 72 | 73 | CREATE OR REPLACE FUNCTION req_float_sketch_finalize(internal) RETURNS req_float_sketch 74 | AS '$libdir/datasketches', 'pg_req_float_sketch_serialize' 75 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 76 | 77 | CREATE OR REPLACE AGGREGATE req_float_sketch_build(real) ( 78 | STYPE = internal, 79 | SFUNC = req_float_sketch_build_agg, 80 | COMBINEFUNC = req_float_sketch_combine, 81 | SERIALFUNC = req_float_sketch_serialize, 82 | DESERIALFUNC = req_float_sketch_deserialize, 83 | FINALFUNC = req_float_sketch_finalize, 84 | PARALLEL = SAFE 85 | ); 86 | 87 | CREATE OR REPLACE AGGREGATE req_float_sketch_build(real, int) ( 88 | STYPE = internal, 89 | SFUNC = req_float_sketch_build_agg, 90 | COMBINEFUNC = req_float_sketch_combine, 91 | SERIALFUNC = req_float_sketch_serialize, 92 | DESERIALFUNC = req_float_sketch_deserialize, 93 | FINALFUNC = req_float_sketch_finalize, 94 | PARALLEL = SAFE 95 | ); 96 | 97 | CREATE OR REPLACE AGGREGATE req_float_sketch_build(real, int, boolean) ( 98 | STYPE = internal, 99 | SFUNC = req_float_sketch_build_agg, 100 | COMBINEFUNC = req_float_sketch_combine, 101 | SERIALFUNC = req_float_sketch_serialize, 102 | DESERIALFUNC = req_float_sketch_deserialize, 103 | FINALFUNC = req_float_sketch_finalize, 104 | PARALLEL = SAFE 105 | ); 106 | 107 | CREATE OR REPLACE AGGREGATE req_float_sketch_merge(req_float_sketch) ( 108 | STYPE = internal, 109 | SFUNC = req_float_sketch_merge_agg, 110 | COMBINEFUNC = req_float_sketch_combine, 111 | SERIALFUNC = req_float_sketch_serialize, 112 | DESERIALFUNC = req_float_sketch_deserialize, 113 | FINALFUNC = req_float_sketch_finalize, 114 | PARALLEL = SAFE 115 | ); 116 | 117 | CREATE OR REPLACE AGGREGATE req_float_sketch_merge(req_float_sketch, int) ( 118 | STYPE = internal, 119 | SFUNC = req_float_sketch_merge_agg, 120 | COMBINEFUNC = req_float_sketch_combine, 121 | SERIALFUNC = req_float_sketch_serialize, 122 | DESERIALFUNC = req_float_sketch_deserialize, 123 | FINALFUNC = req_float_sketch_finalize, 124 | PARALLEL = SAFE 125 | ); 126 | 127 | CREATE OR REPLACE AGGREGATE req_float_sketch_merge(req_float_sketch, int, boolean) ( 128 | STYPE = internal, 129 | SFUNC = req_float_sketch_merge_agg, 130 | COMBINEFUNC = req_float_sketch_combine, 131 | SERIALFUNC = req_float_sketch_serialize, 132 | DESERIALFUNC = req_float_sketch_deserialize, 133 | FINALFUNC = req_float_sketch_finalize, 134 | PARALLEL = SAFE 135 | ); 136 | 137 | CREATE OR REPLACE FUNCTION req_float_sketch_get_rank(req_float_sketch, real) RETURNS double precision 138 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_rank' 139 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 140 | 141 | CREATE OR REPLACE FUNCTION req_float_sketch_get_rank(req_float_sketch, real, boolean) RETURNS double precision 142 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_rank' 143 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 144 | 145 | CREATE OR REPLACE FUNCTION req_float_sketch_get_quantile(req_float_sketch, double precision) RETURNS real 146 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_quantile' 147 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 148 | 149 | CREATE OR REPLACE FUNCTION req_float_sketch_get_quantile(req_float_sketch, double precision, boolean) RETURNS real 150 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_quantile' 151 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 152 | 153 | CREATE OR REPLACE FUNCTION req_float_sketch_get_n(req_float_sketch) RETURNS bigint 154 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_n' 155 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 156 | 157 | CREATE OR REPLACE FUNCTION req_float_sketch_to_string(req_float_sketch) RETURNS TEXT 158 | AS '$libdir/datasketches', 'pg_req_float_sketch_to_string' 159 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 160 | 161 | CREATE OR REPLACE FUNCTION req_float_sketch_get_pmf(req_float_sketch, real[]) RETURNS double precision[] 162 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_pmf' 163 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 164 | 165 | CREATE OR REPLACE FUNCTION req_float_sketch_get_pmf(req_float_sketch, real[], boolean) RETURNS double precision[] 166 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_pmf' 167 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 168 | 169 | CREATE OR REPLACE FUNCTION req_float_sketch_get_cdf(req_float_sketch, real[]) RETURNS double precision[] 170 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_cdf' 171 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 172 | 173 | CREATE OR REPLACE FUNCTION req_float_sketch_get_cdf(req_float_sketch, real[], boolean) RETURNS double precision[] 174 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_cdf' 175 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 176 | 177 | CREATE OR REPLACE FUNCTION req_float_sketch_get_quantiles(req_float_sketch, double precision[]) RETURNS real[] 178 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_quantiles' 179 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 180 | 181 | CREATE OR REPLACE FUNCTION req_float_sketch_get_quantiles(req_float_sketch, double precision[], boolean) RETURNS real[] 182 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_quantiles' 183 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 184 | 185 | CREATE OR REPLACE FUNCTION req_float_sketch_get_histogram(req_float_sketch) RETURNS double precision[] 186 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_histogram' 187 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 188 | 189 | CREATE OR REPLACE FUNCTION req_float_sketch_get_histogram(req_float_sketch, int) RETURNS double precision[] 190 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_histogram' 191 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 192 | 193 | CREATE OR REPLACE FUNCTION req_float_sketch_get_histogram(req_float_sketch, int, boolean) RETURNS double precision[] 194 | AS '$libdir/datasketches', 'pg_req_float_sketch_get_histogram' 195 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 196 | -------------------------------------------------------------------------------- /sql/datasketches_theta_sketch.sql: -------------------------------------------------------------------------------- 1 | -- Licensed to the Apache Software Foundation (ASF) under one 2 | -- or more contributor license agreements. See the NOTICE file 3 | -- distributed with this work for additional information 4 | -- regarding copyright ownership. The ASF licenses this file 5 | -- to you under the Apache License, Version 2.0 (the 6 | -- "License"); you may not use this file except in compliance 7 | -- with the License. You may obtain a copy of the License at 8 | -- 9 | -- http://www.apache.org/licenses/LICENSE-2.0 10 | -- 11 | -- Unless required by applicable law or agreed to in writing, 12 | -- software distributed under the License is distributed on an 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | -- KIND, either express or implied. See the License for the 15 | -- specific language governing permissions and limitations 16 | -- under the License. 17 | 18 | CREATE TYPE theta_sketch; 19 | 20 | CREATE OR REPLACE FUNCTION theta_sketch_in(cstring) RETURNS theta_sketch 21 | AS '$libdir/datasketches', 'pg_sketch_in' 22 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 23 | 24 | CREATE OR REPLACE FUNCTION theta_sketch_out(theta_sketch) RETURNS cstring 25 | AS '$libdir/datasketches', 'pg_sketch_out' 26 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 27 | 28 | CREATE TYPE theta_sketch ( 29 | INPUT = theta_sketch_in, 30 | OUTPUT = theta_sketch_out, 31 | STORAGE = EXTERNAL 32 | ); 33 | 34 | CREATE CAST (bytea as theta_sketch) WITHOUT FUNCTION AS ASSIGNMENT; 35 | CREATE CAST (theta_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT; 36 | 37 | CREATE OR REPLACE FUNCTION theta_sketch_build_agg(internal, anyelement) RETURNS internal 38 | AS '$libdir/datasketches', 'pg_theta_sketch_build_agg' 39 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 40 | 41 | CREATE OR REPLACE FUNCTION theta_sketch_build_agg(internal, anyelement, int) RETURNS internal 42 | AS '$libdir/datasketches', 'pg_theta_sketch_build_agg' 43 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 44 | 45 | CREATE OR REPLACE FUNCTION theta_sketch_build_agg(internal, anyelement, int, real) RETURNS internal 46 | AS '$libdir/datasketches', 'pg_theta_sketch_build_agg' 47 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 48 | 49 | CREATE OR REPLACE FUNCTION theta_sketch_from_internal(internal) RETURNS theta_sketch 50 | AS '$libdir/datasketches', 'pg_theta_sketch_from_internal' 51 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 52 | 53 | CREATE OR REPLACE FUNCTION theta_sketch_get_estimate_from_internal(internal) RETURNS double precision 54 | AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate_from_internal' 55 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 56 | 57 | CREATE OR REPLACE FUNCTION theta_sketch_union_agg(internal, theta_sketch) RETURNS internal 58 | AS '$libdir/datasketches', 'pg_theta_sketch_union_agg' 59 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 60 | 61 | CREATE OR REPLACE FUNCTION theta_sketch_union_agg(internal, theta_sketch, int) RETURNS internal 62 | AS '$libdir/datasketches', 'pg_theta_sketch_union_agg' 63 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 64 | 65 | CREATE OR REPLACE FUNCTION theta_sketch_intersection_agg(internal, theta_sketch) RETURNS internal 66 | AS '$libdir/datasketches', 'pg_theta_sketch_intersection_agg' 67 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 68 | 69 | CREATE OR REPLACE FUNCTION theta_sketch_union_combine(internal, internal) RETURNS internal 70 | AS '$libdir/datasketches', 'pg_theta_sketch_union_combine' 71 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 72 | 73 | CREATE OR REPLACE FUNCTION theta_sketch_intersection_combine(internal, internal) RETURNS internal 74 | AS '$libdir/datasketches', 'pg_theta_sketch_intersection_combine' 75 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 76 | 77 | CREATE OR REPLACE FUNCTION theta_sketch_serialize_state(internal) RETURNS bytea 78 | AS '$libdir/datasketches', 'pg_theta_sketch_serialize_state' 79 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 80 | 81 | CREATE OR REPLACE FUNCTION theta_sketch_deserialize_state(bytea, internal) RETURNS internal 82 | AS '$libdir/datasketches', 'pg_theta_sketch_deserialize_state' 83 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 84 | 85 | CREATE OR REPLACE AGGREGATE theta_sketch_distinct(anyelement) ( 86 | STYPE = internal, 87 | SFUNC = theta_sketch_build_agg, 88 | COMBINEFUNC = theta_sketch_union_combine, 89 | SERIALFUNC = theta_sketch_serialize_state, 90 | DESERIALFUNC = theta_sketch_deserialize_state, 91 | FINALFUNC = theta_sketch_get_estimate_from_internal, 92 | PARALLEL = SAFE 93 | ); 94 | 95 | CREATE OR REPLACE AGGREGATE theta_sketch_distinct(anyelement, int) ( 96 | STYPE = internal, 97 | SFUNC = theta_sketch_build_agg, 98 | COMBINEFUNC = theta_sketch_union_combine, 99 | SERIALFUNC = theta_sketch_serialize_state, 100 | DESERIALFUNC = theta_sketch_deserialize_state, 101 | FINALFUNC = theta_sketch_get_estimate_from_internal, 102 | PARALLEL = SAFE 103 | ); 104 | 105 | CREATE OR REPLACE AGGREGATE theta_sketch_build(anyelement) ( 106 | STYPE = internal, 107 | SFUNC = theta_sketch_build_agg, 108 | COMBINEFUNC = theta_sketch_union_combine, 109 | SERIALFUNC = theta_sketch_serialize_state, 110 | DESERIALFUNC = theta_sketch_deserialize_state, 111 | FINALFUNC = theta_sketch_from_internal, 112 | PARALLEL = SAFE 113 | ); 114 | 115 | CREATE OR REPLACE AGGREGATE theta_sketch_build(anyelement, int) ( 116 | STYPE = internal, 117 | SFUNC = theta_sketch_build_agg, 118 | COMBINEFUNC = theta_sketch_union_combine, 119 | SERIALFUNC = theta_sketch_serialize_state, 120 | DESERIALFUNC = theta_sketch_deserialize_state, 121 | FINALFUNC = theta_sketch_from_internal, 122 | PARALLEL = SAFE 123 | ); 124 | 125 | CREATE OR REPLACE AGGREGATE theta_sketch_build(anyelement, int, real) ( 126 | STYPE = internal, 127 | SFUNC = theta_sketch_build_agg, 128 | COMBINEFUNC = theta_sketch_union_combine, 129 | SERIALFUNC = theta_sketch_serialize_state, 130 | DESERIALFUNC = theta_sketch_deserialize_state, 131 | FINALFUNC = theta_sketch_from_internal, 132 | PARALLEL = SAFE 133 | ); 134 | 135 | CREATE OR REPLACE AGGREGATE theta_sketch_union(theta_sketch) ( 136 | STYPE = internal, 137 | SFUNC = theta_sketch_union_agg, 138 | COMBINEFUNC = theta_sketch_union_combine, 139 | SERIALFUNC = theta_sketch_serialize_state, 140 | DESERIALFUNC = theta_sketch_deserialize_state, 141 | FINALFUNC = theta_sketch_from_internal, 142 | PARALLEL = SAFE 143 | ); 144 | 145 | CREATE OR REPLACE AGGREGATE theta_sketch_union(theta_sketch, int) ( 146 | STYPE = internal, 147 | SFUNC = theta_sketch_union_agg, 148 | COMBINEFUNC = theta_sketch_union_combine, 149 | SERIALFUNC = theta_sketch_serialize_state, 150 | DESERIALFUNC = theta_sketch_deserialize_state, 151 | FINALFUNC = theta_sketch_from_internal, 152 | PARALLEL = SAFE 153 | ); 154 | 155 | CREATE OR REPLACE AGGREGATE theta_sketch_intersection(theta_sketch) ( 156 | STYPE = internal, 157 | SFUNC = theta_sketch_intersection_agg, 158 | COMBINEFUNC = theta_sketch_intersection_combine, 159 | SERIALFUNC = theta_sketch_serialize_state, 160 | DESERIALFUNC = theta_sketch_deserialize_state, 161 | FINALFUNC = theta_sketch_from_internal, 162 | PARALLEL = SAFE 163 | ); 164 | 165 | CREATE OR REPLACE FUNCTION theta_sketch_get_estimate(theta_sketch) RETURNS double precision 166 | AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate' 167 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 168 | 169 | CREATE OR REPLACE FUNCTION theta_sketch_get_estimate_and_bounds(theta_sketch) RETURNS double precision[] 170 | AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate_and_bounds' 171 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 172 | 173 | CREATE OR REPLACE FUNCTION theta_sketch_get_estimate_and_bounds(theta_sketch, int) RETURNS double precision[] 174 | AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate_and_bounds' 175 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 176 | 177 | CREATE OR REPLACE FUNCTION theta_sketch_to_string(theta_sketch) RETURNS TEXT 178 | AS '$libdir/datasketches', 'pg_theta_sketch_to_string' 179 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 180 | 181 | CREATE OR REPLACE FUNCTION theta_sketch_union(theta_sketch, theta_sketch) RETURNS theta_sketch 182 | AS '$libdir/datasketches', 'pg_theta_sketch_union' 183 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 184 | 185 | CREATE OR REPLACE FUNCTION theta_sketch_union(theta_sketch, theta_sketch, int) RETURNS theta_sketch 186 | AS '$libdir/datasketches', 'pg_theta_sketch_union' 187 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 188 | 189 | CREATE OR REPLACE FUNCTION theta_sketch_intersection(theta_sketch, theta_sketch) RETURNS theta_sketch 190 | AS '$libdir/datasketches', 'pg_theta_sketch_intersection' 191 | LANGUAGE C IMMUTABLE PARALLEL SAFE; 192 | 193 | CREATE OR REPLACE FUNCTION theta_sketch_a_not_b(theta_sketch, theta_sketch) RETURNS theta_sketch 194 | AS '$libdir/datasketches', 'pg_theta_sketch_a_not_b' 195 | LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE; 196 | -------------------------------------------------------------------------------- /src/agg_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef AGG_STATE_H 21 | #define AGG_STATE_H 22 | 23 | enum agg_state_type { MUTABLE_SKETCH, IMMUTABLE_SKETCH, UNION, INTERSECTION }; 24 | 25 | struct agg_state { 26 | enum agg_state_type type; 27 | unsigned lg_k; 28 | void* ptr; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/allocator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef PALLOC_ALLOCATOR_H 21 | #define PALLOC_ALLOCATOR_H 22 | 23 | // these extern declarations are to avoid including postgres.h in C++ code 24 | extern "C" { 25 | extern void *palloc(unsigned long long size); 26 | extern void pfree(void *pointer); 27 | } 28 | 29 | #include 30 | #include 31 | 32 | template class palloc_allocator { 33 | public: 34 | typedef T value_type; 35 | typedef value_type* pointer; 36 | typedef const value_type* const_pointer; 37 | typedef value_type& reference; 38 | typedef const value_type& const_reference; 39 | typedef std::size_t size_type; 40 | typedef std::ptrdiff_t difference_type; 41 | 42 | template 43 | struct rebind { typedef palloc_allocator other; }; 44 | 45 | palloc_allocator() = default; 46 | template 47 | palloc_allocator(const palloc_allocator&) {} 48 | 49 | pointer address(reference x) const { return &x; } 50 | const_pointer address(const_reference x) const { 51 | return &x; 52 | } 53 | 54 | pointer allocate(size_type n, const_pointer = 0) { 55 | void* p = palloc(n * sizeof(T)); 56 | if (!p) throw std::bad_alloc(); 57 | return static_cast(p); 58 | } 59 | 60 | void deallocate(pointer p, size_type) { if (p) pfree(p); } 61 | 62 | size_type max_size() const { 63 | return static_cast(-1) / sizeof(T); 64 | } 65 | 66 | template 67 | void construct(pointer p, Args&&... args) { 68 | new(p) value_type(std::forward(args)...); 69 | } 70 | void destroy(pointer p) { p->~value_type(); } 71 | }; 72 | 73 | template<> class palloc_allocator { 74 | public: 75 | typedef void value_type; 76 | typedef void* pointer; 77 | typedef const void* const_pointer; 78 | 79 | template 80 | struct rebind { typedef palloc_allocator other; }; 81 | }; 82 | 83 | 84 | template 85 | inline bool operator==(const palloc_allocator&, const palloc_allocator&) { 86 | return true; 87 | } 88 | 89 | template 90 | inline bool operator!=(const palloc_allocator&, const palloc_allocator&) { 91 | return false; 92 | } 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /src/aod_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef AOD_SKETCH_C_ADAPTER_H 21 | #define AOD_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | void* aod_sketch_new(unsigned num_values); 30 | void* aod_sketch_new_lgk(unsigned num_values, unsigned lg_k); 31 | void* aod_sketch_new_lgk_p(unsigned num_values, unsigned lg_k, float p); 32 | void update_aod_sketch_delete(void* sketchptr); 33 | void compact_aod_sketch_delete(void* sketchptr); 34 | 35 | void aod_sketch_update(void* sketchptr, const void* data, unsigned length, const double* values); 36 | void* aod_sketch_compact(void* sketchptr); 37 | void aod_sketch_union(void* sketchptr1, const void* sketchptr2); 38 | double update_aod_sketch_get_estimate(const void* sketchptr); 39 | double compact_aod_sketch_get_estimate(const void* sketchptr); 40 | void** aod_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs); 41 | char* aod_sketch_to_string(const void* sketchptr, bool print_entries); 42 | 43 | struct ptr_with_size aod_sketch_serialize(const void* sketchptr, unsigned header_size); 44 | void* aod_sketch_deserialize(const char* buffer, unsigned length); 45 | 46 | void* aod_union_new(unsigned num_values); 47 | void* aod_union_new_lgk(unsigned num_values, unsigned lg_k); 48 | void aod_union_delete(void* unionptr); 49 | void aod_union_update(void* unionptr, const void* sketchptr); 50 | void* aod_union_get_result(void* unionptr); 51 | 52 | void* aod_intersection_new(unsigned num_values); 53 | void aod_intersection_delete(void* interptr); 54 | void aod_intersection_update(void* interptr, const void* sketchptr); 55 | void* aod_intersection_get_result(void* interptr); 56 | 57 | void* aod_a_not_b(const void* sketchptr1, const void* sketchptr2); 58 | 59 | void* aod_sketch_to_kll_float_sketch(const void* sketchptr, unsigned column_index, unsigned k); 60 | 61 | void** aod_sketch_students_t_test(const void* sketchptr1, const void* sketchptr2, unsigned* arr_len_out); 62 | void** aod_sketch_to_means(const void* sketchptr, unsigned* arr_len_out); 63 | void** aod_sketch_to_variances(const void* sketchptr, unsigned* arr_len_out); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/base64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include 21 | #include "base64.h" 22 | 23 | static const char bin_to_b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 24 | 25 | static const char b64_to_bin[128] = { 26 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 29 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 30 | 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 31 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 32 | 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 33 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0 34 | }; 35 | 36 | // with full padding 37 | // produces exactly b64_enc_len(srclen) chars 38 | // no \0 termination 39 | void b64_encode(const char* src, unsigned srclen, char* dst) { 40 | unsigned buf = 0; 41 | int pos = 2; 42 | 43 | while (srclen--) { 44 | buf |= (unsigned char)*(src++) << ((pos--) << 3); 45 | 46 | if (pos < 0) { 47 | *dst++ = bin_to_b64[(buf >> 18) & 0x3f]; 48 | *dst++ = bin_to_b64[(buf >> 12) & 0x3f]; 49 | *dst++ = bin_to_b64[(buf >> 6) & 0x3f]; 50 | *dst++ = bin_to_b64[buf & 0x3f]; 51 | pos = 2; 52 | buf = 0; 53 | } 54 | } 55 | if (pos != 2) { 56 | *dst++ = bin_to_b64[(buf >> 18) & 0x3f]; 57 | *dst++ = bin_to_b64[(buf >> 12) & 0x3f]; 58 | *dst++ = (pos == 0) ? bin_to_b64[(buf >> 6) & 0x3f] : '='; 59 | *dst++ = '='; 60 | } 61 | } 62 | 63 | // supports no padding or partial padding (one =) 64 | // ignores invalid chars (fills zeros instead) 65 | // produces exactly b64_dec_len(src, srclen) bytes 66 | void b64_decode(const char* src, unsigned srclen, char* dst) { 67 | unsigned buf = 0; 68 | char c; 69 | int bits = 0; 70 | int pos = 0; 71 | int pad = 0; 72 | 73 | while (srclen--) { 74 | c = *src++; 75 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue; 76 | bits = 0; 77 | if (c != '=') { 78 | if (c > 0 && c < 127) bits = b64_to_bin[(int)c]; 79 | } else { 80 | pad++; 81 | } 82 | buf = (buf << 6) | bits; 83 | pos++; 84 | if (pos == 4) { 85 | *dst++ = (buf >> 16) & 0xff; 86 | if (pad < 2) *dst++ = (buf >> 8) & 0xff; 87 | if (pad == 0) *dst++ = buf & 0xff; 88 | buf = 0; 89 | pos = 0; 90 | } 91 | } 92 | // no padding or partial padding. pos must be 2 or 3 93 | if (pos == 2) { 94 | *dst++ = (buf >> 4) & 0xff; 95 | } else if (pos == 3) { 96 | *dst++ = (buf >> 10) & 0xff; 97 | if (pad == 0) *dst++ = (buf >> 2) & 0xff; 98 | } 99 | } 100 | 101 | // with padding 102 | unsigned b64_enc_len(unsigned srclen) { 103 | return ((srclen + 2) / 3) * 4; 104 | } 105 | 106 | unsigned b64_dec_len(const char* src, unsigned srclen) { 107 | unsigned pad = 0; 108 | if (srclen > 0 && src[srclen - 1] == '=') pad++; 109 | if (srclen > 1 && src[srclen - 2] == '=') pad++; 110 | return ((srclen * 3) >> 2) - pad; 111 | } 112 | -------------------------------------------------------------------------------- /src/base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef _BASE64_H_ 21 | #define _BASE64_H_ 22 | 23 | void b64_encode(const char *src, unsigned srclen, char *dst); 24 | void b64_decode(const char *src, unsigned srclen, char *dst); 25 | unsigned b64_enc_len(unsigned srclen); 26 | unsigned b64_dec_len(const char* src, unsigned srclen); 27 | 28 | #endif // _BASE64_H_ 29 | -------------------------------------------------------------------------------- /src/common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "base64.h" 24 | 25 | // Since version 16 of PG, all functionality for variable-length 26 | // data was moved from postgres.h into the new file varatt.h 27 | #if PG_VERSION_NUM >= 160000 28 | #include "varatt.h" 29 | #endif 30 | 31 | PG_MODULE_MAGIC; 32 | 33 | PG_FUNCTION_INFO_V1(pg_sketch_in); 34 | PG_FUNCTION_INFO_V1(pg_sketch_out); 35 | 36 | Datum pg_sketch_in(PG_FUNCTION_ARGS); 37 | Datum pg_sketch_out(PG_FUNCTION_ARGS); 38 | 39 | void pg_error(const char* message); 40 | Datum pg_float4_get_datum(float x); 41 | Datum pg_float8_get_datum(double x); 42 | 43 | // cstring to type 44 | Datum pg_sketch_in(PG_FUNCTION_ARGS) { 45 | // not invoked for nulls 46 | bytea* decoded; 47 | char* encoded = PG_GETARG_CSTRING(0); 48 | const unsigned encoded_length = strlen(encoded); 49 | const unsigned decoded_length = b64_dec_len(encoded, encoded_length); 50 | decoded = palloc(VARHDRSZ + decoded_length); 51 | b64_decode(encoded, encoded_length, VARDATA(decoded)); 52 | SET_VARSIZE(decoded, VARHDRSZ + decoded_length); 53 | PG_RETURN_BYTEA_P(decoded); 54 | } 55 | 56 | // type to cstring 57 | Datum pg_sketch_out(PG_FUNCTION_ARGS) { 58 | // not invoked for nulls 59 | char* encoded; 60 | bytea* bytes = PG_GETARG_BYTEA_P(0); 61 | const unsigned encoded_length = b64_enc_len(VARSIZE(bytes) - VARHDRSZ); 62 | encoded = palloc(encoded_length + 1); 63 | b64_encode(VARDATA(bytes), VARSIZE(bytes) - VARHDRSZ, encoded); 64 | encoded[encoded_length] = '\0'; 65 | PG_RETURN_CSTRING(encoded); 66 | } 67 | 68 | // These are implementations of redirects defined in postgres_h_substitute.h 69 | 70 | Datum pg_float4_get_datum(float x) { 71 | return Float4GetDatum(x); 72 | } 73 | 74 | Datum pg_float8_get_datum(double x) { 75 | return Float8GetDatum(x); 76 | } 77 | 78 | void pg_error(const char* message) { 79 | ereport( 80 | ERROR, 81 | ( 82 | errcode(ERRCODE_INTERNAL_ERROR), 83 | errmsg("%s", message) 84 | ) 85 | ); 86 | } 87 | -------------------------------------------------------------------------------- /src/cpc_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "cpc_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | #include 26 | 27 | using cpc_sketch_pg = datasketches::cpc_sketch_alloc>; 28 | using cpc_union_pg = datasketches::cpc_union_alloc>; 29 | 30 | void cpc_init() { 31 | datasketches::cpc_init>(); 32 | } 33 | 34 | void cpc_cleanup() { 35 | } 36 | 37 | void* cpc_sketch_new(unsigned lg_k) { 38 | try { 39 | return new (palloc(sizeof(cpc_sketch_pg))) cpc_sketch_pg(lg_k, datasketches::DEFAULT_SEED); 40 | } catch (std::exception& e) { 41 | pg_error(e.what()); 42 | } 43 | pg_unreachable(); 44 | } 45 | 46 | void cpc_sketch_delete(void* sketchptr) { 47 | try { 48 | static_cast(sketchptr)->~cpc_sketch_pg(); 49 | pfree(sketchptr); 50 | } catch (std::exception& e) { 51 | pg_error(e.what()); 52 | } 53 | } 54 | 55 | void cpc_sketch_update(void* sketchptr, const void* data, unsigned length) { 56 | try { 57 | static_cast(sketchptr)->update(data, length); 58 | } catch (std::exception& e) { 59 | pg_error(e.what()); 60 | } 61 | } 62 | 63 | double cpc_sketch_get_estimate(const void* sketchptr) { 64 | try { 65 | return static_cast(sketchptr)->get_estimate(); 66 | } catch (std::exception& e) { 67 | pg_error(e.what()); 68 | } 69 | pg_unreachable(); 70 | } 71 | 72 | Datum* cpc_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs) { 73 | try { 74 | Datum* est_and_bounds = (Datum*) palloc(sizeof(Datum) * 3); 75 | est_and_bounds[0] = pg_float8_get_datum(static_cast(sketchptr)->get_estimate()); 76 | est_and_bounds[1] = pg_float8_get_datum(static_cast(sketchptr)->get_lower_bound(num_std_devs)); 77 | est_and_bounds[2] = pg_float8_get_datum(static_cast(sketchptr)->get_upper_bound(num_std_devs)); 78 | return est_and_bounds; 79 | } catch (std::exception& e) { 80 | pg_error(e.what()); 81 | } 82 | pg_unreachable(); 83 | } 84 | 85 | char* cpc_sketch_to_string(const void* sketchptr) { 86 | try { 87 | auto str = static_cast(sketchptr)->to_string(); 88 | const size_t len = str.length() + 1; 89 | char* buffer = (char*) palloc(len); 90 | strncpy(buffer, str.c_str(), len); 91 | return buffer; 92 | } catch (std::exception& e) { 93 | pg_error(e.what()); 94 | } 95 | pg_unreachable(); 96 | } 97 | 98 | struct ptr_with_size cpc_sketch_serialize(const void* sketchptr, unsigned header_size) { 99 | try { 100 | ptr_with_size p; 101 | auto bytes = new (palloc(sizeof(cpc_sketch_pg::vector_bytes))) cpc_sketch_pg::vector_bytes( 102 | static_cast(sketchptr)->serialize(header_size) 103 | ); 104 | p.ptr = bytes->data(); 105 | p.size = bytes->size(); 106 | return p; 107 | } catch (std::exception& e) { 108 | pg_error(e.what()); 109 | } 110 | pg_unreachable(); 111 | } 112 | 113 | void* cpc_sketch_deserialize(const char* buffer, unsigned length) { 114 | try { 115 | return new (palloc(sizeof(cpc_sketch_pg))) cpc_sketch_pg(cpc_sketch_pg::deserialize(buffer, length, datasketches::DEFAULT_SEED)); 116 | } catch (std::exception& e) { 117 | pg_error(e.what()); 118 | } 119 | pg_unreachable(); 120 | } 121 | 122 | void* cpc_union_new(unsigned lg_k) { 123 | try { 124 | return new (palloc(sizeof(cpc_union_pg))) cpc_union_pg(lg_k, datasketches::DEFAULT_SEED); 125 | } catch (std::exception& e) { 126 | pg_error(e.what()); 127 | } 128 | pg_unreachable(); 129 | } 130 | 131 | void cpc_union_delete(void* unionptr) { 132 | try { 133 | static_cast(unionptr)->~cpc_union_pg(); 134 | pfree(unionptr); 135 | } catch (std::exception& e) { 136 | pg_error(e.what()); 137 | } 138 | } 139 | 140 | void cpc_union_update(void* unionptr, const void* sketchptr) { 141 | try { 142 | static_cast(unionptr)->update(*static_cast(sketchptr)); 143 | } catch (std::exception& e) { 144 | pg_error(e.what()); 145 | } 146 | } 147 | 148 | void* cpc_union_get_result(void* unionptr) { 149 | try { 150 | auto sketchptr = new (palloc(sizeof(cpc_sketch_pg))) cpc_sketch_pg(static_cast(unionptr)->get_result()); 151 | static_cast(unionptr)->~cpc_union_pg(); 152 | pfree(unionptr); 153 | return sketchptr; 154 | } catch (std::exception& e) { 155 | pg_error(e.what()); 156 | } 157 | pg_unreachable(); 158 | } 159 | -------------------------------------------------------------------------------- /src/cpc_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef CPC_SKETCH_C_ADAPTER_H 21 | #define CPC_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | void cpc_init(); 30 | void cpc_cleanup(); 31 | 32 | void* cpc_sketch_new(unsigned lg_k); 33 | void cpc_sketch_delete(void* sketchptr); 34 | 35 | void cpc_sketch_update(void* sketchptr, const void* data, unsigned length); 36 | void cpc_sketch_merge(void* sketchptr1, const void* sketchptr2); 37 | double cpc_sketch_get_estimate(const void* sketchptr); 38 | void** cpc_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs); 39 | char* cpc_sketch_to_string(const void* sketchptr); 40 | 41 | struct ptr_with_size cpc_sketch_serialize(const void* sketchptr, unsigned header_size); 42 | void* cpc_sketch_deserialize(const char* buffer, unsigned length); 43 | 44 | void* cpc_union_new(unsigned lg_k); 45 | void cpc_union_delete(void* unionptr); 46 | void cpc_union_update(void* unionptr, const void* sketchptr); 47 | void* cpc_union_get_result(void* unionptr); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/frequent_strings_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "frequent_strings_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | 26 | #include 27 | 28 | typedef std::basic_string, palloc_allocator> string; 29 | 30 | // delegate to std::hash 31 | struct hash_string { 32 | std::size_t operator()(const string& s) const { 33 | return std::hash()(s.c_str()); 34 | } 35 | }; 36 | 37 | static inline void pg_check_memory_size(size_t requested_index, size_t capacity) { 38 | if (requested_index > capacity) { 39 | std::string msg("Attempt to access memory beyond limits: requested index " 40 | + std::to_string(requested_index) + ", capacity " + std::to_string(capacity)); 41 | pg_error(msg.c_str()); 42 | } 43 | } 44 | 45 | struct serde_string { 46 | size_t size_of_item(const string& item) const { 47 | return sizeof(uint32_t) + item.size(); 48 | } 49 | 50 | size_t serialize(void* ptr, size_t capacity, const string* items, unsigned num) const { 51 | size_t size = sizeof(uint32_t) * num; 52 | for (unsigned i = 0; i < num; i++) { 53 | const uint32_t length = items[i].size(); 54 | memcpy(ptr, &length, sizeof(length)); 55 | ptr = static_cast(ptr) + sizeof(uint32_t); 56 | memcpy(ptr, items[i].c_str(), length); 57 | ptr = static_cast(ptr) + length; 58 | size += length; 59 | } 60 | return size; 61 | } 62 | 63 | size_t deserialize(const void* ptr, size_t capacity, string* items, unsigned num) const { 64 | size_t bytes_read = 0; 65 | for (unsigned i = 0; i < num; i++) { 66 | uint32_t length; 67 | pg_check_memory_size(bytes_read + sizeof(length), capacity); 68 | memcpy(&length, ptr, sizeof(length)); 69 | ptr = static_cast(ptr) + sizeof(uint32_t); 70 | bytes_read += sizeof(length); 71 | pg_check_memory_size(bytes_read + length, capacity); 72 | new (&items[i]) string(static_cast(ptr), length); 73 | ptr = static_cast(ptr) + length; 74 | bytes_read += length; 75 | } 76 | return bytes_read; 77 | } 78 | }; 79 | 80 | using frequent_strings_sketch = datasketches::frequent_items_sketch, palloc_allocator>; 81 | 82 | void* frequent_strings_sketch_new(unsigned lg_k) { 83 | try { 84 | return new (palloc(sizeof(frequent_strings_sketch))) frequent_strings_sketch(lg_k); 85 | } catch (std::exception& e) { 86 | pg_error(e.what()); 87 | } 88 | pg_unreachable(); 89 | } 90 | 91 | void frequent_strings_sketch_delete(void* sketchptr) { 92 | try { 93 | static_cast(sketchptr)->~frequent_strings_sketch(); 94 | pfree(sketchptr); 95 | } catch (std::exception& e) { 96 | pg_error(e.what()); 97 | } 98 | } 99 | 100 | void frequent_strings_sketch_update(void* sketchptr, const char* str, unsigned length, unsigned long long weight) { 101 | try { 102 | static_cast(sketchptr)->update(string(str, length), weight); 103 | } catch (std::exception& e) { 104 | pg_error(e.what()); 105 | } 106 | } 107 | 108 | void frequent_strings_sketch_merge(void* sketchptr1, const void* sketchptr2) { 109 | try { 110 | static_cast(sketchptr1)->merge(*static_cast(sketchptr2)); 111 | } catch (std::exception& e) { 112 | pg_error(e.what()); 113 | } 114 | } 115 | 116 | char* frequent_strings_sketch_to_string(const void* sketchptr, bool print_items) { 117 | try { 118 | auto str = static_cast(sketchptr)->to_string(print_items); 119 | const size_t len = str.length() + 1; 120 | char* buffer = (char*) palloc(len); 121 | strncpy(buffer, str.c_str(), len); 122 | return buffer; 123 | } catch (std::exception& e) { 124 | pg_error(e.what()); 125 | } 126 | pg_unreachable(); 127 | } 128 | 129 | ptr_with_size frequent_strings_sketch_serialize(const void* sketchptr, unsigned header_size) { 130 | try { 131 | ptr_with_size p; 132 | auto bytes = new (palloc(sizeof(frequent_strings_sketch::vector_bytes))) frequent_strings_sketch::vector_bytes( 133 | static_cast(sketchptr)->serialize(header_size, serde_string()) 134 | ); 135 | p.ptr = bytes->data(); 136 | p.size = bytes->size(); 137 | return p; 138 | } catch (std::exception& e) { 139 | pg_error(e.what()); 140 | } 141 | pg_unreachable(); 142 | } 143 | 144 | void* frequent_strings_sketch_deserialize(const char* buffer, unsigned length) { 145 | try { 146 | frequent_strings_sketch* sketchptr = new (palloc(sizeof(frequent_strings_sketch))) 147 | frequent_strings_sketch(frequent_strings_sketch::deserialize(buffer, length, serde_string())); 148 | return sketchptr; 149 | } catch (std::exception& e) { 150 | pg_error(e.what()); 151 | } 152 | pg_unreachable(); 153 | } 154 | 155 | unsigned frequent_strings_sketch_get_serialized_size_bytes(const void* sketchptr) { 156 | try { 157 | return static_cast(sketchptr)->get_serialized_size_bytes(serde_string()); 158 | } catch (std::exception& e) { 159 | pg_error(e.what()); 160 | } 161 | pg_unreachable(); 162 | } 163 | 164 | frequent_strings_sketch_result* frequent_strings_sketch_get_frequent_items(void* sketchptr, bool no_false_positives, unsigned long long threshold) { 165 | try { 166 | auto data = static_cast(sketchptr)->get_frequent_items( 167 | no_false_positives ? datasketches::frequent_items_error_type::NO_FALSE_POSITIVES : datasketches::frequent_items_error_type::NO_FALSE_NEGATIVES, 168 | threshold 169 | ); 170 | auto rows = (frequent_strings_sketch_result_row*) palloc(sizeof(frequent_strings_sketch_result_row) * data.size()); 171 | unsigned i = 0; 172 | for (auto& it: data) { 173 | const string& str = it.get_item(); 174 | rows[i].str = (char*) palloc(str.length() + 1); 175 | strncpy(rows[i].str, str.c_str(), str.length() + 1); 176 | rows[i].estimate = it.get_estimate(); 177 | rows[i].lower_bound = it.get_lower_bound(); 178 | rows[i].upper_bound = it.get_upper_bound(); 179 | ++i; 180 | } 181 | auto result = (frequent_strings_sketch_result*) palloc(sizeof(frequent_strings_sketch_result)); 182 | result->rows = rows; 183 | result->num = data.size(); 184 | return result; 185 | } catch (std::exception& e) { 186 | pg_error(e.what()); 187 | } 188 | pg_unreachable(); 189 | } 190 | -------------------------------------------------------------------------------- /src/frequent_strings_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef FREQUENT_STRINGS_SKETCH_C_ADAPTER_H 21 | #define FREQUENT_STRINGS_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | void* frequent_strings_sketch_new(unsigned lg_k); 30 | void frequent_strings_sketch_delete(void* sketchptr); 31 | 32 | void frequent_strings_sketch_update(void* sketchptr, const char* str, unsigned length, unsigned long long weight); 33 | void frequent_strings_sketch_merge(void* sketchptr1, const void* sketchptr2); 34 | char* frequent_strings_sketch_to_string(const void* sketchptr, bool print_items); 35 | 36 | struct ptr_with_size frequent_strings_sketch_serialize(const void* sketchptr, unsigned header_size); 37 | void* frequent_strings_sketch_deserialize(const char* buffer, unsigned length); 38 | unsigned frequent_strings_sketch_get_serialized_size_bytes(const void* sketchptr); 39 | 40 | struct frequent_strings_sketch_result_row { 41 | char* str; 42 | unsigned long long estimate; 43 | unsigned long long lower_bound; 44 | unsigned long long upper_bound; 45 | }; 46 | 47 | struct frequent_strings_sketch_result { 48 | struct frequent_strings_sketch_result_row* rows; 49 | unsigned num; 50 | }; 51 | 52 | struct frequent_strings_sketch_result* frequent_strings_sketch_get_frequent_items(void* sketchptr, bool no_false_positives, unsigned long long threshold); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/frequent_strings_sketch_pg_functions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "frequent_strings_sketch_c_adapter.h" 29 | 30 | /* PG_FUNCTION_INFO_V1 macro to pass functions to postgres */ 31 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_build_agg); 32 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_merge_agg); 33 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_serialize); 34 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_deserialize); 35 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_combine); 36 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_to_string); 37 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_result_no_false_positives); 38 | PG_FUNCTION_INFO_V1(pg_frequent_strings_sketch_result_no_false_negatives); 39 | 40 | /* function declarations */ 41 | Datum pg_frequent_strings_sketch_build_agg(PG_FUNCTION_ARGS); 42 | Datum pg_frequent_strings_sketch_merge_agg(PG_FUNCTION_ARGS); 43 | Datum pg_frequent_strings_sketch_serialize(PG_FUNCTION_ARGS); 44 | Datum pg_frequent_strings_sketch_deserialize(PG_FUNCTION_ARGS); 45 | Datum pg_frequent_strings_sketch_combine(PG_FUNCTION_ARGS); 46 | Datum pg_frequent_strings_sketch_to_string(PG_FUNCTION_ARGS); 47 | Datum pg_frequent_strings_sketch_result_no_false_positives(PG_FUNCTION_ARGS); 48 | Datum pg_frequent_strings_sketch_result_no_false_negatives(PG_FUNCTION_ARGS); 49 | 50 | Datum frequent_strings_sketch_get_result(PG_FUNCTION_ARGS, bool); 51 | 52 | 53 | Datum pg_frequent_strings_sketch_build_agg(PG_FUNCTION_ARGS) { 54 | void* sketchptr; 55 | unsigned lg_k; 56 | const VarChar* str; 57 | unsigned long long weight; 58 | 59 | MemoryContext oldcontext; 60 | MemoryContext aggcontext; 61 | 62 | if (PG_ARGISNULL(0) && PG_ARGISNULL(2)) { 63 | PG_RETURN_NULL(); 64 | } else if (PG_ARGISNULL(2)) { 65 | PG_RETURN_POINTER(PG_GETARG_POINTER(0)); // no update value. return unmodified state 66 | } 67 | 68 | if (!AggCheckCallContext(fcinfo, &aggcontext)) { 69 | elog(ERROR, "frequent_strings_sketch_build_agg called in non-aggregate context"); 70 | } 71 | oldcontext = MemoryContextSwitchTo(aggcontext); 72 | 73 | if (PG_ARGISNULL(0)) { 74 | lg_k = PG_GETARG_INT32(1); 75 | sketchptr = frequent_strings_sketch_new(lg_k); 76 | } else { 77 | sketchptr = PG_GETARG_POINTER(0); 78 | } 79 | 80 | str = PG_GETARG_VARCHAR_P(2); 81 | 82 | // optional weight 83 | weight = PG_NARGS() > 3 ? PG_GETARG_INT64(3) : 1; 84 | 85 | frequent_strings_sketch_update(sketchptr, VARDATA(str), VARSIZE(str) - VARHDRSZ, weight); 86 | 87 | MemoryContextSwitchTo(oldcontext); 88 | 89 | PG_RETURN_POINTER(sketchptr); 90 | } 91 | 92 | Datum pg_frequent_strings_sketch_merge_agg(PG_FUNCTION_ARGS) { 93 | void* unionptr; 94 | bytea* sketch_bytes; 95 | void* sketchptr; 96 | unsigned lg_k; 97 | 98 | MemoryContext oldcontext; 99 | MemoryContext aggcontext; 100 | 101 | if (PG_ARGISNULL(0) && PG_ARGISNULL(2)) { 102 | PG_RETURN_NULL(); 103 | } else if (PG_ARGISNULL(2)) { 104 | PG_RETURN_POINTER(PG_GETARG_POINTER(0)); // no update value. return unmodified state 105 | } 106 | 107 | if (!AggCheckCallContext(fcinfo, &aggcontext)) { 108 | elog(ERROR, "frequent_strings_sketch_merge_agg called in non-aggregate context"); 109 | } 110 | oldcontext = MemoryContextSwitchTo(aggcontext); 111 | 112 | if (PG_ARGISNULL(0)) { 113 | lg_k = PG_GETARG_INT32(1); 114 | unionptr = frequent_strings_sketch_new(lg_k); 115 | } else { 116 | unionptr = PG_GETARG_POINTER(0); 117 | } 118 | 119 | sketch_bytes = PG_GETARG_BYTEA_P(2); 120 | sketchptr = frequent_strings_sketch_deserialize(VARDATA(sketch_bytes), VARSIZE(sketch_bytes) - VARHDRSZ); 121 | frequent_strings_sketch_merge(unionptr, sketchptr); 122 | frequent_strings_sketch_delete(sketchptr); 123 | 124 | MemoryContextSwitchTo(oldcontext); 125 | 126 | PG_RETURN_POINTER(unionptr); 127 | } 128 | 129 | Datum pg_frequent_strings_sketch_serialize(PG_FUNCTION_ARGS) { 130 | void* sketchptr; 131 | struct ptr_with_size bytes_out; 132 | MemoryContext aggcontext; 133 | if (PG_ARGISNULL(0)) PG_RETURN_NULL(); 134 | if (!AggCheckCallContext(fcinfo, &aggcontext)) { 135 | elog(ERROR, "frequent_strings_sketch_serialize called in non-aggregate context"); 136 | } 137 | sketchptr = PG_GETARG_POINTER(0); 138 | bytes_out = frequent_strings_sketch_serialize(sketchptr, VARHDRSZ); 139 | frequent_strings_sketch_delete(sketchptr); 140 | SET_VARSIZE(bytes_out.ptr, bytes_out.size); 141 | PG_RETURN_BYTEA_P(bytes_out.ptr); 142 | } 143 | 144 | Datum pg_frequent_strings_sketch_deserialize(PG_FUNCTION_ARGS) { 145 | const bytea* bytes_in; 146 | void* sketchptr; 147 | 148 | MemoryContext oldcontext; 149 | MemoryContext aggcontext; 150 | 151 | if (PG_ARGISNULL(0)) PG_RETURN_NULL(); 152 | 153 | if (!AggCheckCallContext(fcinfo, &aggcontext)) { 154 | elog(ERROR, "frequent_strings_sketch_deserialize called in non-aggregate context"); 155 | } 156 | oldcontext = MemoryContextSwitchTo(aggcontext); 157 | 158 | bytes_in = PG_GETARG_BYTEA_P(0); 159 | sketchptr = frequent_strings_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ); 160 | 161 | MemoryContextSwitchTo(oldcontext); 162 | 163 | PG_RETURN_POINTER(sketchptr); 164 | } 165 | 166 | Datum pg_frequent_strings_sketch_combine(PG_FUNCTION_ARGS) { 167 | void* sketchptr1; 168 | void* sketchptr2; 169 | void* sketchptr; 170 | 171 | MemoryContext oldcontext; 172 | MemoryContext aggcontext; 173 | 174 | if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) PG_RETURN_NULL(); 175 | 176 | if (!AggCheckCallContext(fcinfo, &aggcontext)) { 177 | elog(ERROR, "frequent_strings_sketch_combine called in non-aggregate context"); 178 | } 179 | oldcontext = MemoryContextSwitchTo(aggcontext); 180 | 181 | sketchptr1 = PG_GETARG_POINTER(0); 182 | sketchptr2 = PG_GETARG_POINTER(1); 183 | 184 | if (sketchptr1) { 185 | sketchptr = sketchptr1; 186 | if (sketchptr2) { 187 | frequent_strings_sketch_merge(sketchptr, sketchptr2); 188 | } 189 | frequent_strings_sketch_delete(sketchptr2); 190 | } else { 191 | sketchptr = sketchptr2; 192 | } 193 | 194 | MemoryContextSwitchTo(oldcontext); 195 | 196 | PG_RETURN_POINTER(sketchptr); 197 | } 198 | 199 | Datum pg_frequent_strings_sketch_to_string(PG_FUNCTION_ARGS) { 200 | const bytea* bytes_in; 201 | void* sketchptr; 202 | bool print_items; 203 | char* str; 204 | bytes_in = PG_GETARG_BYTEA_P(0); 205 | print_items = PG_NARGS() > 1 ? PG_GETARG_BOOL(1) : false; 206 | sketchptr = frequent_strings_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ); 207 | str = frequent_strings_sketch_to_string(sketchptr, print_items); 208 | frequent_strings_sketch_delete(sketchptr); 209 | PG_RETURN_TEXT_P(cstring_to_text(str)); 210 | } 211 | 212 | Datum frequent_strings_sketch_get_result(PG_FUNCTION_ARGS, bool no_false_positives) { 213 | FuncCallContext* funcctx; 214 | TupleDesc tupdesc; 215 | const bytea* bytes_in; 216 | void* sketchptr; 217 | unsigned long long threshold; 218 | 219 | if (SRF_IS_FIRSTCALL()) { 220 | MemoryContext oldcontext; 221 | funcctx = SRF_FIRSTCALL_INIT(); 222 | oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); 223 | if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) { 224 | ereport( 225 | ERROR, 226 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context that cannot accept type record")) 227 | ); 228 | } 229 | 230 | if (PG_ARGISNULL(0)) SRF_RETURN_DONE(funcctx); 231 | bytes_in = PG_GETARG_BYTEA_P(0); 232 | sketchptr = frequent_strings_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ); 233 | threshold = PG_NARGS() > 1 ? PG_GETARG_INT64(1) : 0; 234 | 235 | funcctx->user_fctx = frequent_strings_sketch_get_frequent_items(sketchptr, no_false_positives, threshold); 236 | funcctx->max_calls = ((struct frequent_strings_sketch_result*) funcctx->user_fctx)->num; 237 | funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc); 238 | 239 | frequent_strings_sketch_delete(sketchptr); 240 | 241 | MemoryContextSwitchTo(oldcontext); 242 | } 243 | 244 | funcctx = SRF_PERCALL_SETUP(); 245 | 246 | if (funcctx->call_cntr < funcctx->max_calls) { 247 | char **values; 248 | HeapTuple tuple; 249 | Datum result; 250 | 251 | struct frequent_strings_sketch_result* frequent_strings = funcctx->user_fctx; 252 | struct frequent_strings_sketch_result_row* row = &frequent_strings->rows[funcctx->call_cntr]; 253 | 254 | values = (char**) palloc(4 * sizeof(char*)); 255 | values[0] = row->str; 256 | values[1] = (char*) palloc(21); 257 | values[2] = (char*) palloc(21); 258 | values[3] = (char*) palloc(21); 259 | 260 | snprintf(values[1], 21, "%llu", row->estimate); 261 | snprintf(values[2], 21, "%llu", row->lower_bound); 262 | snprintf(values[3], 21, "%llu", row->upper_bound); 263 | 264 | tuple = BuildTupleFromCStrings(funcctx->attinmeta, values); 265 | 266 | result = HeapTupleGetDatum(tuple); 267 | 268 | pfree(values[1]); 269 | pfree(values[2]); 270 | pfree(values[3]); 271 | pfree(values); 272 | 273 | SRF_RETURN_NEXT(funcctx, result); 274 | } else { 275 | SRF_RETURN_DONE(funcctx); 276 | } 277 | } 278 | 279 | Datum pg_frequent_strings_sketch_result_no_false_positives(PG_FUNCTION_ARGS) { 280 | return frequent_strings_sketch_get_result(fcinfo, true); 281 | } 282 | 283 | Datum pg_frequent_strings_sketch_result_no_false_negatives(PG_FUNCTION_ARGS) { 284 | return frequent_strings_sketch_get_result(fcinfo, false); 285 | } 286 | -------------------------------------------------------------------------------- /src/global_hooks.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | // PostgreSQL hooks to execute on loading and unloading 21 | // CPC sketch needs global initialization of compression tables 22 | 23 | #include "cpc_sketch_c_adapter.h" 24 | 25 | void _PG_init(void); 26 | void _PG_fini(void); 27 | 28 | void _PG_init() { 29 | cpc_init(); 30 | } 31 | 32 | void _PG_fini() { 33 | cpc_cleanup(); 34 | } 35 | -------------------------------------------------------------------------------- /src/hll_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "hll_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | 26 | using hll_sketch_pg = datasketches::hll_sketch_alloc>; 27 | using hll_union_pg = datasketches::hll_union_alloc>; 28 | 29 | void* hll_sketch_new(unsigned lg_k) { 30 | try { 31 | return new (palloc(sizeof(hll_sketch_pg))) hll_sketch_pg(lg_k); 32 | } catch (std::exception& e) { 33 | pg_error(e.what()); 34 | } 35 | pg_unreachable(); 36 | } 37 | 38 | void* hll_sketch_new_tgt_type(unsigned lg_k, unsigned tgt_type) { 39 | try { 40 | return new (palloc(sizeof(hll_sketch_pg))) hll_sketch_pg( 41 | lg_k, 42 | tgt_type == 4 ? datasketches::target_hll_type::HLL_4 : tgt_type == 6 ? datasketches::target_hll_type::HLL_6 : datasketches::target_hll_type::HLL_8 43 | ); 44 | } catch (std::exception& e) { 45 | pg_error(e.what()); 46 | } 47 | pg_unreachable(); 48 | } 49 | 50 | void hll_sketch_delete(void* sketchptr) { 51 | try { 52 | static_cast(sketchptr)->~hll_sketch_pg(); 53 | pfree(sketchptr); 54 | } catch (std::exception& e) { 55 | pg_error(e.what()); 56 | } 57 | } 58 | 59 | void hll_sketch_update(void* sketchptr, const void* data, unsigned length) { 60 | try { 61 | static_cast(sketchptr)->update(data, length); 62 | } catch (std::exception& e) { 63 | pg_error(e.what()); 64 | } 65 | } 66 | 67 | double hll_sketch_get_estimate(const void* sketchptr) { 68 | try { 69 | return static_cast(sketchptr)->get_estimate(); 70 | } catch (std::exception& e) { 71 | pg_error(e.what()); 72 | } 73 | pg_unreachable(); 74 | } 75 | 76 | Datum* hll_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs) { 77 | try { 78 | Datum* est_and_bounds = (Datum*) palloc(sizeof(Datum) * 3); 79 | est_and_bounds[0] = pg_float8_get_datum(static_cast(sketchptr)->get_estimate()); 80 | est_and_bounds[1] = pg_float8_get_datum(static_cast(sketchptr)->get_lower_bound(num_std_devs)); 81 | est_and_bounds[2] = pg_float8_get_datum(static_cast(sketchptr)->get_upper_bound(num_std_devs)); 82 | return est_and_bounds; 83 | } catch (std::exception& e) { 84 | pg_error(e.what()); 85 | } 86 | pg_unreachable(); 87 | } 88 | 89 | char* hll_sketch_to_string(const void* sketchptr) { 90 | try { 91 | auto str = static_cast(sketchptr)->to_string(); 92 | const size_t len = str.length() + 1; 93 | char* buffer = (char*) palloc(len); 94 | strncpy(buffer, str.c_str(), len); 95 | return buffer; 96 | } catch (std::exception& e) { 97 | pg_error(e.what()); 98 | } 99 | pg_unreachable(); 100 | } 101 | 102 | ptr_with_size hll_sketch_serialize(const void* sketchptr, unsigned header_size) { 103 | try { 104 | ptr_with_size p; 105 | auto bytes = new (palloc(sizeof(hll_sketch_pg::vector_bytes))) hll_sketch_pg::vector_bytes( 106 | static_cast(sketchptr)->serialize_compact(header_size) 107 | ); 108 | p.ptr = bytes->data(); 109 | p.size = bytes->size(); 110 | return p; 111 | } catch (std::exception& e) { 112 | pg_error(e.what()); 113 | } 114 | pg_unreachable(); 115 | } 116 | 117 | void* hll_sketch_deserialize(const char* buffer, unsigned length) { 118 | try { 119 | hll_sketch_pg* sketchptr = new (palloc(sizeof(hll_sketch_pg))) hll_sketch_pg(hll_sketch_pg::deserialize(buffer, length)); 120 | return sketchptr; 121 | } catch (std::exception& e) { 122 | pg_error(e.what()); 123 | } 124 | pg_unreachable(); 125 | } 126 | 127 | void* hll_union_new(unsigned lg_k) { 128 | try { 129 | return new (palloc(sizeof(hll_union_pg))) hll_union_pg(lg_k); 130 | } catch (std::exception& e) { 131 | pg_error(e.what()); 132 | } 133 | pg_unreachable(); 134 | } 135 | 136 | void hll_union_delete(void* unionptr) { 137 | try { 138 | static_cast(unionptr)->~hll_union_pg(); 139 | pfree(unionptr); 140 | } catch (std::exception& e) { 141 | pg_error(e.what()); 142 | } 143 | } 144 | 145 | void hll_union_update(void* unionptr, const void* sketchptr) { 146 | try { 147 | static_cast(unionptr)->update(*static_cast(sketchptr)); 148 | } catch (std::exception& e) { 149 | pg_error(e.what()); 150 | } 151 | } 152 | 153 | void* hll_union_get_result(void* unionptr) { 154 | try { 155 | auto sketchptr = new (palloc(sizeof(hll_sketch_pg))) hll_sketch_pg(static_cast(unionptr)->get_result()); 156 | static_cast(unionptr)->~hll_union_pg(); 157 | pfree(unionptr); 158 | return sketchptr; 159 | } catch (std::exception& e) { 160 | pg_error(e.what()); 161 | } 162 | pg_unreachable(); 163 | } 164 | 165 | void* hll_union_get_result_tgt_type(void* unionptr, unsigned tgt_type) { 166 | try { 167 | auto sketchptr = new (palloc(sizeof(hll_sketch_pg))) hll_sketch_pg(static_cast(unionptr)->get_result( 168 | tgt_type == 4 ? datasketches::target_hll_type::HLL_4 : tgt_type == 6 ? datasketches::target_hll_type::HLL_6 : datasketches::target_hll_type::HLL_8 169 | )); 170 | static_cast(unionptr)->~hll_union_pg(); 171 | pfree(unionptr); 172 | return sketchptr; 173 | } catch (std::exception& e) { 174 | pg_error(e.what()); 175 | } 176 | pg_unreachable(); 177 | } 178 | -------------------------------------------------------------------------------- /src/hll_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef HLL_SKETCH_C_ADAPTER_H 21 | #define HLL_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | void* hll_sketch_new(unsigned lg_k); 30 | void* hll_sketch_new_tgt_type(unsigned lg_k, unsigned tgt_type); 31 | void hll_sketch_delete(void* sketchptr); 32 | 33 | void hll_sketch_update(void* sketchptr, const void* data, unsigned length); 34 | void hll_sketch_merge(void* sketchptr1, const void* sketchptr2); 35 | double hll_sketch_get_estimate(const void* sketchptr); 36 | void** hll_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs); 37 | char* hll_sketch_to_string(const void* sketchptr); 38 | 39 | struct ptr_with_size hll_sketch_serialize(const void* sketchptr, unsigned header_size); 40 | void* hll_sketch_deserialize(const char* buffer, unsigned length); 41 | 42 | void* hll_union_new(unsigned lg_k); 43 | void hll_union_delete(void* unionptr); 44 | void hll_union_update(void* unionptr, const void* sketchptr); 45 | void* hll_union_get_result(void* unionptr); 46 | void* hll_union_get_result_tgt_type(void* unionptr, unsigned tgt_type); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/kll_double_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "kll_double_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | 26 | using kll_double_sketch = datasketches::kll_sketch, palloc_allocator>; 27 | 28 | void* kll_double_sketch_new(unsigned k) { 29 | try { 30 | return new (palloc(sizeof(kll_double_sketch))) kll_double_sketch(k); 31 | } catch (std::exception& e) { 32 | pg_error(e.what()); 33 | } 34 | pg_unreachable(); 35 | } 36 | 37 | void kll_double_sketch_delete(void* sketchptr) { 38 | try { 39 | static_cast(sketchptr)->~kll_double_sketch(); 40 | pfree(sketchptr); 41 | } catch (std::exception& e) { 42 | pg_error(e.what()); 43 | } 44 | } 45 | 46 | void kll_double_sketch_update(void* sketchptr, double value) { 47 | try { 48 | static_cast(sketchptr)->update(value); 49 | } catch (std::exception& e) { 50 | pg_error(e.what()); 51 | } 52 | } 53 | 54 | void kll_double_sketch_merge(void* sketchptr1, const void* sketchptr2) { 55 | try { 56 | static_cast(sketchptr1)->merge(*static_cast(sketchptr2)); 57 | } catch (std::exception& e) { 58 | pg_error(e.what()); 59 | } 60 | } 61 | 62 | double kll_double_sketch_get_rank(const void* sketchptr, double value) { 63 | try { 64 | return static_cast(sketchptr)->get_rank(value); 65 | } catch (std::exception& e) { 66 | pg_error(e.what()); 67 | } 68 | pg_unreachable(); 69 | } 70 | 71 | double kll_double_sketch_get_quantile(const void* sketchptr, double rank) { 72 | try { 73 | return static_cast(sketchptr)->get_quantile(rank); 74 | } catch (std::exception& e) { 75 | pg_error(e.what()); 76 | } 77 | pg_unreachable(); 78 | } 79 | 80 | unsigned long long kll_double_sketch_get_n(const void* sketchptr) { 81 | try { 82 | return static_cast(sketchptr)->get_n(); 83 | } catch (std::exception& e) { 84 | pg_error(e.what()); 85 | } 86 | pg_unreachable(); 87 | } 88 | 89 | double kll_double_sketch_get_max_item(const void *sketchptr) { 90 | try { 91 | return static_cast(sketchptr)->get_max_item(); 92 | } catch (std::exception &e) { 93 | pg_error(e.what()); 94 | } 95 | pg_unreachable(); 96 | } 97 | 98 | double kll_double_sketch_get_min_item(const void *sketchptr) { 99 | try { 100 | return static_cast(sketchptr)->get_min_item(); 101 | } catch (std::exception &e) { 102 | pg_error(e.what()); 103 | } 104 | pg_unreachable(); 105 | } 106 | 107 | char* kll_double_sketch_to_string(const void* sketchptr) { 108 | try { 109 | auto str = static_cast(sketchptr)->to_string(); 110 | const size_t len = str.length() + 1; 111 | char* buffer = (char*) palloc(len); 112 | strncpy(buffer, str.c_str(), len); 113 | return buffer; 114 | } catch (std::exception& e) { 115 | pg_error(e.what()); 116 | } 117 | pg_unreachable(); 118 | } 119 | 120 | ptr_with_size kll_double_sketch_serialize(const void* sketchptr, unsigned header_size) { 121 | try { 122 | ptr_with_size p; 123 | auto bytes = new (palloc(sizeof(kll_double_sketch::vector_bytes))) kll_double_sketch::vector_bytes( 124 | static_cast(sketchptr)->serialize(header_size) 125 | ); 126 | p.ptr = bytes->data(); 127 | p.size = bytes->size(); 128 | return p; 129 | } catch (std::exception& e) { 130 | pg_error(e.what()); 131 | } 132 | pg_unreachable(); 133 | } 134 | 135 | void* kll_double_sketch_deserialize(const char* buffer, unsigned length) { 136 | try { 137 | return new (palloc(sizeof(kll_double_sketch))) kll_double_sketch(kll_double_sketch::deserialize(buffer, length)); 138 | } catch (std::exception& e) { 139 | pg_error(e.what()); 140 | } 141 | pg_unreachable(); 142 | } 143 | 144 | unsigned kll_double_sketch_get_serialized_size_bytes(const void* sketchptr) { 145 | try { 146 | return static_cast(sketchptr)->get_serialized_size_bytes(); 147 | } catch (std::exception& e) { 148 | pg_error(e.what()); 149 | } 150 | pg_unreachable(); 151 | } 152 | 153 | Datum* kll_double_sketch_get_pmf_or_cdf(const void* sketchptr, const double* split_points, unsigned num_split_points, bool is_cdf, bool scale) { 154 | try { 155 | auto array = is_cdf ? 156 | static_cast(sketchptr)->get_CDF(split_points, num_split_points) : 157 | static_cast(sketchptr)->get_PMF(split_points, num_split_points); 158 | Datum* pmf = (Datum*) palloc(sizeof(Datum) * (num_split_points + 1)); 159 | const uint64_t n = static_cast(sketchptr)->get_n(); 160 | for (unsigned i = 0; i < num_split_points + 1; i++) { 161 | if (scale) { 162 | pmf[i] = pg_float8_get_datum(array[i] * n); 163 | } else { 164 | pmf[i] = pg_float8_get_datum(array[i]); 165 | } 166 | } 167 | return pmf; 168 | } catch (std::exception& e) { 169 | pg_error(e.what()); 170 | } 171 | pg_unreachable(); 172 | } 173 | 174 | Datum* kll_double_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions) { 175 | try { 176 | Datum* quantiles = (Datum*) palloc(sizeof(Datum) * num_fractions); 177 | for (unsigned i = 0; i < num_fractions; i++) { 178 | quantiles[i] = pg_float8_get_datum(static_cast(sketchptr)->get_quantile(fractions[i])); 179 | } 180 | return quantiles; 181 | } catch (std::exception& e) { 182 | pg_error(e.what()); 183 | } 184 | pg_unreachable(); 185 | } 186 | -------------------------------------------------------------------------------- /src/kll_double_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef KLL_DOUBLE_SKETCH_C_ADAPTER_H 21 | #define KLL_DOUBLE_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | static const unsigned DEFAULT_K = 200; 30 | 31 | void* kll_double_sketch_new(unsigned k); 32 | void kll_double_sketch_delete(void* sketchptr); 33 | 34 | void kll_double_sketch_update(void* sketchptr, double value); 35 | void kll_double_sketch_merge(void* sketchptr1, const void* sketchptr2); 36 | double kll_double_sketch_get_rank(const void* sketchptr, double value); 37 | double kll_double_sketch_get_quantile(const void* sketchptr, double rank); 38 | unsigned long long kll_double_sketch_get_n(const void* sketchptr); 39 | double kll_double_sketch_get_max_item(const void* sketchptr); 40 | double kll_double_sketch_get_min_item(const void* sketchptr); 41 | char* kll_double_sketch_to_string(const void* sketchptr); 42 | 43 | struct ptr_with_size kll_double_sketch_serialize(const void* sketchptr, unsigned header_size); 44 | void* kll_double_sketch_deserialize(const char* buffer, unsigned length); 45 | unsigned kll_double_sketch_get_serialized_size_bytes(const void* sketchptr); 46 | 47 | void** kll_double_sketch_get_pmf_or_cdf(const void* sketchptr, const double* split_points, unsigned num_split_points, bool is_cdf, bool scale); 48 | void** kll_double_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/kll_float_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "kll_float_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | 26 | using kll_float_sketch = datasketches::kll_sketch, palloc_allocator>; 27 | 28 | void* kll_float_sketch_new(unsigned k) { 29 | try { 30 | return new (palloc(sizeof(kll_float_sketch))) kll_float_sketch(k); 31 | } catch (std::exception& e) { 32 | pg_error(e.what()); 33 | } 34 | pg_unreachable(); 35 | } 36 | 37 | void kll_float_sketch_delete(void* sketchptr) { 38 | try { 39 | static_cast(sketchptr)->~kll_float_sketch(); 40 | pfree(sketchptr); 41 | } catch (std::exception& e) { 42 | pg_error(e.what()); 43 | } 44 | } 45 | 46 | void kll_float_sketch_update(void* sketchptr, float value) { 47 | try { 48 | static_cast(sketchptr)->update(value); 49 | } catch (std::exception& e) { 50 | pg_error(e.what()); 51 | } 52 | } 53 | 54 | void kll_float_sketch_merge(void* sketchptr1, const void* sketchptr2) { 55 | try { 56 | static_cast(sketchptr1)->merge(*static_cast(sketchptr2)); 57 | } catch (std::exception& e) { 58 | pg_error(e.what()); 59 | } 60 | } 61 | 62 | double kll_float_sketch_get_rank(const void* sketchptr, float value) { 63 | try { 64 | return static_cast(sketchptr)->get_rank(value); 65 | } catch (std::exception& e) { 66 | pg_error(e.what()); 67 | } 68 | pg_unreachable(); 69 | } 70 | 71 | float kll_float_sketch_get_quantile(const void* sketchptr, double rank) { 72 | try { 73 | return static_cast(sketchptr)->get_quantile(rank); 74 | } catch (std::exception& e) { 75 | pg_error(e.what()); 76 | } 77 | pg_unreachable(); 78 | } 79 | 80 | unsigned long long kll_float_sketch_get_n(const void* sketchptr) { 81 | try { 82 | return static_cast(sketchptr)->get_n(); 83 | } catch (std::exception& e) { 84 | pg_error(e.what()); 85 | } 86 | pg_unreachable(); 87 | } 88 | 89 | float kll_float_sketch_get_max_item(const void *sketchptr) { 90 | try { 91 | return static_cast(sketchptr)->get_max_item(); 92 | } catch (std::exception &e) { 93 | pg_error(e.what()); 94 | } 95 | pg_unreachable(); 96 | } 97 | 98 | float kll_float_sketch_get_min_item(const void *sketchptr) { 99 | try { 100 | return static_cast(sketchptr)->get_min_item(); 101 | } catch (std::exception &e) { 102 | pg_error(e.what()); 103 | } 104 | pg_unreachable(); 105 | } 106 | 107 | char* kll_float_sketch_to_string(const void* sketchptr) { 108 | try { 109 | auto str = static_cast(sketchptr)->to_string(); 110 | const size_t len = str.length() + 1; 111 | char* buffer = (char*) palloc(len); 112 | strncpy(buffer, str.c_str(), len); 113 | return buffer; 114 | } catch (std::exception& e) { 115 | pg_error(e.what()); 116 | } 117 | pg_unreachable(); 118 | } 119 | 120 | ptr_with_size kll_float_sketch_serialize(const void* sketchptr, unsigned header_size) { 121 | try { 122 | ptr_with_size p; 123 | auto bytes = new (palloc(sizeof(kll_float_sketch::vector_bytes))) kll_float_sketch::vector_bytes( 124 | static_cast(sketchptr)->serialize(header_size) 125 | ); 126 | p.ptr = bytes->data(); 127 | p.size = bytes->size(); 128 | return p; 129 | } catch (std::exception& e) { 130 | pg_error(e.what()); 131 | } 132 | pg_unreachable(); 133 | } 134 | 135 | void* kll_float_sketch_deserialize(const char* buffer, unsigned length) { 136 | try { 137 | return new (palloc(sizeof(kll_float_sketch))) kll_float_sketch(kll_float_sketch::deserialize(buffer, length)); 138 | } catch (std::exception& e) { 139 | pg_error(e.what()); 140 | } 141 | pg_unreachable(); 142 | } 143 | 144 | unsigned kll_float_sketch_get_serialized_size_bytes(const void* sketchptr) { 145 | try { 146 | return static_cast(sketchptr)->get_serialized_size_bytes(); 147 | } catch (std::exception& e) { 148 | pg_error(e.what()); 149 | } 150 | pg_unreachable(); 151 | } 152 | 153 | Datum* kll_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf, bool scale) { 154 | try { 155 | auto array = is_cdf ? 156 | static_cast(sketchptr)->get_CDF(split_points, num_split_points) : 157 | static_cast(sketchptr)->get_PMF(split_points, num_split_points); 158 | Datum* pmf = (Datum*) palloc(sizeof(Datum) * (num_split_points + 1)); 159 | const uint64_t n = static_cast(sketchptr)->get_n(); 160 | for (unsigned i = 0; i < num_split_points + 1; i++) { 161 | if (scale) { 162 | pmf[i] = pg_float8_get_datum(array[i] * n); 163 | } else { 164 | pmf[i] = pg_float8_get_datum(array[i]); 165 | } 166 | } 167 | return pmf; 168 | } catch (std::exception& e) { 169 | pg_error(e.what()); 170 | } 171 | pg_unreachable(); 172 | } 173 | 174 | Datum* kll_float_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions) { 175 | try { 176 | Datum* quantiles = (Datum*) palloc(sizeof(Datum) * num_fractions); 177 | for (unsigned i = 0; i < num_fractions; i++) { 178 | quantiles[i] = pg_float4_get_datum(static_cast(sketchptr)->get_quantile(fractions[i])); 179 | } 180 | return quantiles; 181 | } catch (std::exception& e) { 182 | pg_error(e.what()); 183 | } 184 | pg_unreachable(); 185 | } 186 | -------------------------------------------------------------------------------- /src/kll_float_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef KLL_FLOAT_SKETCH_C_ADAPTER_H 21 | #define KLL_FLOAT_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | static const unsigned DEFAULT_K = 200; 30 | 31 | void* kll_float_sketch_new(unsigned k); 32 | void kll_float_sketch_delete(void* sketchptr); 33 | 34 | void kll_float_sketch_update(void* sketchptr, float value); 35 | void kll_float_sketch_merge(void* sketchptr1, const void* sketchptr2); 36 | double kll_float_sketch_get_rank(const void* sketchptr, float value); 37 | float kll_float_sketch_get_quantile(const void* sketchptr, double rank); 38 | unsigned long long kll_float_sketch_get_n(const void* sketchptr); 39 | float kll_float_sketch_get_max_item(const void* sketchptr); 40 | float kll_float_sketch_get_min_item(const void* sketchptr); 41 | char* kll_float_sketch_to_string(const void* sketchptr); 42 | 43 | struct ptr_with_size kll_float_sketch_serialize(const void* sketchptr, unsigned header_size); 44 | void* kll_float_sketch_deserialize(const char* buffer, unsigned length); 45 | unsigned kll_float_sketch_get_serialized_size_bytes(const void* sketchptr); 46 | 47 | void** kll_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf, bool scale); 48 | void** kll_float_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/postgres_h_substitute.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | // There is some problem compiling C++ code using GCC 4.8.5 (standard on current RHEL) 21 | // with postgres.h included. This is to avoid including postgres.h 22 | 23 | #ifndef POSTGRES_H_SUBSTITUTE 24 | #define POSTGRES_H_SUBSTITUTE 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef void* Datum; 31 | extern Datum pg_float4_get_datum(float x); 32 | extern Datum pg_float8_get_datum(double x); 33 | 34 | extern void pg_error(const char* message); 35 | 36 | // from postgresql c.h to hint compiler and avoid warnings 37 | #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING) 38 | #define pg_unreachable() __builtin_unreachable() 39 | #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING) 40 | #define pg_unreachable() __assume(0) 41 | #else 42 | #define pg_unreachable() abort() 43 | #endif 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/ptr_with_size.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef PTR_WITH_SIZE_H 21 | #define PTR_WITH_SIZE_H 22 | 23 | struct ptr_with_size { 24 | void* ptr; 25 | unsigned long long size; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/quantiles_double_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "quantiles_double_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | 26 | using quantiles_double_sketch = datasketches::quantiles_sketch, palloc_allocator>; 27 | 28 | void* quantiles_double_sketch_new(unsigned k) { 29 | try { 30 | return new (palloc(sizeof(quantiles_double_sketch))) quantiles_double_sketch(k); 31 | } catch (std::exception& e) { 32 | pg_error(e.what()); 33 | } 34 | pg_unreachable(); 35 | } 36 | 37 | void quantiles_double_sketch_delete(void* sketchptr) { 38 | try { 39 | static_cast(sketchptr)->~quantiles_double_sketch(); 40 | pfree(sketchptr); 41 | } catch (std::exception& e) { 42 | pg_error(e.what()); 43 | } 44 | } 45 | 46 | void quantiles_double_sketch_update(void* sketchptr, double value) { 47 | try { 48 | static_cast(sketchptr)->update(value); 49 | } catch (std::exception& e) { 50 | pg_error(e.what()); 51 | } 52 | } 53 | 54 | void quantiles_double_sketch_merge(void* sketchptr1, const void* sketchptr2) { 55 | try { 56 | static_cast(sketchptr1)->merge(*static_cast(sketchptr2)); 57 | } catch (std::exception& e) { 58 | pg_error(e.what()); 59 | } 60 | } 61 | 62 | double quantiles_double_sketch_get_rank(const void* sketchptr, double value) { 63 | try { 64 | return static_cast(sketchptr)->get_rank(value); 65 | } catch (std::exception& e) { 66 | pg_error(e.what()); 67 | } 68 | pg_unreachable(); 69 | } 70 | 71 | double quantiles_double_sketch_get_quantile(const void* sketchptr, double rank) { 72 | try { 73 | return static_cast(sketchptr)->get_quantile(rank); 74 | } catch (std::exception& e) { 75 | pg_error(e.what()); 76 | } 77 | pg_unreachable(); 78 | } 79 | 80 | unsigned long long quantiles_double_sketch_get_n(const void* sketchptr) { 81 | try { 82 | return static_cast(sketchptr)->get_n(); 83 | } catch (std::exception& e) { 84 | pg_error(e.what()); 85 | } 86 | pg_unreachable(); 87 | } 88 | 89 | char* quantiles_double_sketch_to_string(const void* sketchptr) { 90 | try { 91 | auto str = static_cast(sketchptr)->to_string(); 92 | const size_t len = str.length() + 1; 93 | char* buffer = (char*) palloc(len); 94 | strncpy(buffer, str.c_str(), len); 95 | return buffer; 96 | } catch (std::exception& e) { 97 | pg_error(e.what()); 98 | } 99 | pg_unreachable(); 100 | } 101 | 102 | ptr_with_size quantiles_double_sketch_serialize(const void* sketchptr, unsigned header_size) { 103 | try { 104 | ptr_with_size p; 105 | auto bytes = new (palloc(sizeof(quantiles_double_sketch::vector_bytes))) quantiles_double_sketch::vector_bytes( 106 | static_cast(sketchptr)->serialize(header_size) 107 | ); 108 | p.ptr = bytes->data(); 109 | p.size = bytes->size(); 110 | return p; 111 | } catch (std::exception& e) { 112 | pg_error(e.what()); 113 | } 114 | pg_unreachable(); 115 | } 116 | 117 | void* quantiles_double_sketch_deserialize(const char* buffer, unsigned length) { 118 | try { 119 | return new (palloc(sizeof(quantiles_double_sketch))) quantiles_double_sketch(quantiles_double_sketch::deserialize(buffer, length)); 120 | } catch (std::exception& e) { 121 | pg_error(e.what()); 122 | } 123 | pg_unreachable(); 124 | } 125 | 126 | unsigned quantiles_double_sketch_get_serialized_size_bytes(const void* sketchptr) { 127 | try { 128 | return static_cast(sketchptr)->get_serialized_size_bytes(); 129 | } catch (std::exception& e) { 130 | pg_error(e.what()); 131 | } 132 | pg_unreachable(); 133 | } 134 | 135 | Datum* quantiles_double_sketch_get_pmf_or_cdf(const void* sketchptr, const double* split_points, unsigned num_split_points, bool is_cdf, bool scale) { 136 | try { 137 | auto array = is_cdf ? 138 | static_cast(sketchptr)->get_CDF(split_points, num_split_points) : 139 | static_cast(sketchptr)->get_PMF(split_points, num_split_points); 140 | Datum* pmf = (Datum*) palloc(sizeof(Datum) * (num_split_points + 1)); 141 | const uint64_t n = static_cast(sketchptr)->get_n(); 142 | for (unsigned i = 0; i < num_split_points + 1; i++) { 143 | if (scale) { 144 | pmf[i] = pg_float8_get_datum(array[i] * n); 145 | } else { 146 | pmf[i] = pg_float8_get_datum(array[i]); 147 | } 148 | } 149 | return pmf; 150 | } catch (std::exception& e) { 151 | pg_error(e.what()); 152 | } 153 | pg_unreachable(); 154 | } 155 | 156 | Datum* quantiles_double_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions) { 157 | try { 158 | Datum* quantiles = (Datum*) palloc(sizeof(Datum) * num_fractions); 159 | for (unsigned i = 0; i < num_fractions; i++) { 160 | quantiles[i] = pg_float8_get_datum(static_cast(sketchptr)->get_quantile(fractions[i])); 161 | } 162 | return quantiles; 163 | } catch (std::exception& e) { 164 | pg_error(e.what()); 165 | } 166 | pg_unreachable(); 167 | } 168 | -------------------------------------------------------------------------------- /src/quantiles_double_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef QUANTILES_DOUBLE_SKETCH_C_ADAPTER_H 21 | #define QUANTILES_DOUBLE_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | static const unsigned DEFAULT_K = 128; 30 | 31 | void* quantiles_double_sketch_new(unsigned k); 32 | void quantiles_double_sketch_delete(void* sketchptr); 33 | 34 | void quantiles_double_sketch_update(void* sketchptr, double value); 35 | void quantiles_double_sketch_merge(void* sketchptr1, const void* sketchptr2); 36 | double quantiles_double_sketch_get_rank(const void* sketchptr, double value); 37 | double quantiles_double_sketch_get_quantile(const void* sketchptr, double rank); 38 | unsigned long long quantiles_double_sketch_get_n(const void* sketchptr); 39 | char* quantiles_double_sketch_to_string(const void* sketchptr); 40 | 41 | struct ptr_with_size quantiles_double_sketch_serialize(const void* sketchptr, unsigned header_size); 42 | void* quantiles_double_sketch_deserialize(const char* buffer, unsigned length); 43 | unsigned quantiles_double_sketch_get_serialized_size_bytes(const void* sketchptr); 44 | 45 | void** quantiles_double_sketch_get_pmf_or_cdf(const void* sketchptr, const double* split_points, unsigned num_split_points, bool is_cdf, bool scale); 46 | void** quantiles_double_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/req_float_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "req_float_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | 26 | using req_float_sketch = datasketches::req_sketch, palloc_allocator>; 27 | 28 | void* req_float_sketch_new(unsigned k, bool hra) { 29 | try { 30 | return new (palloc(sizeof(req_float_sketch))) req_float_sketch(k, hra); 31 | } catch (std::exception& e) { 32 | pg_error(e.what()); 33 | } 34 | pg_unreachable(); 35 | } 36 | 37 | void req_float_sketch_delete(void* sketchptr) { 38 | try { 39 | static_cast(sketchptr)->~req_float_sketch(); 40 | pfree(sketchptr); 41 | } catch (std::exception& e) { 42 | pg_error(e.what()); 43 | } 44 | } 45 | 46 | void req_float_sketch_update(void* sketchptr, float value) { 47 | try { 48 | static_cast(sketchptr)->update(value); 49 | } catch (std::exception& e) { 50 | pg_error(e.what()); 51 | } 52 | } 53 | 54 | void req_float_sketch_merge(void* sketchptr1, void* sketchptr2) { 55 | try { 56 | static_cast(sketchptr1)->merge(*static_cast(sketchptr2)); 57 | } catch (std::exception& e) { 58 | pg_error(e.what()); 59 | } 60 | } 61 | 62 | double req_float_sketch_get_rank(const void* sketchptr, float value, bool inclusive) { 63 | try { 64 | return static_cast(sketchptr)->get_rank(value, inclusive); 65 | } catch (std::exception& e) { 66 | pg_error(e.what()); 67 | } 68 | pg_unreachable(); 69 | } 70 | 71 | float req_float_sketch_get_quantile(const void* sketchptr, double rank, bool inclusive) { 72 | try { 73 | return static_cast(sketchptr)->get_quantile(rank, inclusive); 74 | } catch (std::exception& e) { 75 | pg_error(e.what()); 76 | } 77 | pg_unreachable(); 78 | } 79 | 80 | unsigned long long req_float_sketch_get_n(const void* sketchptr) { 81 | try { 82 | return static_cast(sketchptr)->get_n(); 83 | } catch (std::exception& e) { 84 | pg_error(e.what()); 85 | } 86 | pg_unreachable(); 87 | } 88 | 89 | char* req_float_sketch_to_string(const void* sketchptr) { 90 | try { 91 | auto str = static_cast(sketchptr)->to_string(); 92 | const size_t len = str.length() + 1; 93 | char* buffer = (char*) palloc(len); 94 | strncpy(buffer, str.c_str(), len); 95 | return buffer; 96 | } catch (std::exception& e) { 97 | pg_error(e.what()); 98 | } 99 | pg_unreachable(); 100 | } 101 | 102 | ptr_with_size req_float_sketch_serialize(const void* sketchptr, unsigned header_size) { 103 | try { 104 | ptr_with_size p; 105 | auto bytes = new (palloc(sizeof(req_float_sketch::vector_bytes))) req_float_sketch::vector_bytes( 106 | static_cast(sketchptr)->serialize(header_size) 107 | ); 108 | p.ptr = bytes->data(); 109 | p.size = bytes->size(); 110 | return p; 111 | } catch (std::exception& e) { 112 | pg_error(e.what()); 113 | } 114 | pg_unreachable(); 115 | } 116 | 117 | void* req_float_sketch_deserialize(const char* buffer, unsigned length) { 118 | try { 119 | return new (palloc(sizeof(req_float_sketch))) req_float_sketch(req_float_sketch::deserialize(buffer, length)); 120 | } catch (std::exception& e) { 121 | pg_error(e.what()); 122 | } 123 | pg_unreachable(); 124 | } 125 | 126 | unsigned req_float_sketch_get_serialized_size_bytes(const void* sketchptr) { 127 | try { 128 | return static_cast(sketchptr)->get_serialized_size_bytes(); 129 | } catch (std::exception& e) { 130 | pg_error(e.what()); 131 | } 132 | pg_unreachable(); 133 | } 134 | 135 | Datum* req_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf, bool scale, bool inclusive) { 136 | try { 137 | auto array = is_cdf ? 138 | static_cast(sketchptr)->get_CDF(split_points, num_split_points, inclusive) : 139 | static_cast(sketchptr)->get_PMF(split_points, num_split_points, inclusive); 140 | Datum* pmf = (Datum*) palloc(sizeof(Datum) * (num_split_points + 1)); 141 | const uint64_t n = static_cast(sketchptr)->get_n(); 142 | for (unsigned i = 0; i < num_split_points + 1; i++) { 143 | if (scale) { 144 | pmf[i] = pg_float8_get_datum(array[i] * n); 145 | } else { 146 | pmf[i] = pg_float8_get_datum(array[i]); 147 | } 148 | } 149 | return pmf; 150 | } catch (std::exception& e) { 151 | pg_error(e.what()); 152 | } 153 | pg_unreachable(); 154 | } 155 | 156 | Datum* req_float_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions, bool inclusive) { 157 | try { 158 | Datum* quantiles = (Datum*) palloc(sizeof(Datum) * num_fractions); 159 | for (unsigned i = 0; i < num_fractions; i++) { 160 | quantiles[i] = pg_float4_get_datum(static_cast(sketchptr)->get_quantile(fractions[i])); 161 | } 162 | return quantiles; 163 | } catch (std::exception& e) { 164 | pg_error(e.what()); 165 | } 166 | pg_unreachable(); 167 | } 168 | -------------------------------------------------------------------------------- /src/req_float_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef REQ_FLOAT_SKETCH_C_ADAPTER_H 21 | #define REQ_FLOAT_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | static const unsigned DEFAULT_K = 12; 30 | 31 | void* req_float_sketch_new(unsigned k, bool hra); 32 | void req_float_sketch_delete(void* sketchptr); 33 | 34 | void req_float_sketch_update(void* sketchptr, float value); 35 | void req_float_sketch_merge(void* sketchptr1, void* sketchptr2); 36 | double req_float_sketch_get_rank(const void* sketchptr, float value, bool inclusive); 37 | float req_float_sketch_get_quantile(const void* sketchptr, double rank, bool inclusive); 38 | unsigned long long req_float_sketch_get_n(const void* sketchptr); 39 | char* req_float_sketch_to_string(const void* sketchptr); 40 | 41 | struct ptr_with_size req_float_sketch_serialize(const void* sketchptr, unsigned header_size); 42 | void* req_float_sketch_deserialize(const char* buffer, unsigned length); 43 | unsigned req_float_sketch_get_serialized_size_bytes(const void* sketchptr); 44 | 45 | void** req_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf, bool scale, bool inclusive); 46 | void** req_float_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions, bool inclusive); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/theta_sketch_c_adapter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #include "theta_sketch_c_adapter.h" 21 | #include "allocator.h" 22 | #include "postgres_h_substitute.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | using theta_sketch_pg = datasketches::theta_sketch_alloc>; 30 | using update_theta_sketch_pg = datasketches::update_theta_sketch_alloc>; 31 | using compact_theta_sketch_pg = datasketches::compact_theta_sketch_alloc>; 32 | using theta_union_pg = datasketches::theta_union_alloc>; 33 | using theta_intersection_pg = datasketches::theta_intersection_alloc>; 34 | using theta_a_not_b_pg = datasketches::theta_a_not_b_alloc>; 35 | using wrapped_compact_theta_sketch_pg = datasketches::wrapped_compact_theta_sketch_alloc>; 36 | 37 | void* theta_sketch_new_default() { 38 | try { 39 | return new (palloc(sizeof(update_theta_sketch_pg))) update_theta_sketch_pg(update_theta_sketch_pg::builder().build()); 40 | } catch (std::exception& e) { 41 | pg_error(e.what()); 42 | } 43 | pg_unreachable(); 44 | } 45 | 46 | void* theta_sketch_new_lgk(unsigned lg_k) { 47 | try { 48 | return new (palloc(sizeof(update_theta_sketch_pg))) update_theta_sketch_pg(update_theta_sketch_pg::builder().set_lg_k(lg_k).build()); 49 | } catch (std::exception& e) { 50 | pg_error(e.what()); 51 | } 52 | pg_unreachable(); 53 | } 54 | 55 | void* theta_sketch_new_lgk_p(unsigned lg_k, float p) { 56 | try { 57 | return new (palloc(sizeof(update_theta_sketch_pg))) update_theta_sketch_pg(update_theta_sketch_pg::builder().set_lg_k(lg_k).set_p(p).build()); 58 | } catch (std::exception& e) { 59 | pg_error(e.what()); 60 | } 61 | pg_unreachable(); 62 | } 63 | 64 | void theta_sketch_delete(void* sketchptr) { 65 | try { 66 | static_cast(sketchptr)->~theta_sketch_pg(); 67 | pfree(sketchptr); 68 | } catch (std::exception& e) { 69 | pg_error(e.what()); 70 | } 71 | } 72 | 73 | void theta_sketch_update(void* sketchptr, const void* data, unsigned length) { 74 | try { 75 | static_cast(sketchptr)->update(data, length); 76 | } catch (std::exception& e) { 77 | pg_error(e.what()); 78 | } 79 | } 80 | 81 | void* theta_sketch_compact(void* sketchptr) { 82 | try { 83 | auto newptr = new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast(sketchptr)->compact()); 84 | static_cast(sketchptr)->~update_theta_sketch_pg(); 85 | pfree(sketchptr); 86 | return newptr; 87 | } catch (std::exception& e) { 88 | pg_error(e.what()); 89 | } 90 | pg_unreachable(); 91 | } 92 | 93 | double theta_sketch_get_estimate(const void* sketchptr) { 94 | try { 95 | return static_cast(sketchptr)->get_estimate(); 96 | } catch (std::exception& e) { 97 | pg_error(e.what()); 98 | } 99 | pg_unreachable(); 100 | } 101 | 102 | Datum* theta_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs) { 103 | try { 104 | Datum* est_and_bounds = (Datum*) palloc(sizeof(Datum) * 3); 105 | est_and_bounds[0] = pg_float8_get_datum(static_cast(sketchptr)->get_estimate()); 106 | est_and_bounds[1] = pg_float8_get_datum(static_cast(sketchptr)->get_lower_bound(num_std_devs)); 107 | est_and_bounds[2] = pg_float8_get_datum(static_cast(sketchptr)->get_upper_bound(num_std_devs)); 108 | return est_and_bounds; 109 | } catch (std::exception& e) { 110 | pg_error(e.what()); 111 | } 112 | pg_unreachable(); 113 | } 114 | 115 | char* theta_sketch_to_string(const void* sketchptr) { 116 | try { 117 | auto str = static_cast(sketchptr)->to_string(); 118 | const size_t len = str.length() + 1; 119 | char* buffer = (char*) palloc(len); 120 | strncpy(buffer, str.c_str(), len); 121 | return buffer; 122 | } catch (std::exception& e) { 123 | pg_error(e.what()); 124 | } 125 | pg_unreachable(); 126 | } 127 | 128 | ptr_with_size theta_sketch_serialize(const void* sketchptr, unsigned header_size) { 129 | try { 130 | ptr_with_size p; 131 | auto bytes = new (palloc(sizeof(compact_theta_sketch_pg::vector_bytes))) compact_theta_sketch_pg::vector_bytes( 132 | static_cast(sketchptr)->serialize(header_size) 133 | ); 134 | p.ptr = bytes->data(); 135 | p.size = bytes->size(); 136 | return p; 137 | } catch (std::exception& e) { 138 | pg_error(e.what()); 139 | } 140 | pg_unreachable(); 141 | } 142 | 143 | void* theta_sketch_deserialize(const char* buffer, unsigned length) { 144 | try { 145 | return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(compact_theta_sketch_pg::deserialize(buffer, length)); 146 | } catch (std::exception& e) { 147 | pg_error(e.what()); 148 | } 149 | pg_unreachable(); 150 | } 151 | 152 | void* theta_union_new_default() { 153 | try { 154 | return new (palloc(sizeof(theta_union_pg))) theta_union_pg(theta_union_pg::builder().build()); 155 | } catch (std::exception& e) { 156 | pg_error(e.what()); 157 | } 158 | pg_unreachable(); 159 | } 160 | 161 | void* theta_union_new(unsigned lg_k) { 162 | try { 163 | return new (palloc(sizeof(theta_union_pg))) theta_union_pg(theta_union_pg::builder().set_lg_k(lg_k).build()); 164 | } catch (std::exception& e) { 165 | pg_error(e.what()); 166 | } 167 | pg_unreachable(); 168 | } 169 | 170 | void theta_union_delete(void* unionptr) { 171 | try { 172 | static_cast(unionptr)->~theta_union_pg(); 173 | pfree(unionptr); 174 | } catch (std::exception& e) { 175 | pg_error(e.what()); 176 | } 177 | } 178 | 179 | void theta_union_update_with_sketch(void* unionptr, const void* sketchptr) { 180 | try { 181 | static_cast(unionptr)->update(*static_cast(sketchptr)); 182 | } catch (std::exception& e) { 183 | pg_error(e.what()); 184 | } 185 | } 186 | 187 | void theta_union_update_with_bytes(void* unionptr, const void* buffer, unsigned length) { 188 | try { 189 | static_cast(unionptr)->update(wrapped_compact_theta_sketch_pg::wrap(buffer, length)); 190 | } catch (std::exception& e) { 191 | pg_error(e.what()); 192 | } 193 | } 194 | 195 | void* theta_union_get_result(void* unionptr) { 196 | try { 197 | auto sketchptr = new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast(unionptr)->get_result()); 198 | static_cast(unionptr)->~theta_union_pg(); 199 | pfree(unionptr); 200 | return sketchptr; 201 | } catch (std::exception& e) { 202 | pg_error(e.what()); 203 | } 204 | pg_unreachable(); 205 | } 206 | 207 | void* theta_intersection_new_default() { 208 | try { 209 | return new (palloc(sizeof(theta_intersection_pg))) theta_intersection_pg; 210 | } catch (std::exception& e) { 211 | pg_error(e.what()); 212 | } 213 | pg_unreachable(); 214 | } 215 | 216 | void theta_intersection_delete(void* interptr) { 217 | try { 218 | static_cast(interptr)->~theta_intersection_pg(); 219 | pfree(interptr); 220 | } catch (std::exception& e) { 221 | pg_error(e.what()); 222 | } 223 | } 224 | 225 | void theta_intersection_update_with_sketch(void* interptr, const void* sketchptr) { 226 | try { 227 | static_cast(interptr)->update(*static_cast(sketchptr)); 228 | } catch (std::exception& e) { 229 | pg_error(e.what()); 230 | } 231 | } 232 | 233 | void theta_intersection_update_with_bytes(void* interptr, const void* buffer, unsigned length) { 234 | try { 235 | static_cast(interptr)->update(wrapped_compact_theta_sketch_pg::wrap(buffer, length)); 236 | } catch (std::exception& e) { 237 | pg_error(e.what()); 238 | } 239 | } 240 | 241 | void* theta_intersection_get_result(void* interptr) { 242 | try { 243 | auto sketchptr = new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast(interptr)->get_result()); 244 | static_cast(interptr)->~theta_intersection_pg(); 245 | pfree(interptr); 246 | return sketchptr; 247 | } catch (std::exception& e) { 248 | pg_error(e.what()); 249 | } 250 | pg_unreachable(); 251 | } 252 | 253 | void* theta_a_not_b(const void* buffer1, unsigned length1, const void* buffer2, unsigned length2) { 254 | try { 255 | theta_a_not_b_pg a_not_b; 256 | return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(a_not_b.compute( 257 | wrapped_compact_theta_sketch_pg::wrap(buffer1, length1), 258 | wrapped_compact_theta_sketch_pg::wrap(buffer2, length2) 259 | )); 260 | } catch (std::exception& e) { 261 | pg_error(e.what()); 262 | } 263 | pg_unreachable(); 264 | } 265 | -------------------------------------------------------------------------------- /src/theta_sketch_c_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | #ifndef THETA_SKETCH_C_ADAPTER_H 21 | #define THETA_SKETCH_C_ADAPTER_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include "ptr_with_size.h" 28 | 29 | void* theta_sketch_new_default(); 30 | void* theta_sketch_new_lgk(unsigned lg_k); 31 | void* theta_sketch_new_lgk_p(unsigned lg_k, float p); 32 | void theta_sketch_delete(void* sketchptr); 33 | 34 | void theta_sketch_update(void* sketchptr, const void* data, unsigned length); 35 | void* theta_sketch_compact(void* sketchptr); 36 | void theta_sketch_union(void* sketchptr1, const void* sketchptr2); 37 | double theta_sketch_get_estimate(const void* sketchptr); 38 | void** theta_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs); 39 | char* theta_sketch_to_string(const void* sketchptr); 40 | 41 | struct ptr_with_size theta_sketch_serialize(const void* sketchptr, unsigned header_size); 42 | void* theta_sketch_deserialize(const char* buffer, unsigned length); 43 | 44 | void* theta_union_new_default(); 45 | void* theta_union_new(unsigned lg_k); 46 | void theta_union_delete(void* unionptr); 47 | void theta_union_update_with_sketch(void* unionptr, const void* sketchptr); 48 | void theta_union_update_with_bytes(void* unionptr, const void* buffer, unsigned length); 49 | void* theta_union_get_result(void* unionptr); 50 | 51 | void* theta_intersection_new_default(); 52 | void theta_intersection_delete(void* interptr); 53 | void theta_intersection_update_with_sketch(void* interptr, const void* sketchptr); 54 | void theta_intersection_update_with_bytes(void* interptr, const void* buffer, unsigned length); 55 | void* theta_intersection_get_result(void* interptr); 56 | 57 | void* theta_a_not_b(const void* buffer1, unsigned length1, const void* buffer2, unsigned length2); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /test/aod_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists aod_sketch_test; 5 | create table aod_sketch_test(sketch aod_sketch); 6 | 7 | -- default lgk 8 | insert into aod_sketch_test 9 | select aod_sketch_build(key, aod) 10 | from (values (1, array[1]), (2, array[1]), (3, array[1]), (4, array[1]), (5, array[1])) as t(key, aod) 11 | ; 12 | 13 | -- lgk = 16 14 | insert into aod_sketch_test 15 | select aod_sketch_build(key, aod, 16) 16 | from (values (4, array[1]), (5, array[1]), (6, array[1]), (7, array[1]), (8, array[1])) as t(key, aod) 17 | ; 18 | 19 | select aod_sketch_get_estimate(sketch) from aod_sketch_test; 20 | select aod_sketch_to_string(sketch) from aod_sketch_test; 21 | 22 | -- default lgk 23 | select aod_sketch_get_estimate(aod_sketch_union(sketch)) from aod_sketch_test; 24 | -- lgk = 16 25 | select aod_sketch_get_estimate(aod_sketch_union(sketch, 16)) from aod_sketch_test; 26 | 27 | select aod_sketch_get_estimate(aod_sketch_intersection(sketch)) from aod_sketch_test; 28 | 29 | select aod_sketch_get_estimate(aod_sketch_a_not_b(aod_sketch_build(key1, aod1), aod_sketch_build(key2, aod2))) 30 | from (values (1, array[1], 2, array[1]), (2, array[1], 3, array[1]), (3, array[1], 4, array[1])) as t(key1, aod1, key2, aod2); 31 | 32 | select aod_sketch_to_kll_float_sketch(sketch, 1) from aod_sketch_test; 33 | select aod_sketch_to_means(sketch) from aod_sketch_test; 34 | select aod_sketch_to_variances(sketch) from aod_sketch_test; 35 | 36 | select aod_sketch_students_t_test(aod_sketch_build(key1, aod1), aod_sketch_build(key2, aod2)) 37 | from (values 38 | (1, array[1], 1, array[1.1]), 39 | (2, array[0.9], 2, array[1]), 40 | (3, array[1.1], 3, array[1.2]), 41 | (4, array[1], 4, array[1.1]) 42 | ) as t(key1, aod1, key2, aod2); 43 | 44 | drop table aod_sketch_test; 45 | drop extension datasketches; 46 | -------------------------------------------------------------------------------- /test/cpc_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists cpc_sketch_test; 5 | create table cpc_sketch_test(sketch cpc_sketch); 6 | 7 | -- default lgk 8 | insert into cpc_sketch_test 9 | select cpc_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- lgk = 8 14 | insert into cpc_sketch_test 15 | select cpc_sketch_build(value, 8) 16 | from (values (4), (5), (6), (7), (8)) as t(value) 17 | ; 18 | 19 | select cpc_sketch_get_estimate(sketch) from cpc_sketch_test; 20 | --select cpc_sketch_to_string(sketch) from cpc_sketch_test; 21 | 22 | -- default lgk and type 23 | select cpc_sketch_get_estimate(cpc_sketch_union(sketch)) from cpc_sketch_test; 24 | -- lgk = 8 25 | select cpc_sketch_get_estimate(cpc_sketch_union(sketch, 8)) from cpc_sketch_test; 26 | 27 | drop table cpc_sketch_test; 28 | drop extension datasketches; 29 | -------------------------------------------------------------------------------- /test/fi_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists frequent_strings_sketch_test; 5 | create table frequent_strings_sketch_test(sketch frequent_strings_sketch); 6 | 7 | insert into frequent_strings_sketch_test 8 | select frequent_strings_sketch_build(8, str) 9 | from (values ('a'), ('b'), ('a'), ('a'), ('c')) as t(str) 10 | ; 11 | 12 | insert into frequent_strings_sketch_test 13 | select frequent_strings_sketch_build(8, str) 14 | from (values ('a'), ('c'), ('c'), ('b'), ('a')) as t(str) 15 | ; 16 | 17 | select frequent_strings_sketch_result_no_false_negatives(frequent_strings_sketch_merge(8, sketch)) as frequent_strings from frequent_strings_sketch_test; 18 | select frequent_strings_sketch_to_string(sketch) from frequent_strings_sketch_test; 19 | 20 | drop table frequent_strings_sketch_test; 21 | drop extension datasketches; 22 | -------------------------------------------------------------------------------- /test/hll_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists hll_sketch_test; 5 | create table hll_sketch_test(sketch hll_sketch); 6 | 7 | -- default lgk and type 8 | insert into hll_sketch_test 9 | select hll_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- lgk = 8 and type = HLL_6 14 | insert into hll_sketch_test 15 | select hll_sketch_build(value, 8, 6) 16 | from (values (4), (5), (6), (7), (8)) as t(value) 17 | ; 18 | 19 | select hll_sketch_get_estimate(sketch) from hll_sketch_test; 20 | --select hll_sketch_to_string(sketch) from hll_sketch_test; 21 | 22 | -- default lgk and type 23 | select hll_sketch_get_estimate(hll_sketch_union(sketch)) from hll_sketch_test; 24 | -- lgk = 8 and type = HLL_6 25 | select hll_sketch_get_estimate(hll_sketch_union(sketch, 8, 6)) from hll_sketch_test; 26 | 27 | drop table hll_sketch_test; 28 | drop extension datasketches; 29 | -------------------------------------------------------------------------------- /test/kll_double_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists kll_sketch_test; 5 | create table kll_sketch_test(sketch kll_double_sketch); 6 | 7 | -- default k 8 | insert into kll_sketch_test 9 | select kll_double_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- k = 20 14 | insert into kll_sketch_test 15 | select kll_double_sketch_build(value, 20) 16 | from (values (6), (7), (8), (9), (10)) as t(value) 17 | ; 18 | 19 | -- get min and max values 20 | select kll_double_sketch_get_min_item(sketch) as min_item from kll_sketch_test; 21 | select kll_double_sketch_get_max_item(sketch) as max_item from kll_sketch_test; 22 | select kll_double_sketch_get_quantiles(sketch, array[0, 1]) as min_max from kll_sketch_test; 23 | select kll_double_sketch_to_string(sketch) from kll_sketch_test; 24 | 25 | -- default k, median 26 | select kll_double_sketch_get_quantile(kll_double_sketch_merge(sketch), 0.5) as median from kll_sketch_test; 27 | -- k = 20, rank of value 6 28 | select kll_double_sketch_get_rank(kll_double_sketch_merge(sketch, 20), 6) as rank from kll_sketch_test; 29 | 30 | select kll_double_sketch_get_pmf(kll_double_sketch_merge(sketch, 20), array[2, 5, 7]) as pmf from kll_sketch_test; 31 | select kll_double_sketch_get_cdf(kll_double_sketch_merge(sketch, 20), array[2, 5, 7]) as cdf from kll_sketch_test; 32 | select kll_double_sketch_get_histogram(kll_double_sketch_merge(sketch, 20), 5) as histogram from kll_sketch_test; 33 | 34 | drop table kll_sketch_test; 35 | drop extension datasketches; 36 | -------------------------------------------------------------------------------- /test/kll_float_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists kll_sketch_test; 5 | create table kll_sketch_test(sketch kll_float_sketch); 6 | 7 | -- default k 8 | insert into kll_sketch_test 9 | select kll_float_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- k = 20 14 | insert into kll_sketch_test 15 | select kll_float_sketch_build(value, 20) 16 | from (values (6), (7), (8), (9), (10)) as t(value) 17 | ; 18 | 19 | -- get min and max values 20 | select kll_float_sketch_get_min_item(sketch) as min_item from kll_sketch_test; 21 | select kll_float_sketch_get_max_item(sketch) as max_item from kll_sketch_test; 22 | select kll_float_sketch_get_quantiles(sketch, array[0, 1]) as min_max from kll_sketch_test; 23 | select kll_float_sketch_to_string(sketch) from kll_sketch_test; 24 | 25 | -- default k, median 26 | select kll_float_sketch_get_quantile(kll_float_sketch_merge(sketch), 0.5) as median from kll_sketch_test; 27 | -- k = 20, rank of value 6 28 | select kll_float_sketch_get_rank(kll_float_sketch_merge(sketch, 20), 6) as rank from kll_sketch_test; 29 | 30 | select kll_float_sketch_get_pmf(kll_float_sketch_merge(sketch, 20), array[2, 5, 7]) as pmf from kll_sketch_test; 31 | select kll_float_sketch_get_cdf(kll_float_sketch_merge(sketch, 20), array[2, 5, 7]) as cdf from kll_sketch_test; 32 | select kll_float_sketch_get_histogram(kll_float_sketch_merge(sketch, 20), 5) as histogram from kll_sketch_test; 33 | 34 | drop table kll_sketch_test; 35 | drop extension datasketches; 36 | -------------------------------------------------------------------------------- /test/quantiles_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists quantiles_sketch_test; 5 | create table quantiles_sketch_test(sketch quantiles_double_sketch); 6 | 7 | -- default k 8 | insert into quantiles_sketch_test 9 | select quantiles_double_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- k = 32 14 | insert into quantiles_sketch_test 15 | select quantiles_double_sketch_build(value, 32) 16 | from (values (6), (7), (8), (9), (10)) as t(value) 17 | ; 18 | 19 | -- get min and max values 20 | select quantiles_double_sketch_get_quantiles(sketch, array[0, 1]) as min_max from quantiles_sketch_test; 21 | select quantiles_double_sketch_to_string(sketch) from quantiles_sketch_test; 22 | 23 | -- default k, median 24 | select quantiles_double_sketch_get_quantile(quantiles_double_sketch_merge(sketch), 0.5) as median from quantiles_sketch_test; 25 | -- k = 32, rank of value 6 26 | select quantiles_double_sketch_get_rank(quantiles_double_sketch_merge(sketch, 32), 6) as rank from quantiles_sketch_test; 27 | 28 | select quantiles_double_sketch_get_pmf(quantiles_double_sketch_merge(sketch, 32), array[2, 5, 7]) as pmf from quantiles_sketch_test; 29 | select quantiles_double_sketch_get_cdf(quantiles_double_sketch_merge(sketch, 32), array[2, 5, 7]) as cdf from quantiles_sketch_test; 30 | select quantiles_double_sketch_get_histogram(quantiles_double_sketch_merge(sketch, 32), 5) as histogram from quantiles_sketch_test; 31 | 32 | drop table quantiles_sketch_test; 33 | drop extension datasketches; 34 | -------------------------------------------------------------------------------- /test/req_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists req_sketch_test; 5 | create table req_sketch_test(sketch req_float_sketch); 6 | 7 | -- default k 8 | insert into req_sketch_test 9 | select req_float_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- k = 20 14 | insert into req_sketch_test 15 | select req_float_sketch_build(value, 20) 16 | from (values (6), (7), (8), (9), (10)) as t(value) 17 | ; 18 | 19 | -- get min and max values 20 | select req_float_sketch_get_quantiles(sketch, array[0, 1]) as min_max from req_sketch_test; 21 | select req_float_sketch_to_string(sketch) from req_sketch_test; 22 | 23 | -- default k, median 24 | select req_float_sketch_get_quantile(req_float_sketch_merge(sketch), 0.5) as median from req_sketch_test; 25 | -- k = 20, rank of value 6 26 | select req_float_sketch_get_rank(req_float_sketch_merge(sketch, 20), 6) as rank from req_sketch_test; 27 | 28 | drop table req_sketch_test; 29 | drop extension datasketches; 30 | -------------------------------------------------------------------------------- /test/theta_sketch_test.sql: -------------------------------------------------------------------------------- 1 | drop extension if exists datasketches cascade; 2 | create extension datasketches; 3 | 4 | drop table if exists theta_sketch_test; 5 | create table theta_sketch_test(sketch theta_sketch); 6 | 7 | -- default lgk 8 | insert into theta_sketch_test 9 | select theta_sketch_build(value) 10 | from (values (1), (2), (3), (4), (5)) as t(value) 11 | ; 12 | 13 | -- lgk = 16 14 | insert into theta_sketch_test 15 | select theta_sketch_build(value, 16) 16 | from (values (4), (5), (6), (7), (8)) as t(value) 17 | ; 18 | 19 | select theta_sketch_get_estimate(sketch) from theta_sketch_test; 20 | --select theta_sketch_to_string(sketch) from theta_sketch_test; 21 | 22 | -- default lgk 23 | select theta_sketch_get_estimate(theta_sketch_union(sketch)) from theta_sketch_test; 24 | -- lgk = 16 25 | select theta_sketch_get_estimate(theta_sketch_union(sketch, 16)) from theta_sketch_test; 26 | 27 | select theta_sketch_get_estimate(theta_sketch_intersection(sketch)) from theta_sketch_test; 28 | 29 | select theta_sketch_get_estimate(theta_sketch_a_not_b(theta_sketch_build(value1), theta_sketch_build(value2))) 30 | from (values (1, 2), (2, 3), (3, 4)) as t(value1, value2); 31 | 32 | drop table theta_sketch_test; 33 | drop extension datasketches; 34 | --------------------------------------------------------------------------------