-
-
Save wesm/450d85e52844aee685c0680111cbb1d7 to your computer and use it in GitHub Desktop.
Parquet direct dictionary decoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pyarrow as pa\n", | |
"import pyarrow.parquet as pq\n", | |
"import pandas as pd\n", | |
"from pandas.util.testing import rands\n", | |
" \n", | |
"NUNIQUE = 1000\n", | |
"STRING_SIZE = 50\n", | |
"LENGTH = 10_000_000\n", | |
"REPEATS = LENGTH // NUNIQUE\n", | |
"\n", | |
"data = [rands(STRING_SIZE) for i in range(NUNIQUE)] * REPEATS\n", | |
"table = pa.table([data], names=['f0'])\n", | |
"\n", | |
"out_stream = pa.BufferOutputStream()\n", | |
"pq.write_table(table, out_stream)\n", | |
"contents = out_stream.getvalue()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1129939" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(contents)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0\n" | |
] | |
} | |
], | |
"source": [ | |
"import gc\n", | |
"class memory_use:\n", | |
" \n", | |
" def __init__(self):\n", | |
" self.start_use = pa.total_allocated_bytes()\n", | |
" \n", | |
" def __enter__(self):\n", | |
" return\n", | |
" \n", | |
" def __exit__(self, type, value, traceback):\n", | |
" gc.collect()\n", | |
" print(pa.total_allocated_bytes() - self.start_use)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"541250112\n" | |
] | |
} | |
], | |
"source": [ | |
"with memory_use():\n", | |
" memory_use_no_dict = pq.read_table(pa.BufferReader(contents))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"41304128\n" | |
] | |
} | |
], | |
"source": [ | |
"with memory_use():\n", | |
" memory_use_dict = pq.read_table(pa.BufferReader(contents), read_dictionary=['f0'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.79 s ± 7.86 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit memory_use_no_dict = pq.read_table(pa.BufferReader(contents))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"106 ms ± 11.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit memory_use_dict = pq.read_table(pa.BufferReader(contents), read_dictionary=['f0'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment