phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maryann...@apache.org
Subject [1/3] git commit: Fix issue PHOENIX-9 Incorrect behaviors in choosing index plans and getting results by column names caused by case-sensitive columns
Date Tue, 28 Jan 2014 22:37:49 GMT
Updated Branches:
  refs/heads/master f71b0c560 -> 92623a0ff


Fix issue PHOENIX-9 Incorrect behaviors in choosing index plans and getting results by column
names caused by case-sensitive columns


Project: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/commit/73a2e609
Tree: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/tree/73a2e609
Diff: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/diff/73a2e609

Branch: refs/heads/master
Commit: 73a2e6093e4004a4c0266bc70f96e4d9ef95b5fe
Parents: 2106de2
Author: maryannxue <maryannxue@apache.org>
Authored: Tue Jan 28 16:57:41 2014 -0500
Committer: maryannxue <maryannxue@apache.org>
Committed: Tue Jan 28 16:57:41 2014 -0500

----------------------------------------------------------------------
 .../phoenix/compile/IndexStatementRewriter.java |  2 +-
 .../phoenix/compile/ProjectionCompiler.java     | 22 ++++--
 .../phoenix/compile/StatementNormalizer.java    |  4 +-
 .../java/org/apache/phoenix/util/CSVLoader.java |  4 +-
 .../apache/phoenix/end2end/CSVLoaderTest.java   | 61 ++++++++++++++-
 .../phoenix/end2end/index/MutableIndexTest.java | 79 +++++++++++++++++++-
 6 files changed, 159 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/73a2e609/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
b/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
index 84ac0a1..99b653c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
@@ -72,7 +72,7 @@ public class IndexStatementRewriter extends ParseNodeRewriter {
         }
         String indexColName = IndexUtil.getIndexColumnName(dataColRef.getColumn());
         // Same alias as before, but use the index column name instead of the data column
name
-        ParseNode indexColNode = new ColumnParseNode(tName, indexColName, node.getAlias());
+        ParseNode indexColNode = new ColumnParseNode(tName, node.isCaseSensitive() ? '"'
+ indexColName + '"' : indexColName, node.getAlias());
         PDataType indexColType = IndexUtil.getIndexColumnDataType(dataColRef.getColumn());
         PDataType dataColType = dataColRef.getColumn().getDataType();
 

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/73a2e609/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
b/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
index 13a70a9..241e368 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
@@ -110,7 +110,9 @@ public class ProjectionCompiler {
             ColumnRef ref = new ColumnRef(tableRef,i);
             Expression expression = ref.newColumnExpression();
             projectedExpressions.add(expression);
-            projectedColumns.add(new ExpressionProjector(ref.getColumn().getName().getString(),
table.getName().getString(), expression, false));
+            String colName = tableColumn.getName().getString();
+            boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
+            ExpressionProjector projector = new ExpressionProjector(colName, table.getName().getString(),
expression, isCaseSensitive);
         }
     }
     
@@ -129,7 +131,9 @@ public class ProjectionCompiler {
             ColumnRef ref = new ColumnRef(tableRef,indexColumn.getPosition());
             Expression expression = ref.newColumnExpression();
             projectedExpressions.add(expression);
-            ExpressionProjector projector = new ExpressionProjector(tableColumn.getName().getString(),
table.getName().getString(), expression, false);
+            String colName = tableColumn.getName().getString();
+            boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
+            ExpressionProjector projector = new ExpressionProjector(colName, table.getName().getString(),
expression, isCaseSensitive);
             projectedColumns.add(projector);
         }
     }
@@ -141,8 +145,10 @@ public class ProjectionCompiler {
             ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
             Expression expression = ref.newColumnExpression();
             projectedExpressions.add(expression);
-            projectedColumns.add(new ExpressionProjector(column.getName().toString(), table.getName()
-                    .getString(), expression, false));
+            String colName = column.getName().toString();
+            boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
+            projectedColumns.add(new ExpressionProjector(colName, table.getName()
+                    .getString(), expression, isCaseSensitive));
         }
     }
 
