Skip to content

Commit

Permalink
expand an reuse UDFLike#likePatternToRegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
wecharyu committed Jun 29, 2024
1 parent 2c22ae8 commit 14e9f76
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public int execute() throws HiveException {
List<String> databases = context.getDb().getAllDatabases();
if (desc.getPattern() != null) {
LOG.debug("pattern: {}", desc.getPattern());
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern()), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern(), true), Pattern.CASE_INSENSITIVE);
databases = databases.stream().filter(name -> pattern.matcher(name).matches()).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public int execute() throws HiveException {
List<String> connectors = context.getDb().getAllDataConnectorNames();
if (desc.getPattern() != null) {
LOG.debug("pattern: {}", desc.getPattern());
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern()), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern(), true), Pattern.CASE_INSENSITIVE);
connectors = connectors.stream().filter(name -> pattern.matcher(name).matches()).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int execute() throws HiveException {
LOG.debug("pattern: {}", desc.getPattern());
List<String> tableNames = context.getDb().getTablesForDb(desc.getDbName(), null);
if (desc.getPattern() != null) {
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern()), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern(), true), Pattern.CASE_INSENSITIVE);
tableNames = tableNames.stream()
.filter(name -> pattern.matcher(name).matches())
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.ddl.DDLOperation;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.UDFLike;

/**
* Operation process showing the tables.
Expand All @@ -59,7 +58,7 @@ public int execute() throws HiveException {
}

private void showTables() throws HiveException {
String pattern = MetaStoreUtils.convertSqlPatternToRegExp(desc.getPattern());
String pattern = UDFLike.likePatternToRegExp(desc.getPattern(), false);
List<String> tableNames = new ArrayList<>(
context.getDb().getTablesByType(desc.getDbName(), pattern, desc.getTypeFilter()));
Collections.sort(tableNames);
Expand All @@ -75,13 +74,12 @@ private void showTables() throws HiveException {

private void showTablesExtended() throws HiveException {
TreeMap<String, String> tableNameToType = new TreeMap<>();
String pattern = MetaStoreUtils.convertSqlPatternToRegExp(desc.getPattern());
String pattern = UDFLike.likePatternToRegExp(desc.getPattern(), false);
TableType typeFilter = desc.getTypeFilter();
TableType[] tableTypes = typeFilter == null ? TableType.values() : new TableType[]{typeFilter};
for (TableType tableType : tableTypes) {
String typeString = tableType.toString();
List<String> tables = context.getDb().getTablesByType(desc.getDbName(), pattern, tableType);
tables.forEach(name -> tableNameToType.put(name, typeString));
tables.forEach(name -> tableNameToType.put(name, tableType.toString()));
}
LOG.debug("Found {} table(s) matching the SHOW EXTENDED TABLES statement.", tableNameToType.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public int execute() throws HiveException {
List<Table> viewObjects = new ArrayList<>(
context.getDb().getMaterializedViewObjectsByPattern(desc.getDbName(), null));
if (desc.getPattern() != null) {
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern()), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(UDFLike.likePatternToRegExp(desc.getPattern(), true), Pattern.CASE_INSENSITIVE);
viewObjects = viewObjects.stream()
.filter(object -> pattern.matcher(object.getTableName()).matches())
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.ddl.DDLOperation;
import org.apache.hadoop.hive.ql.ddl.DDLOperationContext;
import org.apache.hadoop.hive.ql.ddl.ShowUtils;
import org.apache.hadoop.hive.ql.ddl.table.info.show.tables.ShowTablesFormatter;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.UDFLike;

/**
* Operation process showing the views.
Expand All @@ -46,7 +46,7 @@ public int execute() throws HiveException {
throw new HiveException(ErrorMsg.DATABASE_NOT_EXISTS, desc.getDbName());
}

String pattern = MetaStoreUtils.convertSqlPatternToRegExp(desc.getPattern());
String pattern = UDFLike.likePatternToRegExp(desc.getPattern(), false);
List<String> viewNames = context.getDb().getTablesByType(desc.getDbName(), pattern, TableType.VIRTUAL_VIEW);
Collections.sort(viewNames);
LOG.debug("Found {} view(s) matching the SHOW VIEWS statement.", viewNames.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ public static Set<String> getFunctionNamesByLikePattern(String funcPatternStr) {
Set<String> allFuncs = getFunctionNames();
String[] subpatterns = funcPatternStr.trim().split("\\|");
for (String subpattern : subpatterns) {
subpattern = "(?i)" + UDFLike.likePatternToRegExp(subpattern);
subpattern = "(?i)" + UDFLike.likePatternToRegExp(subpattern, true);
try {
Pattern patternObj = Pattern.compile(subpattern);
for (String funcName : allFuncs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ String format(String pattern) {
COMPLEX(ComplexChecker.class) {
@Override
String format(String pattern) {
return "^" + UDFLike.likePatternToRegExp(pattern) + "$";
return "^" + UDFLike.likePatternToRegExp(pattern, true) + "$";
}
},
// Accepts chained LIKE patterns without escaping like "abc%def%ghi%" and
Expand Down
11 changes: 7 additions & 4 deletions ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ private enum PatternType {
public UDFLike() {
}

public static String likePatternToRegExp(String likePattern) {
public static String likePatternToRegExp(String likePattern, boolean quote) {
if (likePattern == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < likePattern.length(); i++) {
// Make a special case for "\\_" and "\\%"
Expand All @@ -77,9 +80,9 @@ public static String likePatternToRegExp(String likePattern) {
if (n == '_') {
sb.append(".");
} else if (n == '%') {
sb.append(".*?");
sb.append(".*");
} else {
sb.append(Pattern.quote(Character.toString(n)));
sb.append(quote ? Pattern.quote(Character.toString(n)) : n);
}
}
return sb.toString();
Expand Down Expand Up @@ -184,7 +187,7 @@ public BooleanWritable evaluate(Text s, Text likePattern) {

parseSimplePattern(strLikePattern);
if (type == PatternType.COMPLEX) {
p = Pattern.compile(likePatternToRegExp(strLikePattern), Pattern.DOTALL);
p = Pattern.compile(likePatternToRegExp(strLikePattern, true), Pattern.DOTALL);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,7 @@ public List<String> getTables(String catName, String dbName, String tablePattern
Database db = null;
try {
db = getDatabase(catName, dbName);
} catch (Exception e) { /* appears exception is not thrown currently if db doesnt exist */ }
} catch (NoSuchObjectException e) { /* appears exception is not thrown currently if db doesnt exist */ }

