forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using legacy Hive view translation logic
- Loading branch information
Showing
8 changed files
with
743 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
presto-hive/src/main/java/io/prestosql/plugin/hive/HiveQlToPrestoTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* 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.plugin.hive; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.PeekingIterator; | ||
import io.prestosql.spi.PrestoException; | ||
|
||
import static com.google.common.collect.Iterators.peekingIterator; | ||
import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_VIEW_TRANSLATION_ERROR; | ||
import static org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer.unescapeSQLString; | ||
|
||
/** | ||
* Translate statements in Hive QL to Presto SQL. | ||
* | ||
* Only translation of quoted literals is currently included. | ||
*/ | ||
public final class HiveQlToPrestoTranslator | ||
{ | ||
// Translation methods consume data from the iterator | ||
private final PeekingIterator<Character> input; | ||
private final StringBuilder output = new StringBuilder(); | ||
|
||
/** | ||
* Translate a HiveQL statement to Presto SQL by fixing quoted identifiers | ||
* and string literals. No other translation is performed. | ||
* | ||
* <p>Backquotes are replaced with double quotes, including SQL-style | ||
* escapes (`` becomes ` and " becomes ""). | ||
* | ||
* <p>Single and double quotes are replaced with single quotes, with | ||
* minimal processing of escape sequences to ensure that the strings end in | ||
* the right place. | ||
*/ | ||
public static String translateHiveViewToPresto(String hiveStatement) | ||
{ | ||
HiveQlToPrestoTranslator translator = new HiveQlToPrestoTranslator(hiveStatement); | ||
return translator.translateQuotedLiterals(); | ||
} | ||
|
||
private HiveQlToPrestoTranslator(String hiveQl) | ||
{ | ||
input = peekingIterator(Lists.charactersOf(hiveQl).iterator()); | ||
} | ||
|
||
private String translateQuotedLiterals() | ||
{ | ||
// Consume input, passing control to other translation methods when | ||
// their delimiters are encountered. | ||
while (input.hasNext()) { | ||
char c = input.next(); | ||
switch (c) { | ||
case '"': | ||
case '\'': | ||
translateString(c); | ||
break; | ||
case '`': | ||
translateQuotedIdentifier(); | ||
break; | ||
default: | ||
output.append(c); | ||
break; | ||
} | ||
} | ||
return output.toString(); | ||
} | ||
|
||
private void translateString(char delimiter) | ||
{ | ||
// Build a copy of the string to pass to Hive's string unescaper | ||
StringBuilder string = new StringBuilder(String.valueOf(delimiter)); | ||
while (input.hasNext()) { | ||
char c = input.next(); | ||
|
||
if (c == delimiter) { | ||
string.append(delimiter); | ||
String unescaped = unescapeSQLString(string.toString()); | ||
output.append("'"); | ||
output.append(unescaped.replace("'", "''")); | ||
output.append("'"); | ||
return; | ||
} | ||
|
||
string.append(c); | ||
|
||
if (c == '\\') { | ||
if (!input.hasNext()) { | ||
break; // skip to end-of-input error | ||
} | ||
string.append(input.next()); | ||
} | ||
} | ||
throw hiveViewParseError("unexpected end of input in string"); | ||
} | ||
|
||
private void translateQuotedIdentifier() | ||
{ | ||
output.append('"'); | ||
while (input.hasNext()) { | ||
char c = input.next(); | ||
|
||
if (c == '"') { | ||
// escape " as "" | ||
output.append("\"\""); | ||
} | ||
else if (c == '`' && input.hasNext() && input.peek() == '`') { | ||
// un-escape `` as ` | ||
output.append('`'); | ||
input.next(); | ||
} | ||
else if (c == '`') { | ||
// end of identifier | ||
output.append('"'); | ||
return; | ||
} | ||
else { | ||
// don't change characters besides ` and " | ||
output.append(c); | ||
} | ||
} | ||
throw hiveViewParseError("unexpected end of input in identifier"); | ||
} | ||
|
||
private static PrestoException hiveViewParseError(String message) | ||
{ | ||
return new PrestoException(HIVE_VIEW_TRANSLATION_ERROR, "Error translating Hive view to Presto: " + message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
presto-hive/src/main/java/io/prestosql/plugin/hive/LegacyHiveViewReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.plugin.hive; | ||
|
||
import io.prestosql.plugin.base.CatalogName; | ||
import io.prestosql.plugin.hive.metastore.Table; | ||
import io.prestosql.spi.PrestoException; | ||
import io.prestosql.spi.connector.ConnectorViewDefinition; | ||
import io.prestosql.spi.type.TypeId; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_INVALID_METADATA; | ||
import static io.prestosql.plugin.hive.HiveMetadata.TABLE_COMMENT; | ||
import static io.prestosql.plugin.hive.HiveQlToPrestoTranslator.translateHiveViewToPresto; | ||
|
||
public class LegacyHiveViewReader | ||
implements ViewReaderUtil.ViewReader | ||
{ | ||
@Override | ||
public ConnectorViewDefinition decodeViewData(String viewData, Table table, CatalogName catalogName) | ||
{ | ||
String viewText = table.getViewExpandedText() | ||
.orElseThrow(() -> new PrestoException(HIVE_INVALID_METADATA, "No view expanded text: " + table.getSchemaTableName())); | ||
return new ConnectorViewDefinition( | ||
translateHiveViewToPresto(viewText), | ||
Optional.of(catalogName.toString()), | ||
Optional.ofNullable(table.getDatabaseName()), | ||
table.getDataColumns().stream() | ||
.map(column -> new ConnectorViewDefinition.ViewColumn(column.getName(), TypeId.of(column.getType().getTypeSignature().toString()))) | ||
.collect(toImmutableList()), | ||
Optional.ofNullable(table.getParameters().get(TABLE_COMMENT)), | ||
Optional.of(table.getOwner()), | ||
false); // don't run as invoker | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.