Search in sources :

Example 31 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class TraceReader method addCustomAnnotations.

private String addCustomAnnotations(String logLine) throws SQLException {
    if (conn.isWrapperFor(PhoenixConnection.class)) {
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        logLine = LogUtil.addCustomAnnotations(logLine, phxConn);
    }
    return logLine;
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection)

Example 32 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class UpgradeUtil method upgradeDescVarLengthRowKeys.

/**
     * Upgrade tables and their indexes due to a bug causing descending row keys to have a row key that
     * prevents them from being sorted correctly (PHOENIX-2067).
     */
public static void upgradeDescVarLengthRowKeys(PhoenixConnection conn, List<String> tablesToUpgrade, boolean bypassUpgrade) throws SQLException {
    if (tablesToUpgrade.isEmpty()) {
        return;
    }
    List<PTable> tablesNeedingUpgrading = Lists.newArrayListWithExpectedSize(tablesToUpgrade.size());
    List<String> invalidTables = Lists.newArrayListWithExpectedSize(tablesToUpgrade.size());
    for (String fullTableName : tablesToUpgrade) {
        PTable table = PhoenixRuntime.getTable(conn, fullTableName);
        if (isInvalidTableToUpgrade(table)) {
            invalidTables.add(fullTableName);
        } else {
            tablesNeedingUpgrading.add(table);
        }
    }
    if (!invalidTables.isEmpty()) {
        StringBuilder buf = new StringBuilder("Only physical tables should be upgraded as their views and indexes will be updated with them: ");
        for (String fullTableName : invalidTables) {
            buf.append(fullTableName);
            buf.append(' ');
        }
        throw new SQLException(buf.toString());
    }
    PhoenixConnection upgradeConn = new PhoenixConnection(conn, true, true);
    try {
        upgradeConn.setAutoCommit(true);
        for (PTable table : tablesNeedingUpgrading) {
            boolean wasUpgraded = false;
            if (!table.rowKeyOrderOptimizable()) {
                wasUpgraded = true;
                upgradeDescVarLengthRowKeys(upgradeConn, conn, table.getSchemaName().getString(), table.getTableName().getString(), true, bypassUpgrade);
            }
            // Upgrade global indexes
            for (PTable index : table.getIndexes()) {
                if (!index.rowKeyOrderOptimizable() && index.getIndexType() != IndexType.LOCAL) {
                    wasUpgraded = true;
                    upgradeDescVarLengthRowKeys(upgradeConn, conn, index.getSchemaName().getString(), index.getTableName().getString(), false, bypassUpgrade);
                }
            }
            String sharedViewIndexName = Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(table.getName().getBytes()));
            // Upgrade view indexes
            wasUpgraded |= upgradeSharedIndex(upgradeConn, conn, sharedViewIndexName, bypassUpgrade);
            String sharedLocalIndexName = Bytes.toString(MetaDataUtil.getLocalIndexPhysicalName(table.getName().getBytes()));
            // Upgrade local indexes
            wasUpgraded |= upgradeSharedIndex(upgradeConn, conn, sharedLocalIndexName, bypassUpgrade);
            if (!wasUpgraded) {
                System.out.println("Upgrade not required for this table or its indexes: " + table.getName().getString());
            }
        }
    } finally {
        upgradeConn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) PTable(org.apache.phoenix.schema.PTable)

Example 33 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class UpgradeUtil method disableViewIndexes.

