Skip to content

Commit

Permalink
skip presto view parsing (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Duin authored Mar 4, 2021
1 parent 8426e45 commit 20f4e44
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [3.9.1] - TBD
### Fixed
* Null pointer exception when creating a metastore tunnel by adding a check for null `configuration-properties`.
* Fixing issue where Presto views cannot be parsed resulting in errors.

## [3.9.0] - 2021-02-26
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ public enum ASTQueryMapping implements QueryMapping {

INSTANCE;

private static final String PRESTO_VIEW_MARKER = "/* Presto View";
private final static String RE_WORD_BOUNDARY = "\\b";
private final static Comparator<CommonToken> ON_START_INDEX = Comparator.comparingInt(CommonToken::getStartIndex);

@Override
public String transformOutboundDatabaseName(MetaStoreMapping metaStoreMapping, String query) {
if (hasNonHiveViewMarker(query)) {
// skipping queries that are not "Hive" view queries. We can't parse those.
return query;
}
ASTNode root;
try {
root = ParseUtils.parse(query);
Expand All @@ -56,6 +61,13 @@ public String transformOutboundDatabaseName(MetaStoreMapping metaStoreMapping, S
return result.toString();
}

private boolean hasNonHiveViewMarker(String query) {
if (query != null && query.trim().startsWith(PRESTO_VIEW_MARKER)) {
return true;
}
return false;
}

private StringBuilder transformDatabaseTableTokens(MetaStoreMapping metaStoreMapping, ASTNode root, String query) {
StringBuilder result = new StringBuilder();
SortedSet<CommonToken> dbNameTokens = extractDbNameTokens(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ public MetaStoreFilterHook getMetastoreFilter() {

@Override
public Table transformOutboundTable(Table table) {
table.setDbName(metaStoreMapping.transformOutboundDatabaseName(table.getDbName()));
String originalDatabaseName = table.getDbName();
String databaseName = metaStoreMapping.transformOutboundDatabaseName(originalDatabaseName);
table.setDbName(databaseName);
if (databaseName.equalsIgnoreCase(originalDatabaseName)) {
// Skip all the view parsing if nothing is going to change, the parsing is not without problems and we can't catch
// all use cases here. For instance Presto creates views that are stored in these fields and this is stored
// differently than Hive. There might be others.
return table;
}
if (table.isSetViewExpandedText()) {
try {
log.debug("Transforming ViewExpandedText: {}", table.getViewExpandedText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ public class ASTQueryMappingTest {

@Before
public void setUp() {
metaStoreMapping = new PrefixMapping(new MetaStoreMappingImpl(PREFIX, "mapping", null,
null, DIRECT, LATENCY, new DefaultMetaStoreFilterHookImpl(new HiveConf())));
metaStoreMapping = new PrefixMapping(new MetaStoreMappingImpl(PREFIX, "mapping", null, null, DIRECT, LATENCY,
new DefaultMetaStoreFilterHookImpl(new HiveConf())));
}

@Test
public void transformOutboundDatabaseNamePrestoMarker() {
ASTQueryMapping queryMapping = ASTQueryMapping.INSTANCE;

String query = "/* Presto View */";

assertThat(queryMapping.transformOutboundDatabaseName(metaStoreMapping, query), is(query));
}

@Test
public void transformOutboundDatabaseNamePrestoExpandedTextMarker() {
ASTQueryMapping queryMapping = ASTQueryMapping.INSTANCE;

String query = "/* Presto View: <base64 of view sql> */";

assertThat(queryMapping.transformOutboundDatabaseName(metaStoreMapping, query),
is(query));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ public void transformOutboundTableView() throws Exception {
assertThat(result.getViewOriginalText(), is(VIEW_ORIGINAL_TEXT_TRANSFORMED));
}

@Test
public void transformOutboundTableViewIgnoreParsingWhenSame() throws Exception {
Table table = new Table();
table.setDbName(OUT_DB_NAME);
table.setViewExpandedText(VIEW_EXPANDED_TEXT);
table.setViewOriginalText(VIEW_ORIGINAL_TEXT);

Table result = databaseMapping.transformOutboundTable(table);
assertThat(result, is(sameInstance(table)));
assertThat(result.getDbName(), is(OUT_DB_NAME));
assertThat(result.getViewExpandedText(), is(VIEW_EXPANDED_TEXT));
assertThat(result.getViewOriginalText(), is(VIEW_ORIGINAL_TEXT));
}

@Test
public void transformOutboundTableViewExpandedTextErrorKeepOriginal() throws Exception {
String viewExpandedText = "error";
Expand Down

0 comments on commit 20f4e44

Please sign in to comment.