Skip to content

Commit

Permalink
Use maps.Keys where applicable (#821)
Browse files Browse the repository at this point in the history
Also pass map size hints in a few more places
  • Loading branch information
serprex authored Dec 14, 2023
1 parent db9a1ca commit 40feee0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
15 changes: 9 additions & 6 deletions flow/connectors/bigquery/merge_statement_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ func (m *mergeStmtGenerator) generateFlattenedCTE() string {
}
flattenedProjs = append(flattenedProjs, castStmt)
}
flattenedProjs = append(flattenedProjs, "_peerdb_timestamp")
flattenedProjs = append(flattenedProjs, "_peerdb_timestamp_nanos")
flattenedProjs = append(flattenedProjs, "_peerdb_record_type")
flattenedProjs = append(flattenedProjs, "_peerdb_unchanged_toast_columns")
flattenedProjs = append(
flattenedProjs,
"_peerdb_timestamp",
"_peerdb_timestamp_nanos",
"_peerdb_record_type",
"_peerdb_unchanged_toast_columns",
)

// normalize anything between last normalized batch id to last sync batchid
return fmt.Sprintf(`WITH _peerdb_flattened AS
Expand Down Expand Up @@ -126,8 +129,8 @@ func (m *mergeStmtGenerator) generateDeDupedCTE() string {
// generateMergeStmt generates a merge statement.
func (m *mergeStmtGenerator) generateMergeStmt(tempTable string) string {
// comma separated list of column names
backtickColNames := make([]string, 0)
pureColNames := make([]string, 0)
backtickColNames := make([]string, 0, len(m.NormalizedTableSchema.Columns))
pureColNames := make([]string, 0, len(m.NormalizedTableSchema.Columns))
for colName := range m.NormalizedTableSchema.Columns {
backtickColNames = append(backtickColNames, fmt.Sprintf("`%s`", colName))
pureColNames = append(pureColNames, colName)
Expand Down
7 changes: 2 additions & 5 deletions flow/connectors/snowflake/qrep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/jackc/pgx/v5/pgtype"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/encoding/protojson"
)

Expand Down Expand Up @@ -308,14 +309,10 @@ func (c *SnowflakeConnector) getColsFromTable(tableName string) (*model.ColumnIn
}
columnMap[colName.String] = colType.String
}
var cols []string
for k := range columnMap {
cols = append(cols, k)
}

return &model.ColumnInformation{
ColumnMap: columnMap,
Columns: cols,
Columns: maps.Keys(columnMap),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion flow/connectors/utils/array.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package utils

func ArrayMinus(first []string, second []string) []string {
lookup := make(map[string]struct{})
lookup := make(map[string]struct{}, len(second))
// Add elements from arrayB to the lookup map
for _, element := range second {
lookup[element] = struct{}{}
Expand Down
12 changes: 6 additions & 6 deletions flow/connectors/utils/map.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package utils

import "strings"
import (
"strings"

"golang.org/x/exp/maps"
)

func KeysToString(m map[string]struct{}) string {
if m == nil {
return ""
}

var keys []string
for k := range m {
keys = append(keys, k)
}
return strings.Join(keys, ",")
return strings.Join(maps.Keys(m), ",")
}
2 changes: 1 addition & 1 deletion flow/e2e/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func CreateSourceTableQRep(pool *pgxpool.Pool, suffix string, tableName string)
}

func generate20MBJson() ([]byte, error) {
xn := make(map[string]interface{})
xn := make(map[string]interface{}, 215000)
for i := 0; i < 215000; i++ {
xn[uuid.New().String()] = uuid.New().String()
}
Expand Down
4 changes: 2 additions & 2 deletions flow/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *RecordItems) toMap() (map[string]interface{}, error) {
return nil, errors.New("colToValIdx is nil")
}

jsonStruct := make(map[string]interface{})
jsonStruct := make(map[string]interface{}, len(r.ColToValIdx))
for col, idx := range r.ColToValIdx {
v := r.Values[idx]
if v.Value == nil {
Expand Down Expand Up @@ -178,7 +178,7 @@ type ToJSONOptions struct {
}

func NewToJSONOptions(unnestCols []string) *ToJSONOptions {
unnestColumns := make(map[string]struct{})
unnestColumns := make(map[string]struct{}, len(unnestCols))
for _, col := range unnestCols {
unnestColumns[col] = struct{}{}
}
Expand Down

0 comments on commit 40feee0

Please sign in to comment.