From 3dcd80326b7e13295898522e6daea8d57aa8c6da Mon Sep 17 00:00:00 2001 From: Sagar Vora <16315650+sagarvora@users.noreply.github.com> Date: Tue, 2 Dec 2025 00:38:13 +0530 Subject: [PATCH] perf: replace sqlparse-based _is_function_call with regex - Remove sqlparse import (no longer used anywhere in query.py) - Add FUNCTION_CALL_PATTERN regex for detecting SQL function calls - Simplify _is_function_call from 6-line sqlparse parsing to 1-line regex match --- frappe/database/query.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/frappe/database/query.py b/frappe/database/query.py index f461959a1a..5cf2715734 100644 --- a/frappe/database/query.py +++ b/frappe/database/query.py @@ -3,7 +3,6 @@ import re from functools import lru_cache from typing import TYPE_CHECKING, Any -import sqlparse from pypika.enums import Arithmetic from pypika.queries import QueryBuilder, Table from pypika.terms import AggregateFunction, ArithmeticExpression, Star, Term, ValueWrapper @@ -98,14 +97,13 @@ SIMPLE_FIELD_PATTERN = re.compile(r"^\w+$", flags=re.ASCII) # More restrictive: must start with letter or underscore IDENTIFIER_PATTERN = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$", flags=re.ASCII) +# Pattern for detecting SQL function calls: identifier followed by opening parenthesis +FUNCTION_CALL_PATTERN = re.compile(r"^\s*[a-zA-Z_][a-zA-Z0-9_]*\s*\(", flags=re.ASCII) + def _is_function_call(field_str: str) -> bool: - """Check if a string is a SQL function call using sqlparse.""" - parsed = sqlparse.parse(field_str.strip()) - if not parsed: - return False - - return any(isinstance(token, sqlparse.sql.Function) for token in parsed[0].tokens) + """Check if a string is a SQL function call.""" + return bool(FUNCTION_CALL_PATTERN.match(field_str)) # Pattern to validate field names in SELECT: