Skip to content

Commit

Permalink
fix(go/adbc/driver/snowflake): handle result sets without Arrow data (#…
Browse files Browse the repository at this point in the history
…864)

Prevents #863 from crashing, but doesn't fix the underlying error.
  • Loading branch information
lidavidm authored Jun 30, 2023
1 parent b8c9838 commit f64da3c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
75 changes: 71 additions & 4 deletions go/adbc/driver/snowflake/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func dropTempSchema(uri, schema string) {
}
}

func TestADBCSnowflake(t *testing.T) {
func withQuirks(t *testing.T, fn func(*SnowflakeQuirks)) {
uri := os.Getenv("SNOWFLAKE_URI")

if uri == "" {
Expand All @@ -258,7 +258,74 @@ func TestADBCSnowflake(t *testing.T) {
// dropping that schema when we're done.
q := &SnowflakeQuirks{dsn: uri, schemaName: createTempSchema(uri)}
defer dropTempSchema(uri, q.schemaName)
suite.Run(t, &validation.DatabaseTests{Quirks: q})
suite.Run(t, &validation.ConnectionTests{Quirks: q})
suite.Run(t, &validation.StatementTests{Quirks: q})

fn(q)
}

func TestValidation(t *testing.T) {
withQuirks(t, func(q *SnowflakeQuirks) {
suite.Run(t, &validation.DatabaseTests{Quirks: q})
suite.Run(t, &validation.ConnectionTests{Quirks: q})
suite.Run(t, &validation.StatementTests{Quirks: q})
})
}

func TestSnowflake(t *testing.T) {
withQuirks(t, func(q *SnowflakeQuirks) {
suite.Run(t, &SnowflakeTests{Quirks: q})
})
}

// ---- Additional Tests --------------------

type SnowflakeTests struct {
suite.Suite

Quirks *SnowflakeQuirks

ctx context.Context
driver adbc.Driver
db adbc.Database
cnxn adbc.Connection
stmt adbc.Statement
}

func (suite *SnowflakeTests) SetupSuite() {
var err error
suite.ctx = context.Background()
suite.driver = suite.Quirks.SetupDriver(suite.T())
suite.db, err = suite.driver.NewDatabase(suite.Quirks.DatabaseOptions())
suite.NoError(err)
}

func (suite *SnowflakeTests) SetupTest() {
var err error
suite.cnxn, err = suite.db.Open(suite.ctx)
suite.NoError(err)

suite.stmt, err = suite.cnxn.NewStatement()
suite.NoError(err)
}

func (suite *SnowflakeTests) TearDownTest() {
suite.NoError(suite.stmt.Close())
suite.NoError(suite.cnxn.Close())
}

func (suite *SnowflakeTests) TearDownSuite() {
suite.db = nil
}

func (suite *SnowflakeTests) TestStatementEmptyResultSet() {
// Regression test for https://github.com/apache/arrow-adbc/issues/863
suite.NoError(suite.stmt.SetSqlQuery("SHOW WAREHOUSES"))

// XXX: there IS data in this result set, but Snowflake doesn't
// appear to support getting the results as Arrow
_, _, err := suite.stmt.ExecuteQuery(suite.ctx)
var adbcErr adbc.Error
suite.ErrorAs(err, &adbcErr)

suite.Equal(adbc.StatusInternal, adbcErr.Code)
suite.Contains(adbcErr.Msg, "Cannot get Arrow data from this result set")
}
19 changes: 19 additions & 0 deletions go/adbc/driver/snowflake/record_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,25 @@ func newRecordReader(ctx context.Context, alloc memory.Allocator, ld gosnowflake
return nil, errToAdbcErr(adbc.StatusInternal, err)
}

if len(batches) == 0 {
if ld.TotalRows() != 0 {
// XXX(https://github.com/apache/arrow-adbc/issues/863): Snowflake won't return Arrow data for certain queries
return nil, adbc.Error{
Msg: "[Snowflake] Cannot get Arrow data from this result set (see apache/arrow-adbc#863)",
Code: adbc.StatusInternal,
}
}
schema := arrow.NewSchema([]arrow.Field{}, nil)
reader, err := array.NewRecordReader(schema, []arrow.Record{})
if err != nil {
return nil, adbc.Error{
Msg: err.Error(),
Code: adbc.StatusInternal,
}
}
return reader, nil
}

ch := make(chan arrow.Record, bufferSize)
r, err := batches[0].GetStream(ctx)
if err != nil {
Expand Down

0 comments on commit f64da3c

Please sign in to comment.