Skip to content
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

Add support for precision for TIMESTAMP in Postgresql type mapping #5124

Merged
merged 8 commits into from
Sep 10, 2020

Conversation

losipiuk
Copy link
Member

@losipiuk losipiuk commented Sep 9, 2020

Replaces #4570

@cla-bot cla-bot bot added the cla-signed label Sep 9, 2020
@losipiuk losipiuk requested review from findepi and kokosing September 9, 2020 20:44
@mosabua mosabua added the needs-docs This pull request requires changes to the documentation label Sep 9, 2020
@mosabua
Copy link
Member

mosabua commented Sep 9, 2020

Please add type mapping docs for postgresql as part of this PR so it is clear to users.

return createPostgreSqlQueryRunner(server, extraProperties, connectorProperties, tables, 3);
}

public static DistributedQueryRunner createPostgreSqlQueryRunner(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather drop this commit.
The added burden of an overload is not justified for arguably small benefit this brings.

@@ -1199,15 +1200,6 @@ public void testArrayTimestampWithTimeZone(boolean insertWithPresto)
tests.execute(getQueryRunner(), sessionWithArrayAsArray(), dataSetup);
}

@DataProvider
public Object[][] testTimestampWithTimeZoneDataProvider()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cmt title is

Rename misleading testTimestampWithTimeZoneDataProvider

it's removal not "rename"

"misleading" is misleading. It was very nicely called "DataProvider", i do not find anything misleading in that and next time would call it the same.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to "Use generic trueFalse data provider for timestamp tests"

long precision = timestampType.getPrecision();
checkArgument(precision <= MAX_SHORT_PRECISION, "Precision is out of range: %s", precision);
Instant instant = localDateTime.atZone(UTC).toInstant();
return instant.getEpochSecond() * MICROSECONDS_PER_SECOND + roundDiv(instant.getNano(), NANOSECONDS_PER_MICROSECOND);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if timestampType.precision < 6, you need to round/truncate here

@martint what is the correct way to do this?
there are some utility methods in io.prestosql.spi.type.Timestamps, but I do not see something that's immediately applicable.

{
return Instant.ofEpochMilli(floorDiv(value, MICROSECONDS_PER_MILLISECOND)).atZone(UTC).toLocalDateTime();
int precision = timestampType.getPrecision();
checkArgument(precision <= MAX_SHORT_PRECISION && precision >= 0, "Precision is out of range: %s", precision);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not really need to validate this here. If someone provided epochMicros, it;'s all you need.

long epochSecond = floorDiv(epochMicros, MICROSECONDS_PER_SECOND);
int nanoFraction = floorMod(epochMicros, MICROSECONDS_PER_SECOND) * NANOSECONDS_PER_MICROSECOND;
Instant instant = Instant.ofEpochSecond(epochSecond, nanoFraction);
return LocalDateTime.ofInstant(instant, UTC);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could unify & reuse with io.prestosql.spi.type.Timestamps#formatTimestamp(int precision, long epochMicros, int picosOfMicro)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but I am not sure if we could extend spi with random methods. We should keep it fairly stable. Also the spi methods cares about picosOfMicro which we do not care about here. With picosOfMicro you cannot create a method which just returns LocalDateTime, which we need here. And have a specific method version which does not take picosOfSeconds and returns LocalDateTime seems to specific to me, to be put in spi.

@@ -262,7 +262,7 @@ public WriteMapping toWriteMapping(ConnectorSession session, Type type)
}
if (TIMESTAMP_MILLIS.equals(type)) {
// TODO use `timestampWriteFunction`
return WriteMapping.longMapping("datetime", timestampWriteFunctionUsingSqlTimestamp());
return WriteMapping.longMapping("datetime", timestampWriteFunctionUsingSqlTimestamp(TIMESTAMP));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return WriteMapping.longMapping("datetime", timestampWriteFunctionUsingSqlTimestamp(TIMESTAMP));
return WriteMapping.longMapping("datetime", timestampWriteFunctionUsingSqlTimestamp(TIMESTAMP_MILLIS));

.addRoundTrip(timestampDataType(), timeGapInKathmandu);
DataTypeTest tests = DataTypeTest.create(true);
for (int precision : List.of(3, 6)) {
// test all standard cases with precision 3 and 6 to make sure the long and short TIMESTAMP WITH TIME ZONE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"TIMESTAMP WITH TIME ZONE"

also, is 3 and 6 actually different cases?
i think in timestamp case, we go with the same code path

(ok to leave the test coverage, but if true, change the commnt)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh - you noticed it too - yeah - it same case. I dropped the coverage.

}
}

private DataType<List<LocalDateTime>> arrayOfTimestampDataType(int precision, boolean insertWithPresto)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this belong to Simplify flow in timestamp with timezone mapping tests commit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. This is specific to TIMESTAMP not TIMESTAMP w/TZ. So it belongs to last commit.

@losipiuk
Copy link
Member Author

Please add type mapping docs for postgresql as part of this PR so it is clear to users.

This is a point change regarding one specific type and adding general type mapping documentation is substantial effort itself.
Added #5131 to track that.

kokosing and others added 4 commits September 10, 2020 11:16
Add support for mapping TIMESTAMP from Presto to
PostgreSQL (and vice versa) for precisions 1 to 6. It covers all
precisions currently supported by PostgreSQL.
}
}

private DataType<List<LocalDateTime>> arrayOfTimestampDataType(int precision, boolean insertWithPresto)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. This is specific to TIMESTAMP not TIMESTAMP w/TZ. So it belongs to last commit.

dataType = arrayDataType(timestampDataType(), "timestamp[]");
dataSetup = postgresCreateAndInsert("tpch.test_array_timestamp");
DataTypeTest tests = DataTypeTest.create(true);
for (int precision : List.of(3, 6)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will drop this for. As representation is same for 3 and 6 for TIMESTAMP

.addRoundTrip(timestampDataType(), timeGapInKathmandu);
DataTypeTest tests = DataTypeTest.create(true);
for (int precision : List.of(3, 6)) {
// test all standard cases with precision 3 and 6 to make sure the long and short TIMESTAMP WITH TIME ZONE
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh - you noticed it too - yeah - it same case. I dropped the coverage.

@losipiuk losipiuk force-pushed the lo/psql-timestamp-wo-tz-p branch from 305f378 to e7fdfe3 Compare September 10, 2020 10:04
@losipiuk losipiuk merged commit be2b97a into trinodb:master Sep 10, 2020
@losipiuk losipiuk mentioned this pull request Sep 11, 2020
9 tasks
@martint martint added this to the 342 milestone Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed needs-docs This pull request requires changes to the documentation
Development

Successfully merging this pull request may close these issues.

5 participants