Here are the examples of the java api org.apache.calcite.schema.SchemaPlus taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
133 Examples
19
Source : MetadataProvider.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Helper method to create a {@link PojoRecordReader} for given arguments.
* @param tableType
* @param filter
* @param provider
* @param userSession
* @return
*/
private static <S> PojoRecordReader<S> getPojoRecordReader(final InfoSchemaTableType tableType, final InfoSchemaFilter filter, final DrillConfig config, final SchemaTreeProvider provider, final UserSession userSession) {
final SchemaPlus rootSchema = provider.createRootSchema(userSession.getCredentials().getUserName(), newSchemaConfigInfoProvider(config, userSession, provider));
return tableType.getRecordReader(rootSchema, filter, userSession.getOptions());
}
19
Source : SchemaTreeProvider.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
private static void addSchemasToCloseList(final SchemaPlus tree, final List<AutoCloseable> toClose) {
for (String subSchemaName : tree.getSubSchemaNames()) {
addSchemasToCloseList(tree.getSubSchema(subSchemaName), toClose);
}
try {
AbstractSchema drillSchemaImpl = tree.unwrap(AbstractSchema.clreplaced);
toClose.add(drillSchemaImpl);
} catch (ClreplacedCastException e) {
// Ignore as the SchemaPlus is not an implementation of Drill schema.
}
}
19
Source : SchemaTreeProvider.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
@Override
public void close() throws Exception {
List<AutoCloseable> toClose = Lists.newArrayList();
for (SchemaPlus tree : schemaTreesToClose) {
addSchemasToCloseList(tree, toClose);
}
AutoCloseables.close(toClose);
}
19
Source : PartitionExplorerImpl.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
public clreplaced ParreplacedionExplorerImpl implements ParreplacedionExplorer {
private final SchemaPlus rootSchema;
public ParreplacedionExplorerImpl(SchemaPlus rootSchema) {
this.rootSchema = rootSchema;
}
@Override
public Iterable<String> getSubParreplacedions(String schema, String table, List<String> parreplacedionColumns, List<String> parreplacedionValues) throws ParreplacedionNotFoundException {
AbstractSchema subSchema = rootSchema.getSubSchema(schema).unwrap(AbstractSchema.clreplaced);
return subSchema.getSubParreplacedions(table, parreplacedionColumns, parreplacedionValues);
}
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
public void scanSchema(SchemaPlus root) {
if (shouldVisitCatalog() && visitCatalog()) {
scanSchema(root.getName(), root);
}
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
protected boolean shouldVisitSchema(String schemaName, SchemaPlus schema) {
try {
// if the schema path is null or empty (try for root schema)
if (schemaName == null || schemaName.isEmpty()) {
return false;
}
AbstractSchema drillSchema = schema.unwrap(AbstractSchema.clreplaced);
if (!drillSchema.showInInformationSchema()) {
return false;
}
if (filter == null) {
return true;
}
final Map<String, String> recordValues = ImmutableMap.of(CATS_COL_CATALOG_NAME, IS_CATALOG_NAME, SHRD_COL_TABLE_SCHEMA, schemaName, SCHS_COL_SCHEMA_NAME, schemaName);
// If the filter evaluates to false then we don't need to visit the schema.
// For other two results (TRUE, INCONCLUSIVE) continue to visit the schema.
return filter.evaluate(recordValues) != Result.FALSE;
} catch (ClreplacedCastException e) {
// ignore and return true as this is not a Drill schema
}
return true;
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Visit the given schema.
*
* @param schemaName Name of the schema
* @param schema Schema object
* @return Whether to continue exploring the contents of the schema or not. Contents are tables within the schema.
*/
public boolean visitSchema(String schemaName, SchemaPlus schema) {
return true;
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Recursively scans the given schema, invoking the visitor as appropriate.
* @param schemaPath the path to the given schema, so far
* @param schema the given schema
*/
private void scanSchema(String schemaPath, SchemaPlus schema) {
// Recursively scan any subschema.
for (String name : schema.getSubSchemaNames()) {
scanSchema(schemaPath + // If we have an empty schema path, then don't insert a leading dot.
(schemaPath == "" ? "" : ".") + name, schema.getSubSchema(name));
}
// Visit this schema and if requested ...
if (shouldVisitSchema(schemaPath, schema) && visitSchema(schemaPath, schema)) {
visitTables(schemaPath, schema);
}
}
19
Source : FileSystemPlugin.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
@Override
public void registerSchemas(SchemaConfig schemaConfig, SchemaPlus parent) throws IOException {
schemaFactory.registerSchemas(schemaConfig, parent);
}
19
Source : AbstractSchema.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
@Override
public Expression getExpression(SchemaPlus parentSchema, String name) {
return EXPRESSION;
}
19
Source : UserSession.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Get default schema from current default schema path and given schema tree.
* @param rootSchema root schema
* @return A {@link org.apache.calcite.schema.SchemaPlus} object.
*/
public SchemaPlus getDefaultSchema(SchemaPlus rootSchema) {
final String defaultSchemaPath = getDefaultSchemaPath();
if (Strings.isNullOrEmpty(defaultSchemaPath)) {
return null;
}
return SchemaUtilites.findSchema(rootSchema, defaultSchemaPath);
}
19
Source : SqlConverter.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
private static SchemaPlus rootSchema(SchemaPlus schema) {
while (true) {
if (schema.getParentSchema() == null) {
return schema;
}
schema = schema.getParentSchema();
}
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Utility method to get the schema path for given schema instance.
*/
public static String getSchemaPath(SchemaPlus schema) {
return SCHEMA_PATH_JOINER.join(getSchemaPathAsList(schema));
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Same utility as {@link #findSchema(SchemaPlus, List)} except the search schema path given here is complete path
* instead of list. Use "." separator to divided the schema into nested schema names.
* @param defaultSchema default schema
* @param schemaPath current schema path
* @return found schema path
*/
public static SchemaPlus findSchema(final SchemaPlus defaultSchema, final String schemaPath) {
final List<String> schemaPathAsList = Lists.newArrayList(schemaPath.split("\\."));
return findSchema(defaultSchema, schemaPathAsList);
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Utility method to throw {@link UserException} with context information
*/
public static void throwSchemaNotFoundException(final SchemaPlus defaultSchema, final String givenSchemaPath) {
throw UserException.validationError().message("Schema [%s] is not valid with respect to either root schema or current default schema.", givenSchemaPath).addContext("Current default schema: ", isRootSchema(defaultSchema) ? "No default schema selected" : getSchemaPath(defaultSchema)).build(logger);
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Search and return schema with given schemaPath. First search in schema tree starting from defaultSchema,
* if not found search starting from rootSchema. Root schema tree is derived from the defaultSchema reference.
*
* @param defaultSchema Reference to the default schema in complete schema tree.
* @param schemaPath Schema path to search.
* @return SchemaPlus object.
*/
public static SchemaPlus findSchema(final SchemaPlus defaultSchema, final List<String> schemaPath) {
if (schemaPath.size() == 0) {
return defaultSchema;
}
SchemaPlus schema;
if ((schema = searchSchemaTree(defaultSchema, schemaPath)) != null) {
return schema;
}
SchemaPlus rootSchema = defaultSchema;
while (rootSchema.getParentSchema() != null) {
rootSchema = rootSchema.getParentSchema();
}
if (rootSchema != defaultSchema && (schema = searchSchemaTree(rootSchema, schemaPath)) != null) {
return schema;
}
return null;
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Looks in schema tree for default temporary workspace instance.
*
* @param defaultSchema default schema
* @param config drill config
* @return default temporary workspace, null if workspace was not found
*/
public static AbstractSchema getTemporaryWorkspace(SchemaPlus defaultSchema, DrillConfig config) {
String temporarySchema = config.getString(ExecConstants.DEFAULT_TEMPORARY_WORKSPACE);
List<String> temporarySchemaPath = Lists.newArrayList(temporarySchema);
SchemaPlus schema = findSchema(defaultSchema, temporarySchemaPath);
return schema == null ? null : unwrapAsDrillSchemaInstance(schema);
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Given reference to default schema in schema tree, search for schema with given <i>schemaPath</i>. Once a schema is
* found resolve it into a mutable <i>AbstractDrillSchema</i> instance. A {@link UserException} is throws when:
* <li>No schema for given <i>schemaPath</i> is found.</li>
* <li>Schema found for given <i>schemaPath</i> is a root schema.</li>
* <li>Resolved schema is not a mutable schema.</li>
*
* @param defaultSchema default schema
* @param schemaPath current schema path
* @return mutable schema, exception otherwise
*/
public static AbstractSchema resolveToMutableDrillSchema(final SchemaPlus defaultSchema, List<String> schemaPath) {
final SchemaPlus schema = findSchema(defaultSchema, schemaPath);
if (schema == null) {
throwSchemaNotFoundException(defaultSchema, SCHEMA_PATH_JOINER.join(schemaPath));
}
if (isRootSchema(schema)) {
throw UserException.validationError().message("Root schema is immutable. Creating or dropping tables/views is not allowed in root schema." + "Select a schema using 'USE schema' command.").build(logger);
}
final AbstractSchema drillSchema = unwrapAsDrillSchemaInstance(schema);
if (!drillSchema.isMutable()) {
throw UserException.validationError().message("Unable to create or drop tables/views. Schema [%s] is immutable.", getSchemaPath(schema)).build(logger);
}
return drillSchema;
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Utility method to search for schema path starting from the given <i>schema</i> reference
*/
private static SchemaPlus searchSchemaTree(SchemaPlus schema, final List<String> schemaPath) {
for (String schemaName : schemaPath) {
schema = schema.getSubSchema(schemaName);
if (schema == null) {
return null;
}
}
return schema;
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Unwrap given <i>SchemaPlus</i> instance as Drill schema instance (<i>AbstractSchema</i>). Once unwrapped, return
* default schema from <i>AbstractSchema</i>. If the given schema is not an instance of <i>AbstractSchema</i> a
* {@link UserException} is thrown.
*/
public static AbstractSchema unwrapAsDrillSchemaInstance(SchemaPlus schemaPlus) {
try {
return (AbstractSchema) schemaPlus.unwrap(AbstractSchema.clreplaced).getDefaultSchema();
} catch (ClreplacedCastException e) {
throw UserException.validationError(e).message("Schema [%s] is not a Drill schema.", getSchemaPath(schemaPlus)).build(logger);
}
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* @param schema current schema
* @return true if the given <i>schema</i> is root schema. False otherwise.
*/
public static boolean isRootSchema(SchemaPlus schema) {
return schema.getParentSchema() == null;
}
19
Source : ShowTablesHandler.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.`TABLES` ...
*/
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlShowTables node = unwrap(sqlNode, SqlShowTables.clreplaced);
List<SqlNode> selectList = Lists.newArrayList();
SqlNode fromClause;
SqlNode where;
// create select columns
selectList.add(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO));
selectList.add(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO));
fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_TABLES), SqlParserPos.ZERO);
final SqlIdentifier db = node.getDb();
String tableSchema;
if (db != null) {
tableSchema = db.toString();
} else {
// If no schema is given in SHOW TABLES command, list tables from current schema
SchemaPlus schema = config.getConverter().getDefaultSchema();
if (SchemaUtilites.isRootSchema(schema)) {
// If the default schema is a root schema, throw an error to select a default schema
throw UserException.validationError().message("No default schema selected. Select a schema using 'USE schema' command").build(logger);
}
final AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
tableSchema = drillSchema.getFullSchemaName();
}
final String charset = Util.getDefaultCharset().name();
where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableSchema, charset, SqlParserPos.ZERO));
SqlNode filter = null;
final SqlNode likePattern = node.getLikePattern();
if (likePattern != null) {
filter = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, likePattern);
} else if (node.getWhereClause() != null) {
filter = node.getWhereClause();
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, filter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
}
19
Source : ShowFileHandler.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException {
SqlIdentifier from = ((SqlShowFiles) sqlNode).getDb();
DrillFileSystem fs;
String defaultLocation;
String fromDir = "./";
SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
SchemaPlus drillSchema = defaultSchema;
// Show files can be used without from clause, in which case we display the files in the default schema
if (from != null) {
// We are not sure if the full from clause is just the schema or includes table name,
// first try to see if the full path specified is a schema
drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names);
if (drillSchema == null) {
// Entire from clause is not a schema, try to obtain the schema without the last part of the specified clause.
drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names.subList(0, from.names.size() - 1));
fromDir = fromDir + from.names.get((from.names.size() - 1));
}
if (drillSchema == null) {
throw UserException.validationError().message("Invalid FROM/IN clause [%s]", from.toString()).build(logger);
}
}
WorkspaceSchema wsSchema;
try {
wsSchema = (WorkspaceSchema) drillSchema.unwrap(AbstractSchema.clreplaced).getDefaultSchema();
} catch (ClreplacedCastException e) {
throw UserException.validationError().message("SHOW FILES is supported in workspace type schema only. Schema [%s] is not a workspace schema.", SchemaUtilites.getSchemaPath(drillSchema)).build(logger);
}
// Get the file system object
fs = wsSchema.getFS();
// Get the default path
defaultLocation = wsSchema.getDefaultLocation();
List<ShowFilesCommandResult> rows = new ArrayList<>();
for (FileStatus fileStatus : FileSystemUtil.listAll(fs, new Path(defaultLocation, fromDir), false)) {
ShowFilesCommandResult result = new ShowFilesCommandResult(fileStatus.getPath().getName(), fileStatus.isDirectory(), fileStatus.isFile(), fileStatus.getLen(), fileStatus.getOwner(), fileStatus.getGroup(), fileStatus.getPermission().toString(), fileStatus.getAccessTime(), fileStatus.getModificationTime());
rows.add(result);
}
return DirectPlan.createDirectPlan(context.getCurrentEndpoint(), rows, ShowFilesCommandResult.clreplaced);
}
19
Source : DropTableHandler.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* If table schema is not indicated in sql call, returns temporary workspace.
* If schema is indicated, resolves to mutable table schema.
*
* @param tableSchema table schema
* @param defaultSchema default schema
* @param config drill config
* @return resolved schema
*/
private AbstractSchema resolveToTemporarySchema(List<String> tableSchema, SchemaPlus defaultSchema, DrillConfig config) {
if (tableSchema.size() == 0) {
return SchemaUtilites.getTemporaryWorkspace(defaultSchema, config);
} else {
return SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, tableSchema);
}
}
19
Source : DescribeSchemaHandler.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) {
SqlIdentifier schema = ((SqlDescribeSchema) sqlNode).getSchema();
SchemaPlus drillSchema = SchemaUtilites.findSchema(config.getConverter().getDefaultSchema(), schema.names);
if (drillSchema != null) {
StoragePlugin storagePlugin;
try {
storagePlugin = context.getStorage().getPlugin(schema.names.get(0));
} catch (ExecutionSetupException e) {
throw new DrillRuntimeException("Failure while retrieving storage plugin", e);
}
String properties;
try {
final Map configMap = mapper.convertValue(storagePlugin.getConfig(), Map.clreplaced);
if (storagePlugin instanceof FileSystemPlugin) {
transformWorkspaces(schema.names, configMap);
}
properties = mapper.writeValuereplacedtring(configMap);
} catch (JsonProcessingException e) {
throw new DrillRuntimeException("Error while trying to convert storage config to json string", e);
}
return DirectPlan.createDirectPlan(context, new DescribeSchemaResult(Joiner.on(".").join(schema.names), properties));
}
throw UserException.validationError().message(String.format("Invalid schema name [%s]", Joiner.on(".").join(schema.names))).build(logger);
}
19
Source : QueryContext.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
/**
* Return reference to default schema instance in a schema tree. Each {@link org.apache.calcite.schema.SchemaPlus}
* instance can refer to its parent and its children. From the returned reference to default schema instance,
* clients can traverse the entire schema tree and know the default schema where to look up the tables first.
*
* @return Reference to default schema instance in a schema tree.
*/
public SchemaPlus getNewDefaultSchema() {
final SchemaPlus rootSchema = getRootSchema();
final SchemaPlus defaultSchema = session.getDefaultSchema(rootSchema);
if (defaultSchema == null) {
return rootSchema;
}
return defaultSchema;
}
19
Source : HiveStoragePlugin.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
// Forced to synchronize this method to allow error recovery
// in the multi-threaded case. Can remove synchronized only
// by restructuring connections and cache to allow better
// recovery from failed secure connections.
@Override
public synchronized void registerSchemas(SchemaConfig schemaConfig, SchemaPlus parent) throws IOException {
try {
schemaFactory.registerSchemas(schemaConfig, parent);
return;
// Hack. We may need to retry the connection. But, we can't because
// the retry logic is implemented in the very connection we need to
// discard and rebuild. To work around, we discard the entire schema
// factory, and all its invalid connections. Very crude, but the
// easiest short-term solution until we refactor the code to do the
// job properly. See DRILL-5510.
} catch (Throwable e) {
// Unwrap exception
Throwable ex = e;
for (; ; ) {
// Case for failing on an invalid cached connection
if (ex instanceof MetaException || // Case for a timed-out impersonated connection, and
// an invalid non-secure connection used to get security
// tokens.
ex instanceof TTransportException) {
break;
}
// All other exceptions are not handled, just preplaced along up
// the stack.
if (ex.getCause() == null || ex.getCause() == ex) {
logger.error("Hive metastore register schemas failed", e);
throw new DrillRuntimeException("Unknown Hive error", e);
}
ex = ex.getCause();
}
}
// Build a new factory which will cause an all new set of
// Hive metastore connections to be created.
try {
schemaFactory.close();
} catch (Throwable t) {
// Ignore, we're in a bad state.
logger.warn("Schema factory forced close failed, error ignored", t);
}
try {
schemaFactory = new HiveSchemaFactory(this, name, hiveConf);
} catch (ExecutionSetupException e) {
throw new DrillRuntimeException(e);
}
// Try the schemas again. If this fails, just give up.
schemaFactory.registerSchemas(schemaConfig, parent);
logger.debug("Successfully recovered from a Hive metastore connection failure.");
}
19
Source : JournalledJdbcSchema.java
with Apache License 2.0
from tzolov
with Apache License 2.0
from tzolov
// Copied from JdbcSchema with modifications
public static JournalledJdbcSchema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {
DataSource dataSource;
try {
dataSource = parseDataSource(operand);
} catch (Exception e) {
throw new IllegalArgumentException("Error while reading dataSource", e);
}
String catalog = (String) operand.get("jdbcCatalog");
String schema = (String) operand.get("jdbcSchema");
Expression expression = null;
if (parentSchema != null) {
expression = Schemas.subSchemaExpression(parentSchema, name, JdbcSchema.clreplaced);
}
final SqlDialect dialect = createDialect(dataSource);
final JdbcConvention convention = JdbcConvention.of(dialect, expression, name);
return new JournalledJdbcSchema(dataSource, dialect, convention, catalog, schema, operand);
}
19
Source : SidewinderSchemaFactory.java
with Apache License 2.0
from srotya
with Apache License 2.0
from srotya
@Override
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {
StorageEngine storageEngine = SidewinderServer.getStorageEngine();
Object dbName = operand.get("dbName");
Object maxResult = operand.get("maxResult");
int maxResultCount = 1000;
if (maxResult != null) {
maxResultCount = Integer.parseInt(maxResult.toString());
}
if (dbName == null) {
try {
return new SidewinderDatabaseSchema(storageEngine, parentSchema, maxResultCount);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return new SidewinderTableSchema(storageEngine, parentSchema, dbName.toString(), maxResultCount);
}
}
19
Source : JSqlSchemaFactory.java
with Apache License 2.0
from smartloli
with Apache License 2.0
from smartloli
@Override
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {
return new JSqlSchema(name);
}
19
Source : JSqlSchema.java
with Apache License 2.0
from smartloli
with Apache License 2.0
from smartloli
// @Override
// public boolean contentsHaveChangedSince(long lastCheck, long now) {
// return super.contentsHaveChangedSince(lastCheck, now);
// }
@Override
public Expression getExpression(SchemaPlus parentSchema, String name) {
return super.getExpression(parentSchema, name);
}
19
Source : CsvSchemaFactory.java
with MIT License
from shezhiming
with MIT License
from shezhiming
/**
* parentSchema 他的父节点,一般为root
* name 数据库的名字,它在model中定义的
* operand 也是在mode中定义的,是Map类型,用于传入自定义参数。
*/
@Override
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {
return new CsvSchema(String.valueOf(operand.get("dataFile")));
}
19
Source : Table.java
with Apache License 2.0
from rayokota
with Apache License 2.0
from rayokota
@Override
public <T> Queryable<T> asQueryable(QueryProvider queryProvider, SchemaPlus schema, String tableName) {
return new AbstractTableQueryable<T>(queryProvider, schema, this, tableName) {
public Enumerator<T> enumerator() {
// noinspection unchecked
return (Enumerator<T>) Linq4j.iterableEnumerator(() -> Iterators.transform(getModifiableCollection().iterator(), Table::toArray));
}
};
}
19
Source : MycatReflectiveSchema.java
with GNU General Public License v3.0
from MyCATApache
with GNU General Public License v3.0
from MyCATApache
/**
* Returns an expression for the object wrapped by this schema (not the
* schema itself).
*/
Expression getTargetExpression(SchemaPlus parentSchema, String name) {
return EnumUtils.convert(Expressions.call(Schemas.unwrap(getExpression(parentSchema, name), MycatReflectiveSchema.clreplaced), BuiltInMethod.REFLECTIVE_SCHEMA_GET_TARGET.method), target.getClreplaced());
}
19
Source : LocalMetastoreHiveSchema.java
with BSD 2-Clause "Simplified" License
from linkedin
with BSD 2-Clause "Simplified" License
from linkedin
@Override
public Expression getExpression(SchemaPlus parentSchema, String name) {
return null;
}
19
Source : ViewTable.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Table macro that returns a view.
*
* @param schema Schema the view will belong to
* @param viewSql SQL query
* @param schemaPath Path of schema
* @param modifiable Whether view is modifiable, or null to deduce it
*/
public static ViewTableMacro viewMacro(SchemaPlus schema, String viewSql, List<String> schemaPath, List<String> viewPath, Boolean modifiable) {
return new ViewTableMacro(CalciteSchema.from(schema), viewSql, schemaPath, viewPath, modifiable);
}
19
Source : ViewTable.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
// to be removed before 2.0
@Deprecated
public static ViewTableMacro viewMacro(SchemaPlus schema, final String viewSql, final List<String> schemaPath) {
return viewMacro(schema, viewSql, schemaPath, null, Boolean.TRUE);
}
19
Source : ViewTable.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
// to be removed before 2.0
@Deprecated
public static ViewTableMacro viewMacro(SchemaPlus schema, String viewSql, List<String> schemaPath, Boolean modifiable) {
return viewMacro(schema, viewSql, schemaPath, null, modifiable);
}
19
Source : SchemaTreeProvider.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Create and return a SchemaTree with given <i>schemaConfig</i>.
* @param schemaConfig
* @return
*/
public SchemaPlus createRootSchema(SchemaConfig schemaConfig) {
final SchemaPlus rootSchema = DynamicSchema.createRootSchema(dContext.getStorage(), schemaConfig);
schemaTreesToClose.add(rootSchema);
return rootSchema;
}
19
Source : PartitionExplorerImpl.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
public clreplaced ParreplacedionExplorerImpl implements ParreplacedionExplorer {
private final SchemaPlus rootSchema;
public ParreplacedionExplorerImpl(SchemaPlus rootSchema) {
this.rootSchema = rootSchema;
}
@Override
public Iterable<String> getSubParreplacedions(String schema, String table, List<String> parreplacedionColumns, List<String> parreplacedionValues) throws ParreplacedionNotFoundException {
AbstractSchema subSchema = rootSchema.getSubSchema(schema.toLowerCase()).unwrap(AbstractSchema.clreplaced);
return subSchema.getSubParreplacedions(table, parreplacedionColumns, parreplacedionValues);
}
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
public void visitFiles(String schemaName, SchemaPlus schema) {
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
protected boolean shouldVisitFiles(String schemaName, SchemaPlus schemaPlus) {
if (filter == null) {
return true;
}
AbstractSchema schema;
try {
schema = schemaPlus.unwrap(AbstractSchema.clreplaced);
} catch (ClreplacedCastException e) {
return false;
}
if (!(schema instanceof WorkspaceSchemaFactory.WorkspaceSchema)) {
return false;
}
WorkspaceSchemaFactory.WorkspaceSchema wsSchema = (WorkspaceSchemaFactory.WorkspaceSchema) schema;
Map<String, String> recordValues = new HashMap<>();
recordValues.put(FILES_COL_SCHEMA_NAME, schemaName);
recordValues.put(FILES_COL_ROOT_SCHEMA_NAME, wsSchema.getSchemaPath().get(0));
recordValues.put(FILES_COL_WORKSPACE_NAME, wsSchema.getName());
return filter.evaluate(recordValues) != Result.FALSE;
}
19
Source : InfoSchemaRecordGenerator.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Recursively scans the given schema, invoking the visitor as appropriate.
* @param schemaPath the path to the given schema, so far
* @param schema the given schema
*/
private void scanSchema(String schemaPath, SchemaPlus schema) {
// Recursively scan any subschema.
for (String name : schema.getSubSchemaNames()) {
scanSchema(schemaPath + // If we have an empty schema path, then don't insert a leading dot.
("".equals(schemaPath) ? "" : ".") + name, schema.getSubSchema(name));
}
// Visit this schema and if requested ...
if (shouldVisitSchema(schemaPath, schema) && visitSchema(schemaPath, schema)) {
visitTables(schemaPath, schema);
}
if (shouldVisitFiles(schemaPath, schema)) {
visitFiles(schemaPath, schema);
}
}
19
Source : UserSession.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
public clreplaced UserSession implements AutoCloseable {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(UserSession.clreplaced);
private boolean supportComplexTypes = false;
private UserCredentials credentials;
private DrillProperties properties;
private SessionOptionManager sessionOptions;
private final AtomicInteger queryCount;
private final String sessionId;
private SchemaPlus defaultSchema;
/**
* Stores list of temporary tables, key is original table name converted to lower case to achieve case-insensitivity,
* value is generated table name. *
*/
private final ConcurrentMap<String, String> temporaryTables;
/**
* Stores list of session temporary locations, key is path to location, value is file system replacedociated with location. *
*/
private final ConcurrentMap<Path, FileSystem> temporaryLocations;
/**
* On session close deletes all session temporary locations recursively and clears temporary locations list.
*/
@Override
public void close() {
for (Map.Entry<Path, FileSystem> entry : temporaryLocations.entrySet()) {
Path path = entry.getKey();
FileSystem fs = entry.getValue();
try {
fs.delete(path, true);
logger.info("Deleted session temporary location [{}] from file system [{}]", path.toUri().getPath(), fs.getUri());
} catch (Exception e) {
logger.warn("Error during session temporary location [{}] deletion from file system [{}]: [{}]", path.toUri().getPath(), fs.getUri(), e.getMessage());
}
}
temporaryLocations.clear();
}
/**
* Implementations of this interface are allowed to increment queryCount.
* {@link org.apache.drill.exec.work.user.UserWorker} should have a member that implements the interface.
* No other core clreplaced should implement this interface. Test clreplacedes may implement (see ControlsInjectionUtil).
*/
public interface QueryCountIncrementer {
void increment(final UserSession session);
}
public static clreplaced Builder {
private UserSession userSession;
public static Builder newBuilder() {
return new Builder();
}
public Builder withCredentials(UserCredentials credentials) {
userSession.credentials = credentials;
return this;
}
public Builder withOptionManager(OptionManager systemOptions) {
userSession.sessionOptions = new SessionOptionManager(systemOptions, userSession);
return this;
}
public Builder withUserProperties(UserProperties properties) {
userSession.properties = DrillProperties.createFromProperties(properties, false);
return this;
}
public Builder setSupportComplexTypes(boolean supportComplexTypes) {
userSession.supportComplexTypes = supportComplexTypes;
return this;
}
private boolean canApplyUserProperty() {
final StringBuilder sb = new StringBuilder();
if (userSession.properties.containsKey(DrillProperties.QUOTING_IDENTIFIERS)) {
sb.append(DrillProperties.QUOTING_IDENTIFIERS).append(",");
}
if (userSession.properties.containsKey(DrillProperties.QUERY_TAGS)) {
sb.append(DrillProperties.QUERY_TAGS);
}
if (userSession.sessionOptions == null && sb.length() > 0) {
logger.warn("User property {} can't be installed as a server option without the session option manager", sb.toString());
return false;
}
return true;
}
public UserSession build() {
if (canApplyUserProperty()) {
if (userSession.properties.containsKey(DrillProperties.QUOTING_IDENTIFIERS)) {
userSession.setSessionOption(PlannerSettings.QUOTING_IDENTIFIERS_KEY, userSession.properties.getProperty(DrillProperties.QUOTING_IDENTIFIERS));
}
if (userSession.properties.containsKey(DrillProperties.QUERY_TAGS)) {
userSession.setSessionOption(ExecConstants.RM_QUERY_TAGS_KEY, userSession.properties.getProperty(DrillProperties.QUERY_TAGS));
}
}
UserSession session = userSession;
userSession = null;
return session;
}
Builder() {
userSession = new UserSession();
}
}
private UserSession() {
queryCount = new AtomicInteger(0);
sessionId = UUID.randomUUID().toString();
temporaryTables = Maps.newConcurrentMap();
temporaryLocations = Maps.newConcurrentMap();
properties = DrillProperties.createEmpty();
}
public boolean isSupportComplexTypes() {
return supportComplexTypes;
}
public SessionOptionManager getOptions() {
return sessionOptions;
}
public UserCredentials getCredentials() {
return credentials;
}
/**
* Replace current user credentials with the given user's credentials. Meant to be called only by a
* {@link InboundImpersonationManager impersonation manager}.
*
* @param impersonationManager impersonation manager making this call
* @param newCredentials user credentials to change to
*/
public void replaceUserCredentials(final InboundImpersonationManager impersonationManager, final UserCredentials newCredentials) {
Preconditions.checkNotNull(impersonationManager, "User credentials can only be replaced by an" + " impersonation manager.");
credentials = newCredentials;
}
public String getTargetUserName() {
return properties.getProperty(DrillProperties.IMPERSONATION_TARGET);
}
public void incrementQueryCount(final QueryCountIncrementer incrementer) {
replacedert incrementer != null;
queryCount.incrementAndGet();
}
public int getQueryCount() {
return queryCount.get();
}
/**
* Update the schema path for the session.
* @param newDefaultSchemaPath New default schema path to set. It could be relative to the current default schema or
* absolute schema.
* @param currentDefaultSchema Current default schema.
* @throws ValidationException If the given default schema path is invalid in current schema tree.
*/
public void setDefaultSchemaPath(String newDefaultSchemaPath, SchemaPlus currentDefaultSchema) throws ValidationException {
final List<String> newDefaultPathAsList = SchemaUtilites.getSchemaPathAsList(newDefaultSchemaPath);
SchemaPlus newDefault;
// First try to find the given schema relative to the current default schema.
newDefault = SchemaUtilites.findSchema(currentDefaultSchema, newDefaultPathAsList);
if (newDefault == null) {
// If we fail to find the schema relative to current default schema, consider the given new default schema path as
// absolute schema path.
newDefault = SchemaUtilites.findSchema(currentDefaultSchema, newDefaultPathAsList);
}
if (newDefault == null) {
SchemaUtilites.throwSchemaNotFoundException(currentDefaultSchema, newDefaultSchemaPath);
}
properties.setProperty(DrillProperties.SCHEMA, SchemaUtilites.getSchemaPath(newDefault));
}
/**
* @return Get current default schema path.
*/
public String getDefaultSchemaPath() {
return properties.getProperty(DrillProperties.SCHEMA, "");
}
/**
* Get default schema from current default schema path and given schema tree.
* @param rootSchema root schema
* @return A {@link org.apache.calcite.schema.SchemaPlus} object.
*/
public SchemaPlus getDefaultSchema(SchemaPlus rootSchema) {
if (defaultSchema != null)
return defaultSchema;
final String defaultSchemaPath = getDefaultSchemaPath();
if (Strings.isNullOrEmpty(defaultSchemaPath)) {
return null;
}
return SchemaUtilites.findSchema(rootSchema, defaultSchemaPath);
}
public void setDefaultSchema(SchemaPlus defaultSchema) {
this.defaultSchema = defaultSchema;
}
/**
* Set the option of a session level.
* Note: Option's kind is automatically detected if such option exists.
*
* @param name option name
* @param value option value
*/
public void setSessionOption(String name, String value) {
sessionOptions.setLocalOption(name, value);
}
/**
* @return unique session identifier
*/
public String getSessionId() {
return sessionId;
}
/**
* Creates and adds session temporary location if absent using schema configuration.
* Before any actions, checks if preplaceded table schema is valid default temporary workspace.
* Generates temporary table name and stores it's original name as key
* and generated name as value in session temporary tables cache.
* Original temporary name is converted to lower case to achieve case-insensitivity.
* If original table name already exists, new name is not regenerated and is reused.
* This can happen if default temporary workspace was changed (file system or location) or
* orphan temporary table name has remained (name was registered but table creation did not succeed).
*
* @param schema table schema
* @param tableName original table name
* @param config drill config
* @return generated temporary table name
* @throws IOException if error during session temporary location creation
*/
public String registerTemporaryTable(AbstractSchema schema, String tableName, DrillConfig config) throws IOException {
addTemporaryLocation(SchemaUtilites.resolveToValidTemporaryWorkspace(schema, config));
String temporaryTableName = new Path(sessionId, UUID.randomUUID().toString()).toUri().getPath();
String oldTemporaryTableName = temporaryTables.putIfAbsent(tableName.toLowerCase(), temporaryTableName);
return oldTemporaryTableName == null ? temporaryTableName : oldTemporaryTableName;
}
/**
* Returns generated temporary table name from the list of session temporary tables, null otherwise.
* Original temporary name is converted to lower case to achieve case-insensitivity.
*
* @param tableName original table name
* @return generated temporary table name
*/
public String resolveTemporaryTableName(String tableName) {
return temporaryTables.get(tableName.toLowerCase());
}
public String getOriginalTableNameFromTemporaryTable(String tableName) {
for (String originalTableName : temporaryTables.keySet()) {
if (temporaryTables.get(originalTableName).equals(tableName)) {
return originalTableName;
}
}
return null;
}
/**
* Checks if preplaceded table is temporary, table name is case-insensitive.
* Before looking for table checks if preplaceded schema is temporary and returns false if not
* since temporary tables are allowed to be created in temporary workspace only.
* If preplaceded workspace is temporary, looks for temporary table.
* First checks if table name is among temporary tables, if not returns false.
* If temporary table named was resolved, checks that temporary table exists on disk,
* to ensure that temporary table actually exists and resolved table name is not orphan
* (for example, in result of unsuccessful temporary table creation).
*
* @param drillSchema table schema
* @param config drill config
* @param tableName original table name
* @return true if temporary table exists in schema, false otherwise
*/
public boolean isTemporaryTable(AbstractSchema drillSchema, DrillConfig config, String tableName) {
if (drillSchema == null || !SchemaUtilites.isTemporaryWorkspace(drillSchema.getFullSchemaName(), config)) {
return false;
}
String temporaryTableName = resolveTemporaryTableName(tableName);
if (temporaryTableName != null) {
Table temporaryTable = SqlHandlerUtil.getTableFromSchema(drillSchema, temporaryTableName);
if (temporaryTable != null && temporaryTable.getJdbcTableType() == Schema.TableType.TABLE) {
return true;
}
}
return false;
}
/**
* Removes temporary table name from the list of session temporary tables.
* Original temporary name is converted to lower case to achieve case-insensitivity.
* Before temporary table drop, checks if preplaceded table schema is valid default temporary workspace.
*
* @param schema table schema
* @param tableName original table name
* @param config drill config
*/
public void removeTemporaryTable(AbstractSchema schema, String tableName, DrillConfig config) {
String temporaryTable = resolveTemporaryTableName(tableName);
if (temporaryTable == null) {
return;
}
SqlHandlerUtil.dropTableFromSchema(SchemaUtilites.resolveToValidTemporaryWorkspace(schema, config), temporaryTable);
temporaryTables.remove(tableName.toLowerCase());
}
/**
* Session temporary tables are stored under temporary workspace location in session folder
* defined by unique session id. These session temporary locations are deleted on session close.
* If default temporary workspace file system or location is changed at runtime,
* new session temporary location will be added with corresponding file system
* to the list of session temporary locations. If location does not exist it will be created and
* {@link StorageStrategy#TEMPORARY} storage rules will be applied to it.
*
* @param temporaryWorkspace temporary workspace
* @throws IOException in case of error during temporary location creation
*/
private void addTemporaryLocation(WorkspaceSchemaFactory.WorkspaceSchema temporaryWorkspace) throws IOException {
DrillFileSystem fs = temporaryWorkspace.getFS();
Path temporaryLocation = new Path(fs.getUri().toString(), new Path(temporaryWorkspace.getDefaultLocation(), sessionId));
FileSystem fileSystem = temporaryLocations.putIfAbsent(temporaryLocation, fs);
if (fileSystem == null) {
StorageStrategy.TEMPORARY.createPathAndApply(fs, temporaryLocation);
Preconditions.checkArgument(fs.exists(temporaryLocation), String.format("Temporary location should exist [%s]", temporaryLocation.toUri().getPath()));
}
}
}
19
Source : UserSession.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Get default schema from current default schema path and given schema tree.
* @param rootSchema root schema
* @return A {@link org.apache.calcite.schema.SchemaPlus} object.
*/
public SchemaPlus getDefaultSchema(SchemaPlus rootSchema) {
if (defaultSchema != null)
return defaultSchema;
final String defaultSchemaPath = getDefaultSchemaPath();
if (Strings.isNullOrEmpty(defaultSchemaPath)) {
return null;
}
return SchemaUtilites.findSchema(rootSchema, defaultSchemaPath);
}
19
Source : UserSession.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
public void setDefaultSchema(SchemaPlus defaultSchema) {
this.defaultSchema = defaultSchema;
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
private static AbstractSchema resolveToDrillSchemaInternal(SchemaPlus defaultSchema, List<String> schemaPath, boolean checkMutable) {
final SchemaPlus schema = findSchema(defaultSchema, schemaPath);
if (schema == null) {
throwSchemaNotFoundException(defaultSchema, SCHEMA_PATH_JOINER.join(schemaPath));
}
if (isRootSchema(schema)) {
throw UserException.validationError().message("Root schema is immutable. Creating or dropping tables/views is not allowed in root schema." + "Select a schema using 'USE schema' command.").build(logger);
}
final AbstractSchema drillSchema = unwrapAsDrillSchemaInstance(schema);
if (!drillSchema.isMutable()) {
throw UserException.validationError().message("Unable to create or drop objects. Schema [%s] is immutable.", getSchemaPath(schema)).build(logger);
}
return drillSchema;
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Utility method to search for schema path starting from the given <i>schema</i> reference
*/
private static SchemaPlus searchSchemaTree(SchemaPlus schema, final List<String> schemaPath) {
for (String schemaName : schemaPath) {
// schemas in Drill are case insensitive and stored in lower case
schema = schema.getSubSchema(schemaName.toLowerCase());
if (schema == null) {
return null;
}
}
return schema;
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Utility method to throw {@link UserException} with context information
*/
public static void throwSchemaNotFoundException(final SchemaPlus defaultSchema, final List<String> givenSchemaPath) {
throw UserException.validationError().message("Schema [%s] is not valid with respect to either root schema or current default schema.", givenSchemaPath).addContext("Current default schema: ", isRootSchema(defaultSchema) ? "No default schema selected" : getSchemaPath(defaultSchema)).build(logger);
}
19
Source : SchemaUtilites.java
with Apache License 2.0
from lealone
with Apache License 2.0
from lealone
/**
* Given reference to default schema in schema tree, search for schema with given <i>schemaPath</i>. Once a schema is
* found resolve it into a mutable <i>AbstractDrillSchema</i> instance. A {@link UserException} is throws when:
* <li>No schema for given <i>schemaPath</i> is found.</li>
* <li>Schema found for given <i>schemaPath</i> is a root schema.</li>
*
* @param defaultSchema
* @param schemaPath
* @return schema, if found. Otherwise, throws an {@link UserException}
*/
public static AbstractSchema resolveToDrillSchema(final SchemaPlus defaultSchema, List<String> schemaPath) {
return resolveToDrillSchemaInternal(defaultSchema, schemaPath, false);
}
See More Examples