Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow analyzer to use correct query typings by passing query parameter types to go-zetasqlite #315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/connection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (t *Tx) Tx() *sql.Tx {
return t.tx
}

func (t *Tx) Conn() *Conn { return t.conn }

func (t *Tx) RollbackIfNotCommitted() error {
if t.committed {
return nil
Expand Down
11 changes: 11 additions & 0 deletions internal/contentdata/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@
zap.String("query", query),
zap.Any("values", values),
)
// We must pass the query parameters to zetasqlite so the analyzer uses the proper typings
if err := tx.Conn().Conn.Raw(func(c interface{}) error {
zetasqliteConn, ok := c.(*zetasqlite.ZetaSQLiteConn)
if !ok {
return fmt.Errorf("failed to get ZetaSQLiteConn from %T", c)
}
zetasqliteConn.SetQueryParameters(params)

Check failure on line 177 in internal/contentdata/repository.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, 1.21.5)

zetasqliteConn.SetQueryParameters undefined (type *zetasqlite.ZetaSQLiteConn has no field or method SetQueryParameters)
return nil
}); err != nil {
return nil, fmt.Errorf("failed to setup connection: %w", err)
}
rows, err := tx.Tx().QueryContext(ctx, query, values...)
if err != nil {
return nil, err
Expand Down
56 changes: 55 additions & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,61 @@ ORDER BY qty DESC;`)
if rowCount != 1 {
t.Fatal("failed to get result")
}

query = client.Query("SELECT * FROM `test.test_dataset.test_table` WHERE @parameter IS NULL OR 'target text' = @parameter")
query.Parameters = []bigquery.QueryParameter{
{
Name: "parameter",
Value: &bigquery.QueryParameterValue{
Type: bigquery.StandardSQLDataType{
TypeKind: "STRING",
},
Value: "test",
},
},
}
it, err = query.Read(ctx)

if err != nil {
t.Fatal(err)
}
for {
var row []bigquery.Value
if err := it.Next(&row); err != nil {
if err != iterator.Done {
t.Fatal(err)
}
break
}
if len(row) != 3 {
t.Fatalf("failed to get row: %v", row)
}
}

query = client.Query("SELECT * FROM UNNEST(@states)")
query.Parameters = []bigquery.QueryParameter{
{
Name: "states",
Value: []string{"WA", "VA", "WV", "WY"},
},
}
it, err = query.Read(ctx)

if err != nil {
t.Fatal(err)
}
for {
var row []bigquery.Value
if err := it.Next(&row); err != nil {
if err != iterator.Done {
t.Fatal(err)
}
break
}
if len(row) != 1 {
t.Fatalf("failed to get row: %v", row)
}
}
}

func TestMultipleProject(t *testing.T) {
Expand Down Expand Up @@ -2579,5 +2634,4 @@ func TestInformationSchema(t *testing.T) {
}
}
})

}
Loading