diff --git a/tiledb/core.cc b/tiledb/core.cc index fc8af53ea5..22ddf5d003 100644 --- a/tiledb/core.cc +++ b/tiledb/core.cc @@ -1594,6 +1594,8 @@ void init_stats() { void disable_stats() { g_stats.reset(nullptr); } +bool stats_enabled() { return (bool)g_stats; } + void increment_stat(std::string key, double value) { auto &stats_counters = g_stats.get()->counters; @@ -1759,6 +1761,7 @@ void init_core(py::module &m) { Stats::disable(); disable_stats(); }); + m.def("stats_enabled", &stats_enabled); m.def("reset_stats", []() { Stats::reset(); init_stats(); diff --git a/tiledb/stats.py b/tiledb/stats.py index 47aeeb4e4c..cbcc73823b 100644 --- a/tiledb/stats.py +++ b/tiledb/stats.py @@ -1,6 +1,8 @@ from json import dumps as json_dumps from json import loads as json_loads +from tiledb import TileDBError + def stats_enable(): """Enable TileDB internal statistics.""" @@ -34,9 +36,12 @@ def stats_dump( :param json: Return stats JSON object (default: False) :param verbose: Print extended internal statistics (default: True) """ - from .main import stats_dump_str, stats_raw_dump_str + from .main import stats_dump_str, stats_enabled, stats_raw_dump_str - stats_str = None + if not stats_enabled(): + raise TileDBError( + "Statistics are not enabled. Call tiledb.stats_enable() first." + ) if json or not verbose: stats_str = stats_raw_dump_str() @@ -50,9 +55,9 @@ def stats_dump( if include_python: from .main import python_internal_stats - stats_json_core["python"] = json_dumps(python_internal_stats(True)) + stats_json_core["python"] = python_internal_stats(True) if json: - return stats_json_core + return json_dumps(stats_json_core) stats_str = "" diff --git a/tiledb/tests/test_fixes.py b/tiledb/tests/test_fixes.py index 3ba9b81d92..a4ab4f65f0 100644 --- a/tiledb/tests/test_fixes.py +++ b/tiledb/tests/test_fixes.py @@ -1,5 +1,6 @@ import concurrent import concurrent.futures +import json import os import subprocess import sys @@ -172,6 +173,61 @@ def test_sc16301_arrow_extra_estimate_dense(self): ) tiledb.stats_disable() + def test_sc58286_fix_stats_dump_return_value_broken(self): + uri = self.path("test_sc58286_fix_stats_dump_return_value_broken") + dim1 = tiledb.Dim(name="d1", dtype="int64", domain=(1, 3)) + att = tiledb.Attr(name="a1", dtype="