-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
12 deletions.
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
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
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,74 @@ | ||
package snowflake | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/clients/shared" | ||
"github.com/artie-labs/transfer/lib/kafkalib" | ||
"github.com/artie-labs/transfer/lib/stringutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func (s *SnowflakeTestSuite) TestGenerateDedupeQueries() { | ||
{ | ||
// Dedupe with one primary key + no `__artie_updated_at` flag. | ||
tableID := NewTableIdentifier("db", "public", "customers") | ||
stagingTableID := shared.TempTableID(tableID, strings.ToLower(stringutil.Random(5))) | ||
|
||
parts := s.stageStore.generateDedupeQueries(tableID, stagingTableID, []string{"id"}, kafkalib.TopicConfig{}) | ||
assert.Len(s.T(), parts, 3) | ||
assert.Equal( | ||
s.T(), | ||
fmt.Sprintf(`CREATE OR REPLACE TRANSIENT TABLE "%s" AS (SELECT * FROM db.public."CUSTOMERS" QUALIFY ROW_NUMBER() OVER (PARTITION BY by id ORDER BY id ASC) = 2)`, stagingTableID.Table()), | ||
parts[0], | ||
) | ||
assert.Equal(s.T(), fmt.Sprintf(`DELETE FROM db.public."CUSTOMERS" t1 USING "%s" t2 WHERE t1.id = t2.id`, stagingTableID.Table()), parts[1]) | ||
assert.Equal(s.T(), fmt.Sprintf(`INSERT INTO db.public."CUSTOMERS" SELECT * FROM "%s"`, stagingTableID.Table()), parts[2]) | ||
} | ||
{ | ||
// Dedupe with one primary key + `__artie_updated_at` flag. | ||
tableID := NewTableIdentifier("db", "public", "customers") | ||
stagingTableID := shared.TempTableID(tableID, strings.ToLower(stringutil.Random(5))) | ||
|
||
parts := s.stageStore.generateDedupeQueries(tableID, stagingTableID, []string{"id"}, kafkalib.TopicConfig{IncludeArtieUpdatedAt: true}) | ||
assert.Len(s.T(), parts, 3) | ||
assert.Equal( | ||
s.T(), | ||
fmt.Sprintf(`CREATE OR REPLACE TRANSIENT TABLE "%s" AS (SELECT * FROM db.public."CUSTOMERS" QUALIFY ROW_NUMBER() OVER (PARTITION BY by id ORDER BY id ASC, __artie_updated_at ASC) = 2)`, stagingTableID.Table()), | ||
parts[0], | ||
) | ||
assert.Equal(s.T(), fmt.Sprintf(`DELETE FROM db.public."CUSTOMERS" t1 USING "%s" t2 WHERE t1.id = t2.id`, stagingTableID.Table()), parts[1]) | ||
assert.Equal(s.T(), fmt.Sprintf(`INSERT INTO db.public."CUSTOMERS" SELECT * FROM "%s"`, stagingTableID.Table()), parts[2]) | ||
} | ||
{ | ||
// Dedupe with composite keys + no `__artie_updated_at` flag. | ||
tableID := NewTableIdentifier("db", "public", "user_settings") | ||
stagingTableID := shared.TempTableID(tableID, strings.ToLower(stringutil.Random(5))) | ||
|
||
parts := s.stageStore.generateDedupeQueries(tableID, stagingTableID, []string{"user_id", "settings"}, kafkalib.TopicConfig{}) | ||
assert.Len(s.T(), parts, 3) | ||
assert.Equal( | ||
s.T(), | ||
fmt.Sprintf(`CREATE OR REPLACE TRANSIENT TABLE "%s" AS (SELECT * FROM db.public."USER_SETTINGS" QUALIFY ROW_NUMBER() OVER (PARTITION BY by user_id, settings ORDER BY user_id ASC, settings ASC) = 2)`, stagingTableID.Table()), | ||
parts[0], | ||
) | ||
assert.Equal(s.T(), fmt.Sprintf(`DELETE FROM db.public."USER_SETTINGS" t1 USING "%s" t2 WHERE t1.user_id = t2.user_id AND t1.settings = t2.settings`, stagingTableID.Table()), parts[1]) | ||
assert.Equal(s.T(), fmt.Sprintf(`INSERT INTO db.public."USER_SETTINGS" SELECT * FROM "%s"`, stagingTableID.Table()), parts[2]) | ||
} | ||
{ | ||
// Dedupe with composite keys + `__artie_updated_at` flag. | ||
tableID := NewTableIdentifier("db", "public", "user_settings") | ||
stagingTableID := shared.TempTableID(tableID, strings.ToLower(stringutil.Random(5))) | ||
|
||
parts := s.stageStore.generateDedupeQueries(tableID, stagingTableID, []string{"user_id", "settings"}, kafkalib.TopicConfig{IncludeArtieUpdatedAt: true}) | ||
assert.Len(s.T(), parts, 3) | ||
assert.Equal( | ||
s.T(), | ||
fmt.Sprintf(`CREATE OR REPLACE TRANSIENT TABLE "%s" AS (SELECT * FROM db.public."USER_SETTINGS" QUALIFY ROW_NUMBER() OVER (PARTITION BY by user_id, settings ORDER BY user_id ASC, settings ASC, __artie_updated_at ASC) = 2)`, stagingTableID.Table()), | ||
parts[0], | ||
) | ||
assert.Equal(s.T(), fmt.Sprintf(`DELETE FROM db.public."USER_SETTINGS" t1 USING "%s" t2 WHERE t1.user_id = t2.user_id AND t1.settings = t2.settings`, stagingTableID.Table()), parts[1]) | ||
assert.Equal(s.T(), fmt.Sprintf(`INSERT INTO db.public."USER_SETTINGS" SELECT * FROM "%s"`, stagingTableID.Table()), parts[2]) | ||
} | ||
} |
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