Skip to content

Commit

Permalink
Add a warning to the Python version of the parser (#30)
Browse files Browse the repository at this point in the history
Adding a warning that gets logged whenever the slow Python version of the parser is used.
  • Loading branch information
RokasUrbonasQC authored Aug 22, 2022
1 parent 82193d4 commit 643fd9e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/pytsql/grammar/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .sa_tsql import parse, SA_ErrorListener, tsqlParser
from .sa_tsql import parse, SA_ErrorListener, tsqlParser, USE_CPP_IMPLEMENTATION
1 change: 0 additions & 1 deletion src/pytsql/grammar/sa_tsql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# This file was auto-generated by speedy-antlr-tool v1.3.1
# https://github.com/amykyta3/speedy-antlr-tool

import sys
import types
from typing import Optional
Expand Down
10 changes: 9 additions & 1 deletion src/pytsql/tsql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
import warnings
from pathlib import Path
from re import Match
from typing import Any, Dict, List, Optional, Union
Expand All @@ -9,7 +10,7 @@
from antlr4 import InputStream, Token
from sqlalchemy.engine import Connection

from pytsql.grammar import SA_ErrorListener, parse, tsqlParser
from pytsql.grammar import USE_CPP_IMPLEMENTATION, SA_ErrorListener, parse, tsqlParser

_REPLACE_START = "<replace>"
_REPLACE_END = "</replace>"
Expand Down Expand Up @@ -143,6 +144,13 @@ def syntaxError(


def _split(code: str) -> List[str]:
if not USE_CPP_IMPLEMENTATION:
warnings.warn(
"Can not find C++ version of the parser, Python version will be used instead."
" Something is likely wrong with your installation of the package,"
" and is preventing the use of the faster C++ parser."
)

logging.info("Started SQL script parsing")

# The default error listener only prints to the console without raising exceptions.
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_py_vs_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from antlr4.Token import CommonToken
from antlr4.tree.Tree import TerminalNodeImpl

from pytsql import tsql
from pytsql.grammar import sa_tsql
from pytsql.tsql import _split

Expand Down Expand Up @@ -135,3 +136,9 @@ def test_compare_py_and_cpp__split(py_parse_mock, cpp_parse_mock, seed):
cpp_parse_mock.assert_called_once()

assert py_result == cpp_result


def test__py_parse__warns():
tsql.USE_CPP_IMPLEMENTATION = False
with pytest.warns(match=r"Can not find C\+\+ version of the parser"):
_split("")

0 comments on commit 643fd9e

Please sign in to comment.