use of org.bson.BsonString in project jackrabbit-oak by apache.
the class MongoUtilsTest method response.
private static BsonDocument response(int code) {
BsonDocument response = new BsonDocument();
response.put("code", new BsonInt32(code));
response.put("errmsg", new BsonString("message"));
return response;
}
use of org.bson.BsonString in project incubator-skywalking by apache.
the class MongoDBMethodInterceptorTest method setUp.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setUp() throws Exception {
interceptor = new MongoDBMethodInterceptor();
Config.Plugin.MongoDB.TRACE_PARAM = true;
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017");
BsonDocument document = new BsonDocument();
document.append("name", new BsonString("by"));
MongoNamespace mongoNamespace = new MongoNamespace("test.user");
Decoder decoder = PowerMockito.mock(Decoder.class);
FindOperation findOperation = new FindOperation(mongoNamespace, decoder);
findOperation.filter(document);
arguments = new Object[] { findOperation };
argumentTypes = new Class[] { findOperation.getClass() };
}
use of org.bson.BsonString in project drill by axbaretto.
the class TestBsonRecordReader method testSpecialCharStringType.
@Test
public void testSpecialCharStringType() throws IOException {
BsonDocument bsonDoc = new BsonDocument();
bsonDoc.append("stringKey", new BsonString("§§§§§§§§§1"));
writer.reset();
bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
assertEquals("§§§§§§§§§1", mapReader.reader("stringKey").readText().toString());
}
use of org.bson.BsonString in project drill by apache.
the class MongoPluginImplementor method implement.
@Override
public void implement(PluginProjectRel project) throws IOException {
runAggregate = runAggregate || project.getProjects().stream().anyMatch(expression -> !expression.isA(SqlKind.INPUT_REF));
visitChild(project.getInput());
if (runAggregate) {
RexToMongoTranslator translator = new RexToMongoTranslator((JavaTypeFactory) project.getCluster().getTypeFactory(), MongoAggregateUtils.mongoFieldNames(project.getInput().getRowType()));
List<BsonElement> items = new ArrayList<>();
for (Pair<RexNode, String> pair : project.getNamedProjects()) {
String name = pair.right;
BsonValue expr = pair.left.accept(translator);
items.add(expr.equals(new BsonString("$" + name)) ? new BsonElement(MongoAggregateUtils.maybeQuote(name), new BsonInt32(1)) : new BsonElement(MongoAggregateUtils.maybeQuote(name), expr));
}
BsonDocument projection = Aggregates.project(new BsonDocument(items)).toBsonDocument();
operations.add(projection);
List<String> outNames = MongoAggregateUtils.mongoFieldNames(project.getRowType());
this.columns = outNames.stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
} else {
List<String> outNames = MongoAggregateUtils.mongoFieldNames(project.getRowType());
this.columns = outNames.stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
}
}
use of org.bson.BsonString in project drill by apache.
the class RexToMongoTranslator method visitCall.
@Override
public BsonValue visitCall(RexCall call) {
String name = isItem(call);
if (name != null) {
return new BsonString("'$" + name + "'");
}
List<BsonValue> strings = call.operands.stream().map(operand -> operand.accept(this)).collect(Collectors.toList());
if (call.getKind() == SqlKind.CAST) {
return strings.get(0);
}
String stdOperator = MONGO_OPERATORS.get(call.getOperator());
if (stdOperator != null) {
return new BsonDocument(stdOperator, new BsonArray(strings));
}
if (call.getOperator() == SqlStdOperatorTable.ITEM) {
RexNode op1 = call.operands.get(1);
if (op1 instanceof RexLiteral) {
if (op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
return new BsonDocument("$arrayElemAt", new BsonArray(Arrays.asList(strings.get(0), new BsonInt32(((RexLiteral) op1).getValueAs(Integer.class)))));
} else if (op1.getType().getSqlTypeName() == SqlTypeName.CHAR) {
return new BsonString(strings.get(0).asString().getValue() + "." + ((RexLiteral) op1).getValueAs(String.class));
}
}
}
if (call.getOperator() == SqlStdOperatorTable.CASE) {
// case(a, b, c) -> $cond:[a, b, c]
// case(a, b, c, d) -> $cond:[a, b, $cond:[c, d, null]]
// case(a, b, c, d, e) -> $cond:[a, b, $cond:[c, d, e]]
BsonDocument result = new BsonDocument();
BsonArray args = new BsonArray();
result.put("$cond", args);
for (int i = 0; i < strings.size(); i += 2) {
args.add(strings.get(i));
args.add(strings.get(i + 1));
if (i == strings.size() - 3) {
args.add(strings.get(i + 2));
break;
}
if (i == strings.size() - 2) {
args.add(BsonNull.VALUE);
break;
}
BsonArray innerArgs = new BsonArray();
BsonDocument innerDocument = new BsonDocument();
innerDocument.put("$cond", innerArgs);
args.add(innerDocument);
args = innerArgs;
}
return result;
}
throw new IllegalArgumentException("Translation of " + call + " is not supported by MongoProject");
}
Aggregations