Skip to content

Commit

Permalink
Enable QUALIFY without GROUP BY / WHERE / HAVING (#38)
Browse files Browse the repository at this point in the history
* Enable `QUALIFY` without `GROUP BY` / `WHERE` / `HAVING`

* handle error
  • Loading branch information
ohaibbq authored Mar 29, 2024
1 parent 42838f7 commit 2ca16b0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
6 changes: 5 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ func newZetaSQLiteConn(db *sql.DB, catalog *internal.Catalog) (*ZetaSQLiteConn,
if err != nil {
return nil, fmt.Errorf("failed to get sqlite3 connection: %w", err)
}
analyzer, err := internal.NewAnalyzer(catalog)
if err != nil {
return nil, fmt.Errorf("failed to create analyzer: %w", err)
}
return &ZetaSQLiteConn{
conn: conn,
analyzer: internal.NewAnalyzer(catalog),
analyzer: analyzer,
}, nil
}

Expand Down
20 changes: 15 additions & 5 deletions internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ type Analyzer struct {
opt *zetasql.AnalyzerOptions
}

func NewAnalyzer(catalog *Catalog) *Analyzer {
func NewAnalyzer(catalog *Catalog) (*Analyzer, error) {
opt, err := newAnalyzerOptions()
if err != nil {
return nil, err
}
return &Analyzer{
catalog: catalog,
opt: newAnalyzerOptions(),
opt: opt,
namePath: &NamePath{},
}
}, nil
}

func newAnalyzerOptions() *zetasql.AnalyzerOptions {
func newAnalyzerOptions() (*zetasql.AnalyzerOptions, error) {
langOpt := zetasql.NewLanguageOptions()
langOpt.SetNameResolutionMode(zetasql.NameResolutionDefault)
langOpt.SetProductMode(types.ProductInternal)
Expand Down Expand Up @@ -90,11 +94,17 @@ func newAnalyzerOptions() *zetasql.AnalyzerOptions {
ast.CreateViewStmt,
ast.DropFunctionStmt,
})
// Enable QUALIFY without WHERE
//https://github.com/google/zetasql/issues/124
err := langOpt.EnableReservableKeyword("QUALIFY", true)
if err != nil {
return nil, err
}
opt := zetasql.NewAnalyzerOptions()
opt.SetAllowUndeclaredParameters(true)
opt.SetLanguage(langOpt)
opt.SetParseLocationRecordType(zetasql.ParseLocationRecordFullNodeScope)
return opt
return opt, nil
}

func (a *Analyzer) SetAutoIndexMode(enabled bool) {
Expand Down
10 changes: 9 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,6 @@ FROM UNNEST(['c', NULL, 'b', 'a']) AS x`,
{"a", nil, "a", "c"},
},
},

{
name: "window range",
query: `
Expand Down Expand Up @@ -3037,6 +3036,15 @@ FROM Produce WHERE Produce.category = 'vegetable' QUALIFY rank <= 3`,
{"cabbage", int64(3)},
},
},
// Regression test goccy/go-zetasqlite#123
{
name: "qualify without group by / where / having",
query: `WITH toks AS (SELECT 1 AS x UNION ALL SELECT 2 AS x)
SELECT x FROM toks QUALIFY MAX(x) OVER (PARTITION BY x) > 1`,
expectedRows: [][]interface{}{
{int64(2)},
},
},
// Regression test goccy/go-zetasqlite#150
{
name: "qualify group",
Expand Down

0 comments on commit 2ca16b0

Please sign in to comment.