@Override protected Void visitShowTables(ShowTables node, Integer context) { builder.append("SHOW TABLES"); node.getSchema().ifPresent((value) -> builder.append(" FROM ") .append(value)); node.getLikePattern().ifPresent((value) -> builder.append(" LIKE ") .append(formatStringLiteral(value))); return null; }
@Override public Node visitShowTables(SqlBaseParser.ShowTablesContext context) { return new ShowTables( getLocation(context), Optional.ofNullable(context.qualifiedName()) .map(AstBuilder::getQualifiedName), getTextIfPresent(context.pattern) .map(AstBuilder::unquote)); }
@Test public void testShowTables() throws Exception { assertStatement("SHOW TABLES", new ShowTables(Optional.empty(), Optional.empty())); assertStatement("SHOW TABLES FROM a", new ShowTables(Optional.of(QualifiedName.of("a")), Optional.empty())); assertStatement("SHOW TABLES IN a LIKE '%'", new ShowTables(Optional.of(QualifiedName.of("a")), Optional.of("%"))); }
@Override protected RelationType visitShowTables(ShowTables showTables, AnalysisContext context) { String catalogName = session.getCatalog().orElse(null); String schemaName = session.getSchema().orElse(null); Optional<QualifiedName> schema = showTables.getSchema(); if (schema.isPresent()) { List<String> parts = schema.get().getParts(); if (parts.size() > 2) { throw new SemanticException(INVALID_SCHEMA_NAME, showTables, "Too many parts in schema name: %s", schema); } if (parts.size() == 2) { catalogName = parts.get(0); } schemaName = schema.get().getSuffix(); } if (catalogName == null) { throw new SemanticException(CATALOG_NOT_SPECIFIED, showTables, "Catalog must be specified when session catalog is not set"); } if (schemaName == null) { throw new SemanticException(SCHEMA_NOT_SPECIFIED, showTables, "Schema must be specified when session schema is not set"); } if (!metadata.listSchemaNames(session, catalogName).contains(schemaName)) { throw new SemanticException(MISSING_SCHEMA, showTables, "Schema '%s' does not exist", schemaName); } Expression predicate = equal(nameReference("table_schema"), new StringLiteral(schemaName)); Optional<String> likePattern = showTables.getLikePattern(); if (likePattern.isPresent()) { Expression likePredicate = new LikePredicate(nameReference("table_name"), new StringLiteral(likePattern.get()), null); predicate = logicalAnd(predicate, likePredicate); } Query query = simpleQuery( selectList(aliasedName("table_name", "Table")), from(catalogName, TABLE_TABLES), predicate, ordering(ascending("table_name"))); return process(query, context); }