-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for timestamp type to Pinot connector #12145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import io.airlift.slice.Slices; | ||
| import io.trino.plugin.pinot.client.PinotDataFetcher; | ||
| import io.trino.plugin.pinot.client.PinotDataTableWithSize; | ||
| import io.trino.plugin.pinot.conversion.PinotTimestamps; | ||
| import io.trino.spi.Page; | ||
| import io.trino.spi.PageBuilder; | ||
| import io.trino.spi.TrinoException; | ||
|
|
@@ -165,6 +166,7 @@ private void writeBlock(BlockBuilder blockBuilder, Type columnType, int columnId | |
| writeBooleanBlock(blockBuilder, columnType, columnIdx); | ||
| } | ||
| else if (javaType.equals(long.class)) { | ||
| // Applies to timestamp as well since precision is milliseconds | ||
| writeLongBlock(blockBuilder, columnType, columnIdx); | ||
| } | ||
| else if (javaType.equals(double.class)) { | ||
|
|
@@ -253,6 +255,8 @@ private long getLong(int rowIndex, int columnIndex) | |
| return floatToIntBits(currentDataTable.getDataTable().getFloat(rowIndex, columnIndex)); | ||
| case LONG: | ||
| return currentDataTable.getDataTable().getLong(rowIndex, columnIndex); | ||
| case TIMESTAMP: | ||
| return PinotTimestamps.toMicros(currentDataTable.getDataTable().getLong(rowIndex, columnIndex)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does Pinot only support timestamps with precision <= 6? If not we should probably add a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pinot timestamps are milliseconds, see https://github.com/apache/pinot/blob/master/pinot-spi/src/main/java/org/apache/pinot/spi/data/FieldSpec.java#L468 |
||
| default: | ||
| throw new PinotException(PINOT_DECODE_ERROR, Optional.empty(), format("Unexpected pinot type: '%s'", dataType)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * 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.plugin.pinot.conversion; | ||
|
|
||
| import com.google.common.primitives.Longs; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.format.DateTimeParseException; | ||
|
|
||
| import static io.trino.spi.type.Timestamps.MICROSECONDS_PER_MILLISECOND; | ||
| import static java.time.ZoneOffset.UTC; | ||
| import static java.time.format.DateTimeFormatter.ISO_INSTANT; | ||
|
|
||
| public final class PinotTimestamps | ||
| { | ||
| private static final DateTimeFormatter PINOT_TIMESTAMP_FORMATTER = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be: Can you also add a test for Pinot timestamp Strings e.g.:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be: |
||
| DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS][.SS][.S]"); | ||
|
|
||
| private PinotTimestamps() {} | ||
|
|
||
| public static long toMicros(long millis) | ||
| { | ||
| return millis * MICROSECONDS_PER_MILLISECOND; | ||
| } | ||
|
|
||
| public static long toMicros(Instant instant) | ||
| { | ||
| return toMicros(instant.toEpochMilli()); | ||
| } | ||
|
|
||
| public static LocalDateTime tryParse(String value) | ||
| { | ||
| Long epochMillis = Longs.tryParse(value); | ||
| if (epochMillis != null) { | ||
| return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), UTC); | ||
| } | ||
| // Try parsing using standard formats | ||
| LocalDateTime timestamp = tryParse(PINOT_TIMESTAMP_FORMATTER, value); | ||
| if (timestamp == null) { | ||
| timestamp = tryParse(ISO_INSTANT, value); | ||
| } | ||
| return timestamp; | ||
|
hashhar marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private static LocalDateTime tryParse(DateTimeFormatter formatter, String value) | ||
| { | ||
| try { | ||
| return formatter.parse(value, LocalDateTime::from); | ||
| } | ||
| catch (DateTimeParseException e) { | ||
| // Ignore | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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.plugin.pinot.decoders; | ||
|
|
||
| import io.trino.plugin.pinot.conversion.PinotTimestamps; | ||
| import io.trino.spi.TrinoException; | ||
| import io.trino.spi.block.BlockBuilder; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; | ||
| import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS; | ||
| import static java.lang.String.format; | ||
| import static java.time.ZoneOffset.UTC; | ||
|
|
||
| public class TimestampDecoder | ||
| implements Decoder | ||
| { | ||
| @Override | ||
| public void decode(Supplier<Object> getter, BlockBuilder output) | ||
| { | ||
| Object value = getter.get(); | ||
| if (value == null) { | ||
| output.appendNull(); | ||
| } | ||
| else { | ||
| LocalDateTime timestamp; | ||
| if (value instanceof String) { | ||
| String valueString = (String) value; | ||
| timestamp = PinotTimestamps.tryParse(valueString); | ||
| if (timestamp == null) { | ||
| throw new TrinoException(NOT_SUPPORTED, format( | ||
| "Unable to parse string representation of type TIMESTAMP: %s [%s]", | ||
| value, | ||
| value.getClass().getSimpleName())); | ||
| } | ||
| } | ||
| else if (value instanceof Double || value instanceof Long) { | ||
| timestamp = LocalDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), UTC); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid casts from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed this to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hashhar seems like the test fails because the aggregate function returns double instead of long, I need to dig further if we don't want to cast from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hashhar @xiangfu0 Pinot's MAX function returns double https://docs.pinot.apache.org/users/user-guide-query/supported-aggregations I think the options are either keeping
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think number is ok.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've now made this change |
||
| } | ||
| else { | ||
| throw new TrinoException(NOT_SUPPORTED, format( | ||
| "Unsupported representation of type TIMESTAMP: %s [%s]", | ||
| value, | ||
| value.getClass().getSimpleName())); | ||
| } | ||
| long epochMicros = PinotTimestamps.toMicros(timestamp.atOffset(UTC).toInstant()); | ||
| TIMESTAMP_MILLIS.writeLong(output, epochMicros); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why write microseconds to this TIMESTAMP_MILLIS?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nizarhejazi @ddcprg can you check this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xiangfu0 I'm absent at the moment. I'll retake this in 2 weeks time
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reminder
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a temporary conversion to micros to be able to invoke |
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.