use of org.apache.calcite.schema.SchemaPlus in project drill by apache.
the class SchemaTreeProvider method createRootSchema.
/**
* Create and return a SchemaTree with given <i>schemaConfig</i>.
* @param schemaConfig
* @return
*/
public SchemaPlus createRootSchema(SchemaConfig schemaConfig) {
try {
final SchemaPlus rootSchema = SimpleCalciteSchema.createRootSchema(false);
dContext.getSchemaFactory().registerSchemas(schemaConfig, rootSchema);
schemaTreesToClose.add(rootSchema);
return rootSchema;
} catch (IOException e) {
// We can't proceed further without a schema, throw a runtime exception.
throw UserException.resourceError(e).message("Failed to create schema tree.").build(logger);
}
}
use of org.apache.calcite.schema.SchemaPlus in project drill by apache.
the class QueryContext method getNewDefaultSchema.
/**
* 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;
}
use of org.apache.calcite.schema.SchemaPlus in project drill by apache.
the class UserSession method setDefaultSchemaPath.
/**
* 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 = Lists.newArrayList(newDefaultSchemaPath.split("\\."));
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));
}
use of org.apache.calcite.schema.SchemaPlus in project drill by apache.
the class DescribeTableHandler method rewrite.
/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlDescribeTable node = unwrap(sqlNode, SqlDescribeTable.class);
try {
List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));
SqlNode fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_COLUMNS), null, SqlParserPos.ZERO, null);
final SqlIdentifier table = node.getTable();
final SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
final List<String> schemaPathGivenInCmd = Util.skipLast(table.names);
final SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
final String charset = Util.getDefaultCharset().name();
if (schema == null) {
SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.SCHEMA_PATH_JOINER.join(schemaPathGivenInCmd));
}
if (SchemaUtilites.isRootSchema(schema)) {
throw UserException.validationError().message("No schema selected.").build(logger);
}
final String tableName = Util.last(table.names);
// find resolved schema path
final String schemaPath = SchemaUtilites.unwrapAsDrillSchemaInstance(schema).getFullSchemaName();
if (schema.getTable(tableName) == null) {
throw UserException.validationError().message("Unknown table [%s] in schema [%s]", tableName, schemaPath).build(logger);
}
SqlNode schemaCondition = null;
if (!SchemaUtilites.isRootSchema(schema)) {
schemaCondition = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(schemaPath, charset, SqlParserPos.ZERO));
}
SqlNode where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, charset, SqlParserPos.ZERO));
where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
SqlNode columnFilter = null;
if (node.getColumn() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString(), charset, SqlParserPos.ZERO));
} else if (node.getColumnQualifier() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, node.getColumnQualifier());
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
} catch (Exception ex) {
throw UserException.planError(ex).message("Error while rewriting DESCRIBE query: %d", ex.getMessage()).build(logger);
}
}
use of org.apache.calcite.schema.SchemaPlus in project drill by apache.
the class JdbcStoragePlugin method registerSchemas.
@Override
public void registerSchemas(SchemaConfig config, SchemaPlus parent) {
JdbcCatalogSchema schema = new JdbcCatalogSchema(name);
SchemaPlus holder = parent.add(name, schema);
schema.setHolder(holder);
}
Aggregations