Skip to content

Commit f4a3539

Browse files
authored
feat: add logging filter to suppress erroneous sqlglot fallback warnings (#94)
Adds a logging filter to suppress a misleading sqlglot logger warning that prints when running procedural code. It should be parsed as a Command, which is what the warning is stating.
1 parent 53306a4 commit f4a3539

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

sqlspec/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
StatementParameters,
4545
SupportedSchemaModel,
4646
)
47+
from sqlspec.utils.logging import suppress_erroneous_sqlglot_log_messages
48+
49+
suppress_erroneous_sqlglot_log_messages()
4750

4851
__all__ = (
4952
"SQL",

sqlspec/utils/logging.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212

1313
from sqlspec._serialization import encode_json
1414

15-
__all__ = ("StructuredFormatter", "correlation_id_var", "get_correlation_id", "get_logger", "set_correlation_id")
15+
__all__ = (
16+
"SqlglotCommandFallbackFilter",
17+
"StructuredFormatter",
18+
"correlation_id_var",
19+
"get_correlation_id",
20+
"get_logger",
21+
"set_correlation_id",
22+
"suppress_erroneous_sqlglot_log_messages",
23+
)
1624

1725
correlation_id_var: "ContextVar[Optional[str]]" = ContextVar("correlation_id", default=None)
1826

@@ -86,6 +94,26 @@ def filter(self, record: LogRecord) -> bool:
8694
return True
8795

8896

97+
class SqlglotCommandFallbackFilter(logging.Filter):
98+
"""Filter to suppress sqlglot's confusing 'Falling back to Command' warning.
99+
100+
This filter suppresses the warning message that sqlglot emits when it
101+
encounters unsupported syntax and falls back to parsing as a Command.
102+
This is expected behavior in SQLSpec and the warning is confusing to users.
103+
"""
104+
105+
def filter(self, record: LogRecord) -> bool:
106+
"""Suppress the 'Falling back to Command' warning message.
107+
108+
Args:
109+
record: The log record to evaluate
110+
111+
Returns:
112+
False if the record contains the fallback warning, True otherwise
113+
"""
114+
return "Falling back to parsing as a 'Command'" not in record.getMessage()
115+
116+
89117
def get_logger(name: "Optional[str]" = None) -> logging.Logger:
90118
"""Get a logger instance with standardized configuration.
91119
@@ -121,3 +149,15 @@ def log_with_context(logger: logging.Logger, level: int, message: str, **extra_f
121149
record = logger.makeRecord(logger.name, level, "(unknown file)", 0, message, (), None)
122150
record.extra_fields = extra_fields
123151
logger.handle(record)
152+
153+
154+
def suppress_erroneous_sqlglot_log_messages() -> None:
155+
"""Suppress confusing sqlglot warning messages.
156+
157+
Adds a filter to the sqlglot logger to suppress the warning message
158+
about falling back to parsing as a Command. This is expected behavior
159+
in SQLSpec and the warning is confusing to users.
160+
"""
161+
sqlglot_logger = logging.getLogger("sqlglot")
162+
if not any(isinstance(f, SqlglotCommandFallbackFilter) for f in sqlglot_logger.filters):
163+
sqlglot_logger.addFilter(SqlglotCommandFallbackFilter())

0 commit comments

Comments
 (0)