Skip to content
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

SNOW-978164: Fix stmt.Exec for DML #978

Merged
merged 13 commits into from
Dec 4, 2023
Merged
6 changes: 5 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ func (sc *snowflakeConn) ExecContext(
return sc.handleMultiExec(ctx, data.Data)
}
logger.Debug("DDL")
return driver.ResultNoRows, nil
return &snowflakeResult{
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
affectedRows: 0,
insertID: -1,
queryID: data.Data.QueryID,
}, nil
}

func (sc *snowflakeConn) QueryContext(
Expand Down
5 changes: 4 additions & 1 deletion connection_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,13 @@ func getResultType(ctx context.Context) resultType {

// isDml returns true if the statement type code is in the range of DML.
func isDml(v int64) bool {
return statementTypeIDDml <= v && v <= statementTypeIDMultiTableInsert
return (statementTypeIDDml <= v && v <= statementTypeIDMultiTableInsert) || v == statementTypeIDSelect
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
}

func updateRows(data execResponseData) (int64, error) {
if data.RowSet == nil {
return 0, nil
}
var count int64
for i, n := 0, len(data.RowType); i < n; i++ {
v, err := strconv.ParseInt(*data.RowSet[0][i], 10, 64)
Expand Down
16 changes: 0 additions & 16 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ func TestRowsClose(t *testing.T) {
})
}

func TestResultNoRows(t *testing.T) {
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
// DDL
runDBTest(t, func(dbt *DBTest) {
row, err := dbt.exec("CREATE OR REPLACE TABLE test(c1 int)")
if err != nil {
t.Fatalf("failed to execute DDL. err: %v", err)
}
if _, err = row.RowsAffected(); err == nil {
t.Fatal("should have failed to get RowsAffected")
}
if _, err = row.LastInsertId(); err == nil {
t.Fatal("should have failed to get LastInsertID")
}
})
}

func TestRowsWithoutChunkDownloader(t *testing.T) {
sts1 := "1"
sts2 := "Test1"
Expand Down
62 changes: 62 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,68 @@ func openConn(t *testing.T) *sql.Conn {
return conn
}

func TestDMLExec(t *testing.T) {
query := "SELECT 1"
runDBTest(t, func(dbt *DBTest) {
testcases := []struct {
name string
f func(dbt *DBTest) (any, error)
}{
{
name: "Exec",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.Exec()
},
},
{
name: "ExecContext",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.ExecContext(context.Background())
},
},
}
for _, tc := range testcases {
_, err := tc.f(dbt)
if err != nil {
t.Error(err)
}
}
})
}

func TestDDLExec(t *testing.T) {
query := "CREATE OR REPLACE TABLE TestDDLExec (num NUMBER)"
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
runDBTest(t, func(dbt *DBTest) {
testcases := []struct {
name string
f func(dbt *DBTest) (any, error)
}{
{
name: "Exec",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.Exec()
},
},
{
name: "ExecContext",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.ExecContext(context.Background())
},
},
}
for _, tc := range testcases {
_, err := tc.f(dbt)
if err != nil {
t.Error(err)
}
}
})
}

func TestFailedQueryIdInSnowflakeError(t *testing.T) {
failingQuery := "SELECTT 1"
failingExec := "INSERT 1 INTO NON_EXISTENT_TABLE"
Expand Down