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

[CALCITE-6509] MongoAdapter throws 'Invalid JSON Number' exception if first character of field name is number #3896

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -114,10 +114,14 @@ static String quote(String s) {
}

private static boolean needsQuote(String s) {
for (int i = 0, n = s.length(); i < n; i++) {
if (!s.isEmpty()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field name can contain '$' ? The new code doesn't cover when '$' is in the middle of the field like 'te$st'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is valid to use $ in the middle of an identifier. It is invalid only when it is at the beginning of the string.

db.zip.aggregate({ $project: { te$st: "$zip_code" } })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Looks good. Do you think we should add this case in a unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because $ in the middle will work in any case, whether quoted or unquoted. Instead of writing all possible tests for quoting, it would be better to rewrite the Mongo adapter to use MongoDB builders. This way, we can avoid such mistakes.

See: https://issues.apache.org/jira/browse/CALCITE-6510

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newly modified code and test cases do not cover the original code logic. Testing coverage is not enough for this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your reply, I know it works for me. But only focus on the code, I don't know.

Copy link
Contributor Author

@dssysolyatin dssysolyatin Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the best option would be to find a specification for fieldName and add it as a comment. I will try to find it tomorrow. Because usually, people don't write tests to check if letters 'e', 'a', or 'b' are allowed in the middle of a string if the specification states that they are allowed.

&& (!Character.isJavaIdentifierStart(s.charAt(0)) || s.charAt(0) == '$')) {
return true;
}

for (int i = 1, n = s.length(); i < n; i++) {
char c = s.charAt(i);
if (!Character.isJavaIdentifierPart(c)
|| c == '$') {
if (!Character.isJavaIdentifierPart(c)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static void setUp() throws Exception {
doc.put("value", new BsonInt32(1231));
doc.put("ownerId", new BsonString("531e7789e4b0853ddb861313"));
doc.put("arr", new BsonArray(Arrays.asList(new BsonString("a"), new BsonString("b"))));
doc.put("1_minute_aggregation", new BsonInt32(10));
datatypes.insertOne(doc);

schema = new MongoSchema(database);
Expand Down Expand Up @@ -605,18 +606,22 @@ private CalciteAssert.AssertThat assertModel(URL url) {
"{$limit: 5}"));
}

@Disabled("broken; [CALCITE-2115] is logged to fix it")
@Test void testProject() {
assertModel(MODEL)
.query("select state, city, 0 as zero from zips order by state, city")
.limit(2)
.returns("STATE=AK; CITY=AKHIOK; ZERO=0\n"
+ "STATE=AK; CITY=AKIACHAK; ZERO=0\n")
.returns("STATE=AK; CITY=ANCHORAGE; ZERO=0\n"
+ "STATE=AK; CITY=FAIRBANKS; ZERO=0\n")
.queryContains(
mongoChecker(
"{$project: {CITY: '$city', STATE: '$state'}}",
"{$sort: {STATE: 1, CITY: 1}}",
"{$project: {STATE: 1, CITY: 1, ZERO: {$literal: 0}}}"));
"{$project: {STATE: '$state', CITY: '$city', ZERO: {$literal: 0}}}",
"{$sort: {STATE: 1, CITY: 1}}"));

assertModel(MODEL)
.query("select cast(_MAP['1_minute_aggregation'] as INT) as \"1_minute_aggregation\" "
+ "from \"mongo_raw\".\"datatypes\"")
.queryContains(
mongoChecker("{$project: {'1_minute_aggregation': 1}}"));
}

@Test void testFilter() {
Expand Down
Loading