Skip to content

Commit

Permalink
MongoDB: simpler bsonToYqlColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
ninaiad committed Jan 19, 2025
1 parent 1503bab commit 9cb62cf
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions app/server/datasource/mongodb/type_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,19 @@ func bsonToYqlColumn(docs []bson.Raw, doSkipUnsupported bool, logger *zap.Logger
}

prevType, prevTypeExists := deducedTypes[key]
prevTypeString := prevType.String()

t, err := typeMap(elem.Value(), logger)
if err != nil {
if errors.Is(err, errNull) {
if !prevTypeExists {
ambiguousFields[key] = struct{}{}
}
ambiguousFields[key] = struct{}{}

continue
} else if errors.Is(err, errEmptyArray) {
if prevTypeExists && prevType.GetListType() == nil {
deducedTypes[key] = common.MakePrimitiveType(Ydb.Type_UTF8)

logger.Debug(fmt.Sprintf("bsonToYqlColumn: keeping serialized %v. prev: %v curr: []", key, prevTypeString))
} else if !prevTypeExists {
logger.Debug(fmt.Sprintf("bsonToYqlColumn: keeping serialized %v. prev: %v curr: []", key, prevType.String()))
} else {
ambiguousFields[key] = struct{}{}
}

Expand All @@ -116,31 +113,29 @@ func bsonToYqlColumn(docs []bson.Raw, doSkipUnsupported bool, logger *zap.Logger

tString := t.String()

if !prevTypeExists {
if prevTypeExists && prevType.String() != tString {
deducedTypes[key] = common.MakePrimitiveType(Ydb.Type_UTF8)

logger.Debug(fmt.Sprintf("bsonToYqlColumn: keeping serialized %v. prev: %v curr: %v", key, prevType.String(), tString))
} else {
deducedTypes[key] = t

logger.Debug(fmt.Sprintf("bsonToYqlColumn: new column %v %v", key, tString))
} else if prevTypeString != tString {
deducedTypes[key] = common.MakePrimitiveType(Ydb.Type_UTF8)

logger.Debug(fmt.Sprintf("bsonToYqlColumn: keeping serialized %v. curr: %v prev: %v", key, prevTypeString, tString))
}
}
}

if !doSkipUnsupported {
for field := range ambiguousFields {
if _, ok := deducedTypes[field]; !ok {
deducedTypes[field] = common.MakePrimitiveType(Ydb.Type_UTF8)
}
for field := range ambiguousFields {
if _, ok := deducedTypes[field]; !ok {
deducedTypes[field] = common.MakePrimitiveType(Ydb.Type_UTF8)
}
}

columns := make([]*Ydb.Column, 0, len(deducedTypes))

for columnName, deducedType := range deducedTypes {
if columnName == idColumn {
columns = append(columns, &Ydb.Column{Name: columnName, Type: deducedTypes[columnName]})
columns = append(columns, &Ydb.Column{Name: columnName, Type: deducedType})
} else {
columns = append(columns, &Ydb.Column{Name: columnName, Type: common.MakeOptionalType(deducedType)})
}
Expand Down

0 comments on commit 9cb62cf

Please sign in to comment.