use of org.apache.calcite.sql.SqlNodeList in project drill by axbaretto.
the class ShowSchemasHandler method rewrite.
/**
* Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.SCHEMATA ...
*/
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlShowSchemas node = unwrap(sqlNode, SqlShowSchemas.class);
List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO));
SqlNode fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_SCHEMATA), null, SqlParserPos.ZERO, null);
SqlNode where = null;
final SqlNode likePattern = node.getLikePattern();
if (likePattern != null) {
where = DrillParserUtil.createCondition(new SqlIdentifier(SCHS_COL_SCHEMA_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, likePattern);
} else if (node.getWhereClause() != null) {
where = node.getWhereClause();
}
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
}
use of org.apache.calcite.sql.SqlNodeList 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.calcite.sql.SqlNodeList in project drill by apache.
the class MetastoreAnalyzeTableHandler method getColumnList.
/**
* Generates the column list with {@link SchemaPath#DYNAMIC_STAR} and columns required for analyze.
*/
private SqlNodeList getColumnList(List<SchemaPath> projectingColumns) {
SqlNodeList columnList = new SqlNodeList(SqlParserPos.ZERO);
columnList.add(new SqlIdentifier(SchemaPath.DYNAMIC_STAR, SqlParserPos.ZERO));
projectingColumns.stream().map(segmentColumn -> new SqlIdentifier(segmentColumn.getRootSegmentPath(), SqlParserPos.ZERO)).forEach(columnList::add);
return columnList;
}
use of org.apache.calcite.sql.SqlNodeList 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);
}
}
use of org.apache.calcite.sql.SqlNodeList in project drill by apache.
the class RefreshMetadataHandler method getColumnList.
/**
* Generates the column list specified in the Refresh statement
* @param sqlrefreshMetadata sql parse node representing refresh statement
* @return list of columns specified in the refresh command
*/
private SqlNodeList getColumnList(final SqlRefreshMetadata sqlrefreshMetadata) {
SqlNodeList columnList = sqlrefreshMetadata.getFieldList();
if (SqlNodeList.isEmptyList(columnList)) {
columnList = new SqlNodeList(SqlParserPos.ZERO);
columnList.add(new SqlIdentifier(SchemaPath.STAR_COLUMN.rootName(), SqlParserPos.ZERO));
}
return columnList;
}
Aggregations