File size: 8,665 Bytes
4fc86f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pytest
import pyarrow as pa
from pyarrow import Codec
from pyarrow import fs
import numpy as np
groups = [
'acero',
'azure',
'brotli',
'bz2',
'cython',
'dataset',
'hypothesis',
'fastparquet',
'gandiva',
'gcs',
'gdb',
'gzip',
'hdfs',
'large_memory',
'lz4',
'memory_leak',
'nopandas',
'orc',
'pandas',
'parquet',
'parquet_encryption',
's3',
'snappy',
'substrait',
'flight',
'slow',
'requires_testing_data',
'zstd',
]
defaults = {
'acero': False,
'azure': False,
'brotli': Codec.is_available('brotli'),
'bz2': Codec.is_available('bz2'),
'cython': False,
'dataset': False,
'fastparquet': False,
'flight': False,
'gandiva': False,
'gcs': False,
'gdb': True,
'gzip': Codec.is_available('gzip'),
'hdfs': False,
'hypothesis': False,
'large_memory': False,
'lz4': Codec.is_available('lz4'),
'memory_leak': False,
'nopandas': False,
'orc': False,
'pandas': False,
'parquet': False,
'parquet_encryption': False,
'requires_testing_data': True,
's3': False,
'slow': False,
'snappy': Codec.is_available('snappy'),
'substrait': False,
'zstd': Codec.is_available('zstd'),
}
try:
import cython # noqa
defaults['cython'] = True
except ImportError:
pass
try:
import fastparquet # noqa
defaults['fastparquet'] = True
except ImportError:
pass
try:
import pyarrow.gandiva # noqa
defaults['gandiva'] = True
except ImportError:
pass
try:
import pyarrow.acero # noqa
defaults['acero'] = True
except ImportError:
pass
try:
import pyarrow.dataset # noqa
defaults['dataset'] = True
except ImportError:
pass
try:
import pyarrow.orc # noqa
defaults['orc'] = True
except ImportError:
pass
try:
import pandas # noqa
defaults['pandas'] = True
except ImportError:
defaults['nopandas'] = True
try:
import pyarrow.parquet # noqa
defaults['parquet'] = True
except ImportError:
pass
try:
import pyarrow.parquet.encryption # noqa
defaults['parquet_encryption'] = True
except ImportError:
pass
try:
import pyarrow.flight # noqa
defaults['flight'] = True
except ImportError:
pass
try:
from pyarrow.fs import AzureFileSystem # noqa
defaults['azure'] = True
except ImportError:
pass
try:
from pyarrow.fs import GcsFileSystem # noqa
defaults['gcs'] = True
except ImportError:
pass
try:
from pyarrow.fs import S3FileSystem # noqa
defaults['s3'] = True
except ImportError:
pass
try:
from pyarrow.fs import HadoopFileSystem # noqa
defaults['hdfs'] = True
except ImportError:
pass
try:
import pyarrow.substrait # noqa
defaults['substrait'] = True
except ImportError:
pass
# Doctest should ignore files for the modules that are not built
def pytest_ignore_collect(path, config):
if config.option.doctestmodules:
# don't try to run doctests on the /tests directory
if "/pyarrow/tests/" in str(path):
return True
doctest_groups = [
'dataset',
'orc',
'parquet',
'flight',
'substrait',
]
# handle cuda, flight, etc
for group in doctest_groups:
if 'pyarrow/{}'.format(group) in str(path):
if not defaults[group]:
return True
if 'pyarrow/parquet/encryption' in str(path):
if not defaults['parquet_encryption']:
return True
if 'pyarrow/cuda' in str(path):
try:
import pyarrow.cuda # noqa
return False
except ImportError:
return True
if 'pyarrow/fs' in str(path):
try:
from pyarrow.fs import S3FileSystem # noqa
return False
except ImportError:
return True
if getattr(config.option, "doctest_cython", False):
if "/pyarrow/tests/" in str(path):
return True
if "/pyarrow/_parquet_encryption" in str(path):
return True
return False
# Save output files from doctest examples into temp dir
@pytest.fixture(autouse=True)
def _docdir(request):
# Trigger ONLY for the doctests
doctest_m = request.config.option.doctestmodules
doctest_c = getattr(request.config.option, "doctest_cython", False)
if doctest_m or doctest_c:
# Get the fixture dynamically by its name.
tmpdir = request.getfixturevalue('tmpdir')
# Chdir only for the duration of the test.
with tmpdir.as_cwd():
yield
else:
yield
# Define doctest_namespace for fs module docstring import
@pytest.fixture(autouse=True)
def add_fs(doctest_namespace, request, tmp_path):
# Trigger ONLY for the doctests
doctest_m = request.config.option.doctestmodules
doctest_c = getattr(request.config.option, "doctest_cython", False)
if doctest_m or doctest_c:
# fs import
doctest_namespace["fs"] = fs
# Creation of an object and file with data
local = fs.LocalFileSystem()
path = tmp_path / 'pyarrow-fs-example.dat'
with local.open_output_stream(str(path)) as stream:
stream.write(b'data')
doctest_namespace["local"] = local
doctest_namespace["local_path"] = str(tmp_path)
doctest_namespace["path"] = str(path)
yield
# Define udf fixture for test_udf.py and test_substrait.py
@pytest.fixture(scope="session")
def unary_func_fixture():
"""
Register a unary scalar function.
"""
from pyarrow import compute as pc
def unary_function(ctx, x):
return pc.call_function("add", [x, 1],
memory_pool=ctx.memory_pool)
func_name = "y=x+1"
unary_doc = {"summary": "add function",
"description": "test add function"}
pc.register_scalar_function(unary_function,
func_name,
unary_doc,
{"array": pa.int64()},
pa.int64())
return unary_function, func_name
@pytest.fixture(scope="session")
def unary_agg_func_fixture():
"""
Register a unary aggregate function (mean)
"""
from pyarrow import compute as pc
def func(ctx, x):
return pa.scalar(np.nanmean(x))
func_name = "mean_udf"
func_doc = {"summary": "y=avg(x)",
"description": "find mean of x"}
pc.register_aggregate_function(func,
func_name,
func_doc,
{
"x": pa.float64(),
},
pa.float64()
)
return func, func_name
@pytest.fixture(scope="session")
def varargs_agg_func_fixture():
"""
Register a unary aggregate function
"""
from pyarrow import compute as pc
def func(ctx, *args):
sum = 0.0
for arg in args:
sum += np.nanmean(arg)
return pa.scalar(sum)
func_name = "sum_mean"
func_doc = {"summary": "Varargs aggregate",
"description": "Varargs aggregate"}
pc.register_aggregate_function(func,
func_name,
func_doc,
{
"x": pa.int64(),
"y": pa.float64()
},
pa.float64()
)
return func, func_name
|