Skip to content

Commit

Permalink
Merge pull request #1200 from shangyian/fix-greenlet-cube-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
shangyian authored Oct 11, 2024
2 parents 81bc49a + c9be191 commit fd1f143
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ jobs:
prerelease: true
enable-pep582: true

- uses: pre-commit/[email protected]
name: Force check of all pdm.lock files
with:
extra_args: pdm-lock-check --all-files

- name: Run Tests
if: |
(matrix.library == 'client' && steps.filter.outputs.client == 'true') ||
Expand All @@ -84,11 +89,6 @@ jobs:
export MODULE=${{ matrix.library == 'server' && 'datajunction_server' || matrix.library == 'client' && 'datajunction' || matrix.library == 'djqs' && 'djqs' || matrix.library == 'djrs' && 'datajunction_reflection'}}
pdm run pytest ${{ (matrix.library == 'server' || matrix.library == 'client') && '-n auto' || '' }} --cov-fail-under=100 --cov=$MODULE --cov-report term-missing -vv tests/ --doctest-modules $MODULE --without-integration --without-slow-integration
- uses: pre-commit/[email protected]
name: Force check of all pdm.lock files
with:
extra_args: pdm-lock-check --all-files

build-javascript:
runs-on: ubuntu-latest
strategy:
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ repos:
entry: pdm lock --check --project .
files: ^pyproject.toml$
- repo: https://github.com/pdm-project/pdm
rev: 2.11.1
rev: 2.18.2
hooks:
- id: pdm-lock-check
name: pdm-lock-check-server
Expand All @@ -124,7 +124,7 @@ repos:
entry: pdm lock --check --project datajunction-query
files: ^datajunction-query/pyproject.toml$
- repo: https://github.com/pdm-project/pdm
rev: 2.8.1
rev: 2.18.2
hooks:
- id: pdm-lock-check
name: pdm-lock-check-reflection
Expand Down
55 changes: 53 additions & 2 deletions datajunction-server/datajunction_server/api/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ async def build_and_save_node_sql( # pylint: disable=too-many-locals
# If it's a cube, we'll build SQL for the metrics in the cube, along with any additional
# dimensions or filters provided in the arguments
if node.type == NodeType.CUBE:
node = cast(
Node,
await Node.get_cube_by_name(session, node_name),
)
dimensions = list(
OrderedDict.fromkeys(node.current.cube_node_dimensions + dimensions),
)
Expand Down Expand Up @@ -362,6 +366,7 @@ async def get_sql_for_metrics( # pylint: disable=too-many-locals
),
ignore_errors: Optional[bool] = True,
use_materialized: Optional[bool] = True,
background_tasks: BackgroundTasks,
) -> TranslatedSQL:
"""
Return SQL for a set of metrics with dimensions and filters
Expand All @@ -384,6 +389,21 @@ async def get_sql_for_metrics( # pylint: disable=too-many-locals
engine_version=engine_version,
query_type=QueryBuildType.METRICS,
):
# Update the node SQL in a background task to keep it up-to-date
background_tasks.add_task(
build_and_save_sql_for_metrics,
session=session,
metrics=metrics,
dimensions=dimensions,
filters=filters,
orderby=orderby,
limit=limit,
engine_name=engine_name,
engine_version=engine_version,
access_control=access_control,
ignore_errors=ignore_errors,
use_materialized=use_materialized,
)
engine = (
await get_engine(session, engine_name, engine_version) # type: ignore
if engine_name
Expand All @@ -395,6 +415,37 @@ async def get_sql_for_metrics( # pylint: disable=too-many-locals
dialect=engine.dialect if engine else None,
)

return await build_and_save_sql_for_metrics(
session,
metrics,
dimensions,
filters,
orderby,
limit,
engine_name,
engine_version,
access_control,
ignore_errors=ignore_errors, # type: ignore
use_materialized=use_materialized, # type: ignore
)


async def build_and_save_sql_for_metrics( # pylint: disable=too-many-arguments,too-many-locals
session: AsyncSession,
metrics: List[str],
dimensions: List[str],
filters: List[str] = None,
orderby: List[str] = None,
limit: Optional[int] = None,
engine_name: Optional[str] = None,
engine_version: Optional[str] = None,
access_control: Optional[access.AccessControlStore] = None,
ignore_errors: bool = True,
use_materialized: bool = True,
):
"""
Builds and saves SQL for metrics.
"""
translated_sql, _, _ = await build_sql_for_multiple_metrics(
session,
metrics,
Expand All @@ -413,8 +464,8 @@ async def get_sql_for_metrics( # pylint: disable=too-many-locals
session=session,
nodes=metrics,
dimensions=dimensions,
filters=filters,
orderby=orderby,
filters=filters, # type: ignore
orderby=orderby, # type: ignore
limit=limit,
engine_name=engine_name,
engine_version=engine_version,
Expand Down

0 comments on commit fd1f143

Please sign in to comment.