You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2014/10/27 06:13:37 UTC
[1/2] git commit: PHOENIX-1366 ORDINAL_POSITION incorrect for
multi-tenant table over tenant-specific connection (Bruno Dumon)
Repository: phoenix
Updated Branches:
refs/heads/3.0 2bdc33bc5 -> 0d90e2fc9
PHOENIX-1366 ORDINAL_POSITION incorrect for multi-tenant table over tenant-specific connection (Bruno Dumon)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/60fee11b
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/60fee11b
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/60fee11b
Branch: refs/heads/3.0
Commit: 60fee11be5e9968d7922215c24aaeb29cdaf416b
Parents: 2bdc33b
Author: James Taylor <jt...@salesforce.com>
Authored: Sun Oct 26 20:46:03 2014 -0700
Committer: James Taylor <jt...@salesforce.com>
Committed: Sun Oct 26 20:51:21 2014 -0700
----------------------------------------------------------------------
.../end2end/TenantSpecificTablesDDLIT.java | 23 ++++++++++-----
.../phoenix/jdbc/PhoenixDatabaseMetaData.java | 31 ++++++++++++++++----
2 files changed, 42 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/phoenix/blob/60fee11b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
index 589e963..42fe5b8 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
@@ -22,6 +22,8 @@ import static org.apache.phoenix.exception.SQLExceptionCode.CANNOT_DROP_PK;
import static org.apache.phoenix.exception.SQLExceptionCode.CANNOT_MODIFY_VIEW_PK;
import static org.apache.phoenix.exception.SQLExceptionCode.CANNOT_MUTATE_TABLE;
import static org.apache.phoenix.exception.SQLExceptionCode.TABLE_UNDEFINED;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.KEY_SEQ;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ORDINAL_POSITION;
import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA;
import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_TABLE;
import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.TYPE_SEQUENCE;
@@ -554,21 +556,22 @@ public class TenantSpecificTablesDDLIT extends BaseTenantSpecificTablesIT {
// make sure tenants see parent table's columns and their own
rs = meta.getColumns(null, null, StringUtil.escapeLike(TENANT_TABLE_NAME) + "%", null);
assertTrue(rs.next());
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "user");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "user", 1);
assertTrue(rs.next());
// (tenant_id column is not visible in tenant-specific connection)
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "tenant_type_id");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "tenant_type_id", 2);
+ assertEquals(1, rs.getInt(KEY_SEQ));
assertTrue(rs.next());
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "id");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "id", 3);
assertTrue(rs.next());
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "tenant_col");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME, "tenant_col", 4);
assertTrue(rs.next());
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME_NO_TENANT_TYPE_ID, "user");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME_NO_TENANT_TYPE_ID, "user", 1);
assertTrue(rs.next());
// (tenant_id column is not visible in tenant-specific connection)
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME_NO_TENANT_TYPE_ID, "id");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME_NO_TENANT_TYPE_ID, "id", 2);
assertTrue(rs.next());
- assertColumnMetaData(rs, null, TENANT_TABLE_NAME_NO_TENANT_TYPE_ID, "tenant_col");
+ assertColumnMetaData(rs, null, TENANT_TABLE_NAME_NO_TENANT_TYPE_ID, "tenant_col", 3);
assertFalse(rs.next());
}
finally {
@@ -587,4 +590,10 @@ public class TenantSpecificTablesDDLIT extends BaseTenantSpecificTablesIT {
assertEquals(table, rs.getString("TABLE_NAME"));
assertEquals(SchemaUtil.normalizeIdentifier(column), rs.getString("COLUMN_NAME"));
}
+
+ private void assertColumnMetaData(ResultSet rs, String schema, String table, String column, int ordinalPosition)
+ throws SQLException {
+ assertColumnMetaData(rs, schema, table, column);
+ assertEquals(ordinalPosition, rs.getInt(ORDINAL_POSITION));
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/phoenix/blob/60fee11b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
index f0b709c..0cf34dc 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
@@ -26,8 +26,12 @@ import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.List;
import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.KeyValue;
+import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.phoenix.compile.ColumnProjector;
import org.apache.phoenix.compile.ExpressionProjector;
@@ -55,6 +59,7 @@ import org.apache.phoenix.schema.PTable.LinkType;
import org.apache.phoenix.schema.PTableType;
import org.apache.phoenix.schema.RowKeyValueAccessor;
import org.apache.phoenix.schema.SortOrder;
+import org.apache.phoenix.schema.tuple.ResultTuple;
import org.apache.phoenix.schema.tuple.SingleKeyValueTuple;
import org.apache.phoenix.schema.tuple.Tuple;
import org.apache.phoenix.util.ByteUtil;
@@ -398,7 +403,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
SQL_DATA_TYPE + "," +
SQL_DATETIME_SUB + "," +
CHAR_OCTET_LENGTH + "," +
- ORDINAL_POSITION + "," +
+ "CASE WHEN TENANT_POS_SHIFT THEN ORDINAL_POSITION-1 ELSE ORDINAL_POSITION END AS " + ORDINAL_POSITION + "," +
"CASE " + NULLABLE + " WHEN " + DatabaseMetaData.attributeNoNulls + " THEN '" + Boolean.FALSE.toString() + "' WHEN " + DatabaseMetaData.attributeNullable + " THEN '" + Boolean.TRUE.toString() + "' END AS " + IS_NULLABLE + "," +
SCOPE_CATALOG + "," +
SCOPE_SCHEMA + "," +
@@ -410,8 +415,8 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
DATA_TYPE + " " + TYPE_ID + "," +// raw type id for potential internal consumption
VIEW_CONSTANT + "," +
MULTI_TENANT + "," +
- KEY_SEQ +
- " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS);
+ "CASE WHEN TENANT_POS_SHIFT THEN KEY_SEQ-1 ELSE KEY_SEQ END AS " + KEY_SEQ +
+ " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS + "(TENANT_POS_SHIFT BOOLEAN)");
StringBuilder where = new StringBuilder();
addTenantIdFilter(where, catalog);
if (schemaPattern != null) {
@@ -456,7 +461,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
} else {
buf.append(" where " + where);
}
- buf.append(" order by " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + ORDINAL_POSITION);
+ buf.append(" order by " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + SYSTEM_CATALOG_ALIAS + "." + ORDINAL_POSITION);
Statement stmt;
if (isTenantSpecificConnection) {
@@ -492,6 +497,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
private final int multiTenantIndex;
private final int keySeqIndex;
private boolean inMultiTenantTable;
+ private boolean tenantColumnSkipped;
private TenantColumnFilteringIterator(ResultIterator delegate, RowProjector rowProjector) throws SQLException {
super(delegate);
@@ -510,15 +516,30 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
&& getColumn(tuple, columnFamilyIndex) == null && getColumn(tuple, columnNameIndex) == null) {
// new table, check if it is multitenant
inMultiTenantTable = getColumn(tuple, multiTenantIndex) == Boolean.TRUE;
+ tenantColumnSkipped = false;
// skip row representing table
tuple = super.next();
}
- if (tuple != null && inMultiTenantTable && new Short((short)1).equals(getColumn(tuple, keySeqIndex))) {
+ if (tuple != null && inMultiTenantTable && !tenantColumnSkipped
+ && new Long(1L).equals(getColumn(tuple, keySeqIndex))) {
+ tenantColumnSkipped = true;
// skip tenant id primary key column
return next();
}
+ if (tuple != null && tenantColumnSkipped) {
+ ResultTuple resultTuple = (ResultTuple)tuple;
+ List<Cell> cells = resultTuple.getResult().listCells();
+ KeyValue kv = new KeyValue(resultTuple.getResult().getRow(), QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES,
+ Bytes.toBytes("TENANT_POS_SHIFT"), PDataType.TRUE_BYTES);
+ List<Cell> newCells = Lists.newArrayListWithCapacity(cells.size() + 1);
+ newCells.addAll(cells);
+ newCells.add(kv);
+ Collections.sort(newCells, KeyValue.COMPARATOR);
+ resultTuple.setResult(Result.create(newCells));
+ }
+
return tuple;
}
[2/2] git commit: PHOENIX-1366 Use static constants
Posted by ja...@apache.org.
PHOENIX-1366 Use static constants
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/0d90e2fc
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/0d90e2fc
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/0d90e2fc
Branch: refs/heads/3.0
Commit: 0d90e2fc92e6013a48d6479fc8208733edab9d52
Parents: 60fee11
Author: James Taylor <jt...@salesforce.com>
Authored: Sun Oct 26 22:13:31 2014 -0700
Committer: James Taylor <jt...@salesforce.com>
Committed: Sun Oct 26 22:13:31 2014 -0700
----------------------------------------------------------------------
.../phoenix/jdbc/PhoenixDatabaseMetaData.java | 32 +++++++++++---------
1 file changed, 18 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/phoenix/blob/0d90e2fc/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
index 0cf34dc..5560d48 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java
@@ -29,7 +29,6 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringEscapeUtils;
-import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
@@ -257,6 +256,9 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
public static final String PARENT_TENANT_ID = "PARENT_TENANT_ID";
public static final byte[] PARENT_TENANT_ID_BYTES = Bytes.toBytes(PARENT_TENANT_ID);
+ private static final String TENANT_POS_SHIFT = "TENANT_POS_SHIFT";
+ private static final byte[] TENANT_POS_SHIFT_BYTES = Bytes.toBytes(TENANT_POS_SHIFT);
+
private final PhoenixConnection connection;
private final ResultSet emptyResultSet;
@@ -403,7 +405,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
SQL_DATA_TYPE + "," +
SQL_DATETIME_SUB + "," +
CHAR_OCTET_LENGTH + "," +
- "CASE WHEN TENANT_POS_SHIFT THEN ORDINAL_POSITION-1 ELSE ORDINAL_POSITION END AS " + ORDINAL_POSITION + "," +
+ "CASE WHEN " + TENANT_POS_SHIFT + " THEN " + ORDINAL_POSITION + "-1 ELSE " + ORDINAL_POSITION + " END AS " + ORDINAL_POSITION + "," +
"CASE " + NULLABLE + " WHEN " + DatabaseMetaData.attributeNoNulls + " THEN '" + Boolean.FALSE.toString() + "' WHEN " + DatabaseMetaData.attributeNullable + " THEN '" + Boolean.TRUE.toString() + "' END AS " + IS_NULLABLE + "," +
SCOPE_CATALOG + "," +
SCOPE_SCHEMA + "," +
@@ -415,8 +417,8 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
DATA_TYPE + " " + TYPE_ID + "," +// raw type id for potential internal consumption
VIEW_CONSTANT + "," +
MULTI_TENANT + "," +
- "CASE WHEN TENANT_POS_SHIFT THEN KEY_SEQ-1 ELSE KEY_SEQ END AS " + KEY_SEQ +
- " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS + "(TENANT_POS_SHIFT BOOLEAN)");
+ "CASE WHEN " + TENANT_POS_SHIFT + " THEN " + KEY_SEQ + "-1 ELSE " + KEY_SEQ + " END AS " + KEY_SEQ +
+ " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS + "(" + TENANT_POS_SHIFT + " BOOLEAN)");
StringBuilder where = new StringBuilder();
addTenantIdFilter(where, catalog);
if (schemaPattern != null) {
@@ -521,23 +523,25 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho
tuple = super.next();
}
- if (tuple != null && inMultiTenantTable && !tenantColumnSkipped
- && new Long(1L).equals(getColumn(tuple, keySeqIndex))) {
- tenantColumnSkipped = true;
- // skip tenant id primary key column
- return next();
+ if (tuple != null && inMultiTenantTable && !tenantColumnSkipped) {
+ Object value = getColumn(tuple, keySeqIndex);
+ if (value != null && ((Number)value).longValue() == 1L) {
+ tenantColumnSkipped = true;
+ // skip tenant id primary key column
+ return next();
+ }
}
if (tuple != null && tenantColumnSkipped) {
ResultTuple resultTuple = (ResultTuple)tuple;
- List<Cell> cells = resultTuple.getResult().listCells();
- KeyValue kv = new KeyValue(resultTuple.getResult().getRow(), QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES,
- Bytes.toBytes("TENANT_POS_SHIFT"), PDataType.TRUE_BYTES);
- List<Cell> newCells = Lists.newArrayListWithCapacity(cells.size() + 1);
+ List<KeyValue> cells = resultTuple.getResult().list();
+ KeyValue kv = new KeyValue(resultTuple.getResult().getRow(), TABLE_FAMILY_BYTES,
+ TENANT_POS_SHIFT_BYTES, PDataType.TRUE_BYTES);
+ List<KeyValue> newCells = Lists.newArrayListWithCapacity(cells.size() + 1);
newCells.addAll(cells);
newCells.add(kv);
Collections.sort(newCells, KeyValue.COMPARATOR);
- resultTuple.setResult(Result.create(newCells));
+ resultTuple.setResult(new Result(newCells));
}
return tuple;