forked from prestodb/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for interval types to JDBC driver
- Loading branch information
Showing
4 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoIntervalDayTime.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,89 @@ | ||
/* | ||
* 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:https://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 com.facebook.presto.jdbc; | ||
|
||
import java.util.Objects; | ||
|
||
public class PrestoIntervalDayTime | ||
{ | ||
private static final long MILLIS_IN_SECOND = 1000; | ||
private static final long MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; | ||
private static final long MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; | ||
private static final long MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | ||
|
||
private final long milliSeconds; | ||
|
||
public PrestoIntervalDayTime(long milliSeconds) | ||
{ | ||
this.milliSeconds = milliSeconds; | ||
} | ||
|
||
public PrestoIntervalDayTime(int day, int hour, int minute, int second, int millis) | ||
{ | ||
milliSeconds = toMillis(day, hour, minute, second, millis); | ||
} | ||
|
||
public long getMilliSeconds() | ||
{ | ||
return milliSeconds; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(milliSeconds); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) | ||
{ | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
PrestoIntervalDayTime other = (PrestoIntervalDayTime) obj; | ||
return Objects.equals(this.milliSeconds, other.milliSeconds); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return formatMillis(milliSeconds); | ||
} | ||
|
||
private static long toMillis(int day, int hour, int minute, int second, int millis) | ||
{ | ||
return (day * MILLIS_IN_DAY) + | ||
(hour * MILLIS_IN_HOUR) + | ||
(minute * MILLIS_IN_MINUTE) + | ||
(second * MILLIS_IN_SECOND) + | ||
millis; | ||
} | ||
|
||
private static String formatMillis(long millis) | ||
{ | ||
long day = millis / MILLIS_IN_DAY; | ||
millis %= MILLIS_IN_DAY; | ||
long hour = millis / MILLIS_IN_HOUR; | ||
millis %= MILLIS_IN_HOUR; | ||
long minute = millis / MILLIS_IN_MINUTE; | ||
millis %= MILLIS_IN_MINUTE; | ||
long second = millis / MILLIS_IN_SECOND; | ||
millis %= MILLIS_IN_SECOND; | ||
|
||
return String.format("%d %02d:%02d:%02d.%03d", day, hour, minute, second, millis); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoIntervalYearMonth.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,66 @@ | ||
/* | ||
* 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:https://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 com.facebook.presto.jdbc; | ||
|
||
import java.util.Objects; | ||
|
||
public class PrestoIntervalYearMonth | ||
{ | ||
private final long months; | ||
|
||
public PrestoIntervalYearMonth(long months) | ||
{ | ||
this.months = months; | ||
} | ||
|
||
public PrestoIntervalYearMonth(int year, int months) | ||
{ | ||
this.months = (12 * year) + months; | ||
} | ||
|
||
public long getMonths() | ||
{ | ||
return months; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(months); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) | ||
{ | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
PrestoIntervalYearMonth other = (PrestoIntervalYearMonth) obj; | ||
return Objects.equals(this.months, other.months); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return formatMonths(months); | ||
} | ||
|
||
private static String formatMonths(long months) | ||
{ | ||
return (months / 12) + "-" + (months % 12); | ||
} | ||
} |
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