org.apache.calcite.adapter.mongodb.MongoTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of calcite-mongodb Show documentation
Show all versions of calcite-mongodb Show documentation
MongoDB adapter for Calcite
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.adapter.mongodb;
import org.apache.calcite.adapter.java.AbstractQueryableTable;
import org.apache.calcite.linq4j.AbstractEnumerable;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.Enumerator;
import org.apache.calcite.linq4j.QueryProvider;
import org.apache.calcite.linq4j.Queryable;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.TranslatableTable;
import org.apache.calcite.schema.impl.AbstractTableQueryable;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.Util;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Table based on a MongoDB collection.
*/
public class MongoTable extends AbstractQueryableTable
implements TranslatableTable {
private final String collectionName;
/** Creates a MongoTable. */
MongoTable(String collectionName) {
super(Object[].class);
this.collectionName = collectionName;
}
@Override public String toString() {
return "MongoTable {" + collectionName + "}";
}
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
final RelDataType mapType =
typeFactory.createMapType(
typeFactory.createSqlType(SqlTypeName.VARCHAR),
typeFactory.createTypeWithNullability(
typeFactory.createSqlType(SqlTypeName.ANY), true));
return typeFactory.builder().add("_MAP", mapType).build();
}
@Override public Queryable asQueryable(QueryProvider queryProvider,
SchemaPlus schema, String tableName) {
return new MongoQueryable<>(queryProvider, schema, this, tableName);
}
@Override public RelNode toRel(
RelOptTable.ToRelContext context,
RelOptTable relOptTable) {
final RelOptCluster cluster = context.getCluster();
return new MongoTableScan(cluster, cluster.traitSetOf(MongoRel.CONVENTION),
relOptTable, this, null);
}
/** Executes a "find" operation on the underlying collection.
*
* For example,
* zipsTable.find("{state: 'OR'}", "{city: 1, zipcode: 1}")
*
* @param mongoDb MongoDB connection
* @param filterJson Filter JSON string, or null
* @param projectJson Project JSON string, or null
* @param fields List of fields to project; or null to return map
* @return Enumerator of results
*/
private Enumerable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy