Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip presto view parsing #211

Merged
merged 7 commits into from
Mar 4, 2021
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
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what's going on here - why is the db name the same in this case?

Copy link
Contributor Author

@patduin patduin Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it is that's true that's why I'm checking:
if our transform didn't change the database name we don't need to try and do the parsing just return the table as is (nothing would have changed).

This creeped in when we removed the IdentityMapping (which was just returning table for non prefixed metastores). So the if will mimic that behaviour again.

// Skip all the view parsing if nothing is going to change, the parsing is not without problems and we can't catch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth keeping the code on line 123 since that will throw an error still?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original can't have the same string as the extended cause it also needs the colon. if we add it will it still throw an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I return the table in this if so we'll not even try to parse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but if the database name is different it will still throw an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because I skip parsing in the parser class for Presto markers. The if in here is to ensure other crazy markers don't also through errors. The code here is to basically get back to same support as in pre WD 3.6.0. WD 3.7.0 introduced the problems.
If we really have to support Presto I got this follow up: #212

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for creating that issue. So for now we'll return the Presto view "as is" but that could mean it won't actually work since it doesn't have the correct prefixes and that's what #212 is all about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct this is fixing and reverting old behaviour. Make it work again if there are no prefixes to remove.

// 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