Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ protected Node visitShowCatalogs(ShowCatalogs node, Void context)
Optional<Expression> predicate = Optional.empty();
Optional<String> likePattern = node.getLikePattern();
if (likePattern.isPresent()) {
predicate = Optional.of(new LikePredicate(identifier("Catalog"), new StringLiteral(likePattern.get()), Optional.empty()));
predicate = Optional.of(new LikePredicate(
identifier("Catalog"),
new StringLiteral(likePattern.get()),
node.getEscape().map(StringLiteral::new)));
}

return simpleQuery(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.sql.query;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestShowQueries
{
private QueryAssertions assertions;

@BeforeClass
public void init()
{
assertions = new QueryAssertions();
}

@AfterClass(alwaysRun = true)
public void teardown()
{
assertions.close();
assertions = null;
}

@Test
public void testShowCatalogsLikeWithEscape()
{
assertions.assertFails("SHOW CATALOGS LIKE 't$_%' ESCAPE ''", "Escape string must be a single character");
assertions.assertFails("SHOW CATALOGS LIKE 't$_%' ESCAPE '$$'", "Escape string must be a single character");
assertions.assertQuery("SHOW CATALOGS LIKE '%$_%' ESCAPE '$'", "VALUES('testing_catalog')");
assertions.assertQuery("SHOW CATALOGS LIKE '$_%' ESCAPE '$'", "SELECT 'testing_catalog' WHERE FALSE");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ statement
(LIKE pattern=string (ESCAPE escape=string)?)? #showTables
| SHOW SCHEMAS ((FROM | IN) identifier)?
(LIKE pattern=string (ESCAPE escape=string)?)? #showSchemas
| SHOW CATALOGS (LIKE pattern=string)? #showCatalogs
| SHOW CATALOGS
(LIKE pattern=string (ESCAPE escape=string)?)? #showCatalogs
| SHOW COLUMNS (FROM | IN) qualifiedName #showColumns
| SHOW STATS FOR qualifiedName #showStats
| SHOW STATS FOR '(' querySpecification ')' #showStatsForQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,10 @@ protected Void visitShowCatalogs(ShowCatalogs node, Integer context)
builder.append(" LIKE ")
.append(formatStringLiteral(value)));

node.getEscape().ifPresent((value) ->
builder.append(" ESCAPE ")
.append(formatStringLiteral(value)));

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ public Node visitShowCatalogs(SqlBaseParser.ShowCatalogsContext context)
{
return new ShowCatalogs(getLocation(context),
getTextIfPresent(context.pattern)
.map(AstBuilder::unquote),
getTextIfPresent(context.escape)
.map(AstBuilder::unquote));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
Expand All @@ -25,28 +26,35 @@ public final class ShowCatalogs
extends Statement
{
private final Optional<String> likePattern;
private final Optional<String> escape;

public ShowCatalogs(Optional<String> likePattern)
public ShowCatalogs(Optional<String> likePattern, Optional<String> escape)
{
this(Optional.empty(), likePattern);
this(Optional.empty(), likePattern, escape);
}

public ShowCatalogs(NodeLocation location, Optional<String> likePattern)
public ShowCatalogs(NodeLocation location, Optional<String> likePattern, Optional<String> escape)
{
this(Optional.of(location), likePattern);
this(Optional.of(location), likePattern, escape);
}

public ShowCatalogs(Optional<NodeLocation> location, Optional<String> likePattern)
public ShowCatalogs(Optional<NodeLocation> location, Optional<String> likePattern, Optional<String> escape)
{
super(location);
this.likePattern = requireNonNull(likePattern, "likePattern is null");
this.escape = requireNonNull(escape, "escape is null");
}

public Optional<String> getLikePattern()
{
return likePattern;
}

public Optional<String> getEscape()
{
return escape;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
Expand All @@ -62,7 +70,7 @@ public List<Node> getChildren()
@Override
public int hashCode()
{
return 0;
return Objects.hash(likePattern, escape);
}

@Override
Expand All @@ -71,12 +79,20 @@ public boolean equals(Object obj)
if (this == obj) {
return true;
}
return (obj != null) && (getClass() == obj.getClass());
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
ShowCatalogs o = (ShowCatalogs) obj;
return Objects.equals(likePattern, o.likePattern) &&
Objects.equals(escape, o.escape);
}

@Override
public String toString()
{
return toStringHelper(this).toString();
return toStringHelper(this)
.add("likePattern", likePattern)
.add("escape", escape)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,9 @@ public void testShowSession()
@Test
public void testShowCatalogs()
{
assertStatement("SHOW CATALOGS", new ShowCatalogs(Optional.empty()));
assertStatement("SHOW CATALOGS LIKE '%'", new ShowCatalogs(Optional.of("%")));
assertStatement("SHOW CATALOGS", new ShowCatalogs(Optional.empty(), Optional.empty()));
assertStatement("SHOW CATALOGS LIKE '%'", new ShowCatalogs(Optional.of("%"), Optional.empty()));
assertStatement("SHOW CATALOGS LIKE '%$_%' ESCAPE '$'", new ShowCatalogs(Optional.of("%$_%"), Optional.of("$")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ public Object[][] getStatements()
{"WITH t AS (SELECT 1 SELECT t.* FROM t",
"line 1:21: mismatched input 'SELECT'. Expecting: '%', '(', ')', '*', '+', ',', '-', '.', '/', 'AND', 'AS', 'AT', 'EXCEPT', 'FETCH', 'FROM', " +
"'GROUP', 'HAVING', 'INTERSECT', 'LIMIT', 'OFFSET', 'OR', 'ORDER', 'SELECT', 'TABLE', 'UNION', 'VALUES', 'WHERE', '[', '||', <EOF>, " +
"<identifier>, <predicate>"}
"<identifier>, <predicate>"},
{"SHOW CATALOGS LIKE '%$_%' ESCAPE",
"line 1:33: mismatched input '<EOF>'. Expecting: <string>"}
};
}

Expand Down