Skip to content

Commit

Permalink
fix-predicate-timestamp-and-decimal (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
feloxx authored Jan 19, 2024
1 parent cb83b1e commit 0127a21
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
package org.apache.paimon.presto;

import org.apache.paimon.data.BinaryString;
import org.apache.paimon.data.Decimal;
import org.apache.paimon.data.Timestamp;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.predicate.PredicateBuilder;
import org.apache.paimon.shade.guava30.com.google.common.base.Preconditions;
import org.apache.paimon.types.RowType;

import com.facebook.presto.common.predicate.Domain;
Expand All @@ -38,20 +39,23 @@
import com.facebook.presto.common.type.IntegerType;
import com.facebook.presto.common.type.MapType;
import com.facebook.presto.common.type.RealType;
import com.facebook.presto.common.type.SqlTimestampWithTimeZone;
import com.facebook.presto.common.type.TimeType;
import com.facebook.presto.common.type.TimestampType;
import com.facebook.presto.common.type.TimestampWithTimeZoneType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.VarbinaryType;
import com.facebook.presto.common.type.VarcharType;
import io.airlift.slice.Slice;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/** Presto filter to Paimon predicate. */
public class PrestoFilterConverter {
Expand Down Expand Up @@ -208,8 +212,23 @@ private Object getLiteralValue(Type type, Object prestoNativeValue) {
return Math.toIntExact(((Long) prestoNativeValue));
}

if (type instanceof TimestampType || type instanceof TimeType) {
return TimeUnit.MILLISECONDS.toMicros((Long) prestoNativeValue);
if (type instanceof TimeType) {
return (int) ((long) prestoNativeValue / 1_000);
}

if (type instanceof TimestampType) {
return Timestamp.fromLocalDateTime(
Instant.ofEpochMilli((Long) prestoNativeValue)
.atZone(ZoneId.systemDefault())
.toLocalDateTime());
}

if (type instanceof TimestampWithTimeZoneType) {
if (prestoNativeValue instanceof Long) {
return prestoNativeValue;
}
return Timestamp.fromEpochMillis(
((SqlTimestampWithTimeZone) prestoNativeValue).getMillisUtc());
}

if (type instanceof VarcharType || type instanceof CharType) {
Expand All @@ -221,23 +240,21 @@ private Object getLiteralValue(Type type, Object prestoNativeValue) {
}

if (type instanceof DecimalType) {
// Refer to trino.
DecimalType decimalType = (DecimalType) type;
Object value =
Objects.requireNonNull(
prestoNativeValue, "The prestoNativeValue must be non-null");
if (Decimals.isShortDecimal(decimalType)) {
Preconditions.checkArgument(
value instanceof Long,
"A short decimal should be represented by a Long value but was %s",
value.getClass().getName());
return BigDecimal.valueOf((long) value).movePointLeft(decimalType.getScale());
BigDecimal bigDecimal;
if (prestoNativeValue instanceof Long) {
bigDecimal =
BigDecimal.valueOf((long) prestoNativeValue)
.movePointLeft(decimalType.getScale());
} else {
bigDecimal =
new BigDecimal(
Decimals.decodeUnscaledValue((Slice) prestoNativeValue),
decimalType.getScale());
}
Preconditions.checkArgument(
value instanceof Slice,
"A long decimal should be represented by a Slice value but was %s",
value.getClass().getName());
return new BigDecimal(
Decimals.decodeUnscaledValue((Slice) value), decimalType.getScale());
return Decimal.fromBigDecimal(
bigDecimal, decimalType.getPrecision(), decimalType.getScale());
}

throw new UnsupportedOperationException("Unsupported type: " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -206,7 +207,13 @@ private void appendTo(Type prestoType, DataType paimonType, Object value, BlockB
prestoType.writeLong(
output, encodeShortScaledValue(decimal, decimalType.getScale()));
} else if (prestoType.equals(TIMESTAMP)) {
prestoType.writeLong(output, ((Timestamp) value).toSQLTimestamp().getTime());
prestoType.writeLong(
output,
((Timestamp) value)
.toLocalDateTime()
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli());
} else if (prestoType.equals(TIME)) {
prestoType.writeLong(output, (int) value * 1_000);
} else {
Expand Down
Loading

0 comments on commit 0127a21

Please sign in to comment.