Skip to content

Commit

Permalink
Add memefish integration (#2)
Browse files Browse the repository at this point in the history
* Implement logParseStatement, logParseStatements

* Add --log-memefish flag

* Update logParseStatement
  • Loading branch information
apstndb authored Oct 20, 2024
1 parent 2729953 commit 681dba8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
cloud.google.com/go/spanner v1.62.0
github.com/apstndb/gsqlsep v0.0.0-20230324124551-0e8335710080
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/cloudspannerecosystem/memefish v0.0.0-20241019141852-f27abfc63edf
github.com/google/go-cmp v0.6.0
github.com/jessevdk/go-flags v1.4.0
github.com/olekukonko/tablewriter v0.0.5
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.0 h1:oVLqHXhnYtUwM89y9T1fXGaK9wTkXHgNp8/ZNMQzUxE=
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.0/go.mod h1:dppbR7CwXD4pgtV9t3wD1812RaLDcBjtblcDF5f1vI0=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
Expand Down Expand Up @@ -646,6 +648,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudspannerecosystem/memefish v0.0.0-20241019141852-f27abfc63edf h1:wke4O0w5mfmBtBtkEVTveX47PLz4iyybIaHuALGlugM=
github.com/cloudspannerecosystem/memefish v0.0.0-20241019141852-f27abfc63edf/go.mod h1:A3kg33HoLP0687AgabrwhnXBN8nx96ki2qJ3MisirOM=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -833,6 +837,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/k0kubun/pp v1.3.1-0.20200204103551-99835366d1cc h1:XLjmW07gT7cG/wb6mavIrvAIWBYaTacPo8UOnxGSspA=
github.com/k0kubun/pp v1.3.1-0.20200204103551-99835366d1cc/go.mod h1:qK2ivXw91omfE1uXcpR5kWbAMZRdDOnGbqWlZ7reRFk=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=
Expand All @@ -848,7 +854,10 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/lyft/protoc-gen-star/v2 v2.0.1/go.mod h1:RcCdONR2ScXaYnQC5tUzxzlpA3WVYF7/opLeUgcQs/o=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ type spannerOptions struct {
Verbose bool `short:"v" long:"verbose" description:"Display verbose output."`
Credential string `long:"credential" description:"Use the specific credential file"`
Prompt string `long:"prompt" description:"Set the prompt to the specified format"`
LogMemefish bool `long:"log-memefish" description:"Emit SQL parse log using memefish"`
HistoryFile string `long:"history" description:"Set the history file to the specified path"`
Priority string `long:"priority" description:"Set default request priority (HIGH|MEDIUM|LOW)"`
Role string `long:"role" description:"Use the specific database role"`
Endpoint string `long:"endpoint" description:"Set the Spanner API endpoint (host:port)"`
DirectedRead string `long:"directed-read" description:"Directed read option (replica_location:replica_type). The replicat_type is optional and either READ_ONLY or READ_WRITE"`
}

var logMemefish bool

func main() {
var gopts globalOptions
// process config files at first
Expand All @@ -63,6 +66,9 @@ func main() {
}

opts := gopts.Spanner

logMemefish = opts.LogMemefish

if opts.ProjectId == "" || opts.InstanceId == "" || opts.DatabaseId == "" {
exitf("Missing parameters: -p, -i, -d are required\n")
}
Expand Down
2 changes: 2 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func (s *Session) RunAnalyzeQuery(ctx context.Context, stmt spanner.Statement) (
}

func (s *Session) runQueryWithOptions(ctx context.Context, stmt spanner.Statement, opts spanner.QueryOptions) (*spanner.RowIterator, *spanner.ReadOnlyTransaction) {
logParseStatement(stmt.SQL)
if s.InReadWriteTransaction() {
// The current Go Spanner client library does not apply client-level directed read options to read-write transactions.
// Therefore, we explicitly set query-level options here to fail the query during a read-write transaction.
Expand All @@ -283,6 +284,7 @@ func (s *Session) runQueryWithOptions(ctx context.Context, stmt spanner.Statemen
// It returns error if there is no running read-write transaction.
// useUpdate flag enforce to use Update function internally and disable `THEN RETURN` result printing.
func (s *Session) RunUpdate(ctx context.Context, stmt spanner.Statement, useUpdate bool) ([]Row, []string, int64, *pb.ResultSetMetadata, error) {
logParseStatement(stmt.SQL)
if !s.InReadWriteTransaction() {
return nil, nil, 0, nil, errors.New("read-write transaction is not running")
}
Expand Down
33 changes: 33 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
Expand All @@ -28,6 +29,8 @@ import (
"cloud.google.com/go/spanner"
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
pb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/cloudspannerecosystem/memefish"
"github.com/cloudspannerecosystem/memefish/token"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -403,6 +406,7 @@ func (s *BulkDdlStatement) Execute(ctx context.Context, session *Session) (*Resu
}

func executeDdlStatements(ctx context.Context, session *Session, ddls []string) (*Result, error) {
logParseStatements(ddls)
op, err := session.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: session.DatabasePath(),
Statements: ddls,
Expand Down Expand Up @@ -1186,3 +1190,32 @@ func parsePriority(priority string) (pb.RequestOptions_Priority, error) {
return pb.RequestOptions_PRIORITY_UNSPECIFIED, fmt.Errorf("invalid priority: %q", priority)
}
}

func logParseStatement(stmt string) {
if !logMemefish {
return
}
n, err := newParser("", stmt).ParseStatement()
if err != nil {
log.Printf("SQL can't parsed as a statement, err: %v", err)
} else {
log.Printf("parsed: %v", n.SQL())
}
}

func logParseStatements(stmts []string) {
for _, stmt := range stmts {
logParseStatement(stmt)
}
}

func newParser(filepath, s string) *memefish.Parser {
return &memefish.Parser{
Lexer: &memefish.Lexer{
File: &token.File{
FilePath: filepath,
Buffer: s,
},
},
}
}

0 comments on commit 681dba8

Please sign in to comment.