jasonlin45 commented on code in PR #3325: URL: https://github.com/apache/arrow-adbc/pull/3325#discussion_r2334301859
########## go/adbc/driver/databricks/statement.go: ########## @@ -0,0 +1,225 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package databricks + +import ( + "context" + "database/sql" + "database/sql/driver" + "fmt" + + "github.com/apache/arrow-adbc/go/adbc" + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + + dbsqlrows "github.com/databricks/databricks-sql-go/rows" +) + +type statementImpl struct { + conn *connectionImpl + query string + parameters []interface{} + prepared *sql.Stmt +} + +func (s *statementImpl) Close() error { + if s.conn == nil { + return adbc.Error{ + Msg: "statement already closed", + Code: adbc.StatusInvalidState, + } + } + if s.prepared != nil { + return s.prepared.Close() + } + return nil +} + +func (s *statementImpl) SetOption(key, val string) error { + // No statement-specific options are supported yet + return adbc.Error{ + Code: adbc.StatusNotImplemented, + Msg: fmt.Sprintf("unsupported statement option: %s", key), + } +} + +func (s *statementImpl) SetSqlQuery(query string) error { + s.query = query + // Reset prepared statement if query changes + if s.prepared != nil { + if err := s.prepared.Close(); err != nil { + return adbc.Error{ + Code: adbc.StatusInvalidState, + Msg: fmt.Sprintf("failed to close previous prepared statement: %w", err), + } + } + s.prepared = nil + } + return nil +} + +func (s *statementImpl) Prepare(ctx context.Context) error { + if s.query == "" { + return adbc.Error{ + Code: adbc.StatusInvalidState, + Msg: "no query set", + } + } + + stmt, err := s.conn.conn.PrepareContext(ctx, s.query) + if err != nil { + return adbc.Error{ + Code: adbc.StatusInvalidState, + Msg: fmt.Sprintf("failed to prepare statement: %v", err), + } + } + + s.prepared = stmt + return nil +} + +func (s *statementImpl) ExecuteQuery(ctx context.Context) (array.RecordReader, int64, error) { Review Comment: Hmmm you're right, we don't! I dug into what the implementation would look like and I think I understand the limitation here. We need to access a raw connection via `s.conn.conn.Raw(...)` below. However, there is no way for us to do what `ExecuteUpdate` is doing with `s.prepared.ExecContext` and also use a raw connection to get Arrow batches. Therefore, I believe the best course of action for now is to throw an error if we try to use `ExecuteQuery` with a prepared statement. Or maybe there is a completely different approach here that I am missing that you know of! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
