From a73e8ce82f9d6e12753dc35989100f8b11f62c61 Mon Sep 17 00:00:00 2001 From: Piotr Fus Date: Tue, 5 Sep 2023 12:23:42 +0200 Subject: [PATCH] SNOW-898296: Enable HTAP optimisations in tests --- .github/workflows/build-test.yml | 3 +- connection.go | 21 +++- driver_test.go | 2 + htap_test.go | 163 +++++++++++++++++++++++++++++++ statement_test.go | 3 + 5 files changed, 187 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 291b1ee6a..dc1fb7ecf 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -48,7 +48,8 @@ jobs: env: PARAMETERS_SECRET: ${{ secrets.PARAMETERS_SECRET }} CLOUD_PROVIDER: ${{ matrix.cloud }} - run: ./ci/test.sh +# run: ./ci/test.sh + run: go test -run TestHTAPOptimizations - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: diff --git a/connection.go b/connection.go index a48af3600..e6f323aa2 100644 --- a/connection.go +++ b/connection.go @@ -152,10 +152,23 @@ func (sc *snowflakeConn) exec( } logger.WithContext(ctx).Info("Exec/Query SUCCESS") - sc.cfg.Database = data.Data.FinalDatabaseName - sc.cfg.Schema = data.Data.FinalSchemaName - sc.cfg.Role = data.Data.FinalRoleName - sc.cfg.Warehouse = data.Data.FinalWarehouseName + println("query: " + query) + println("db: " + data.Data.FinalDatabaseName) + if data.Data.FinalDatabaseName != "" { + sc.cfg.Database = data.Data.FinalDatabaseName + } + println("schema: " + data.Data.FinalSchemaName) + if data.Data.FinalSchemaName != "" { + sc.cfg.Schema = data.Data.FinalSchemaName + } + println("warehouse: " + data.Data.FinalWarehouseName) + if data.Data.FinalWarehouseName != "" { + sc.cfg.Warehouse = data.Data.FinalWarehouseName + } + println("role: " + data.Data.FinalRoleName) + if data.Data.FinalRoleName != "" { + sc.cfg.Role = data.Data.FinalRoleName + } sc.populateSessionParameters(data.Data.Parameters) return data, err } diff --git a/driver_test.go b/driver_test.go index 653c62777..e718cc630 100644 --- a/driver_test.go +++ b/driver_test.go @@ -342,6 +342,8 @@ func runSnowflakeConnTest(t *testing.T, test func(sc *snowflakeConn)) { t.Fatal(err) } + sc.Exec("ALTER SESSION SET ENABLE_SNOW_654741_FOR_TESTING = true", nil) + test(sc) } diff --git a/htap_test.go b/htap_test.go index e2e71eb20..590540eda 100644 --- a/htap_test.go +++ b/htap_test.go @@ -1,10 +1,13 @@ package gosnowflake import ( + "context" "encoding/json" + "fmt" "reflect" "strings" "testing" + "time" ) func TestMarshallAndDecodeOpaqueContext(t *testing.T) { @@ -416,3 +419,163 @@ func htapTestSnowflakeConn() *snowflakeConn { }, } } + +func TestHTAPOptimizations(t *testing.T) { + runSnowflakeConnTest(t, func(sc *snowflakeConn) { + runID := time.Now().UnixMilli() + t.Run("Schema", func(t *testing.T) { + newSchema := fmt.Sprintf("test_schema_%v", runID) + if strings.EqualFold(sc.cfg.Schema, newSchema) { + t.Errorf("schema should not be switched") + } + + // TODO replace with mustExec + sc.Exec(fmt.Sprintf("CREATE SCHEMA %v", newSchema), nil) + defer sc.Exec(fmt.Sprintf("DROP SCHEMA %v", newSchema), nil) + + if !strings.EqualFold(sc.cfg.Schema, newSchema) { + t.Errorf("schema should be switched, expected %v, got %v", newSchema, sc.cfg.Schema) + } + + query, _ := sc.Query("SELECT 1", nil) + query.Close() + + if !strings.EqualFold(sc.cfg.Schema, newSchema) { + t.Errorf("schema should be switched, expected %v, got %v", newSchema, sc.cfg.Schema) + } + }) + t.Run("Database", func(t *testing.T) { + newDatabase := fmt.Sprintf("test_database_%v", runID) + if strings.EqualFold(sc.cfg.Database, newDatabase) { + t.Errorf("database should not be switched") + } + + // TODO replace with mustExec + sc.Exec(fmt.Sprintf("CREATE DATABASE %v", newDatabase), nil) + defer sc.Exec(fmt.Sprintf("DROP DATABASE %v", newDatabase), nil) + + if !strings.EqualFold(sc.cfg.Database, newDatabase) { + t.Errorf("database should be switched, expected %v, got %v", newDatabase, sc.cfg.Database) + } + + query, _ := sc.Query("SELECT 1", nil) + query.Close() + + if !strings.EqualFold(sc.cfg.Database, newDatabase) { + t.Errorf("database should be switched, expected %v, got %v", newDatabase, sc.cfg.Database) + } + }) + t.Run("Warehouse", func(t *testing.T) { + newWarehouse := fmt.Sprintf("test_warehouse_%v", runID) + if strings.EqualFold(sc.cfg.Warehouse, newWarehouse) { + t.Errorf("warehouse should not be switched") + } + + // TODO replace with mustExec + sc.Exec(fmt.Sprintf("CREATE WAREHOUSE %v", newWarehouse), nil) + defer sc.Exec(fmt.Sprintf("DROP WAREHOUSE %v", newWarehouse), nil) + + if !strings.EqualFold(sc.cfg.Warehouse, newWarehouse) { + t.Errorf("warehouse should be switched, expected %v, got %v", newWarehouse, sc.cfg.Warehouse) + } + + query, _ := sc.Query("SELECT 1", nil) + query.Close() + + if !strings.EqualFold(sc.cfg.Warehouse, newWarehouse) { + t.Errorf("warehouse should be switched, expected %v, got %v", newWarehouse, sc.cfg.Warehouse) + } + }) + t.Run("Role", func(t *testing.T) { + if strings.EqualFold(sc.cfg.Role, "PUBLIC") { + t.Errorf("role should not be public for this test") + } + + // TODO replace with mustExec + sc.Exec("USE ROLE public", nil) + + if !strings.EqualFold(sc.cfg.Role, "PUBLIC") { + t.Errorf("role should be switched, expected public, got %v", sc.cfg.Warehouse) + } + + query, _ := sc.Query("SELECT 1", nil) + query.Close() + + if !strings.EqualFold(sc.cfg.Role, "PUBLIC") { + t.Errorf("role should be switched, expected public, got %v", sc.cfg.Warehouse) + } + }) + t.Run("Session param - DATE_OUTPUT_FORMAT", func(t *testing.T) { + if !strings.EqualFold(*sc.cfg.Params["date_output_format"], "YYYY-MM-DD") { + t.Errorf("should use default date_output_format, but got: %v", *sc.cfg.Params["date_output_format"]) + } + + // TODO replace with mustExec + sc.Exec("ALTER SESSION SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY'", nil) + defer sc.Exec("ALTER SESSION SET DATE_OUTPUT_FORMAT = 'YYYY-MM-DD'", nil) + + if !strings.EqualFold(*sc.cfg.Params["date_output_format"], "DD-MM-YYYY") { + t.Errorf("role should be switched, expected public, got %v", sc.cfg.Warehouse) + } + + query, _ := sc.Query("SELECT 1", nil) + query.Close() + + if !strings.EqualFold(*sc.cfg.Params["date_output_format"], "DD-MM-YYYY") { + t.Errorf("role should be switched, expected public, got %v", sc.cfg.Warehouse) + } + }) + }) +} + +func TestConnIsCleanAfterClose(t *testing.T) { + t.Skip("Fails, because connection is returned to a pool dirty") + ctx := context.Background() + runID := time.Now().UnixMilli() + + db := openDB(t) + defer db.Close() + db.SetMaxOpenConns(1) + + conn, err := db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + conn.ExecContext(ctx, forceJSON) + + var dbName string + rows1, err := conn.QueryContext(ctx, "SELECT CURRENT_DATABASE()") + if err != nil { + t.Fatal(err) + } + rows1.Next() + rows1.Scan(&dbName) + + newDbName := fmt.Sprintf("test_database_%v", runID) + _, err = conn.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %v", newDbName)) + if err != nil { + t.Fatal(err) + } + + rows1.Close() + conn.Close() + + conn2, err := db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + + var dbName2 string + rows2, err := conn2.QueryContext(ctx, "SELECT CURRENT_DATABASE()") + if err != nil { + t.Fatal(err) + } + defer rows2.Close() + rows2.Next() + rows2.Scan(&dbName2) + + if !strings.EqualFold(dbName, dbName2) { + t.Errorf("fresh connection from pool should have original database") + } +} diff --git a/statement_test.go b/statement_test.go index 68f1e7a7b..963e0a61c 100644 --- a/statement_test.go +++ b/statement_test.go @@ -36,6 +36,9 @@ func openConn(t *testing.T) *sql.Conn { if conn, err = db.Conn(context.Background()); err != nil { t.Fatalf("failed to open connection: %v", err) } + + conn.ExecContext(context.Background(), "ALTER SESSION SET ENABLE_SNOW_654741_FOR_TESTING = true") + return conn }