Skip to content

Commit

Permalink
ORC-1542: Use Java 16 Pattern Matching for instanceof (JEP-394)
Browse files Browse the repository at this point in the history
### 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 <dongjoon@apache.org>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
  • Loading branch information
dongjoon-hyun committed Dec 5, 2023
1 parent a0537bc commit 4cbe9db
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 79 deletions.
3 changes: 1 addition & 2 deletions java/core/src/java/org/apache/orc/TypeDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
71 changes: 20 additions & 51 deletions java/core/src/java/org/apache/orc/impl/ColumnStatisticsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions java/core/src/java/org/apache/orc/impl/ParserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 2 additions & 4 deletions java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions java/core/src/java/org/apache/orc/impl/StreamName.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions java/core/src/java/org/threeten/extra/chrono/HybridDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions java/tools/src/java/org/apache/orc/tools/FileDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 4cbe9db

Please sign in to comment.