|
| 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 | +from __future__ import annotations |
| 19 | + |
| 20 | +from concurrent.futures import ThreadPoolExecutor |
| 21 | + |
| 22 | +import pyarrow as pa |
| 23 | +from datafusion import Config, SessionContext, col, lit |
| 24 | +from datafusion import functions as f |
| 25 | +from datafusion.common import SqlSchema |
| 26 | + |
| 27 | + |
| 28 | +def _run_in_threads(fn, count: int = 8) -> None: |
| 29 | + with ThreadPoolExecutor(max_workers=count) as executor: |
| 30 | + futures = [executor.submit(fn, i) for i in range(count)] |
| 31 | + for future in futures: |
| 32 | + # Propagate any exception raised in the worker thread. |
| 33 | + future.result() |
| 34 | + |
| 35 | + |
| 36 | +def test_concurrent_access_to_shared_structures() -> None: |
| 37 | + """Exercise SqlSchema, Config, and DataFrame concurrently.""" |
| 38 | + |
| 39 | + schema = SqlSchema("concurrency") |
| 40 | + config = Config() |
| 41 | + ctx = SessionContext() |
| 42 | + |
| 43 | + batch = pa.record_batch([pa.array([1, 2, 3], type=pa.int32())], names=["value"]) |
| 44 | + df = ctx.create_dataframe([[batch]]) |
| 45 | + |
| 46 | + config_key = "datafusion.execution.batch_size" |
| 47 | + expected_rows = batch.num_rows |
| 48 | + |
| 49 | + def worker(index: int) -> None: |
| 50 | + schema.name = f"concurrency-{index}" |
| 51 | + assert schema.name.startswith("concurrency-") |
| 52 | + # Exercise getters that use internal locks. |
| 53 | + assert isinstance(schema.tables, list) |
| 54 | + assert isinstance(schema.views, list) |
| 55 | + assert isinstance(schema.functions, list) |
| 56 | + |
| 57 | + config.set(config_key, str(1024 + index)) |
| 58 | + assert config.get(config_key) is not None |
| 59 | + # Access the full config map to stress lock usage. |
| 60 | + assert config_key in config.get_all() |
| 61 | + |
| 62 | + batches = df.collect() |
| 63 | + assert sum(batch.num_rows for batch in batches) == expected_rows |
| 64 | + |
| 65 | + _run_in_threads(worker, count=12) |
| 66 | + |
| 67 | + |
| 68 | +def test_config_set_during_get_all() -> None: |
| 69 | + """Ensure config writes proceed while another thread reads all entries.""" |
| 70 | + |
| 71 | + config = Config() |
| 72 | + key = "datafusion.execution.batch_size" |
| 73 | + |
| 74 | + def reader() -> None: |
| 75 | + for _ in range(200): |
| 76 | + # get_all should not hold the lock while converting to Python objects |
| 77 | + config.get_all() |
| 78 | + |
| 79 | + def writer() -> None: |
| 80 | + for index in range(200): |
| 81 | + config.set(key, str(1024 + index)) |
| 82 | + |
| 83 | + with ThreadPoolExecutor(max_workers=2) as executor: |
| 84 | + reader_future = executor.submit(reader) |
| 85 | + writer_future = executor.submit(writer) |
| 86 | + reader_future.result(timeout=10) |
| 87 | + writer_future.result(timeout=10) |
| 88 | + |
| 89 | + assert config.get(key) is not None |
| 90 | + |
| 91 | + |
| 92 | +def test_case_builder_reuse_from_multiple_threads() -> None: |
| 93 | + """Ensure the case builder can be safely reused across threads.""" |
| 94 | + |
| 95 | + ctx = SessionContext() |
| 96 | + values = pa.array([0, 1, 2, 3, 4], type=pa.int32()) |
| 97 | + df = ctx.create_dataframe([[pa.record_batch([values], names=["value"])]]) |
| 98 | + |
| 99 | + base_builder = f.case(col("value")) |
| 100 | + |
| 101 | + def add_case(i: int) -> None: |
| 102 | + nonlocal base_builder |
| 103 | + base_builder = base_builder.when(lit(i), lit(f"value-{i}")) |
| 104 | + |
| 105 | + _run_in_threads(add_case, count=8) |
| 106 | + |
| 107 | + with ThreadPoolExecutor(max_workers=2) as executor: |
| 108 | + otherwise_future = executor.submit(base_builder.otherwise, lit("default")) |
| 109 | + case_expr = otherwise_future.result() |
| 110 | + |
| 111 | + result = df.select(case_expr.alias("label")).collect() |
| 112 | + assert sum(batch.num_rows for batch in result) == len(values) |
| 113 | + |
| 114 | + predicate_builder = f.when(col("value") == lit(0), lit("zero")) |
| 115 | + |
| 116 | + def add_predicate(i: int) -> None: |
| 117 | + predicate_builder.when(col("value") == lit(i + 1), lit(f"value-{i + 1}")) |
| 118 | + |
| 119 | + _run_in_threads(add_predicate, count=4) |
| 120 | + |
| 121 | + with ThreadPoolExecutor(max_workers=2) as executor: |
| 122 | + end_future = executor.submit(predicate_builder.end) |
| 123 | + predicate_expr = end_future.result() |
| 124 | + |
| 125 | + result = df.select(predicate_expr.alias("label")).collect() |
| 126 | + assert sum(batch.num_rows for batch in result) == len(values) |
0 commit comments