PHOENIX-2310 Fix to address issue in Phoenix / Pig integration that was causing PhoenixConfigurationUtil.getUpsertColumnMetadataList() to return an invalid SQL statement if View DDL had recently been executed
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/288eda9a Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/288eda9a Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/288eda9a Branch: refs/heads/txn Commit: 288eda9ab8dffcb7b5ae8bc8e706797d4b20d173 Parents: 9fa160d Author: Jan <[email protected]> Authored: Wed Oct 7 18:25:05 2015 -0700 Committer: Jan <[email protected]> Committed: Wed Oct 7 18:25:05 2015 -0700 ---------------------------------------------------------------------- .../apache/phoenix/schema/DelegateColumn.java | 10 +-- .../apache/phoenix/schema/DelegateTable.java | 5 ++ .../util/PhoenixConfigurationUtilTest.java | 73 ++++++++++++++++++++ 3 files changed, 83 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/288eda9a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateColumn.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateColumn.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateColumn.java index 7ff8eb8..ddb0a1a 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateColumn.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateColumn.java @@ -17,11 +17,6 @@ */ package org.apache.phoenix.schema; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; - -import org.apache.phoenix.expression.Expression; import org.apache.phoenix.util.SizedUtil; public class DelegateColumn extends DelegateDatum implements PColumn { @@ -83,5 +78,10 @@ public class DelegateColumn extends DelegateDatum implements PColumn { @Override public boolean isRowTimestamp() { return getDelegate().isRowTimestamp(); + } + + @Override + public String toString() { + return getDelegate().toString(); } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/288eda9a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java index 203c5e6..5a0d63b 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java @@ -251,4 +251,9 @@ public class DelegateTable implements PTable { public int getRowTimestampColPos() { return delegate.getRowTimestampColPos(); } + + @Override + public String toString() { + return delegate.toString(); + } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/288eda9a/phoenix-core/src/test/java/org/apache/phoenix/mapreduce/util/PhoenixConfigurationUtilTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/mapreduce/util/PhoenixConfigurationUtilTest.java b/phoenix-core/src/test/java/org/apache/phoenix/mapreduce/util/PhoenixConfigurationUtilTest.java index 0ba849f..4d325c2 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/mapreduce/util/PhoenixConfigurationUtilTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/mapreduce/util/PhoenixConfigurationUtilTest.java @@ -40,6 +40,79 @@ import org.junit.Test; public class PhoenixConfigurationUtilTest extends BaseConnectionlessQueryTest { private static final String ORIGINAL_CLUSTER_QUORUM = "myzookeeperhost"; private static final String OVERRIDE_CLUSTER_QUORUM = "myoverridezookeeperhost"; + + @Test + /** + * This test reproduces the bug filed in PHOENIX-2310. + * + * When invoking PhoenixConfigurationUtil.getUpsertStatement(), + * if upserting into a Phoenix View and the View DDL had recently been issued such that MetdataClient cache had + * been updated as a result of the create table versus from data in SYSTEM.CATALOG, the Upsert statement + * would contain the Object.toString() classname + hashcode instead of the correct cf.column_name representation + * which would cause the calling Pig script to fail. + */ + public void testUpsertStatementOnNewViewWithReferencedCols() throws Exception { + + // Arrange + Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES)); + + try { + final String tableName = "TEST_TABLE_WITH_VIEW"; + final String viewName = "TEST_VIEW"; + String ddl = "CREATE TABLE "+ tableName + + " (a_string varchar not null, a_binary varbinary not null, col1 integer" + + " CONSTRAINT pk PRIMARY KEY (a_string, a_binary))\n"; + conn.createStatement().execute(ddl); + String viewDdl = "CREATE VIEW "+ viewName + + " AS SELECT * FROM " + tableName + "\n"; + conn.createStatement().execute(viewDdl); + final Configuration configuration = new Configuration (); + configuration.set(HConstants.ZOOKEEPER_QUORUM, getUrl()); + PhoenixConfigurationUtil.setOutputTableName(configuration, viewName); + PhoenixConfigurationUtil.setPhysicalTableName(configuration, viewName); + PhoenixConfigurationUtil.setUpsertColumnNames(configuration, new String[] {"A_STRING", "A_BINARY", "COL1"}); + + // Act + final String upserStatement = PhoenixConfigurationUtil.getUpsertStatement(configuration); + + // Assert + final String expectedUpsertStatement = "UPSERT INTO " + viewName + " (\"A_STRING\", \"A_BINARY\", \"0\".\"COL1\") VALUES (?, ?, ?)"; + assertEquals(expectedUpsertStatement, upserStatement); + } finally { + conn.close(); + } + } + + @Test + public void testUpsertStatementOnNewTableWithReferencedCols() throws Exception { + + // Arrange + Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES)); + + try { + final String tableName = "TEST_TABLE_WITH_REF_COLS"; + String ddl = "CREATE TABLE "+ tableName + + " (a_string varchar not null, a_binary varbinary not null, col1 integer" + + " CONSTRAINT pk PRIMARY KEY (a_string, a_binary))\n"; + conn.createStatement().execute(ddl); + final Configuration configuration = new Configuration (); + configuration.set(HConstants.ZOOKEEPER_QUORUM, getUrl()); + PhoenixConfigurationUtil.setOutputTableName(configuration, tableName); + PhoenixConfigurationUtil.setPhysicalTableName(configuration, tableName); + PhoenixConfigurationUtil.setUpsertColumnNames(configuration, new String[] {"A_STRING", "A_BINARY", "COL1"}); + + // Act + final String upserStatement = PhoenixConfigurationUtil.getUpsertStatement(configuration); + + // Assert + final String expectedUpsertStatement = "UPSERT INTO " + tableName + " (\"A_STRING\", \"A_BINARY\", \"0\".\"COL1\") VALUES (?, ?, ?)"; + assertEquals(expectedUpsertStatement, upserStatement); + } finally { + conn.close(); + } + } + + @Test public void testUpsertStatement() throws Exception { Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES));
