Skip to content

Commit

Permalink
[CALCITE-6509] MongoAdapter throws 'Invalid JSON Number' exception if…
Browse files Browse the repository at this point in the history
… first character of field name is number
  • Loading branch information
dssysolyatin authored and dssysolyatin committed Oct 10, 2024
1 parent 7ce986f commit 785a13e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,15 @@ static List<String> mongoFieldNames(final RelDataType rowType) {
}

static String maybeQuote(String s) {
if (!needsQuote(s)) {
return s;
}
// MongoDB Extended JSON conforms to the JSON RFC. It is safe to use double quotes for
// everything.
return quote(s);
}

static String quote(String s) {
return "'" + s + "'"; // TODO: handle embedded quotes
}

private static boolean needsQuote(String s) {
for (int i = 0, n = s.length(); i < n; i++) {
char c = s.charAt(i);
if (!Character.isJavaIdentifierPart(c)
|| c == '$') {
return true;
}
}
return false;
}

/** Translator from {@link RexNode} to strings in MongoDB's expression
* language. */
static class RexToMongoTranslator extends RexVisitorImpl<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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 @@ -613,18 +614,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

0 comments on commit 785a13e

Please sign in to comment.