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

MongoDB: Quoted field name if it contains dollar sign($) or dot(.) #3386

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class MongoSession
{
private static final Logger log = Logger.get(MongoSession.class);
private static final List<String> SYSTEM_TABLES = Arrays.asList("system.indexes", "system.users", "system.version");
private static final List<String> ESCAPE_CHARACTERS = Arrays.asList(".", "$");

private static final String TABLE_NAME_KEY = "table";
private static final String FIELDS_KEY = "fields";
Expand Down Expand Up @@ -597,7 +598,7 @@ else if (value instanceof Document) {
return Optional.empty();
}

parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(key)), fieldType.get())));
parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(quote(key))), fieldType.get())));
}
typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
}
Expand All @@ -611,4 +612,13 @@ private boolean isView(SchemaTableName tableName)
Object view = views.find(new Document("_id", tableName.toString())).first();
return view != null;
}

private static String quote(String name)
{
boolean escaped = ESCAPE_CHARACTERS.stream().anyMatch(name::contains);
if (escaped) {
return "\"" + name + "\"";
}
return name;
}
}