Skip to content

Fixes #29504: Stop cutting PowerBI BigQuery native query SQL at comment markers - #36207

Merged
harshsoni2024 merged 4 commits into
mainfrom
fix-29504-powerbi-bigquery-native-query
Oct 26, 2026
Merged

harshsoni2024 merged 4 commits into
mainfrom
fix-29504-powerbi-bigquery-native-query

Conversation

@harshsoni2024

@harshsoni2024 harshsoni2024 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #29504

PowerBI finds the upstream tables of a table backed by a BigQuery Value.NativeQuery by pulling the SQL out of the M expression and parsing it. _parse_bigquery_query_source cleaned that SQL with regexes that cannot tell a comment from a string literal, and then flattened it onto one line:

  • -- was stripped to the end of the line, so a literal such as '--' cut the query and left an unterminated string.
  • /* */ and // were stripped from the whole M expression, the SQL literal included. A literal such as 'https://…' or 'gs://…' cut the M string, so the SQL was never found, and one such as 'exports/*.csv' lost everything up to the next */.
  • Flattening made a BigQuery # comment run to the end of the query.

When the cut took the FROM with it, the table resolved no upstream table and its data model got no lineage. The M comment strip now matches M text literals first and keeps them, and the SQL reaches LineageParser as written, with its line breaks and comments. SqlGlot, SqlFluff and SqlParse know where a literal ends and where each BigQuery comment (--, #, /* */) ends.

This needs no SQL tokenizer of our own, the approach #29946 was closed over: the SQL is no longer scanned for comments at all. M text literals have one syntax ("…", with "" as the only escape), so skipping them takes one exact pattern.

The issue's own expression (where ISO2 <> '--' on the last line) already gets its lineage on main. SqlGlot and SqlFluff fail on the cut query, with the TokenError/IndexError and "partially unparsable" errors the issue quotes, but the SqlParse fallback still finds full_CountriesTbl_Current_State. The same holds on 1.13.0.0 with the reporter's collate-sqllineage 2.1.3. So the regression tests use shapes that do lose lineage on main. With this change the issue's expression reaches the parser intact, and SqlGlot parses it on the first try.

Not changed: three other SQL extractors in this connector flatten the SQL onto one line the same way: the Snowflake native query parser (_parse_snowflake_query_source), the Databricks one (parse_databricks_native_query_source), and _extract_tables_from_sql, which reads SQL Server Sql.Database queries for semantic-model tables and dataflows.

  • None of them strips --, so a real -- comment hides the rest of the query. select id, amount -- net of tax#(lf)from sales.orders resolves no table on any of the three, while the same query without the comment resolves sales.orders.
  • The Snowflake and Databricks parsers also strip // from the SQL, so 'https://…' before FROM resolves no table there.

Those paths are untouched by this PR and have their own dialect rules (Snowflake treats // as a comment), so they are left for a follow-up; no issue filed yet.

Type of change:

  • Bug fix

High-level design:

N/A — small bug fix in one method; the approach is described above.

Tests:

Use cases covered

  • A BigQuery native query with '--' in a literal before FROM resolves its upstream table.
  • A literal holding // ('https://…') before FROM resolves its upstream table.
  • A literal holding /* ('exports/*.csv') before FROM, with a /* */ comment after it, resolves its upstream table.
  • A BigQuery # comment line no longer hides the FROM after it.
  • An M block comment holding an older Source step is still ignored, so lineage goes to the live query's table, not the commented-out one.
  • Real -- comments, and M /* */ and // comments, including a commented-out Value.NativeQuery step, still work: the existing test_parse_database_source cases, unchanged.

Unit tests

  • I added unit tests for the new/changed logic.
  • Added: ingestion/tests/unit/topology/dashboard/test_powerbi_bigquery_native_query.py (5 tests). A real PowerbiSource, with only its connection patched, is driven through _parse_table_info_from_source_exp, which the data model lineage stage calls. Each literal case puts its marker before FROM on the same line, so a cut there leaves no table for the SqlParse fallback to find.
  • On main, 4 of the 5 fail: the table resolves no upstream table (None). test_block_commented_source_step_is_ignored passes on main; it guards the block-comment half of the new pattern, which nothing else tests.
  • Each part of the fix was reverted in-process, and each revert fails a test:
    • the literal-unaware M comment strip: [m-line-comment-marker] and [m-block-comment-marker];
    • a literal-unaware block-comment strip run ahead of the new pattern: [m-block-comment-marker];
    • the block-comment alternative dropped from the pattern, or re.DOTALL dropped from the strip: test_block_commented_source_step_is_ignored;
    • the SQL // strip: [m-line-comment-marker];
    • the -- strip: [sql-line-comment-marker];
    • newline flattening: test_bigquery_hash_comment_ends_at_its_line, and the existing test_parse_database_source, whose -- main table comment then hides the JOIN.
  • Ran every unit test that covers a changed file (the new file, plus every test importing a changed module): test_powerbi.py 59, test_powerbi_bigquery_native_query.py 5, test_powerbi_client.py 14, test_powerbi_metrics.py 22, test_powerbi_report_pages.py 17, test_powerbi_resilience.py 17, test_powerbi_workspace_state.py 12, test_dashboard_owned_connection.py 5, source/dashboard/powerbi/test_connection.py 39, test_file_client_path_traversal.py 5, test_file_client_tmpdir.py 6, test_powerbi_filter_query.py 10, test_powerbi_table_measures.py 13: 224 passed, 0 failed.
  • Coverage (pytest-cov): every changed executable line is covered. File totals are constants.py 100% and metadata.py 66%; the rest of metadata.py is untouched.
  • Static checks: ruff check, ruff format --check and scripts/check_ruff_suppressions.py --check are clean. basedpyright (--baselinemode=discard) reports 0 errors on metadata.py and constants.py.

Backend integration tests

  • Not applicable (no backend API changes).

Ingestion integration tests

  • I ran the integration tests covering my changed files locally (PR CI runs only unit tests; the merge queue runs tests/integration/).
  • tests/integration/powerbi/test_powerbi_file_client.py: 1 passed. It is the only PowerBI integration test, and it covers the file client, not lineage. No integration test added: the change is confined to parsing the M expression, before any API or server call, and the unit test drives that boundary with a real source.

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

No PowerBI dataset backed by a BigQuery native query was available. The M expressions were replayed through _parse_bigquery_source, with the real LineageParser (collate-sqllineage 2.1.8), on main and on this branch.

  1. The issue's expression, with debug logging:

    • On main: the SQL handed to LineageParser ends where ISO2 <> ' (query hash 33262d6d, the same as the production logs in Lineage: SQLParse & SQLGlot query masking and tokenization failures #28894). SqlGlot raises TokenError (IndexError: string index out of range), SqlFluff reports "select_statement is partially unparsable", and the SqlParse fallback returns full_CountriesTbl_Current_State.
    • On this branch: the SQL arrives as written, line breaks included, ending where ISO2 <> '--'. SqlGlot parses it on the first try and returns the same table.
  2. Query shapes, main vs this branch:

    Query shape main This PR
    Issue's expression ('--' on the last line) ok ok
    One-line SQL, '--' before FROM no upstream table ok
    '--' in a CTE, JOIN on later lines ok ok
    'https://…' literal no upstream table ok
    'exports/*.csv' literal, /* */ comment after FROM no upstream table ok
    BigQuery # comment line no upstream table ok
    Real -- comment mid-query ok ok
    Real /* */ and -- comments in the SQL ok ok
    Block-commented old Source step above the live one ok ok
  • Not run: a live PowerBI tenant.

UI screen recording:

Not applicable — ingestion-only change.

Checklist:

  • I have read the CONTRIBUTING document.

  • My PR title is Fixes <issue-number>: <short explanation>

  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.

  • I have commented on my code, particularly in hard-to-understand areas.

  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed. Not applicable: no schema change.

  • For a UI feature or feature task: as part of PR creation, I ran the PR build in Docker with sample data and embedded a verified GitHub-hosted recording plus build/health evidence above (or explained why this requirement is not applicable). Not applicable: ingestion-only change.

  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

  • Self-review: I reviewed the final diff against REVIEW.md (the pr-checklist skill runs it) and fixed every 🔴 finding, or answered it on the line below.
    Pass 1 on 6d6e89c0d5f: no 🔴. 🟡 the Snowflake, Databricks and _extract_tables_from_sql paths keep the same flattening and // strip: pre-existing and outside [PowerBI] BigQuery NativeQuery lineage lost: -- comment stripper truncates SQL string literals containing "--" #29504, stated under "Not changed" with the trigger inputs, re-run on this branch, and left for a follow-up. 🟡 the block-comment half of the new pattern had no test (dropping it, or running a literal-unaware block strip ahead of it, left every test green): fixed in f8eec1699d8, a test-only change that both mutants now fail. Pass 2 on f8eec1699d8: no 🔴. Noted, not a finding: re.DOTALL on the strip went untested, because the test's block comment sat on one line. The comment now spans lines (2fa90bd34dc), and dropping the flag fails the test. Pass 3 on 2fa90bd34dc: no 🔴.

  • Decision check: if this PR decides something the next contributor could undo — a schema field, REST path or format another component or repo must agree with, a new default or limit, an ordering that must hold, or a rule stated only in this description — it adds a record under docs/decisions/ (see its README.md). This description is gone from git log after the squash-merge.
    Not applicable: a self-evident bug fix that sets no contract, default or ordering.

  • I have added a test that covers the exact scenario we are fixing. For complex issues, comment the issue number in the test for future reference.
    The tests cover the defect the issue reports, a '--' literal cutting the SQL, in shapes that lose lineage on main; the reported expression itself is rescued by SqlParse on main (see above).

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issue was found.

Summary

This PR keeps comment markers inside BigQuery SQL strings from cutting off Power BI lineage queries.

  • Power BI keeps BigQuery query text intact for lineage parsing.

The ancestry check passed, but the target merge bases differed. The full PR diff was therefore reviewed as the changes since the last review. No previous review threads were supplied. Tests were inspected, not run.

Reviews (2) · Last reviewed commit: "Merge branch 'main' into fix-29504-power..." · Reviewed by Greptile

…nt markers

_parse_bigquery_query_source stripped -- and // from the native query SQL,
and /* */ and // from the whole M expression, with regexes that cannot tell
a comment from a string literal, then flattened the SQL onto one line. A
literal such as '--' or 'https://...' cut the query, and a BigQuery #
comment ran to its end. A table whose FROM was cut resolved no upstream
table, so its data model got no lineage.

The M comment strip now matches M text literals first and keeps them, and
the SQL reaches LineageParser as written, with its line breaks and
comments. The parsers know where a literal ends.
Dropping /\*.*?\*/ from the pattern, or stripping block comments ahead of
it without regard to literals, left every test green. A block-commented
Source step above the live one, and a '/*' inside a SQL literal before
FROM, now fail under each of those.
With the comment on one line, dropping re.DOTALL from the M comment strip
left every test green.
@harshsoni2024
harshsoni2024 requested a review from a team as a code owner October 26, 2026 07:53
@harshsoni2024
harshsoni2024 requested review from mohittilala and a balanced review from Copilot October 26, 2026 07:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@gitar-bot

gitar-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Preserving SQL comments and line breaks in PowerBI BigQuery lineage parsing could misattribute upstream tables if the parser interprets them unexpectedly.

Stops the PowerBI BigQuery native query parser from cutting SQL at comment markers that appear inside string literals, such as '--', 'https://…', or 'exports/*.csv'. The M comment strip now preserves text literals, and the SQL reaches LineageParser with its line breaks and comments intact. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@harshsoni2024
harshsoni2024 added this pull request to the merge queue Oct 26, 2026
harshsoni2024 added a commit that referenced this pull request Oct 26, 2026
@sonarqubecloud

Copy link
Copy Markdown

Merged via the queue into main with commit f444f05 Oct 26, 2026
83 checks passed
@harshsoni2024
harshsoni2024 deleted the fix-29504-powerbi-bigquery-native-query branch October 26, 2026 15:07
stout-yeoman pushed a commit that referenced this pull request Nov 4, 2026
…- comments end at their line (#36748)

_extract_tables_from_sql turned #(lf) into newlines and then collapsed all
whitespace onto one line before calling LineageParser. A -- comment then ran
to the end of the query, so every table after it was dropped from lineage.
The SQL now reaches LineageParser with its line breaks, as the BigQuery
native query path already does since #36207.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ingestion safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PowerBI] BigQuery NativeQuery lineage lost: -- comment stripper truncates SQL string literals containing "--"

3 participants