From d82cc2b5501585205b5776fc14b2647b046f13db Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 7 Apr 2022 22:19:52 +0200 Subject: [PATCH 1/5] Remove Slice usage from Char, String and Binary literals This allows us to keep core/parser independent of Slice JDK 11 requirement. --- .../sql/analyzer/ExpressionAnalyzer.java | 5 ++--- .../ConnectorExpressionTranslator.java | 6 +++--- .../trino/sql/planner/DomainTranslator.java | 7 ++++--- .../sql/planner/ExpressionInterpreter.java | 7 ++++--- .../trino/sql/planner/LiteralInterpreter.java | 7 ++++--- .../SqlToRowExpressionTranslator.java | 8 ++++---- .../trino/sql/TestExpressionInterpreter.java | 9 +-------- .../assertions/ExpressionVerifier.java | 8 +++++--- core/trino-parser/pom.xml | 5 ----- .../java/io/trino/sql/tree/BinaryLiteral.java | 20 +++++++++---------- .../java/io/trino/sql/tree/CharLiteral.java | 12 +++++------ .../java/io/trino/sql/tree/StringLiteral.java | 11 ++++------ .../io/trino/spi/expression/Constant.java | 6 ++++++ 13 files changed, 51 insertions(+), 60 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java b/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java index 64b4dba687dc..1341e2be9597 100644 --- a/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java +++ b/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java @@ -19,7 +19,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import com.google.common.collect.Streams; -import io.airlift.slice.SliceUtf8; import io.trino.Session; import io.trino.execution.warnings.WarningCollector; import io.trino.metadata.BoundSignature; @@ -982,14 +981,14 @@ protected Type visitArrayConstructor(ArrayConstructor node, StackableAstVisitorC @Override protected Type visitStringLiteral(StringLiteral node, StackableAstVisitorContext context) { - VarcharType type = VarcharType.createVarcharType(SliceUtf8.countCodePoints(node.getSlice())); + VarcharType type = VarcharType.createVarcharType(node.length()); return setExpressionType(node, type); } @Override protected Type visitCharLiteral(CharLiteral node, StackableAstVisitorContext context) { - CharType type = CharType.createCharType(node.getValue().length()); + CharType type = CharType.createCharType(node.length()); return setExpressionType(node, type); } diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java b/core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java index a68c9e1dbe1f..bbf8a5046ee7 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/ConnectorExpressionTranslator.java @@ -459,7 +459,7 @@ protected Optional visitBooleanLiteral(BooleanLiteral node, @Override protected Optional visitStringLiteral(StringLiteral node, Void context) { - return Optional.of(new Constant(node.getSlice(), typeOf(node))); + return Optional.of(new Constant(Slices.utf8Slice(node.getValue()), typeOf(node))); } @Override @@ -477,13 +477,13 @@ protected Optional visitDecimalLiteral(DecimalLiteral node, @Override protected Optional visitCharLiteral(CharLiteral node, Void context) { - return Optional.of(new Constant(node.getSlice(), typeOf(node))); + return Optional.of(new Constant(Slices.utf8Slice(node.getValue()), typeOf(node))); } @Override protected Optional visitBinaryLiteral(BinaryLiteral node, Void context) { - return Optional.of(new Constant(node.getValue(), typeOf(node))); + return Optional.of(new Constant(Slices.wrappedBuffer(node.getValue()), typeOf(node))); } @Override diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/DomainTranslator.java b/core/trino-main/src/main/java/io/trino/sql/planner/DomainTranslator.java index 5254f49f805b..8823a74140ca 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/DomainTranslator.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/DomainTranslator.java @@ -1004,10 +1004,11 @@ private Optional tryVisitLikePredicate(LikePredicate node, Boo VarcharType varcharType = (VarcharType) type; Symbol symbol = Symbol.from(node.getValue()); - Slice pattern = ((StringLiteral) node.getPattern()).getSlice(); + Slice pattern = Slices.utf8Slice(((StringLiteral) node.getPattern()).getValue()); Optional escape = node.getEscape() .map(StringLiteral.class::cast) - .map(StringLiteral::getSlice); + .map(StringLiteral::getValue) + .map(Slices::utf8Slice); int patternConstantPrefixBytes = LikeFunctions.patternConstantPrefixBytes(pattern, escape); if (patternConstantPrefixBytes == pattern.length()) { @@ -1077,7 +1078,7 @@ private Optional tryVisitStartsWithFunction(FunctionCall node, } Symbol symbol = Symbol.from(target); - Slice constantPrefix = ((StringLiteral) prefix).getSlice(); + Slice constantPrefix = Slices.utf8Slice(((StringLiteral) prefix).getValue()); return createRangeDomain(type, constantPrefix).map(domain -> new ExtractionResult(TupleDomain.withColumnDomains(ImmutableMap.of(symbol, domain)), node)); } diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java b/core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java index cd432878cf71..b7484064d4ca 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/ExpressionInterpreter.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Primitives; import io.airlift.slice.Slice; +import io.airlift.slice.Slices; import io.trino.Session; import io.trino.execution.warnings.WarningCollector; import io.trino.metadata.FunctionNullability; @@ -1211,11 +1212,11 @@ private JoniRegexp getConstantPattern(LikePredicate node) StringLiteral pattern = (StringLiteral) node.getPattern(); if (node.getEscape().isPresent()) { - Slice escape = ((StringLiteral) node.getEscape().get()).getSlice(); - result = LikeFunctions.likePattern(pattern.getSlice(), escape); + Slice escape = Slices.utf8Slice(((StringLiteral) node.getEscape().get()).getValue()); + result = LikeFunctions.likePattern(Slices.utf8Slice(pattern.getValue()), escape); } else { - result = LikeFunctions.compileLikePattern(pattern.getSlice()); + result = LikeFunctions.compileLikePattern(Slices.utf8Slice(pattern.getValue())); } likePatternCache.put(node, result); diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/LiteralInterpreter.java b/core/trino-main/src/main/java/io/trino/sql/planner/LiteralInterpreter.java index fb0ccda2001c..529f85146c3b 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/LiteralInterpreter.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/LiteralInterpreter.java @@ -15,6 +15,7 @@ import com.google.common.collect.ImmutableList; import io.airlift.slice.Slice; +import io.airlift.slice.Slices; import io.trino.Session; import io.trino.metadata.ResolvedFunction; import io.trino.spi.connector.ConnectorSession; @@ -125,19 +126,19 @@ protected Object visitDecimalLiteral(DecimalLiteral node, Void context) @Override protected Slice visitStringLiteral(StringLiteral node, Void context) { - return node.getSlice(); + return Slices.utf8Slice(node.getValue()); } @Override protected Object visitCharLiteral(CharLiteral node, Void context) { - return node.getSlice(); + return Slices.utf8Slice(node.getValue()); } @Override protected Slice visitBinaryLiteral(BinaryLiteral node, Void context) { - return node.getValue(); + return Slices.wrappedBuffer(node.getValue()); } @Override diff --git a/core/trino-main/src/main/java/io/trino/sql/relational/SqlToRowExpressionTranslator.java b/core/trino-main/src/main/java/io/trino/sql/relational/SqlToRowExpressionTranslator.java index d26d73b632f1..88d648c212a9 100644 --- a/core/trino-main/src/main/java/io/trino/sql/relational/SqlToRowExpressionTranslator.java +++ b/core/trino-main/src/main/java/io/trino/sql/relational/SqlToRowExpressionTranslator.java @@ -79,8 +79,8 @@ import java.util.Map; import static com.google.common.collect.ImmutableList.toImmutableList; -import static io.airlift.slice.SliceUtf8.countCodePoints; import static io.airlift.slice.Slices.utf8Slice; +import static io.airlift.slice.Slices.wrappedBuffer; import static io.trino.spi.function.OperatorType.EQUAL; import static io.trino.spi.function.OperatorType.HASH_CODE; import static io.trino.spi.function.OperatorType.INDETERMINATE; @@ -228,19 +228,19 @@ protected RowExpression visitDecimalLiteral(DecimalLiteral node, Void context) @Override protected RowExpression visitStringLiteral(StringLiteral node, Void context) { - return constant(node.getSlice(), createVarcharType(countCodePoints(node.getSlice()))); + return constant(utf8Slice(node.getValue()), createVarcharType(node.length())); } @Override protected RowExpression visitCharLiteral(CharLiteral node, Void context) { - return constant(node.getSlice(), createCharType(node.getValue().length())); + return constant(utf8Slice(node.getValue()), createCharType(node.length())); } @Override protected RowExpression visitBinaryLiteral(BinaryLiteral node, Void context) { - return constant(node.getValue(), VARBINARY); + return constant(wrappedBuffer(node.getValue()), VARBINARY); } @Override diff --git a/core/trino-main/src/test/java/io/trino/sql/TestExpressionInterpreter.java b/core/trino-main/src/test/java/io/trino/sql/TestExpressionInterpreter.java index b0fe42d3aba2..a7c8104076d4 100644 --- a/core/trino-main/src/test/java/io/trino/sql/TestExpressionInterpreter.java +++ b/core/trino-main/src/test/java/io/trino/sql/TestExpressionInterpreter.java @@ -1895,14 +1895,7 @@ private static void assertLike(byte[] value, String pattern, boolean expected) private static StringLiteral rawStringLiteral(Slice slice) { - return new StringLiteral(slice.toStringUtf8()) - { - @Override - public Slice getSlice() - { - return slice; - } - }; + return new StringLiteral(slice.toStringUtf8()); } private static void assertOptimizedEquals(@Language("SQL") String actual, @Language("SQL") String expected) diff --git a/core/trino-main/src/test/java/io/trino/sql/planner/assertions/ExpressionVerifier.java b/core/trino-main/src/test/java/io/trino/sql/planner/assertions/ExpressionVerifier.java index f3ee7ce2c275..5175bc4f9270 100644 --- a/core/trino-main/src/test/java/io/trino/sql/planner/assertions/ExpressionVerifier.java +++ b/core/trino-main/src/test/java/io/trino/sql/planner/assertions/ExpressionVerifier.java @@ -116,9 +116,7 @@ protected Boolean visitStringLiteral(StringLiteral actual, Node expectedExpressi return false; } - StringLiteral expected = (StringLiteral) expectedExpression; - - return actual.getValue().equals(expected.getValue()); + return getValueFromLiteral(actual).equals(getValueFromLiteral(expectedExpression)); } @Override @@ -203,6 +201,10 @@ private static String getValueFromLiteral(Node expression) return ((GenericLiteral) expression).getValue(); } + if (expression instanceof StringLiteral) { + return ((StringLiteral) expression).getValue(); + } + throw new IllegalArgumentException("Unsupported literal expression type: " + expression.getClass().getName()); } diff --git a/core/trino-parser/pom.xml b/core/trino-parser/pom.xml index 5851411202c2..deabd3d8f516 100644 --- a/core/trino-parser/pom.xml +++ b/core/trino-parser/pom.xml @@ -18,11 +18,6 @@ - - io.airlift - slice - - com.google.code.findbugs jsr305 diff --git a/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java b/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java index 4cfe1442438e..3ae5e49d2e47 100644 --- a/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java +++ b/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java @@ -14,11 +14,9 @@ package io.trino.sql.tree; import com.google.common.io.BaseEncoding; -import io.airlift.slice.Slice; -import io.airlift.slice.Slices; import io.trino.sql.parser.ParsingException; -import java.util.Objects; +import java.util.Arrays; import java.util.Optional; import java.util.regex.Pattern; @@ -32,7 +30,7 @@ public class BinaryLiteral private static final Pattern WHITESPACE_PATTERN = Pattern.compile("[ \\r\\n\\t]"); private static final Pattern NOT_HEX_DIGIT_PATTERN = Pattern.compile(".*[^A-F0-9].*"); - private final Slice value; + private final byte[] value; public BinaryLiteral(String value) { @@ -50,7 +48,7 @@ public BinaryLiteral(Optional location, String value) if (hexString.length() % 2 != 0) { throw new ParsingException("Binary literal must contain an even number of digits", location.get()); } - this.value = Slices.wrappedBuffer(BaseEncoding.base16().decode(hexString)); + this.value = BaseEncoding.base16().decode(hexString); } public BinaryLiteral(NodeLocation location, String value) @@ -63,12 +61,12 @@ public BinaryLiteral(NodeLocation location, String value) */ public String toHexString() { - return BaseEncoding.base16().encode(value.getBytes()); + return BaseEncoding.base16().encode(value); } - public Slice getValue() + public byte[] getValue() { - return value; + return value.clone(); } @Override @@ -88,13 +86,13 @@ public boolean equals(Object o) } BinaryLiteral that = (BinaryLiteral) o; - return Objects.equals(value, that.value); + return Arrays.equals(value, that.value); } @Override public int hashCode() { - return value.hashCode(); + return Arrays.hashCode(value); } @Override @@ -104,6 +102,6 @@ public boolean shallowEquals(Node other) return false; } - return Objects.equals(value, ((BinaryLiteral) other).value); + return Arrays.equals(value, ((BinaryLiteral) other).value); } } diff --git a/core/trino-parser/src/main/java/io/trino/sql/tree/CharLiteral.java b/core/trino-parser/src/main/java/io/trino/sql/tree/CharLiteral.java index cce465c98921..10da9a530f0a 100644 --- a/core/trino-parser/src/main/java/io/trino/sql/tree/CharLiteral.java +++ b/core/trino-parser/src/main/java/io/trino/sql/tree/CharLiteral.java @@ -14,19 +14,17 @@ package io.trino.sql.tree; import com.google.common.base.CharMatcher; -import io.airlift.slice.Slice; import java.util.Objects; import java.util.Optional; -import static io.airlift.slice.Slices.utf8Slice; import static java.util.Objects.requireNonNull; public class CharLiteral extends Literal { private final String value; - private final Slice slice; + private final int length; public CharLiteral(String value) { @@ -42,8 +40,8 @@ public CharLiteral(Optional location, String value) { super(location); requireNonNull(value, "value is null"); - this.value = value; - this.slice = utf8Slice(CharMatcher.is(' ').trimTrailingFrom(value)); + this.length = value.length(); + this.value = CharMatcher.is(' ').trimTrailingFrom(value); } public String getValue() @@ -51,9 +49,9 @@ public String getValue() return value; } - public Slice getSlice() + public int length() { - return slice; + return length; } @Override diff --git a/core/trino-parser/src/main/java/io/trino/sql/tree/StringLiteral.java b/core/trino-parser/src/main/java/io/trino/sql/tree/StringLiteral.java index 89405b3876ab..8a89fd552f83 100644 --- a/core/trino-parser/src/main/java/io/trino/sql/tree/StringLiteral.java +++ b/core/trino-parser/src/main/java/io/trino/sql/tree/StringLiteral.java @@ -13,19 +13,16 @@ */ package io.trino.sql.tree; -import io.airlift.slice.Slice; - import java.util.Objects; import java.util.Optional; -import static io.airlift.slice.Slices.utf8Slice; import static java.util.Objects.requireNonNull; public class StringLiteral extends Literal { private final String value; - private final Slice slice; + private final int length; public StringLiteral(String value) { @@ -42,7 +39,7 @@ private StringLiteral(Optional location, String value) super(location); requireNonNull(value, "value is null"); this.value = value; - this.slice = utf8Slice(value); + this.length = value.codePointCount(0, value.length()); } public String getValue() @@ -50,9 +47,9 @@ public String getValue() return value; } - public Slice getSlice() + public int length() { - return slice; + return length; } @Override diff --git a/core/trino-spi/src/main/java/io/trino/spi/expression/Constant.java b/core/trino-spi/src/main/java/io/trino/spi/expression/Constant.java index 0b9f08293158..0f7a1c8d5906 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/expression/Constant.java +++ b/core/trino-spi/src/main/java/io/trino/spi/expression/Constant.java @@ -13,6 +13,7 @@ */ package io.trino.spi.expression; +import io.airlift.slice.Slice; import io.trino.spi.type.BooleanType; import io.trino.spi.type.Type; @@ -73,6 +74,11 @@ public boolean equals(Object o) @Override public String toString() { + // String and Char literals can be wrapped as Slice multiple times thus + // generating different toString representations that can be checked for equality in tests' assertions. + if (value instanceof Slice) { + return "Slice[hash=" + value.hashCode() + ",length=" + ((Slice) value).length() + "]::" + getType(); + } return value + "::" + getType(); } } From 6dcf4405e03b8d5332b906aeba525f34ae98580c Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 7 Apr 2022 22:22:44 +0200 Subject: [PATCH 2/5] Replace Pattern usage with CharMatcher in BinaryLiteral --- .../main/java/io/trino/sql/tree/BinaryLiteral.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java b/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java index 3ae5e49d2e47..aee452a72347 100644 --- a/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java +++ b/core/trino-parser/src/main/java/io/trino/sql/tree/BinaryLiteral.java @@ -13,12 +13,12 @@ */ package io.trino.sql.tree; +import com.google.common.base.CharMatcher; import com.google.common.io.BaseEncoding; import io.trino.sql.parser.ParsingException; import java.util.Arrays; import java.util.Optional; -import java.util.regex.Pattern; import static java.util.Locale.ENGLISH; import static java.util.Objects.requireNonNull; @@ -27,8 +27,10 @@ public class BinaryLiteral extends Literal { // the grammar could possibly include whitespace in the value it passes to us - private static final Pattern WHITESPACE_PATTERN = Pattern.compile("[ \\r\\n\\t]"); - private static final Pattern NOT_HEX_DIGIT_PATTERN = Pattern.compile(".*[^A-F0-9].*"); + private static final CharMatcher WHITESPACE_MATCHER = CharMatcher.whitespace(); + private static final CharMatcher HEX_DIGIT_MATCHER = CharMatcher.inRange('A', 'F') + .or(CharMatcher.inRange('0', '9')) + .precomputed(); private final byte[] value; @@ -41,8 +43,8 @@ public BinaryLiteral(Optional location, String value) { super(location); requireNonNull(value, "value is null"); - String hexString = WHITESPACE_PATTERN.matcher(value).replaceAll("").toUpperCase(ENGLISH); - if (NOT_HEX_DIGIT_PATTERN.matcher(hexString).matches()) { + String hexString = WHITESPACE_MATCHER.removeFrom(value).toUpperCase(ENGLISH); + if (!HEX_DIGIT_MATCHER.matchesAllOf(hexString)) { throw new ParsingException("Binary literal can only contain hexadecimal digits", location.get()); } if (hexString.length() % 2 != 0) { From 3a001f838a8a67e8fc174fbc290380252cb60348 Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 7 Apr 2022 22:26:35 +0200 Subject: [PATCH 3/5] Remove unneccesary contains check on remove --- .../src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java b/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java index 1341e2be9597..156be67de73c 100644 --- a/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java +++ b/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java @@ -2602,7 +2602,7 @@ private void addOrReplaceExpressionCoercion(Expression expression, Type type, Ty if (typeCoercion.isTypeOnlyCoercion(type, superType)) { typeOnlyCoercions.add(ref); } - else if (typeOnlyCoercions.contains(ref)) { + else { typeOnlyCoercions.remove(ref); } } From f56f9d1119b91e8fccb68f00202a226e5968923d Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 7 Apr 2022 22:31:55 +0200 Subject: [PATCH 4/5] Simplify ExpressionAnalyzer code --- .../java/io/trino/sql/analyzer/ExpressionAnalyzer.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java b/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java index 156be67de73c..7991115a39fc 100644 --- a/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java +++ b/core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java @@ -477,11 +477,6 @@ public Multimap getTableColumnReferences() return tableColumnReferences; } - public Multimap, Field> getReferencedFields() - { - return referencedFields; - } - public List getSourceFields() { return sourceFields; @@ -2211,7 +2206,7 @@ protected Type visitInPredicate(InPredicate node, StackableAstVisitorContext { throw semanticException(NOT_SUPPORTED, function, "IN-PREDICATE with %s function is not yet supported", function.getName().getSuffix()); }); - extractExpressions(ImmutableList.of(value), DereferenceExpression.class).stream() + extractExpressions(ImmutableList.of(value), DereferenceExpression.class) .forEach(dereference -> { QualifiedName qualifiedName = DereferenceExpression.getQualifiedName(dereference); if (qualifiedName != null) { @@ -2867,8 +2862,7 @@ private static void updateAnalysis(Analysis analysis, ExpressionAnalyzer analyze analyzer.getSortKeyCoercionsForFrameBoundCalculation(), analyzer.getSortKeyCoercionsForFrameBoundComparison()); analysis.addFrameBoundCalculations(analyzer.getFrameBoundCalculations()); - analyzer.getResolvedFunctions().entrySet() - .forEach(entry -> analysis.addResolvedFunction(entry.getKey().getNode(), entry.getValue(), session.getUser())); + analyzer.getResolvedFunctions().forEach((key, value) -> analysis.addResolvedFunction(key.getNode(), value, session.getUser())); analysis.addColumnReferences(analyzer.getColumnReferences()); analysis.addLambdaArgumentReferences(analyzer.getLambdaArgumentReferences()); analysis.addTableColumnReferences(accessControl, session.getIdentity(), analyzer.getTableColumnReferences()); From 08fbc390e93b4160451974c19a6d5034fb1d2203 Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 7 Apr 2022 23:16:24 +0200 Subject: [PATCH 5/5] Remove unused TreePrinter class --- .../main/java/io/trino/sql/TreePrinter.java | 475 ------------------ 1 file changed, 475 deletions(-) delete mode 100644 core/trino-parser/src/main/java/io/trino/sql/TreePrinter.java diff --git a/core/trino-parser/src/main/java/io/trino/sql/TreePrinter.java b/core/trino-parser/src/main/java/io/trino/sql/TreePrinter.java deleted file mode 100644 index 12d0b96adb7b..000000000000 --- a/core/trino-parser/src/main/java/io/trino/sql/TreePrinter.java +++ /dev/null @@ -1,475 +0,0 @@ -/* - * 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.trino.sql; - -import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import io.trino.sql.tree.AliasedRelation; -import io.trino.sql.tree.AllColumns; -import io.trino.sql.tree.ArithmeticBinaryExpression; -import io.trino.sql.tree.AstVisitor; -import io.trino.sql.tree.BinaryLiteral; -import io.trino.sql.tree.BooleanLiteral; -import io.trino.sql.tree.ComparisonExpression; -import io.trino.sql.tree.Cube; -import io.trino.sql.tree.DefaultTraversalVisitor; -import io.trino.sql.tree.DereferenceExpression; -import io.trino.sql.tree.Expression; -import io.trino.sql.tree.FunctionCall; -import io.trino.sql.tree.GroupingElement; -import io.trino.sql.tree.GroupingSets; -import io.trino.sql.tree.Identifier; -import io.trino.sql.tree.InPredicate; -import io.trino.sql.tree.LikePredicate; -import io.trino.sql.tree.LogicalExpression; -import io.trino.sql.tree.LongLiteral; -import io.trino.sql.tree.Node; -import io.trino.sql.tree.OrderBy; -import io.trino.sql.tree.QualifiedName; -import io.trino.sql.tree.Query; -import io.trino.sql.tree.QuerySpecification; -import io.trino.sql.tree.Rollup; -import io.trino.sql.tree.Row; -import io.trino.sql.tree.SampledRelation; -import io.trino.sql.tree.Select; -import io.trino.sql.tree.SimpleGroupBy; -import io.trino.sql.tree.SingleColumn; -import io.trino.sql.tree.SortItem; -import io.trino.sql.tree.StringLiteral; -import io.trino.sql.tree.SubqueryExpression; -import io.trino.sql.tree.Table; -import io.trino.sql.tree.TableSubquery; -import io.trino.sql.tree.Values; -import io.trino.sql.tree.WindowDefinition; -import io.trino.sql.tree.WindowReference; -import io.trino.sql.tree.WindowSpecification; - -import java.io.PrintStream; -import java.util.IdentityHashMap; -import java.util.List; - -public class TreePrinter -{ - private static final String INDENT = " "; - - private final IdentityHashMap resolvedNameReferences; - private final PrintStream out; - - public TreePrinter(IdentityHashMap resolvedNameReferences, PrintStream out) - { - this.resolvedNameReferences = new IdentityHashMap<>(resolvedNameReferences); - this.out = out; - } - - public void print(Node root) - { - AstVisitor printer = new DefaultTraversalVisitor() - { - @Override - protected Void visitNode(Node node, Integer indentLevel) - { - throw new UnsupportedOperationException("not yet implemented: " + node); - } - - @Override - protected Void visitQuery(Query node, Integer indentLevel) - { - print(indentLevel, "Query "); - - indentLevel++; - - print(indentLevel, "QueryBody"); - process(node.getQueryBody(), indentLevel); - if (node.getOrderBy().isPresent()) { - print(indentLevel, "OrderBy"); - process(node.getOrderBy().get(), indentLevel + 1); - } - - if (node.getLimit().isPresent()) { - print(indentLevel, "Limit: " + node.getLimit().get()); - } - - return null; - } - - @Override - protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) - { - print(indentLevel, "QuerySpecification "); - - indentLevel++; - - process(node.getSelect(), indentLevel); - - if (node.getFrom().isPresent()) { - print(indentLevel, "From"); - process(node.getFrom().get(), indentLevel + 1); - } - - if (node.getWhere().isPresent()) { - print(indentLevel, "Where"); - process(node.getWhere().get(), indentLevel + 1); - } - - if (node.getGroupBy().isPresent()) { - String distinct = ""; - if (node.getGroupBy().get().isDistinct()) { - distinct = "[DISTINCT]"; - } - print(indentLevel, "GroupBy" + distinct); - for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) { - print(indentLevel, "SimpleGroupBy"); - if (groupingElement instanceof SimpleGroupBy) { - for (Expression column : groupingElement.getExpressions()) { - process(column, indentLevel + 1); - } - } - else if (groupingElement instanceof GroupingSets) { - print(indentLevel + 1, "GroupingSets"); - for (List set : ((GroupingSets) groupingElement).getSets()) { - print(indentLevel + 2, "GroupingSet["); - for (Expression expression : set) { - process(expression, indentLevel + 3); - } - print(indentLevel + 2, "]"); - } - } - else if (groupingElement instanceof Cube) { - print(indentLevel + 1, "Cube"); - for (Expression column : groupingElement.getExpressions()) { - process(column, indentLevel + 1); - } - } - else if (groupingElement instanceof Rollup) { - print(indentLevel + 1, "Rollup"); - for (Expression column : groupingElement.getExpressions()) { - process(column, indentLevel + 1); - } - } - } - } - - if (node.getHaving().isPresent()) { - print(indentLevel, "Having"); - process(node.getHaving().get(), indentLevel + 1); - } - - if (!node.getWindows().isEmpty()) { - print(indentLevel, "Window"); - for (WindowDefinition windowDefinition : node.getWindows()) { - process(windowDefinition, indentLevel + 1); - } - } - - if (node.getOrderBy().isPresent()) { - print(indentLevel, "OrderBy"); - process(node.getOrderBy().get(), indentLevel + 1); - } - - if (node.getLimit().isPresent()) { - print(indentLevel, "Limit: " + node.getLimit().get()); - } - - return null; - } - - @Override - protected Void visitOrderBy(OrderBy node, Integer indentLevel) - { - for (SortItem sortItem : node.getSortItems()) { - process(sortItem, indentLevel); - } - - return null; - } - - @Override - protected Void visitWindowDefinition(WindowDefinition node, Integer indentLevel) - { - print(indentLevel, "WindowDefinition[" + node.getName() + "]"); - process(node.getWindow(), indentLevel + 1); - - return null; - } - - @Override - protected Void visitWindowReference(WindowReference node, Integer indentLevel) - { - print(indentLevel, "WindowReference[" + node.getName() + "]"); - - return null; - } - - @Override - public Void visitWindowSpecification(WindowSpecification node, Integer indentLevel) - { - if (node.getExistingWindowName().isPresent()) { - print(indentLevel, "ExistingWindowName " + node.getExistingWindowName().get()); - } - - if (!node.getPartitionBy().isEmpty()) { - print(indentLevel, "PartitionBy"); - for (Expression expression : node.getPartitionBy()) { - process(expression, indentLevel + 1); - } - } - - if (node.getOrderBy().isPresent()) { - print(indentLevel, "OrderBy"); - process(node.getOrderBy().get(), indentLevel + 1); - } - - if (node.getFrame().isPresent()) { - print(indentLevel, "Frame"); - process(node.getFrame().get(), indentLevel + 1); - } - - return null; - } - - @Override - protected Void visitSelect(Select node, Integer indentLevel) - { - String distinct = ""; - if (node.isDistinct()) { - distinct = "[DISTINCT]"; - } - print(indentLevel, "Select" + distinct); - - super.visitSelect(node, indentLevel + 1); // visit children - - return null; - } - - @Override - protected Void visitAllColumns(AllColumns node, Integer indent) - { - StringBuilder aliases = new StringBuilder(); - if (!node.getAliases().isEmpty()) { - aliases.append(" [Aliases: "); - Joiner.on(", ").appendTo(aliases, node.getAliases()); - aliases.append("]"); - } - print(indent, "All columns" + aliases.toString()); - - if (node.getTarget().isPresent()) { - super.visitAllColumns(node, indent + 1); // visit child - } - - return null; - } - - @Override - protected Void visitSingleColumn(SingleColumn node, Integer indent) - { - if (node.getAlias().isPresent()) { - print(indent, "Alias: " + node.getAlias().get()); - } - - super.visitSingleColumn(node, indent + 1); // visit children - - return null; - } - - @Override - protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) - { - print(indentLevel, node.getOperator().toString()); - - super.visitComparisonExpression(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitArithmeticBinary(ArithmeticBinaryExpression node, Integer indentLevel) - { - print(indentLevel, node.getOperator().toString()); - - super.visitArithmeticBinary(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitLogicalExpression(LogicalExpression node, Integer indentLevel) - { - print(indentLevel, node.getOperator().toString()); - - super.visitLogicalExpression(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitStringLiteral(StringLiteral node, Integer indentLevel) - { - print(indentLevel, "String[" + node.getValue() + "]"); - return null; - } - - @Override - protected Void visitBinaryLiteral(BinaryLiteral node, Integer indentLevel) - { - print(indentLevel, "Binary[" + node.toHexString() + "]"); - return null; - } - - @Override - protected Void visitBooleanLiteral(BooleanLiteral node, Integer indentLevel) - { - print(indentLevel, "Boolean[" + node.getValue() + "]"); - return null; - } - - @Override - protected Void visitLongLiteral(LongLiteral node, Integer indentLevel) - { - print(indentLevel, "Long[" + node.getValue() + "]"); - return null; - } - - @Override - protected Void visitLikePredicate(LikePredicate node, Integer indentLevel) - { - print(indentLevel, "LIKE"); - - super.visitLikePredicate(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitIdentifier(Identifier node, Integer indentLevel) - { - QualifiedName resolved = resolvedNameReferences.get(node); - String resolvedName = ""; - if (resolved != null) { - resolvedName = "=>" + resolved.toString(); - } - print(indentLevel, "Identifier[" + node.getValue() + resolvedName + "]"); - return null; - } - - @Override - protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) - { - QualifiedName resolved = resolvedNameReferences.get(node); - String resolvedName = ""; - if (resolved != null) { - resolvedName = "=>" + resolved.toString(); - } - print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]"); - return null; - } - - @Override - protected Void visitFunctionCall(FunctionCall node, Integer indentLevel) - { - String name = Joiner.on('.').join(node.getName().getParts()); - print(indentLevel, "FunctionCall[" + name + "]"); - - super.visitFunctionCall(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitTable(Table node, Integer indentLevel) - { - String name = Joiner.on('.').join(node.getName().getParts()); - print(indentLevel, "Table[" + name + "]"); - - return null; - } - - @Override - protected Void visitValues(Values node, Integer indentLevel) - { - print(indentLevel, "Values"); - - super.visitValues(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitRow(Row node, Integer indentLevel) - { - print(indentLevel, "Row"); - - super.visitRow(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitAliasedRelation(AliasedRelation node, Integer indentLevel) - { - print(indentLevel, "Alias[" + node.getAlias() + "]"); - - super.visitAliasedRelation(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitSampledRelation(SampledRelation node, Integer indentLevel) - { - print(indentLevel, "TABLESAMPLE[" + node.getType() + " (" + node.getSamplePercentage() + ")]"); - - super.visitSampledRelation(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitTableSubquery(TableSubquery node, Integer indentLevel) - { - print(indentLevel, "SubQuery"); - - super.visitTableSubquery(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitInPredicate(InPredicate node, Integer indentLevel) - { - print(indentLevel, "IN"); - - super.visitInPredicate(node, indentLevel + 1); - - return null; - } - - @Override - protected Void visitSubqueryExpression(SubqueryExpression node, Integer indentLevel) - { - print(indentLevel, "SubQuery"); - - super.visitSubqueryExpression(node, indentLevel + 1); - - return null; - } - }; - - printer.process(root, 0); - } - - private void print(Integer indentLevel, String value) - { - out.println(Strings.repeat(INDENT, indentLevel) + value); - } -}