-
Notifications
You must be signed in to change notification settings - Fork 97
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
feat(go/adbc/driver/snowflake): support parameter binding #1808
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// 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 snowflake | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/apache/arrow-adbc/go/adbc" | ||
"github.com/apache/arrow/go/v17/arrow" | ||
"github.com/apache/arrow/go/v17/arrow/array" | ||
) | ||
|
||
func convertArrowToNamedValue(batch arrow.Record, index int) ([]driver.NamedValue, error) { | ||
// see goTypeToSnowflake in gosnowflake | ||
// technically, snowflake can bind an array of values at once, but | ||
// only for INSERT, so we can't take advantage of that without | ||
// analyzing the query ourselves | ||
params := make([]driver.NamedValue, batch.NumCols()) | ||
for i, field := range batch.Schema().Fields() { | ||
rawColumn := batch.Column(i) | ||
params[i].Ordinal = i + 1 | ||
switch column := rawColumn.(type) { | ||
case *array.Boolean: | ||
params[i].Value = sql.NullBool{ | ||
Bool: column.Value(index), | ||
Valid: column.IsValid(index), | ||
} | ||
Comment on lines
+41
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something for us to think about is that go1.22 added |
||
case *array.Float32: | ||
// Snowflake only recognizes float64 | ||
params[i].Value = sql.NullFloat64{ | ||
Float64: float64(column.Value(index)), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.Float64: | ||
params[i].Value = sql.NullFloat64{ | ||
Float64: column.Value(index), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.Int8: | ||
// Snowflake only recognizes int64 | ||
params[i].Value = sql.NullInt64{ | ||
Int64: int64(column.Value(index)), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.Int16: | ||
params[i].Value = sql.NullInt64{ | ||
Int64: int64(column.Value(index)), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.Int32: | ||
params[i].Value = sql.NullInt64{ | ||
Int64: int64(column.Value(index)), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.Int64: | ||
params[i].Value = sql.NullInt64{ | ||
Int64: column.Value(index), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.String: | ||
params[i].Value = sql.NullString{ | ||
String: column.Value(index), | ||
Valid: column.IsValid(index), | ||
} | ||
case *array.LargeString: | ||
params[i].Value = sql.NullString{ | ||
String: column.Value(index), | ||
Valid: column.IsValid(index), | ||
} | ||
default: | ||
return nil, adbc.Error{ | ||
Code: adbc.StatusNotImplemented, | ||
Msg: fmt.Sprintf("[Snowflake] Unsupported bind param '%s' type %s", field.Name, field.Type.String()), | ||
zeroshade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
return params, nil | ||
} | ||
|
||
type snowflakeBindReader struct { | ||
doQuery func([]driver.NamedValue) (array.RecordReader, error) | ||
currentBatch arrow.Record | ||
nextIndex int64 | ||
// may be nil if we bound only a batch | ||
stream array.RecordReader | ||
} | ||
|
||
func (r *snowflakeBindReader) Release() { | ||
if r.currentBatch != nil { | ||
r.currentBatch.Release() | ||
r.currentBatch = nil | ||
} | ||
if r.stream != nil { | ||
r.stream.Release() | ||
r.stream = nil | ||
} | ||
} | ||
|
||
func (r *snowflakeBindReader) Next() (array.RecordReader, error) { | ||
params, err := r.NextParams() | ||
if err != nil { | ||
// includes EOF | ||
return nil, err | ||
} | ||
return r.doQuery(params) | ||
} | ||
|
||
func (r *snowflakeBindReader) NextParams() ([]driver.NamedValue, error) { | ||
for r.currentBatch == nil || r.nextIndex >= r.currentBatch.NumRows() { | ||
// We can be used both by binding a stream or by binding a | ||
// batch. In the latter case, we have to release the batch, | ||
// but not in the former case. Unify the cases by always | ||
// releasing the batch, adding an "extra" retain so that the | ||
// release does not cause issues. | ||
if r.currentBatch != nil { | ||
r.currentBatch.Release() | ||
} | ||
zeroshade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r.currentBatch = nil | ||
if r.stream != nil && r.stream.Next() { | ||
r.currentBatch = r.stream.Record() | ||
r.currentBatch.Retain() | ||
r.nextIndex = 0 | ||
continue | ||
} else if r.stream != nil && r.stream.Err() != nil { | ||
return nil, r.stream.Err() | ||
} else { | ||
// no more params | ||
return nil, io.EOF | ||
} | ||
} | ||
|
||
params, err := convertArrowToNamedValue(r.currentBatch, int(r.nextIndex)) | ||
r.nextIndex++ | ||
return params, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 snowflake | ||
|
||
import ( | ||
"io" | ||
"sync/atomic" | ||
|
||
"github.com/apache/arrow-adbc/go/adbc" | ||
"github.com/apache/arrow/go/v17/arrow" | ||
"github.com/apache/arrow/go/v17/arrow/array" | ||
) | ||
|
||
type readerIter interface { | ||
Release() | ||
|
||
Next() (array.RecordReader, error) | ||
} | ||
|
||
type concatReader struct { | ||
refCount atomic.Int64 | ||
readers readerIter | ||
currentReader array.RecordReader | ||
schema *arrow.Schema | ||
err error | ||
} | ||
|
||
func (r *concatReader) nextReader() { | ||
if r.currentReader != nil { | ||
r.currentReader.Release() | ||
r.currentReader = nil | ||
} | ||
reader, err := r.readers.Next() | ||
if err == io.EOF { | ||
r.currentReader = nil | ||
} else if err != nil { | ||
r.err = err | ||
} else { | ||
// May be nil | ||
r.currentReader = reader | ||
} | ||
} | ||
func (r *concatReader) Init(readers readerIter) error { | ||
r.readers = readers | ||
r.refCount.Store(1) | ||
r.nextReader() | ||
if r.err != nil { | ||
r.Release() | ||
return r.err | ||
} else if r.currentReader == nil { | ||
r.Release() | ||
r.err = adbc.Error{ | ||
Code: adbc.StatusInternal, | ||
Msg: "[Snowflake] No data in this stream", | ||
} | ||
return r.err | ||
} | ||
r.schema = r.currentReader.Schema() | ||
return nil | ||
} | ||
func (r *concatReader) Retain() { | ||
r.refCount.Add(1) | ||
} | ||
func (r *concatReader) Release() { | ||
if r.refCount.Add(-1) == 0 { | ||
if r.currentReader != nil { | ||
r.currentReader.Release() | ||
} | ||
r.readers.Release() | ||
} | ||
} | ||
func (r *concatReader) Schema() *arrow.Schema { | ||
if r.schema == nil { | ||
panic("did not call concatReader.Init") | ||
} | ||
return r.schema | ||
} | ||
func (r *concatReader) Next() bool { | ||
for r.currentReader != nil && !r.currentReader.Next() { | ||
r.nextReader() | ||
} | ||
if r.currentReader == nil || r.err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
func (r *concatReader) Record() arrow.Record { | ||
return r.currentReader.Record() | ||
} | ||
func (r *concatReader) Err() error { | ||
return r.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate this, but nothing we can do right now i guess