if (MetaStoreUtils.isDatabaseRemote(db)) {
// TODO: remote database does not support list table names by pattern yet.
Expand Down Expand Up @@ -2930,7 +2930,7 @@ public List<String> getTables(String catName, String dbName, String tablePattern
String[] patterns = tablePattern.split("\\|");
for (String pattern : patterns) {
pattern = "(?i)" + pattern.replaceAll("\\.\\*", "\\*").replaceAll("\\*", ".*");
String filter = hive_metastoreConstants.HIVE_FILTER_FIELD_TABLE_NAME + " like \"" + pattern + "\"";
String filter = String.format("%s like \"%s\"", hive_metastoreConstants.HIVE_FILTER_FIELD_TABLE_NAME, pattern);
tables.addAll(listTableNamesByFilter(catName, dbName, filter, (short) -1));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1337,33 +1337,6 @@ public static String getHttpPath(String httpPath) {
return httpPath;
}

public static String convertSqlPatternToRegExp(String likePattern) {
if (likePattern == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < likePattern.length(); i++) {
// Make a special case for "\\_" and "\\%"
char n = likePattern.charAt(i);
if (n == '\\'
&& i + 1 < likePattern.length()
&& (likePattern.charAt(i + 1) == '_' || likePattern.charAt(i + 1) == '%')) {
sb.append(likePattern.charAt(i + 1));
i++;
continue;
}

if (n == '_') {
sb.append(".");
} else if (n == '%') {
sb.append(".*");
} else {
sb.append(n);
}
}
return sb.toString();
}

public static boolean isDatabaseRemote(Database db) {
return db != null && db.getType() == DatabaseType.REMOTE;
}
Expand Down

0 comments on commit 14e9f76

Please sign in to comment.