Search in sources :

Example 41 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class PhoenixRuntime method getPkColumns.

private static List<PColumn> getPkColumns(PTable ptable, Connection conn) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    if (ptable.getType() == PTableType.INDEX) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection)

Example 42 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class PhoenixRuntime method getColumn.

private static PColumn getColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table == null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName == null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    // we're dealing with an index table.
    if (table.getType() == PTableType.INDEX) {
        columnName = IndexUtil.getIndexColumnName(familyName, columnName);
    }
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(columnName);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return pColumn;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) SQLException(java.sql.SQLException) PColumnFamily(org.apache.phoenix.schema.PColumnFamily)

Example 43 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class WhereCompilerTest method testAndPKAndNotPK.

@Test
public void testAndPKAndNotPK() throws SQLException {
    String query = "select * from bugTable where ID = 'i2' and company = 'c3'";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    pconn.createStatement().execute("create table bugTable(ID varchar primary key,company varchar)");
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    PColumn column = plan.getTableRef().getTable().getColumnForColumnName("COMPANY");
    assertEquals(singleKVFilter(constantComparison(CompareOp.EQUAL, new KeyValueColumnExpression(column), "c3")), filter);
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) TestUtil.multiEncodedKVFilter(org.apache.phoenix.util.TestUtil.multiEncodedKVFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) TestUtil.singleKVFilter(org.apache.phoenix.util.TestUtil.singleKVFilter) Scan(org.apache.hadoop.hbase.client.Scan) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 44 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class ColumnExpressionTest method testSerializationWithNullScale.

@Test
public void testSerializationWithNullScale() throws Exception {
    int maxLen = 30;
    PName colName = PNameFactory.newName("c1");
    PColumn column = new PColumnImpl(colName, PNameFactory.newName("f1"), PBinary.INSTANCE, maxLen, null, true, 20, SortOrder.getDefault(), 0, null, false, null, false, false, colName.getBytes());
    ColumnExpression colExp = new KeyValueColumnExpression(column);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dOut = new DataOutputStream(baos);
    colExp.write(dOut);
    dOut.flush();
    ColumnExpression colExp2 = new KeyValueColumnExpression();
    byte[] bytes = baos.toByteArray();
    DataInputStream dIn = new DataInputStream(new ByteArrayInputStream(bytes, 0, bytes.length));
    colExp2.readFields(dIn);
    assertEquals(maxLen, colExp2.getMaxLength().intValue());
    assertNull(colExp2.getScale());
    assertEquals(PBinary.INSTANCE, colExp2.getDataType());
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PColumnImpl(org.apache.phoenix.schema.PColumnImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) PName(org.apache.phoenix.schema.PName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 45 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class AlterMultiTenantTableWithViewsIT method getIndexOfPkColumn.

private int getIndexOfPkColumn(PhoenixConnection conn, String columnName, String tableName) throws SQLException {
    String normalizedTableName = SchemaUtil.normalizeIdentifier(tableName);
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), normalizedTableName));
    List<PColumn> pkCols = table.getPKColumns();
    String normalizedColumnName = SchemaUtil.normalizeIdentifier(columnName);
    int i = 0;
    for (PColumn pkCol : pkCols) {
        if (pkCol.getName().getString().equals(normalizedColumnName)) {
            return i;
        }
        i++;
    }
    return -1;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Aggregations

PColumn (org.apache.phoenix.schema.PColumn)87 PTable (org.apache.phoenix.schema.PTable)47 Expression (org.apache.phoenix.expression.Expression)20 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)20 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)17 ArrayList (java.util.ArrayList)16 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)16 ColumnRef (org.apache.phoenix.schema.ColumnRef)16 PName (org.apache.phoenix.schema.PName)16 TableRef (org.apache.phoenix.schema.TableRef)16 PTableKey (org.apache.phoenix.schema.PTableKey)15 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)14 SQLException (java.sql.SQLException)13 PColumnFamily (org.apache.phoenix.schema.PColumnFamily)13 PColumnImpl (org.apache.phoenix.schema.PColumnImpl)12 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)11 Test (org.junit.Test)11 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 Hint (org.apache.phoenix.parse.HintNode.Hint)10 List (java.util.List)9