You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by sa...@apache.org on 2015/03/30 20:18:50 UTC
[1/2] phoenix git commit: PHOENIX-1722 Speedup CONVERT_TZ function
(Vaclav Loffelmann)
Repository: phoenix
Updated Branches:
refs/heads/4.3 50a6a9ac0 -> a24c3af65
PHOENIX-1722 Speedup CONVERT_TZ function (Vaclav Loffelmann)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/d8a23081
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/d8a23081
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/d8a23081
Branch: refs/heads/4.3
Commit: d8a23081c9dffc01901c1f35853b678607d15423
Parents: 50a6a9a
Author: Samarth <sa...@salesforce.com>
Authored: Mon Mar 30 11:18:07 2015 -0700
Committer: Samarth <sa...@salesforce.com>
Committed: Mon Mar 30 11:18:07 2015 -0700
----------------------------------------------------------------------
.../end2end/ConvertTimezoneFunctionIT.java | 24 +++++-
.../apache/phoenix/cache/JodaTimezoneCache.java | 84 ++++++++++++++++++++
.../function/ConvertTimezoneFunction.java | 38 +++------
.../function/TimezoneOffsetFunction.java | 25 ++----
.../phoenix/cache/JodaTimezoneCacheTest.java | 51 ++++++++++++
5 files changed, 172 insertions(+), 50 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/phoenix/blob/d8a23081/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
index d89a03b..f415dc6 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
@@ -23,8 +23,10 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import org.apache.phoenix.exception.SQLExceptionCode;
+import static org.junit.Assert.assertFalse;
import org.junit.Test;
/**
@@ -129,7 +131,7 @@ public class ConvertTimezoneFunctionIT extends BaseHBaseManagedTimeIT {
try {
ResultSet rs = conn.createStatement().executeQuery(
"SELECT k1, dates, CONVERT_TZ(dates, 'UNKNOWN_TIMEZONE', 'America/Adak') FROM TIMEZONE_OFFSET_TEST");
-
+
rs.next();
rs.getDate(3).getTime();
fail();
@@ -137,4 +139,24 @@ public class ConvertTimezoneFunctionIT extends BaseHBaseManagedTimeIT {
assertEquals(SQLExceptionCode.ILLEGAL_DATA.getErrorCode(), e.getErrorCode());
}
}
+
+ @Test
+ public void testConvertMultipleRecords() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ String ddl = "CREATE TABLE IF NOT EXISTS TIMEZONE_OFFSET_TEST (k1 INTEGER NOT NULL, dates DATE CONSTRAINT pk PRIMARY KEY (k1))";
+ Statement stmt = conn.createStatement();
+ stmt.execute(ddl);
+ stmt.execute("UPSERT INTO TIMEZONE_OFFSET_TEST (k1, dates) VALUES (1, TO_DATE('2014-03-01 00:00:00'))");
+ stmt.execute("UPSERT INTO TIMEZONE_OFFSET_TEST (k1, dates) VALUES (2, TO_DATE('2014-03-01 00:00:00'))");
+ conn.commit();
+
+ ResultSet rs = stmt.executeQuery(
+ "SELECT k1, dates, CONVERT_TZ(dates, 'UTC', 'America/Adak') FROM TIMEZONE_OFFSET_TEST");
+
+ assertTrue(rs.next());
+ assertEquals(1393596000000L, rs.getDate(3).getTime()); //Fri, 28 Feb 2014 14:00:00
+ assertTrue(rs.next());
+ assertEquals(1393596000000L, rs.getDate(3).getTime()); //Fri, 28 Feb 2014 14:00:00
+ assertFalse(rs.next());
+ }
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/d8a23081/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java b/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java
new file mode 100644
index 0000000..54904d7
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2015 Apache Software Foundation.
+ *
+ * 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.apache.phoenix.cache;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.schema.IllegalDataException;
+import org.joda.time.DateTimeZone;
+
+public class JodaTimezoneCache {
+
+ public static final int CACHE_EXPRIRE_TIME_MINUTES = 10;
+ private static final LoadingCache<ByteBuffer, DateTimeZone> cachedJodaTimeZones = createTimezoneCache();
+
+ /**
+ * Returns joda's DateTimeZone instance from cache or create new instance and cache it.
+ *
+ * @param timezoneId Timezone Id as accepted by {@code DateTimeZone.forID()}. E.g. Europe/Isle_of_Man
+ * @return joda's DateTimeZone instance
+ * @throws IllegalDataException if unknown timezone id is passed
+ */
+ public static DateTimeZone getInstance(ByteBuffer timezoneId) {
+ try {
+ return cachedJodaTimeZones.get(timezoneId);
+ } catch (ExecutionException ex) {
+ throw new IllegalDataException(ex);
+ } catch (UncheckedExecutionException e) {
+ throw new IllegalDataException("Unknown timezone " + Bytes.toString(timezoneId.array()));
+ }
+ }
+
+ /**
+ * Returns joda's DateTimeZone instance from cache or create new instance and cache it.
+ *
+ * @param timezoneId Timezone Id as accepted by {@code DateTimeZone.forID()}. E.g. Europe/Isle_of_Man
+ * @return joda's DateTimeZone instance
+ * @throws IllegalDataException if unknown timezone id is passed
+ */
+ public static DateTimeZone getInstance(ImmutableBytesWritable timezoneId) {
+ return getInstance(ByteBuffer.wrap(timezoneId.copyBytes()));
+ }
+
+ /**
+ * Returns joda's DateTimeZone instance from cache or create new instance and cache it.
+ *
+ * @param timezoneId Timezone Id as accepted by {@code DateTimeZone.forID()}. E.g. Europe/Isle_of_Man
+ * @return joda's DateTimeZone instance
+ * @throws IllegalDataException if unknown timezone id is passed
+ */
+ public static DateTimeZone getInstance(String timezoneId) {
+ return getInstance(ByteBuffer.wrap(Bytes.toBytes(timezoneId)));
+ }
+
+ private static LoadingCache<ByteBuffer, DateTimeZone> createTimezoneCache() {
+ return CacheBuilder.newBuilder().expireAfterAccess(CACHE_EXPRIRE_TIME_MINUTES, TimeUnit.MINUTES).build(new CacheLoader<ByteBuffer, DateTimeZone>() {
+
+ @Override
+ public DateTimeZone load(ByteBuffer timezone) throws Exception {
+ return DateTimeZone.forID(Bytes.toString(timezone.array()));
+ }
+ });
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/d8a23081/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
index dcde31f..3ea47a6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
@@ -15,21 +15,17 @@
*/
package org.apache.phoenix.expression.function;
-import java.sql.Date;
import java.sql.SQLException;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.TimeZone;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.cache.JodaTimezoneCache;
import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode;
-import org.apache.phoenix.schema.IllegalDataException;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PDate;
import org.apache.phoenix.schema.types.PVarchar;
import org.apache.phoenix.schema.tuple.Tuple;
+import org.joda.time.DateTimeZone;
/**
* Build in function CONVERT_TZ(date, 'timezone_from', 'timezone_to). Convert date from one timezone to
@@ -43,7 +39,6 @@ import org.apache.phoenix.schema.tuple.Tuple;
public class ConvertTimezoneFunction extends ScalarFunction {
public static final String NAME = "CONVERT_TZ";
- private final Map<String, TimeZone> cachedTimeZones = new HashMap<String, TimeZone>();
public ConvertTimezoneFunction() {
}
@@ -62,40 +57,25 @@ public class ConvertTimezoneFunction extends ScalarFunction {
if (!children.get(0).evaluate(tuple, ptr)) {
return false;
}
-
- Date dateo = (Date) PDate.INSTANCE.toObject(ptr, children.get(0).getSortOrder());
- Long date = dateo.getTime();
+ long date = PDate.INSTANCE.getCodec().decodeLong(ptr, children.get(0).getSortOrder());
if (!children.get(1).evaluate(tuple, ptr)) {
return false;
}
- TimeZone timezoneFrom = getTimezoneFromCache(Bytes.toString(ptr.get(), ptr.getOffset(), ptr.getLength()));
+ DateTimeZone timezoneFrom = JodaTimezoneCache.getInstance(ptr);
if (!children.get(2).evaluate(tuple, ptr)) {
return false;
}
- TimeZone timezoneTo = TimeZone.getTimeZone(Bytes.toString(ptr.get(), ptr.getOffset(), ptr.getLength()));
-
- long dateInUtc = date - timezoneFrom.getOffset(date);
- long dateInTo = dateInUtc + timezoneTo.getOffset(dateInUtc);
-
- ptr.set(PDate.INSTANCE.toBytes(new Date(dateInTo)));
+ DateTimeZone timezoneTo = JodaTimezoneCache.getInstance(ptr);
+ long convertedDate = date - timezoneFrom.getOffset(date) + timezoneTo.getOffset(date);
+ byte[] outBytes = new byte[8];
+ PDate.INSTANCE.getCodec().encodeLong(convertedDate, outBytes, 0);
+ ptr.set(outBytes);
return true;
}
- private TimeZone getTimezoneFromCache(String timezone) throws IllegalDataException {
- if (!cachedTimeZones.containsKey(timezone)) {
- TimeZone tz = TimeZone.getTimeZone(timezone);
- if (!tz.getID().equals(timezone)) {
- throw new IllegalDataException("Invalid timezone " + timezone);
- }
- cachedTimeZones.put(timezone, tz);
- return tz;
- }
- return cachedTimeZones.get(timezone);
- }
-
@Override
public PDataType getDataType() {
return PDate.INSTANCE;
http://git-wip-us.apache.org/repos/asf/phoenix/blob/d8a23081/phoenix-core/src/main/java/org/apache/phoenix/expression/function/TimezoneOffsetFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/TimezoneOffsetFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/TimezoneOffsetFunction.java
index 2cfbc25..8c70346 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/TimezoneOffsetFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/TimezoneOffsetFunction.java
@@ -18,22 +18,18 @@
package org.apache.phoenix.expression.function;
-import java.sql.Date;
import java.sql.SQLException;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.TimeZone;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.cache.JodaTimezoneCache;
import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode;
-import org.apache.phoenix.schema.IllegalDataException;
import org.apache.phoenix.schema.types.PDate;
import org.apache.phoenix.schema.types.PInteger;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PVarchar;
import org.apache.phoenix.schema.tuple.Tuple;
+import org.joda.time.DateTimeZone;
/**
* Returns offset (shift in minutes) of timezone at particular datetime in minutes.
@@ -45,7 +41,6 @@ public class TimezoneOffsetFunction extends ScalarFunction {
public static final String NAME = "TIMEZONE_OFFSET";
private static final int MILLIS_TO_MINUTES = 60 * 1000;
- private final Map<String, TimeZone> cachedTimeZones = new HashMap<String, TimeZone>();
public TimezoneOffsetFunction() {
}
@@ -64,24 +59,14 @@ public class TimezoneOffsetFunction extends ScalarFunction {
if (!children.get(0).evaluate(tuple, ptr)) {
return false;
}
-
- String timezone = Bytes.toString(ptr.get(), ptr.getOffset(), ptr.getLength());
+ DateTimeZone timezoneInstance = JodaTimezoneCache.getInstance(ptr);
if (!children.get(1).evaluate(tuple, ptr)) {
return false;
}
+ long date = PDate.INSTANCE.getCodec().decodeLong(ptr, children.get(1).getSortOrder());
- if (!cachedTimeZones.containsKey(timezone)) {
- TimeZone tz = TimeZone.getTimeZone(timezone);
- if (!tz.getID().equals(timezone)) {
- throw new IllegalDataException("Invalid timezone " + timezone);
- }
- cachedTimeZones.put(timezone, tz);
- }
-
- Date date = (Date) PDate.INSTANCE.toObject(ptr, children.get(1).getSortOrder());
- int offset = cachedTimeZones.get(timezone).getOffset(date.getTime());
-
+ int offset = timezoneInstance.getOffset(date);
ptr.set(PInteger.INSTANCE.toBytes(offset / MILLIS_TO_MINUTES));
return true;
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/d8a23081/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java b/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
new file mode 100644
index 0000000..f388703
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2015 Apache Software Foundation.
+ *
+ * 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.apache.phoenix.cache;
+
+import java.nio.ByteBuffer;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.schema.IllegalDataException;
+import org.joda.time.DateTimeZone;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+public class JodaTimezoneCacheTest {
+
+ @Test
+ public void testGetInstanceByteBufferUTC() {
+ DateTimeZone instance = JodaTimezoneCache.getInstance(ByteBuffer.wrap(Bytes.toBytes("UTC")));
+ assertTrue(instance instanceof DateTimeZone);
+ }
+
+ @Test
+ public void testGetInstanceString() {
+ DateTimeZone instance = JodaTimezoneCache.getInstance("America/St_Vincent");
+ assertTrue(instance instanceof DateTimeZone);
+ }
+
+ @Test(expected = IllegalDataException.class)
+ public void testGetInstanceStringUnknown() {
+ JodaTimezoneCache.getInstance("SOME_UNKNOWN_TIMEZONE");
+ }
+
+ @Test
+ public void testGetInstanceImmutableBytesWritable() {
+ ImmutableBytesWritable ptr = new ImmutableBytesWritable(Bytes.toBytes("Europe/Isle_of_Man"));
+ DateTimeZone instance = JodaTimezoneCache.getInstance(ptr);
+ assertTrue(instance instanceof DateTimeZone);
+ }
+}
[2/2] phoenix git commit: Fix IDE compiler errors for
JodaTimezoneCacheTest (James Taylor)
Posted by sa...@apache.org.
Fix IDE compiler errors for JodaTimezoneCacheTest (James Taylor)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/a24c3af6
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/a24c3af6
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/a24c3af6
Branch: refs/heads/4.3
Commit: a24c3af653aa4bb000c431f639d267dd84edd12a
Parents: d8a2308
Author: Samarth <sa...@salesforce.com>
Authored: Mon Mar 30 11:19:35 2015 -0700
Committer: Samarth <sa...@salesforce.com>
Committed: Mon Mar 30 11:19:35 2015 -0700
----------------------------------------------------------------------
.../org/apache/phoenix/cache/JodaTimezoneCacheTest.java | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/phoenix/blob/a24c3af6/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java b/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
index f388703..e9b6c67 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java
@@ -15,12 +15,14 @@
*/
package org.apache.phoenix.cache;
+import static org.junit.Assert.assertNotNull;
+
import java.nio.ByteBuffer;
+
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.phoenix.schema.IllegalDataException;
import org.joda.time.DateTimeZone;
-import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class JodaTimezoneCacheTest {
@@ -28,13 +30,13 @@ public class JodaTimezoneCacheTest {
@Test
public void testGetInstanceByteBufferUTC() {
DateTimeZone instance = JodaTimezoneCache.getInstance(ByteBuffer.wrap(Bytes.toBytes("UTC")));
- assertTrue(instance instanceof DateTimeZone);
+ assertNotNull(instance);
}
@Test
public void testGetInstanceString() {
DateTimeZone instance = JodaTimezoneCache.getInstance("America/St_Vincent");
- assertTrue(instance instanceof DateTimeZone);
+ assertNotNull(instance);
}
@Test(expected = IllegalDataException.class)
@@ -46,6 +48,6 @@ public class JodaTimezoneCacheTest {
public void testGetInstanceImmutableBytesWritable() {
ImmutableBytesWritable ptr = new ImmutableBytesWritable(Bytes.toBytes("Europe/Isle_of_Man"));
DateTimeZone instance = JodaTimezoneCache.getInstance(ptr);
- assertTrue(instance instanceof DateTimeZone);
+ assertNotNull(instance);
}
}