Skip to content

Commit

Permalink
modif app.py superset pour optimiser parsing query flask
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain GREFFIER committed Nov 14, 2024
1 parent 58f9be9 commit 4a93709
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions superset/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,67 @@
import logging
import os
from typing import Optional
from functools import wraps, lru_cache
from unittest.mock import patch

from flask import Flask
import sqlparse

from superset.initialization import SupersetAppInitializer

logger = logging.getLogger(__name__)

def auto_patch(mock_mapping):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
patches = []
try:
# Apply patches before executing the function
for function_path, side_effect in mock_mapping.items():
patcher = patch(function_path, side_effect=side_effect)
patches.append(patcher.start()) # Start each patch

# Execute the original function with patches in place
return func(*args, **kwargs)
finally:
# Ensure patches are cleaned up after function execution
for patcher in patches:
patcher.stop()

return wrapper
return decorator

# Getting original references to the to-be intercepted function before hand
# so as to avoid call recursion once the patch is done
orig_sqlparse_parse_func = sqlparse.parse
orig_sqlparse_format_func = sqlparse.format

@lru_cache(maxsize=100)
def cached_sqlparse_parse(*args, **kwargs):
return orig_sqlparse_parse_func(*args, **kwargs)

# It will be useful to cache in case, original sqlparse.format function is
# used
# @lru_cache(maxsize=100)
def mock_sqlparse_format(sql, *args, **kwargs):
# One can retain the sqlparse_format in case it is required for their use case.
# value = orig_sqlparse_format_func(sql, *args, **kwargs)
value = sql
return value

def mock_sqlparse_parse(*args, **kwargs):
value = cached_sqlparse_parse(*args, **kwargs)
return value

patch_map = {
'sqlparse.format': mock_sqlparse_format,
'sqlparse.parse': mock_sqlparse_parse
}


# Patch all respective original entities with it's counter part mock entities
@auto_patch(patch_map)
def create_app(superset_config_module: Optional[str] = None) -> Flask:
app = SupersetApp(__name__)

Expand All @@ -42,9 +95,9 @@ def create_app(superset_config_module: Optional[str] = None) -> Flask:
return app

# Make sure that bootstrap errors ALWAYS get logged
except Exception:
except Exception as ex:
logger.exception("Failed to create app")
raise
raise ex


class SupersetApp(Flask):
Expand Down

0 comments on commit 4a93709

Please sign in to comment.