Skip to content

Commit

Permalink
Snowflake fix query re-run
Browse files Browse the repository at this point in the history
  • Loading branch information
delfrrr committed Sep 16, 2024
1 parent 84021fd commit ee51545
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ cypress/screenshots
testtmp*

# .env
.env.*
.env.*
data
34 changes: 20 additions & 14 deletions src/server/dekart/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ func (s Server) RunQuery(ctx context.Context, req *proto.RunQueryRequest) (*prot
`select
reports.id,
queries.query_source_id,
datasets.connection_id
datasets.connection_id,
queries.query_text
from queries
left join datasets on queries.id = datasets.query_id
left join reports on (datasets.report_id = reports.id or queries.report_id = reports.id)
Expand All @@ -250,8 +251,9 @@ func (s Server) RunQuery(ctx context.Context, req *proto.RunQueryRequest) (*prot
var reportID string
var prevQuerySourceId string
var connectionID sql.NullString
var queryText string
for queriesRows.Next() {
err := queriesRows.Scan(&reportID, &prevQuerySourceId, &connectionID)
err := queriesRows.Scan(&reportID, &prevQuerySourceId, &connectionID, &queryText)
if err != nil {
log.Err(err).Send()
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -277,7 +279,7 @@ func (s Server) RunQuery(ctx context.Context, req *proto.RunQueryRequest) (*prot
return nil, status.Error(codes.NotFound, err.Error())
}

if !report.CanWrite {
if !(report.CanWrite || report.Discoverable) {
err := fmt.Errorf("permission denied")
log.Warn().Err(err).Send()
return nil, status.Error(codes.PermissionDenied, err.Error())
Expand All @@ -290,23 +292,27 @@ func (s Server) RunQuery(ctx context.Context, req *proto.RunQueryRequest) (*prot
return nil, status.Error(codes.Internal, err.Error())
}

err = s.storeQuerySync(ctx, req.QueryId, req.QueryText, prevQuerySourceId)

if err != nil {
code := codes.Internal
if _, ok := err.(*queryWasNotUpdated); ok {
code = codes.Canceled
log.Warn().Err(err).Send()
} else {
log.Error().Err(err).Send()
if report.CanWrite {
// update query text if it was changed by user if user has write permission
// otherwise use query text from db
queryText = req.QueryText
err = s.storeQuerySync(ctx, req.QueryId, req.QueryText, prevQuerySourceId)
if err != nil {
code := codes.Internal
if _, ok := err.(*queryWasNotUpdated); ok {
code = codes.Canceled
log.Warn().Err(err).Send()
} else {
log.Error().Err(err).Send()
}
return nil, status.Error(code, err.Error())
}
return nil, status.Error(code, err.Error())
}

err = s.runQuery(ctx, runQueryOptions{
reportID: reportID,
queryID: req.QueryId,
queryText: req.QueryText,
queryText: queryText,
connection: connection,
userBucketName: s.getBucketNameFromConnection(connection),
})
Expand Down
1 change: 1 addition & 0 deletions src/server/dekart/querysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (s Server) storeQuerySync(ctx context.Context, queryID string, queryText st
}
affectedRows, _ := result.RowsAffected()
if affectedRows == 0 {
log.Warn().Str("prevQuerySourceId", prevQuerySourceId).Str("newQuerySourceId", newQuerySourceId).Msg("Query text not updated")
return &queryWasNotUpdated{}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions src/server/dekart/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (s Server) GetUserStream(req *proto.GetUserStreamRequest, srv proto.Dekart_
for {
select {
case sequence := <-ping:
return s.sendUserStreamResponse(ctx, srv, sequence)
return s.sendUserStreamResponse(srv.Context(), srv, sequence)
case <-ctx.Done():
return nil
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (s Server) GetReportListStream(req *proto.ReportListRequest, srv proto.Deka
for {
select {
case sequence := <-ping:
return s.sendReportList(ctx, srv, sequence)
return s.sendReportList(srv.Context(), srv, sequence)
case <-ctx.Done():
return nil
}
Expand Down

0 comments on commit ee51545

Please sign in to comment.