Repository: phoenix Updated Branches: refs/heads/calcite 73f2eb1d9 -> 4954621f3
PHOENIX-3816 Implement SET_OPTION for consistency in phoenix-calcite Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/4954621f Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/4954621f Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/4954621f Branch: refs/heads/calcite Commit: 4954621f3a137a559b43be6f5dd902182af40c3d Parents: 73f2eb1 Author: Ankit Singhal <[email protected]> Authored: Tue May 2 13:04:37 2017 +0530 Committer: Ankit Singhal <[email protected]> Committed: Tue May 2 13:04:37 2017 +0530 ---------------------------------------------------------------------- .../apache/phoenix/end2end/AlterSessionIT.java | 12 ++- .../phoenix/calcite/PhoenixPrepareImpl.java | 39 ++++++++++ .../phoenix/calcite/SqlOperatorBaseTest.java | 78 ++++++++++---------- 3 files changed, 85 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/4954621f/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterSessionIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterSessionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterSessionIT.java index bf666e4..7dd8b5d 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterSessionIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterSessionIT.java @@ -18,7 +18,6 @@ package org.apache.phoenix.end2end; import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.sql.Connection; @@ -60,11 +59,16 @@ public class AlterSessionIT extends ParallelStatsDisabledIT { try (Connection conn = DriverManager.getConnection(getUrl(), props)) { Statement st = conn.createStatement(); st.execute("alter session set Consistency = 'timeline'"); - ResultSet rs = st.executeQuery("explain select * from " + tableName); assertEquals(Consistency.TIMELINE, conn.unwrap(PhoenixConnection.class).getConsistency()); + + st.execute("alter session RESET Consistency"); + assertEquals(Consistency.STRONG, conn.unwrap(PhoenixConnection.class).getConsistency()); + + st.execute("alter session set Consistency = 'timeline'"); + assertEquals(Consistency.TIMELINE, conn.unwrap(PhoenixConnection.class).getConsistency()); + ResultSet rs = st.executeQuery("explain select * from " + tableName); String queryPlan = QueryUtil.getExplainPlan(rs); assertTrue(queryPlan.indexOf("TIMELINE") > 0); - // turn off timeline read consistency st.execute("alter session set Consistency = 'strong'"); rs = st.executeQuery("explain select * from " + tableName); @@ -78,7 +82,7 @@ public class AlterSessionIT extends ParallelStatsDisabledIT { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); try (Connection conn = DriverManager.getConnection(getUrl() + PhoenixRuntime.JDBC_PROTOCOL_TERMINATOR + "Consistency=TIMELINE", props)) { - assertEquals(Consistency.TIMELINE, ((PhoenixConnection)conn).getConsistency()); + assertEquals(Consistency.TIMELINE, conn.unwrap(PhoenixConnection.class).getConsistency()); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("explain select * from " + tableName); String queryPlan = QueryUtil.getExplainPlan(rs); http://git-wip-us.apache.org/repos/asf/phoenix/blob/4954621f/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java index b6d0035..91b0cb6 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java @@ -9,6 +9,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -22,14 +23,19 @@ import org.apache.calcite.plan.RelOptCostFactory; import org.apache.calcite.plan.RelOptPlanner; import org.apache.calcite.plan.RelOptRule; import org.apache.calcite.prepare.CalcitePrepareImpl; +import org.apache.calcite.prepare.Prepare.PreparedResult; import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.RelRoot; import org.apache.calcite.rel.convert.ConverterRule; +import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.rex.RexBuilder; import org.apache.calcite.runtime.Hook; import org.apache.calcite.runtime.Hook.Closeable; import org.apache.calcite.schema.SchemaPlus; import org.apache.calcite.sql.SqlColumnDefInPkConstraintNode; import org.apache.calcite.sql.SqlColumnDefNode; +import org.apache.calcite.sql.SqlExplainFormat; +import org.apache.calcite.sql.SqlExplainLevel; import org.apache.calcite.sql.SqlFunctionArguementNode; import org.apache.calcite.sql.SqlIdentifier; import org.apache.calcite.sql.SqlIndexExpressionNode; @@ -38,12 +44,14 @@ import org.apache.calcite.sql.SqlLiteral; import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.SqlNodeList; import org.apache.calcite.sql.SqlOptionNode; +import org.apache.calcite.sql.SqlSetOption; import org.apache.calcite.sql.parser.SqlParser; import org.apache.calcite.sql.parser.SqlParserPos; import org.apache.calcite.sql.parser.SqlParserUtil; import org.apache.calcite.tools.Program; import org.apache.calcite.util.Holder; import org.apache.calcite.util.NlsString; +import org.apache.hadoop.hbase.client.Consistency; import org.apache.hadoop.hbase.util.Pair; import org.apache.phoenix.calcite.parse.SqlAlterIndex; import org.apache.phoenix.calcite.parse.SqlAlterTable; @@ -111,6 +119,7 @@ import org.apache.phoenix.schema.PTable.IndexType; import org.apache.phoenix.schema.PTableType; import org.apache.phoenix.schema.Sequence; import org.apache.phoenix.schema.SortOrder; +import org.apache.phoenix.util.PhoenixRuntime; import org.apache.phoenix.util.SchemaUtil; import com.google.common.base.Function; @@ -234,6 +243,7 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { } } + private List<Closeable> addHooks(final CalciteSchema rootSchema, boolean materializationEnabled, final boolean forceDecorrelate) { final List<Closeable> hooks = Lists.newArrayList(); @@ -530,6 +540,34 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { client.alterIndex(alterIndex); break; } + case SET_OPTION: { + SqlSetOption alterSessionNode = (SqlSetOption) node; + if (SqlKind.SESSION.toString().equals(alterSessionNode.getScope()) + && alterSessionNode.getName().getSimple().equalsIgnoreCase( + PhoenixRuntime.CONSISTENCY_ATTRIB.toUpperCase())) { + SqlNode value = alterSessionNode.getValue(); + if (value != null) { + Consistency consistency = null; + try { + consistency = + Consistency + .valueOf(((SqlLiteral) value).toValue().toUpperCase()); + } catch (IllegalArgumentException e) { + throw new SQLException("Illegal consistency value:" + value + + ". Expecting values out of : " + + Arrays.asList(Consistency.values())); + } + if (consistency != null) { + connection.setConsistency(consistency); + } + } else { + // reset + connection.setConsistency(Consistency.STRONG); + } + } + break; + } + case OTHER_DDL: { if (node instanceof SqlUpdateStatistics) { SqlUpdateStatistics updateStatsNode = (SqlUpdateStatistics) node; @@ -741,4 +779,5 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { } return CONNECTIONLESS_PHOENIX_CONNECTION; } + } http://git-wip-us.apache.org/repos/asf/phoenix/blob/4954621f/phoenix-core/src/test/java/org/apache/phoenix/calcite/SqlOperatorBaseTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/calcite/SqlOperatorBaseTest.java b/phoenix-core/src/test/java/org/apache/phoenix/calcite/SqlOperatorBaseTest.java index ecfe51e..a25ffb0 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/calcite/SqlOperatorBaseTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/calcite/SqlOperatorBaseTest.java @@ -2246,49 +2246,47 @@ public abstract class SqlOperatorBaseTest { false); } - @Test public void testOverlapsOperator() { - tester.setFor(SqlStdOperatorTable.OVERLAPS, VM_EXPAND); - if (Bug.FRG187_FIXED) { - tester.checkBoolean( - "(date '1-2-3', date '1-2-3') overlaps (date '1-2-3', interval '1' year)", - Boolean.TRUE); - tester.checkBoolean( - "(date '1-2-3', date '1-2-3') overlaps (date '4-5-6', interval '1' year)", - Boolean.FALSE); - tester.checkBoolean( - "(date '1-2-3', date '4-5-6') overlaps (date '2-2-3', date '3-4-5')", - Boolean.TRUE); - tester.checkNull( - "(cast(null as date), date '1-2-3') overlaps (date '1-2-3', interval '1' year)"); - tester.checkNull( - "(date '1-2-3', date '1-2-3') overlaps (date '1-2-3', cast(null as date))"); + @Test + public void testOverlapsOperator() { + tester.setFor(SqlStdOperatorTable.OVERLAPS, VM_EXPAND); + tester.checkBoolean( + "(date '1-2-3', date '1-2-3') overlaps (date '1-2-3', interval '1' year)", + Boolean.TRUE); + tester.checkBoolean( + "(date '1-2-3', date '1-2-3') overlaps (date '4-5-6', interval '1' year)", + Boolean.FALSE); + tester.checkBoolean("(date '1-2-3', date '4-5-6') overlaps (date '2-2-3', date '3-4-5')", + Boolean.TRUE); + tester.checkNull( + "(cast(null as date), date '1-2-3') overlaps (date '1-2-3', interval '1' year)"); + tester.checkNull( + "(date '1-2-3', date '1-2-3') overlaps (date '1-2-3', cast(null as date))"); - tester.checkBoolean( - "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', time '1:2:3')", - Boolean.TRUE); - tester.checkBoolean( - "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', time '1:2:2')", - Boolean.FALSE); - tester.checkBoolean( - "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', interval '2' hour)", - Boolean.TRUE); - tester.checkNull( - "(time '1:2:3', cast(null as time)) overlaps (time '23:59:59', time '1:2:3')"); - tester.checkNull( - "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', cast(null as interval hour))"); + tester.checkBoolean( + "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', time '1:2:3')", + Boolean.TRUE); + tester.checkBoolean( + "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', time '1:2:2')", + Boolean.FALSE); + tester.checkBoolean( + "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', interval '2' hour)", + Boolean.TRUE); + tester.checkNull( + "(time '1:2:3', cast(null as time)) overlaps (time '23:59:59', time '1:2:3')"); + tester.checkNull( + "(time '1:2:3', interval '1' second) overlaps (time '23:59:59', cast(null as interval hour))"); - tester.checkBoolean( - "(timestamp '1-2-3 4:5:6', timestamp '1-2-3 4:5:6' ) overlaps (timestamp '1-2-3 4:5:6', interval '1 2:3:4.5' day to second)", - Boolean.TRUE); - tester.checkBoolean( - "(timestamp '1-2-3 4:5:6', timestamp '1-2-3 4:5:6' ) overlaps (timestamp '2-2-3 4:5:6', interval '1 2:3:4.5' day to second)", - Boolean.FALSE); - tester.checkNull( - "(timestamp '1-2-3 4:5:6', cast(null as interval day) ) overlaps (timestamp '1-2-3 4:5:6', interval '1 2:3:4.5' day to second)"); - tester.checkNull( - "(timestamp '1-2-3 4:5:6', timestamp '1-2-3 4:5:6' ) overlaps (cast(null as timestamp), interval '1 2:3:4.5' day to second)"); + tester.checkBoolean( + "(timestamp '1-2-3 4:5:6', timestamp '1-2-3 4:5:6' ) overlaps (timestamp '1-2-3 4:5:6', interval '1 2:3:4.5' day to second)", + Boolean.TRUE); + tester.checkBoolean( + "(timestamp '1-2-3 4:5:6', timestamp '1-2-3 4:5:6' ) overlaps (timestamp '2-2-3 4:5:6', interval '1 2:3:4.5' day to second)", + Boolean.FALSE); + tester.checkNull( + "(timestamp '1-2-3 4:5:6', cast(null as interval day) ) overlaps (timestamp '1-2-3 4:5:6', interval '1 2:3:4.5' day to second)"); + tester.checkNull( + "(timestamp '1-2-3 4:5:6', timestamp '1-2-3 4:5:6' ) overlaps (cast(null as timestamp), interval '1 2:3:4.5' day to second)"); } - } @Test public void testLessThanOperator() { tester.setFor(SqlStdOperatorTable.LESS_THAN);