public static PhoenixConnection disableViewIndexes(PhoenixConnection connParam) throws SQLException, IOException, InterruptedException, TimeoutException {
    Properties props = PropertiesUtil.deepCopy(connParam.getClientInfo());
    Long originalScn = null;
    String str = props.getProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB);
    if (str != null) {
        originalScn = Long.valueOf(str);
    }
    // don't use the passed timestamp as scn because we want to query all view indexes up to now.
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(HConstants.LATEST_TIMESTAMP));
    Set<String> physicalTables = new HashSet<>();
    SQLException sqlEx = null;
    PhoenixConnection globalConnection = null;
    PhoenixConnection toReturn = null;
    try {
        globalConnection = new PhoenixConnection(connParam, connParam.getQueryServices(), props);
        String tenantId = null;
        try (HBaseAdmin admin = globalConnection.getQueryServices().getAdmin()) {
            String fetchViewIndexes = "SELECT " + TENANT_ID + ", " + TABLE_SCHEM + ", " + TABLE_NAME + ", " + DATA_TABLE_NAME + " FROM " + SYSTEM_CATALOG_NAME + " WHERE " + VIEW_INDEX_ID + " IS NOT NULL";
            String disableIndexDDL = "ALTER INDEX %s ON %s DISABLE";
            try (ResultSet rs = globalConnection.createStatement().executeQuery(fetchViewIndexes)) {
                while (rs.next()) {
                    tenantId = rs.getString(1);
                    String indexSchema = rs.getString(2);
                    String indexName = rs.getString(3);
                    String viewName = rs.getString(4);
                    String fullIndexName = SchemaUtil.getTableName(indexSchema, indexName);
                    String fullViewName = SchemaUtil.getTableName(indexSchema, viewName);
                    PTable viewPTable = null;
                    // Users would need to rebuild the view indexes. 
                    if (tenantId != null && !tenantId.isEmpty()) {
                        Properties newProps = PropertiesUtil.deepCopy(globalConnection.getClientInfo());
                        newProps.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
                        PTable indexPTable = null;
                        try (PhoenixConnection tenantConnection = new PhoenixConnection(globalConnection, globalConnection.getQueryServices(), newProps)) {
                            viewPTable = PhoenixRuntime.getTable(tenantConnection, fullViewName);
                            tenantConnection.createStatement().execute(String.format(disableIndexDDL, indexName, fullViewName));
                            indexPTable = PhoenixRuntime.getTable(tenantConnection, fullIndexName);
                        }
                        int offset = indexPTable.getBucketNum() != null ? 1 : 0;
                        // positions are stored 1 based
                        int existingTenantIdPosition = ++offset;
                        int existingViewIdxIdPosition = ++offset;
                        int newTenantIdPosition = existingViewIdxIdPosition;
                        int newViewIdxPosition = existingTenantIdPosition;
                        String tenantIdColumn = indexPTable.getColumns().get(existingTenantIdPosition - 1).getName().getString();
                        int index = 0;
                        String updatePosition = "UPSERT INTO " + SYSTEM_CATALOG_NAME + " ( " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + COLUMN_NAME + "," + COLUMN_FAMILY + "," + ORDINAL_POSITION + ") SELECT " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + COLUMN_NAME + "," + COLUMN_FAMILY + "," + "?" + " FROM " + SYSTEM_CATALOG_NAME + " WHERE " + TENANT_ID + " = ? " + " AND " + TABLE_NAME + " = ? " + " AND " + (indexSchema == null ? TABLE_SCHEM + " IS NULL" : TABLE_SCHEM + " = ? ") + " AND " + COLUMN_NAME + " = ? ";
                        // update view index position
                        try (PreparedStatement s = globalConnection.prepareStatement(updatePosition)) {
                            index = 0;
                            s.setInt(++index, newViewIdxPosition);
                            s.setString(++index, tenantId);
                            s.setString(++index, indexName);
                            if (indexSchema != null) {
                                s.setString(++index, indexSchema);
                            }
                            s.setString(++index, MetaDataUtil.getViewIndexIdColumnName());
                            s.executeUpdate();
                        }
                        // update tenant id position
                        try (PreparedStatement s = globalConnection.prepareStatement(updatePosition)) {
                            index = 0;
                            s.setInt(++index, newTenantIdPosition);
                            s.setString(++index, tenantId);
                            s.setString(++index, indexName);
                            if (indexSchema != null) {
                                s.setString(++index, indexSchema);
                            }
                            s.setString(++index, tenantIdColumn);
                            s.executeUpdate();
                        }
                        globalConnection.commit();
                    } else {
                        viewPTable = PhoenixRuntime.getTable(globalConnection, fullViewName);
                        globalConnection.createStatement().execute(String.format(disableIndexDDL, indexName, fullViewName));
                    }
                    String indexPhysicalTableName = MetaDataUtil.getViewIndexTableName(viewPTable.getPhysicalName().getString());
                    if (physicalTables.add(indexPhysicalTableName)) {
                        final TableName tableName = TableName.valueOf(indexPhysicalTableName);
                        admin.disableTable(tableName);
                        admin.truncateTable(tableName, false);
                    }
                }
            }
        }
        if (originalScn != null) {
            props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(originalScn));
        }
        toReturn = new PhoenixConnection(globalConnection, globalConnection.getQueryServices(), props);
    } catch (SQLException e) {
        sqlEx = e;
    } finally {
        sqlEx = closeConnection(connParam, sqlEx);
        sqlEx = closeConnection(globalConnection, sqlEx);
        if (sqlEx != null) {
            throw sqlEx;
        }
    }
    return toReturn;
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) PTable(org.apache.phoenix.schema.PTable) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableName(org.apache.hadoop.hbase.TableName) PLong(org.apache.phoenix.schema.types.PLong) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Example 34 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class CreateTableCompilerTest method testCreateTableWithDuplicateColumns.

@Test
public void testCreateTableWithDuplicateColumns() throws SQLException {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    String ddl = "CREATE TABLE T (ID INTEGER PRIMARY KEY, DUPE INTEGER, DUPE INTEGER)";
    try {
        conn.createStatement().execute(ddl);
        fail();
    } catch (ColumnAlreadyExistsException e) {
        assertEquals("DUPE", e.getColumnName());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ColumnAlreadyExistsException(org.apache.phoenix.schema.ColumnAlreadyExistsException) Properties(java.util.Properties) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest) Test(org.junit.Test)

Example 35 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class LimitCompilerTest method compileStatement.

private static QueryPlan compileStatement(String query, List<Object> binds) throws SQLException {
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
    TestUtil.bindParams(pstmt, binds);
    return pstmt.compileQuery();
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement)

Aggregations

PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)216 Test (org.junit.Test)111 Connection (java.sql.Connection)57 Properties (java.util.Properties)52 PTable (org.apache.phoenix.schema.PTable)52 Scan (org.apache.hadoop.hbase.client.Scan)51 PhoenixPreparedStatement (org.apache.phoenix.jdbc.PhoenixPreparedStatement)48 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)47 PTableKey (org.apache.phoenix.schema.PTableKey)43 ResultSet (java.sql.ResultSet)41 PreparedStatement (java.sql.PreparedStatement)40 SQLException (java.sql.SQLException)40 Filter (org.apache.hadoop.hbase.filter.Filter)29 SkipScanFilter (org.apache.phoenix.filter.SkipScanFilter)29 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)28 TestUtil.multiEncodedKVFilter (org.apache.phoenix.util.TestUtil.multiEncodedKVFilter)28 TestUtil.singleKVFilter (org.apache.phoenix.util.TestUtil.singleKVFilter)28 Statement (java.sql.Statement)19 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)17 PColumn (org.apache.phoenix.schema.PColumn)17