From 4cbe9db7b768c810bb5de930f96b3ab378ab068c Mon Sep 17 00:00:00 2001 From: Dongjoon Hyun Date: Tue, 5 Dec 2023 09:29:39 -0800 Subject: [PATCH] ORC-1542: Use Java 16 `Pattern Matching for instanceof` (JEP-394) ### What changes were proposed in this pull request? This PR aims to use Java 16 `Pattern Matching for instanceof` (JEP-394) syntax. ### Why are the changes needed? Since Apache ORC 2.0 supports Java 17+, we can take advantage of this new syntax. ### How was this patch tested? Pass the CIs. Closes #1681 from dongjoon-hyun/ORC-1542. Authored-by: Dongjoon Hyun Signed-off-by: Dongjoon Hyun --- .../java/org/apache/orc/TypeDescription.java | 3 +- .../apache/orc/impl/ColumnStatisticsImpl.java | 71 ++++++------------- .../java/org/apache/orc/impl/ParserUtils.java | 3 +- .../org/apache/orc/impl/RecordReaderImpl.java | 6 +- .../apache/orc/impl/RecordReaderUtils.java | 3 +- .../java/org/apache/orc/impl/StreamName.java | 3 +- .../org/threeten/extra/chrono/HybridDate.java | 3 +- .../apache/orc/impl/HadoopShimsCurrent.java | 3 +- .../java/org/apache/orc/tools/FileDump.java | 3 +- .../apache/orc/tools/convert/CsvReader.java | 6 +- .../apache/orc/tools/convert/JsonReader.java | 6 +- .../org/apache/orc/tools/json/ListType.java | 3 +- 12 files changed, 34 insertions(+), 79 deletions(-) diff --git a/java/core/src/java/org/apache/orc/TypeDescription.java b/java/core/src/java/org/apache/orc/TypeDescription.java index 8c02e68071..8ea9fca1b2 100644 --- a/java/core/src/java/org/apache/orc/TypeDescription.java +++ b/java/core/src/java/org/apache/orc/TypeDescription.java @@ -411,13 +411,12 @@ public boolean equals(Object other) { * argument; {@code false} otherwise. */ public boolean equals(Object other, boolean checkAttributes) { - if (other == null || !(other instanceof TypeDescription)) { + if (other == null || !(other instanceof TypeDescription castOther)) { return false; } if (other == this) { return true; } - TypeDescription castOther = (TypeDescription) other; if (category != castOther.category || maxLength != castOther.maxLength || scale != castOther.scale || diff --git a/java/core/src/java/org/apache/orc/impl/ColumnStatisticsImpl.java b/java/core/src/java/org/apache/orc/impl/ColumnStatisticsImpl.java index 680e9f14ea..c5e13cc3c0 100644 --- a/java/core/src/java/org/apache/orc/impl/ColumnStatisticsImpl.java +++ b/java/core/src/java/org/apache/orc/impl/ColumnStatisticsImpl.java @@ -52,12 +52,10 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof ColumnStatisticsImpl)) { + if (!(o instanceof ColumnStatisticsImpl that)) { return false; } - ColumnStatisticsImpl that = (ColumnStatisticsImpl) o; - if (count != that.count) { return false; } @@ -102,8 +100,7 @@ public void updateBoolean(boolean value, int repetitions) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof BooleanStatisticsImpl) { - BooleanStatisticsImpl bkt = (BooleanStatisticsImpl) other; + if (other instanceof BooleanStatisticsImpl bkt) { trueCount += bkt.trueCount; } else { if (isStatsExists() && trueCount != 0) { @@ -143,15 +140,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof BooleanStatisticsImpl)) { + if (!(o instanceof BooleanStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - BooleanStatisticsImpl that = (BooleanStatisticsImpl) o; - return trueCount == that.trueCount; } @@ -213,8 +208,7 @@ public void reset() { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof CollectionColumnStatisticsImpl) { - CollectionColumnStatisticsImpl otherColl = (CollectionColumnStatisticsImpl) other; + if (other instanceof CollectionColumnStatisticsImpl otherColl) { if(count == 0) { minimum = otherColl.minimum; @@ -272,15 +266,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof CollectionColumnStatisticsImpl)) { + if (!(o instanceof CollectionColumnStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - CollectionColumnStatisticsImpl that = (CollectionColumnStatisticsImpl) o; - if (minimum != that.minimum) { return false; } @@ -383,8 +375,7 @@ public void updateInteger(long value, int repetitions) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof IntegerStatisticsImpl) { - IntegerStatisticsImpl otherInt = (IntegerStatisticsImpl) other; + if (other instanceof IntegerStatisticsImpl otherInt) { if (!hasMinimum) { hasMinimum = otherInt.hasMinimum; minimum = otherInt.minimum; @@ -471,15 +462,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof IntegerStatisticsImpl)) { + if (!(o instanceof IntegerStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - IntegerStatisticsImpl that = (IntegerStatisticsImpl) o; - if (minimum != that.minimum) { return false; } @@ -557,8 +546,7 @@ public void updateDouble(double value) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof DoubleStatisticsImpl) { - DoubleStatisticsImpl dbl = (DoubleStatisticsImpl) other; + if (other instanceof DoubleStatisticsImpl dbl) { if (!hasMinimum) { hasMinimum = dbl.hasMinimum; minimum = dbl.minimum; @@ -628,15 +616,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof DoubleStatisticsImpl)) { + if (!(o instanceof DoubleStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - DoubleStatisticsImpl that = (DoubleStatisticsImpl) o; - if (hasMinimum != that.hasMinimum) { return false; } @@ -753,8 +739,7 @@ public void updateString(byte[] bytes, int offset, int length, @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof StringStatisticsImpl) { - StringStatisticsImpl str = (StringStatisticsImpl) other; + if (other instanceof StringStatisticsImpl str) { if (count == 0) { if (str.count != 0) { minimum = new Text(str.minimum); @@ -884,15 +869,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof StringStatisticsImpl)) { + if (!(o instanceof StringStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - StringStatisticsImpl that = (StringStatisticsImpl) o; - if (sum != that.sum) { return false; } @@ -1050,15 +1033,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof BinaryStatisticsImpl)) { + if (!(o instanceof BinaryStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - BinaryStatisticsImpl that = (BinaryStatisticsImpl) o; - return sum == that.sum; } @@ -1129,8 +1110,7 @@ public void updateDecimal64(long value, int scale) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof DecimalStatisticsImpl) { - DecimalStatisticsImpl dec = (DecimalStatisticsImpl) other; + if (other instanceof DecimalStatisticsImpl dec) { if (minimum == null) { minimum = (dec.minimum != null ? new HiveDecimalWritable(dec.minimum) : null); maximum = (dec.maximum != null ? new HiveDecimalWritable(dec.maximum) : null); @@ -1209,15 +1189,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof DecimalStatisticsImpl)) { + if (!(o instanceof DecimalStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - DecimalStatisticsImpl that = (DecimalStatisticsImpl) o; - if (minimum != null ? !minimum.equals(that.minimum) : that.minimum != null) { return false; } @@ -1323,8 +1301,7 @@ public void updateDecimal64(long value, int valueScale) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof Decimal64StatisticsImpl) { - Decimal64StatisticsImpl dec = (Decimal64StatisticsImpl) other; + if (other instanceof Decimal64StatisticsImpl dec) { if (getNumberOfValues() == 0) { minimum = dec.minimum; maximum = dec.maximum; @@ -1420,15 +1397,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof Decimal64StatisticsImpl)) { + if (!(o instanceof Decimal64StatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - Decimal64StatisticsImpl that = (Decimal64StatisticsImpl) o; - if (minimum != that.minimum || maximum != that.maximum || hasSum != that.hasSum) { @@ -1508,8 +1483,7 @@ public void updateDate(int value) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof DateStatisticsImpl) { - DateStatisticsImpl dateStats = (DateStatisticsImpl) other; + if (other instanceof DateStatisticsImpl dateStats) { minimum = Math.min(minimum, dateStats.minimum); maximum = Math.max(maximum, dateStats.maximum); } else { @@ -1588,15 +1562,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof DateStatisticsImpl)) { + if (!(o instanceof DateStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - DateStatisticsImpl that = (DateStatisticsImpl) o; - if (minimum != that.minimum) { return false; } @@ -1702,8 +1674,7 @@ public void updateTimestamp(long value, int nanos) { @Override public void merge(ColumnStatisticsImpl other) { - if (other instanceof TimestampStatisticsImpl) { - TimestampStatisticsImpl timestampStats = (TimestampStatisticsImpl) other; + if (other instanceof TimestampStatisticsImpl timestampStats) { if (count == 0) { if (timestampStats.count != 0) { minimum = timestampStats.minimum; @@ -1817,15 +1788,13 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof TimestampStatisticsImpl)) { + if (!(o instanceof TimestampStatisticsImpl that)) { return false; } if (!super.equals(o)) { return false; } - TimestampStatisticsImpl that = (TimestampStatisticsImpl) o; - return minimum == that.minimum && maximum == that.maximum && minNanos == that.minNanos && maxNanos == that.maxNanos; } diff --git a/java/core/src/java/org/apache/orc/impl/ParserUtils.java b/java/core/src/java/org/apache/orc/impl/ParserUtils.java index f493f078ac..df2f8b5e19 100644 --- a/java/core/src/java/org/apache/orc/impl/ParserUtils.java +++ b/java/core/src/java/org/apache/orc/impl/ParserUtils.java @@ -450,8 +450,7 @@ private ColumnVector navigate(ColumnVector parent, int posn) { return ((StructColumnVector) parent).fields[posn]; } else if (parent instanceof UnionColumnVector) { return ((UnionColumnVector) parent).fields[posn]; - } else if (parent instanceof MapColumnVector) { - MapColumnVector m = (MapColumnVector) parent; + } else if (parent instanceof MapColumnVector m) { return posn == 0 ? m.keys : m.values; } throw new IllegalArgumentException("Unknown complex column vector " + parent.getClass()); diff --git a/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java b/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java index 6d65e9e5c0..20abe5f18c 100644 --- a/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java +++ b/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java @@ -696,8 +696,7 @@ static TruthValue evaluatePredicateProto(OrcProto.ColumnStatistics statsProto, predicate.getColumnName(), writerVersion); return TruthValue.YES_NO_NULL; } else if ((category == TypeDescription.Category.DOUBLE || - category == TypeDescription.Category.FLOAT) && cs instanceof DoubleColumnStatistics) { - DoubleColumnStatistics dstas = (DoubleColumnStatistics) cs; + category == TypeDescription.Category.FLOAT) && cs instanceof DoubleColumnStatistics dstas) { if (Double.isNaN(dstas.getSum())) { LOG.debug("Not using predication pushdown on {} because stats contain NaN values", predicate.getColumnName()); @@ -1048,8 +1047,7 @@ private static Comparable getBaseObjectForComparison(PredicateLeaf.Type type, } break; case STRING: - if (obj instanceof ChronoLocalDate) { - ChronoLocalDate date = (ChronoLocalDate) obj; + if (obj instanceof ChronoLocalDate date) { return date.format(DateTimeFormatter.ISO_LOCAL_DATE .withChronology(date.getChronology())); } diff --git a/java/core/src/java/org/apache/orc/impl/RecordReaderUtils.java b/java/core/src/java/org/apache/orc/impl/RecordReaderUtils.java index d249a2cc21..27ba78d4be 100644 --- a/java/core/src/java/org/apache/orc/impl/RecordReaderUtils.java +++ b/java/core/src/java/org/apache/orc/impl/RecordReaderUtils.java @@ -583,8 +583,7 @@ public int compareTo(Key other) { @Override public boolean equals(Object rhs) { - if (rhs instanceof Key) { - Key o = (Key) rhs; + if (rhs instanceof Key o) { return 0 == compareTo(o); } return false; diff --git a/java/core/src/java/org/apache/orc/impl/StreamName.java b/java/core/src/java/org/apache/orc/impl/StreamName.java index fe80cd4a78..be9d622a41 100644 --- a/java/core/src/java/org/apache/orc/impl/StreamName.java +++ b/java/core/src/java/org/apache/orc/impl/StreamName.java @@ -49,8 +49,7 @@ public StreamName(int column, OrcProto.Stream.Kind kind, @Override public boolean equals(Object obj) { - if (obj instanceof StreamName) { - StreamName other = (StreamName) obj; + if (obj instanceof StreamName other) { return other.column == column && other.kind == kind && encryption == other.encryption; } else { diff --git a/java/core/src/java/org/threeten/extra/chrono/HybridDate.java b/java/core/src/java/org/threeten/extra/chrono/HybridDate.java index 9e94f5178a..29d8586789 100644 --- a/java/core/src/java/org/threeten/extra/chrono/HybridDate.java +++ b/java/core/src/java/org/threeten/extra/chrono/HybridDate.java @@ -523,8 +523,7 @@ public boolean equals(Object obj) { if (this == obj) { return true; } - if (obj instanceof HybridDate) { - HybridDate otherDate = (HybridDate) obj; + if (obj instanceof HybridDate otherDate) { return this.isoDate.equals(otherDate.isoDate); } return false; diff --git a/java/shims/src/java/org/apache/orc/impl/HadoopShimsCurrent.java b/java/shims/src/java/org/apache/orc/impl/HadoopShimsCurrent.java index b05fae739c..335ff411b9 100644 --- a/java/shims/src/java/org/apache/orc/impl/HadoopShimsCurrent.java +++ b/java/shims/src/java/org/apache/orc/impl/HadoopShimsCurrent.java @@ -118,8 +118,7 @@ public ZeroCopyReaderShim getZeroCopyReader(FSDataInputStream in, @Override public boolean endVariableLengthBlock(OutputStream output) throws IOException { - if (output instanceof HdfsDataOutputStream) { - HdfsDataOutputStream hdfs = (HdfsDataOutputStream) output; + if (output instanceof HdfsDataOutputStream hdfs) { hdfs.hsync(EnumSet.of(HdfsDataOutputStream.SyncFlag.END_BLOCK)); return true; } diff --git a/java/tools/src/java/org/apache/orc/tools/FileDump.java b/java/tools/src/java/org/apache/orc/tools/FileDump.java index 3c5bfd77a4..696dc3c7db 100644 --- a/java/tools/src/java/org/apache/orc/tools/FileDump.java +++ b/java/tools/src/java/org/apache/orc/tools/FileDump.java @@ -179,8 +179,7 @@ static Reader getReader(final Path path, final Configuration conf, final boolean sideFileExists = fs.exists(sideFile); boolean openDataFile = false; boolean openSideFile = false; - if (fs instanceof DistributedFileSystem) { - DistributedFileSystem dfs = (DistributedFileSystem) fs; + if (fs instanceof DistributedFileSystem dfs) { openDataFile = !dfs.isFileClosed(path); openSideFile = sideFileExists && !dfs.isFileClosed(sideFile); } diff --git a/java/tools/src/java/org/apache/orc/tools/convert/CsvReader.java b/java/tools/src/java/org/apache/orc/tools/convert/CsvReader.java index 0da2f11c44..236adf38cb 100644 --- a/java/tools/src/java/org/apache/orc/tools/convert/CsvReader.java +++ b/java/tools/src/java/org/apache/orc/tools/convert/CsvReader.java @@ -293,12 +293,10 @@ public void convert(String[] values, ColumnVector column, int row) { TemporalAccessor temporalAccessor = dateTimeFormatter.parseBest(values[offset], ZonedDateTime::from, OffsetDateTime::from, LocalDateTime::from); - if (temporalAccessor instanceof ZonedDateTime) { - ZonedDateTime zonedDateTime = ((ZonedDateTime) temporalAccessor); + if (temporalAccessor instanceof ZonedDateTime zonedDateTime) { Timestamp timestamp = Timestamp.from(zonedDateTime.toInstant()); vector.set(row, timestamp); - } else if (temporalAccessor instanceof OffsetDateTime) { - OffsetDateTime offsetDateTime = (OffsetDateTime) temporalAccessor; + } else if (temporalAccessor instanceof OffsetDateTime offsetDateTime) { Timestamp timestamp = Timestamp.from(offsetDateTime.toInstant()); vector.set(row, timestamp); } else if (temporalAccessor instanceof LocalDateTime) { diff --git a/java/tools/src/java/org/apache/orc/tools/convert/JsonReader.java b/java/tools/src/java/org/apache/orc/tools/convert/JsonReader.java index 8faf519016..37e834e4ea 100644 --- a/java/tools/src/java/org/apache/orc/tools/convert/JsonReader.java +++ b/java/tools/src/java/org/apache/orc/tools/convert/JsonReader.java @@ -171,12 +171,10 @@ public void convert(JsonElement value, ColumnVector vect, int row) { TimestampColumnVector vector = (TimestampColumnVector) vect; TemporalAccessor temporalAccessor = dateTimeFormatter.parseBest(value.getAsString(), ZonedDateTime::from, OffsetDateTime::from, LocalDateTime::from); - if (temporalAccessor instanceof ZonedDateTime) { - ZonedDateTime zonedDateTime = ((ZonedDateTime) temporalAccessor); + if (temporalAccessor instanceof ZonedDateTime zonedDateTime) { Timestamp timestamp = Timestamp.from(zonedDateTime.toInstant()); vector.set(row, timestamp); - } else if (temporalAccessor instanceof OffsetDateTime) { - OffsetDateTime offsetDateTime = (OffsetDateTime) temporalAccessor; + } else if (temporalAccessor instanceof OffsetDateTime offsetDateTime) { Timestamp timestamp = Timestamp.from(offsetDateTime.toInstant()); vector.set(row, timestamp); } else if (temporalAccessor instanceof LocalDateTime) { diff --git a/java/tools/src/java/org/apache/orc/tools/json/ListType.java b/java/tools/src/java/org/apache/orc/tools/json/ListType.java index 64d4854d38..7ccf43c3fd 100644 --- a/java/tools/src/java/org/apache/orc/tools/json/ListType.java +++ b/java/tools/src/java/org/apache/orc/tools/json/ListType.java @@ -63,8 +63,7 @@ public boolean subsumes(HiveType other) { @Override public void merge(HiveType other) { - if (other instanceof ListType) { - ListType otherList = (ListType) other; + if (other instanceof ListType otherList) { if (elementType.subsumes(otherList.elementType)) { elementType.merge(otherList.elementType); } else if (otherList.elementType.subsumes(elementType)) {