generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of dateTimeSerialization (#2363)
- Loading branch information
Showing
7 changed files
with
127 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
.../finos/legend/engine/plan/execution/stores/relational/serialization/ValueTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2023 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.plan.execution.stores.relational.serialization; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.function.Function; | ||
|
||
class ValueTransformer | ||
{ | ||
private final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
private final DateTimeFormatter timeFormatterWithZ = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnnZ"); | ||
private final ZoneOffset offset = ZoneOffset.UTC; | ||
|
||
protected ValueTransformer() | ||
{ | ||
|
||
} | ||
|
||
protected Object transformWrappedRelationalValue(Object relationalValue, Function<Object, ?> transformer) | ||
{ | ||
if (relationalValue instanceof Timestamp) | ||
{ | ||
return "\"" + timeFormatterWithZ.format(((Timestamp) relationalValue).toInstant().atOffset(offset)) + "\""; | ||
|
||
} | ||
else if (relationalValue instanceof java.sql.Date) | ||
{ | ||
return "\"" + ((java.sql.Date) relationalValue).toLocalDate().format(dateFormat) + "\""; | ||
} | ||
else | ||
{ | ||
return transformer.apply(relationalValue); | ||
} | ||
|
||
} | ||
|
||
protected Object transformRelationalValue(Object relationalValue, Function<Object, ?> transformer) | ||
{ | ||
if (relationalValue instanceof Timestamp) | ||
{ | ||
return timeFormatterWithZ.format(((Timestamp) relationalValue).toInstant().atOffset(offset)); | ||
|
||
} | ||
else if (relationalValue instanceof java.sql.Date) | ||
{ | ||
return ((java.sql.Date) relationalValue).toLocalDate().format(dateFormat); | ||
} | ||
else | ||
{ | ||
return transformer.apply(relationalValue); | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...os/legend/engine/plan/execution/stores/relational/serialization/TestValueTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.plan.execution.stores.relational.serialization; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.sql.Date; | ||
import java.sql.Timestamp; | ||
|
||
public class TestValueTransformer | ||
{ | ||
@Test | ||
public void testValueTransformer() | ||
{ | ||
ValueTransformer valueTransformer = new ValueTransformer(); | ||
Timestamp time = new Timestamp(1696532242123L); | ||
Date date = new Date(1696532242123L); | ||
|
||
|
||
org.eclipse.collections.api.block.function.Function<Object, Object> transform = value -> value + "Transformed"; | ||
Assert.assertEquals("2023-10-05T18:57:22.123000000+0000", valueTransformer.transformRelationalValue(time, transform)); | ||
Assert.assertEquals("2023-10-05", valueTransformer.transformRelationalValue(date, transform)); | ||
Assert.assertEquals("otherTransformed", valueTransformer.transformRelationalValue("other", transform)); | ||
|
||
Assert.assertEquals("\"2023-10-05T18:57:22.123000000+0000\"", valueTransformer.transformWrappedRelationalValue(time, transform)); | ||
Assert.assertEquals("\"2023-10-05\"", valueTransformer.transformWrappedRelationalValue(date, transform)); | ||
Assert.assertEquals("otherTransformed", valueTransformer.transformWrappedRelationalValue("other", transform)); | ||
|
||
} | ||
|
||
} |