SQL parser: StrictFragmentValidator refuses GROUP BY GROUPING SETS as Unparseable (ad-hoc SQL rejected)

Aperta
#6,941 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
sql, typescript
Ambito
databases

Direzione di ricerca

Start with the StrictFragmentValidator.ValidateReadOnlyStatement path in the sql-parser package and find where GROUP BY clause parsing handles ROLLUP and CUBE, since those pass. Reproduce with the GROUPING SETS statement from the issue and confirm it returns Unparseable while IsReadOnlyQuery accepts it. Done when the GROUPING SETS statements, including the CTE form, validate as read-only and the ROLLUP and CUBE cases still pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

On MJ 6.2.2, StrictFragmentValidator.ValidateReadOnlyStatement refuses any SQL Server query that uses GROUP BY GROUPING SETS as Unparseable ("the SQL could not be parsed"). The statement is a plain read. ROLLUP, CUBE and GROUPING() all pass, and the token-based IsReadOnlyQuery accepts the same SQL.

Because the strict validator gates ad-hoc SQL in MJServer, a valid read-only report that uses GROUPING SETS can't be run ad hoc.

Repro (@memberjunction/sql-parser 6.2.2, SQLServerDialect)

const { StrictFragmentValidator, IsReadOnlyQuery } = require('@memberjunction/sql-parser');
const { SQLServerDialect } = require('@memberjunction/sql-dialect');
const dialect = new SQLServerDialect();

const sql = "SELECT Region, Stage, COUNT(*) AS n FROM dbo.T GROUP BY GROUPING SETS ((Region, Stage), (Region), ())";
StrictFragmentValidator.ValidateReadOnlyStatement(sql, dialect);
// { IsValid: false, Refusal: 'Unparseable', Reason: 'the SQL could not be parsed' }
IsReadOnlyQuery(sql, dialect);
// { IsReadOnly: true }
Statement ValidateReadOnlyStatement IsReadOnlyQuery
GROUP BY GROUPING SETS ((a, b), (a), ()) Unparseable read-only
same inside a WITH CTE query Unparseable read-only
GROUP BY ROLLUP (a) valid read-only
GROUP BY CUBE (a, b) valid read-only
GROUPING(a) with ROLLUP valid read-only

Where it bites

ResolverBase.assertFullQueryUsesReadableEntityViews runs the strict validator. That check is called from:

  • AdhocQueryResolver (ad-hoc SQL, including re-running a saved query's SQL from an artifact)
  • GetDataContextDataResolver (data context SQL)
  • TestQuerySQLResolver (transient test SQL)

The resulting error is Invalid ad-hoc SQL: not a safe read-only statement — refusing under uncertainty. Saved queries run through RunQuery aren't affected.

Found on a client deployment: a shared report that uses GROUPING SETS for region subtotals and a grand total failed when re-run ad hoc. The client rewrote it with UNION ALL as a workaround.

Expected

GROUPING SETS (T-SQL, and PostgreSQL, which supports the same syntax) should parse, and the statement should be accepted as read-only like ROLLUP and CUBE. Failing closed on unreadable SQL is right, but this is standard read syntax the parser should know.

Lingua principale
TSQL
Stelle
32
Fork
7
Merge medio
1g 16h
PR unite (30g)
504

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di MemberJunction/MJ

Tutte le issue di MemberJunction/MJ

Issue simili

Altre issue su Databases

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.