diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp index 7243fd546..15b5164a0 100644 --- a/src/query/interpreter.cpp +++ b/src/query/interpreter.cpp @@ -1563,7 +1563,8 @@ std::vector> AnalyzeGraphQueryHandler::AnalyzeGraphCreat total_degree += *vertex.OutDegree(view) + *vertex.InDegree(view); }); - auto average_degree = static_cast(total_degree) / static_cast(no_vertices); + auto average_degree = + no_vertices > 0 ? static_cast(total_degree) / static_cast(no_vertices) : 0; auto index_stats = storage::LabelIndexStats{.count = no_vertices, .avg_degree = average_degree}; execution_db_accessor->SetIndexStats(index_info, index_stats); label_stats.emplace_back(std::make_pair(index_info, index_stats)); @@ -1597,8 +1598,9 @@ std::vector> AnalyzeGraphQueryHandler::AnalyzeGraphCreat return prev_result + utils::ChiSquaredValue(value_entry.second, avg_group_size); }); - double average_degree = - static_cast(vertex_degree_counter[label_property]) / static_cast(count_property_value); + double average_degree = count_property_value > 0 ? static_cast(vertex_degree_counter[label_property]) / + static_cast(count_property_value) + : 0; auto index_stats = storage::LabelPropertyIndexStats{.count = count_property_value, diff --git a/tests/e2e/analyze_graph/optimize_indexes.py b/tests/e2e/analyze_graph/optimize_indexes.py index 54b2a33c8..f92c051f0 100644 --- a/tests/e2e/analyze_graph/optimize_indexes.py +++ b/tests/e2e/analyze_graph/optimize_indexes.py @@ -454,5 +454,23 @@ def test_given_supernode_when_subquery_and_union_then_carry_information(memgraph assert expected_explain == result_with_analysis +def test_given_empty_graph_when_analyzing_graph_return_zero_degree(memgraph): + memgraph.execute("CREATE INDEX ON :Node;") + + label_stats = next(memgraph.execute_and_fetch("analyze graph;")) + + expected_analysis = { + "label": "Node", + "property": None, + "num estimation nodes": 0, + "num groups": None, + "avg group size": None, + "chi-squared value": None, + "avg degree": 0.0, + } + + assert set(label_stats) == set(expected_analysis) + + if __name__ == "__main__": sys.exit(pytest.main([__file__, "-rA"]))