@@ -155,8 +161,10 @@ public class ProjectionCompiler {
             ColumnRef ref = new ColumnRef(tableRef, indexColumn.getPosition());
             Expression expression = ref.newColumnExpression();
             projectedExpressions.add(expression);
-            projectedColumns.add(new ExpressionProjector(column.getName().toString(), 
-                    table.getName().getString(), expression, false));
+            String colName = column.getName().toString();
+            boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
+            projectedColumns.add(new ExpressionProjector(colName, 
+                    table.getName().getString(), expression, isCaseSensitive));
         }
     }
     
@@ -236,7 +244,7 @@ public class ProjectionCompiler {
                     }
                 }
                 String columnAlias = aliasedNode.getAlias() != null ? aliasedNode.getAlias()
: SchemaUtil.normalizeIdentifier(aliasedNode.getNode().getAlias());
-                boolean isCaseSensitive = (columnAlias != null && (aliasedNode.isCaseSensitve()
|| SchemaUtil.isCaseSensitive(columnAlias)))  || selectVisitor.isCaseSensitive;
+                boolean isCaseSensitive = aliasedNode.getAlias() != null ? aliasedNode.isCaseSensitve()
: (columnAlias != null ? SchemaUtil.isCaseSensitive(aliasedNode.getNode().getAlias()) : selectVisitor.isCaseSensitive);
                 String name = columnAlias == null ? expression.toString() : columnAlias;
                 projectedColumns.add(new ExpressionProjector(name, table.getName().getString(),
expression, isCaseSensitive));
             }

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/73a2e609/phoenix-core/src/main/java/org/apache/phoenix/compile/StatementNormalizer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/StatementNormalizer.java
b/phoenix-core/src/main/java/org/apache/phoenix/compile/StatementNormalizer.java
index 8eb769e..3ce2e68 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/StatementNormalizer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/StatementNormalizer.java
@@ -95,7 +95,9 @@ public class StatementNormalizer extends ParseNodeRewriter {
                 && node.getAlias() != null 
                 && node.getTableName() != null
                 && SchemaUtil.normalizeIdentifier(node.getAlias()).equals(node.getName()))
{
-            node = NODE_FACTORY.column(NODE_FACTORY.table(node.getSchemaName(), node.getTableName()),
node.isCaseSensitive() ? '"' + node.getName() + '"' : node.getName(), node.getFullName());
+            node = NODE_FACTORY.column(NODE_FACTORY.table(node.getSchemaName(), node.getTableName()),

+                    node.isCaseSensitive() ? '"' + node.getName() + '"' : node.getName(),

+                    node.isCaseSensitive() ? '"' + node.getFullName() + '"' : node.getFullName());
         }
         return super.visit(node);
     }

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/73a2e609/phoenix-core/src/main/java/org/apache/phoenix/util/CSVLoader.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/CSVLoader.java b/phoenix-core/src/main/java/org/apache/phoenix/util/CSVLoader.java
index 508818d..ca37604 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/CSVLoader.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/CSVLoader.java
@@ -222,7 +222,7 @@ public class CSVLoader {
             int i = 0;
             columnType = new ColumnInfo[columnNameToTypeMap.size()];
             for (Map.Entry<String, Integer> entry : columnNameToTypeMap.entrySet())
{
-                columnType[i++] = new ColumnInfo(entry.getKey(),entry.getValue());
+                columnType[i++] = new ColumnInfo('"' + entry.getKey() + '"',entry.getValue());
             }
 	    } else {
             // Leave "null" as indication to skip b/c it doesn't exist
@@ -237,7 +237,7 @@ public class CSVLoader {
                     }
                     unfoundColumnCount++;
                 } else {
-                    columnType[i] = new ColumnInfo(columnName, sqlType);
+                    columnType[i] = new ColumnInfo('"' + columnName + '"', sqlType);
                 }
             }
             if (unfoundColumnCount == columns.size()) {

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/73a2e609/phoenix-core/src/test/java/org/apache/phoenix/end2end/CSVLoaderTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/end2end/CSVLoaderTest.java b/phoenix-core/src/test/java/org/apache/phoenix/end2end/CSVLoaderTest.java
index 144a6bd..80129c2 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/end2end/CSVLoaderTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/end2end/CSVLoaderTest.java
@@ -49,6 +49,7 @@ public class CSVLoaderTest extends BaseHBaseManagedTimeTest {
 			"KEY1,A,2147483647,1.1,0,TRUE,9223372036854775807,0,1990-12-31 10:59:59,1999-12-31 23:59:59\n"
+ 
 			"KEY2,B,-2147483648,-1.1,2147483647,FALSE,-9223372036854775808,9223372036854775807,2000-01-01
00:00:01,2012-02-29 23:59:59\n";
 	private static final String STOCK_TABLE = "STOCK_SYMBOL";
+	private static final String STOCK_TABLE_CASESENSITIVE = "STOCK_SYMBOL_CASESENSITIVE";
 	private static final String STOCK_CSV_VALUES = 
 			"AAPL,APPLE Inc.\n" + 
 			"CRM,SALESFORCE\n" + 
@@ -61,6 +62,7 @@ public class CSVLoaderTest extends BaseHBaseManagedTimeTest {
 			"WMT,Walmart\n";
     private static final String[] STOCK_COLUMNS_WITH_BOGUS = new String[] {"SYMBOL", "BOGUS"};
     private static final String[] STOCK_COLUMNS = new String[] {"SYMBOL", "COMPANY"};
+    private static final String[] STOCK_COLUMNS_CASESENSITIVE = new String[] {"SYMBOL", "\"Company\""};
     private static final String STOCK_CSV_VALUES_WITH_HEADER =  STOCK_COLUMNS[0] + "," +
STOCK_COLUMNS[1] + "\n" + STOCK_CSV_VALUES;
     private static final String STOCK_CSV_VALUES_WITH_DELIMITER = "APPL" + '\u0001' + '\u0002'
+ "APPLE\n" +
                                                                   " Inc" + '\u0002' + "\n"
+
@@ -281,4 +283,61 @@ public class CSVLoaderTest extends BaseHBaseManagedTimeTest {
         assertFalse(phoenixResultSet.next());
         conn.close();
     }
-}
\ No newline at end of file
+    
+    @Test
+    public void testCaseSensitiveCSVUpsertWithColumns() throws Exception {
+        // Create table
+        String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE_CASESENSITIVE + "(SYMBOL
VARCHAR NOT NULL PRIMARY KEY, \"Company\" VARCHAR);";
+        PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
+        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
+        
+        // Upsert CSV file
+        CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE_CASESENSITIVE, Arrays.<String>asList(STOCK_COLUMNS_CASESENSITIVE),
true);
+        CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
+        csvUtil.upsert(reader);
+
+        // Compare Phoenix ResultSet with CSV file content
+        PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, \"Company\" FROM
" + STOCK_TABLE_CASESENSITIVE);
+        ResultSet phoenixResultSet = statement.executeQuery();
+        reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
+        String[] csvData;
+        while ((csvData = reader.readNext()) != null) {
+            assertTrue (phoenixResultSet.next());
+            for (int i=0; i<csvData.length; i++) {
+                assertEquals(csvData[i], phoenixResultSet.getString(i+1));
+            }
+        }
+        
+        assertFalse(phoenixResultSet.next());
+        conn.close();
+    }
+    
+    
+    @Test
+    public void testCaseSensitiveCSVUpsertWithNoColumns() throws Exception {
+        // Create table
+        String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE_CASESENSITIVE + "(SYMBOL
VARCHAR NOT NULL PRIMARY KEY, \"Company\" VARCHAR);";
+        PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
+        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
+        
+        // Upsert CSV file
+        CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE_CASESENSITIVE, null, true);
+        CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
+        csvUtil.upsert(reader);
+
+        // Compare Phoenix ResultSet with CSV file content
+        PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, \"Company\" FROM
" + STOCK_TABLE_CASESENSITIVE);
+        ResultSet phoenixResultSet = statement.executeQuery();
+        reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
+        String[] csvData;
+        while ((csvData = reader.readNext()) != null) {
+            assertTrue (phoenixResultSet.next());
+            for (int i=0; i<csvData.length; i++) {
+                assertEquals(csvData[i], phoenixResultSet.getString(i+1));
+            }
+        }
+        
+        assertFalse(phoenixResultSet.next());
+        conn.close();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/73a2e609/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/MutableIndexTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/MutableIndexTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/MutableIndexTest.java
index 0f56f85..0486bda 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/MutableIndexTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/MutableIndexTest.java
@@ -674,4 +674,81 @@ public class MutableIndexTest extends BaseMutableIndexTest {
         assertEquals("3", rs.getString(3));
         assertFalse(rs.next());
     }
-}
\ No newline at end of file
+    
+    @Test
+    public void testIndexWithCaseSensitiveCols() throws Exception {
+        String query;
+        ResultSet rs;
+        
+        Properties props = new Properties(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.setAutoCommit(false);
+        try {
+            conn.createStatement().execute("CREATE TABLE cs (k VARCHAR NOT NULL PRIMARY KEY,
\"V1\" VARCHAR, \"v2\" VARCHAR)");
+            query = "SELECT * FROM cs";
+            rs = conn.createStatement().executeQuery(query);
+            assertFalse(rs.next());
+
+            conn.createStatement().execute("CREATE INDEX ics ON cs (\"v2\") INCLUDE (\"V1\")");
+            query = "SELECT * FROM ics";
+            rs = conn.createStatement().executeQuery(query);
+            assertFalse(rs.next());
+
+            PreparedStatement stmt = conn.prepareStatement("UPSERT INTO cs VALUES(?,?,?)");
+            stmt.setString(1,"a");
+            stmt.setString(2, "x");
+            stmt.setString(3, "1");
+            stmt.execute();
+            stmt.setString(1,"b");
+            stmt.setString(2, "y");
+            stmt.setString(3, "2");
+            stmt.execute();
+            conn.commit();
+
+            query = "SELECT * FROM cs WHERE \"v2\" = '1'";
+            rs = conn.createStatement().executeQuery("EXPLAIN " + query);
+            assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER ICS ['1']", QueryUtil.getExplainPlan(rs));
+
+            rs = conn.createStatement().executeQuery(query);
+            assertTrue(rs.next());
+            assertEquals("a",rs.getString(1));
+            assertEquals("x",rs.getString(2));
+            assertEquals("1",rs.getString(3));
+            assertEquals("a",rs.getString("k"));
+            assertEquals("x",rs.getString("V1"));
+            assertEquals("1",rs.getString("v2"));
+            assertFalse(rs.next());
+
+            query = "SELECT \"V1\", \"V1\" as foo1, \"v2\" as foo, \"v2\" as \"Foo1\", \"v2\"
FROM cs ORDER BY foo";
+            rs = conn.createStatement().executeQuery("EXPLAIN " + query);
+            assertEquals("CLIENT PARALLEL 1-WAY FULL SCAN OVER ICS", QueryUtil.getExplainPlan(rs));
+
+            rs = conn.createStatement().executeQuery(query);
+            assertTrue(rs.next());
+            assertEquals("x",rs.getString(1));
+            assertEquals("x",rs.getString("V1"));
+            assertEquals("x",rs.getString(2));
+            assertEquals("x",rs.getString("foo1"));
+            assertEquals("1",rs.getString(3));
+            assertEquals("1",rs.getString("Foo"));
+            assertEquals("1",rs.getString(4));
+            assertEquals("1",rs.getString("Foo1"));
+            assertEquals("1",rs.getString(5));
+            assertEquals("1",rs.getString("v2"));
+            assertTrue(rs.next());
+            assertEquals("y",rs.getString(1));
+            assertEquals("y",rs.getString("V1"));
+            assertEquals("y",rs.getString(2));
+            assertEquals("y",rs.getString("foo1"));
+            assertEquals("2",rs.getString(3));
+            assertEquals("2",rs.getString("Foo"));
+            assertEquals("2",rs.getString(4));
+            assertEquals("2",rs.getString("Foo1"));
+            assertEquals("2",rs.getString(5));
+            assertEquals("2",rs.getString("v2"));
+            assertFalse(rs.next());
+        } finally {
+            conn.close();
+        }
+    }
+}


Mime
View raw message