use of org.apache.drill.exec.store.AbstractSchema in project drill by apache.
the class InfoSchemaRecordGenerator method visitTables.
/**
* Visit the tables in the given schema. The
* @param schemaPath the path to the given schema
* @param schema the given schema
*/
public void visitTables(String schemaPath, SchemaPlus schema) {
final AbstractSchema drillSchema = schema.unwrap(AbstractSchema.class);
final List<String> tableNames = Lists.newArrayList(schema.getTableNames());
for (Pair<String, ? extends Table> tableNameToTable : drillSchema.getTablesByNames(tableNames)) {
final String tableName = tableNameToTable.getKey();
final Table table = tableNameToTable.getValue();
final TableType tableType = table.getJdbcTableType();
// Visit the table, and if requested ...
if (shouldVisitTable(schemaPath, tableName, tableType) && visitTable(schemaPath, tableName, table)) {
// ... do for each of the table's fields.
final RelDataType tableRow = table.getRowType(new JavaTypeFactoryImpl());
for (RelDataTypeField field : tableRow.getFieldList()) {
if (shouldVisitColumn(schemaPath, tableName, field.getName())) {
visitField(schemaPath, tableName, field);
}
}
}
}
}
use of org.apache.drill.exec.store.AbstractSchema in project drill by apache.
the class SchemaUtilites method resolveToMutableDrillSchema.
/**
* 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;
}
use of org.apache.drill.exec.store.AbstractSchema in project drill by axbaretto.
the class ShowTablesHandler method rewrite.
/**
* Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.`TABLES` ...
*/
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlShowTables node = unwrap(sqlNode, SqlShowTables.class);
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);
}
use of org.apache.drill.exec.store.AbstractSchema in project drill by axbaretto.
the class InfoSchemaRecordGenerator method shouldVisitSchema.
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.class);
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);
// For other two results (TRUE, INCONCLUSIVE) continue to visit the schema.
return filter.evaluate(recordValues) != Result.FALSE;
} catch (ClassCastException e) {
// ignore and return true as this is not a Drill schema
}
return true;
}
use of org.apache.drill.exec.store.AbstractSchema 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 ForemanSetupException {
DrillSqlDescribeTable node = unwrap(sqlNode, DrillSqlDescribeTable.class);
try {
List<SqlNode> selectList = Arrays.asList(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(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.COLUMNS.name()), SqlParserPos.ZERO);
SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
List<String> schemaPathGivenInCmd = Util.skipLast(node.getTable().names);
SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
if (schema == null) {
SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.getSchemaPath(schemaPathGivenInCmd));
}
if (SchemaUtilites.isRootSchema(schema)) {
throw UserException.validationError().message("No schema selected.").build(logger);
}
// find resolved schema path
AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
String schemaPath = drillSchema.getFullSchemaName();
String tableName = Util.last(node.getTable().names);
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, Util.getDefaultCharset().name(), SqlParserPos.ZERO));
}
SqlNode tableNameColumn = new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO);
// if table names are case insensitive, wrap column values and condition in lower function
if (!drillSchema.areTableNamesCaseSensitive()) {
tableNameColumn = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, tableNameColumn);
tableName = tableName.toLowerCase();
}
SqlNode where = DrillParserUtil.createCondition(tableNameColumn, SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, Util.getDefaultCharset().name(), SqlParserPos.ZERO));
where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
SqlNode columnFilter = null;
if (node.getColumn() != null) {
columnFilter = DrillParserUtil.createCondition(SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO)), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString().toLowerCase(), Util.getDefaultCharset().name(), SqlParserPos.ZERO));
} else if (node.getColumnQualifier() != null) {
SqlNode columnQualifier = node.getColumnQualifier();
SqlNode column = new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO);
if (columnQualifier instanceof SqlCharStringLiteral) {
NlsString conditionString = ((SqlCharStringLiteral) columnQualifier).getNlsString();
columnQualifier = SqlCharStringLiteral.createCharString(conditionString.getValue().toLowerCase(), conditionString.getCharsetName(), columnQualifier.getParserPosition());
column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
}
columnFilter = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, columnQualifier);
}
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);
}
}
Aggregations