Search in sources :

Example 81 with PTableKey

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

the class ConnectionQueryServicesImpl method addColumnQualifierColumn.

// Special method for adding the column qualifier column for 4.10.
private PhoenixConnection addColumnQualifierColumn(PhoenixConnection oldMetaConnection, Long timestamp) throws SQLException {
    Properties props = PropertiesUtil.deepCopy(oldMetaConnection.getClientInfo());
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(timestamp));
    // Cannot go through DriverManager or you end up in an infinite loop because it'll call init again
    PhoenixConnection metaConnection = new PhoenixConnection(oldMetaConnection, this, props);
    PTable sysCatalogPTable = metaConnection.getTable(new PTableKey(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME));
    int numColumns = sysCatalogPTable.getColumns().size();
    try (PreparedStatement mutateTable = metaConnection.prepareStatement(MetaDataClient.MUTATE_TABLE)) {
        mutateTable.setString(1, null);
        mutateTable.setString(2, SYSTEM_CATALOG_SCHEMA);
        mutateTable.setString(3, SYSTEM_CATALOG_TABLE);
        mutateTable.setString(4, PTableType.SYSTEM.getSerializedValue());
        mutateTable.setLong(5, sysCatalogPTable.getSequenceNumber() + 1);
        mutateTable.setInt(6, numColumns + 1);
        mutateTable.execute();
    }
    List<Mutation> tableMetadata = new ArrayList<>();
    tableMetadata.addAll(metaConnection.getMutationState().toMutations(metaConnection.getSCN()).next().getSecond());
    metaConnection.rollback();
    PColumn column = new PColumnImpl(PNameFactory.newName("COLUMN_QUALIFIER"), PNameFactory.newName(DEFAULT_COLUMN_FAMILY_NAME), PVarbinary.INSTANCE, null, null, true, numColumns, SortOrder.ASC, null, null, false, null, false, false, Bytes.toBytes("COLUMN_QUALIFIER"));
    String upsertColumnMetadata = "UPSERT INTO " + SYSTEM_CATALOG_SCHEMA + ".\"" + SYSTEM_CATALOG_TABLE + "\"( " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + COLUMN_NAME + "," + COLUMN_FAMILY + "," + DATA_TYPE + "," + NULLABLE + "," + COLUMN_SIZE + "," + DECIMAL_DIGITS + "," + ORDINAL_POSITION + "," + SORT_ORDER + "," + DATA_TABLE_NAME + "," + ARRAY_SIZE + "," + VIEW_CONSTANT + "," + IS_VIEW_REFERENCED + "," + PK_NAME + "," + KEY_SEQ + "," + COLUMN_DEF + "," + IS_ROW_TIMESTAMP + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    try (PreparedStatement colUpsert = metaConnection.prepareStatement(upsertColumnMetadata)) {
        colUpsert.setString(1, null);
        colUpsert.setString(2, SYSTEM_CATALOG_SCHEMA);
        colUpsert.setString(3, SYSTEM_CATALOG_TABLE);
        colUpsert.setString(4, "COLUMN_QUALIFIER");
        colUpsert.setString(5, DEFAULT_COLUMN_FAMILY);
        colUpsert.setInt(6, column.getDataType().getSqlType());
        colUpsert.setInt(7, ResultSetMetaData.columnNullable);
        colUpsert.setNull(8, Types.INTEGER);
        colUpsert.setNull(9, Types.INTEGER);
        colUpsert.setInt(10, sysCatalogPTable.getBucketNum() != null ? numColumns : (numColumns + 1));
        colUpsert.setInt(11, SortOrder.ASC.getSystemValue());
        colUpsert.setString(12, null);
        colUpsert.setNull(13, Types.INTEGER);
        colUpsert.setBytes(14, null);
        colUpsert.setBoolean(15, false);
        colUpsert.setString(16, sysCatalogPTable.getPKName() == null ? null : sysCatalogPTable.getPKName().getString());
        colUpsert.setNull(17, Types.SMALLINT);
        colUpsert.setNull(18, Types.VARCHAR);
        colUpsert.setBoolean(19, false);
        colUpsert.execute();
    }
    tableMetadata.addAll(metaConnection.getMutationState().toMutations(metaConnection.getSCN()).next().getSecond());
    metaConnection.rollback();
    metaConnection.getQueryServices().addColumn(tableMetadata, sysCatalogPTable, Collections.<String, List<Pair<String, Object>>>emptyMap(), Collections.<String>emptySet(), Lists.newArrayList(column));
    metaConnection.removeTable(null, SYSTEM_CATALOG_NAME, null, timestamp);
    ConnectionQueryServicesImpl.this.removeTable(null, SYSTEM_CATALOG_NAME, null, timestamp);
    clearCache();
    return metaConnection;
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PColumnImpl(org.apache.phoenix.schema.PColumnImpl) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) PTable(org.apache.phoenix.schema.PTable) PTinyint(org.apache.phoenix.schema.types.PTinyint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) MultiRowMutationEndpoint(org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint) PColumn(org.apache.phoenix.schema.PColumn) Mutation(org.apache.hadoop.hbase.client.Mutation) PTableKey(org.apache.phoenix.schema.PTableKey) Pair(org.apache.hadoop.hbase.util.Pair)

Example 82 with PTableKey

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

the class SkipScanBigFilterTest method testIntersect.

private void testIntersect(byte[][] regionBoundaries, byte[][] guidePosts) throws Exception {
    String ddl = "create table PERF.BIG_OLAP_DOC (\n" + "client_id                           integer not null\n" + ",customer_id                        integer\n" + ",time_id                            integer not null\n" + ",conversion_type_id                 integer not null\n" + ",device_type                        varchar(16)\n" + ",keyword_id                         bigint not null\n" + ",creative_id                        bigint not null\n" + ",placement_id                       bigint not null\n" + ",product_target_id                  bigint not null\n" + ",network                            varchar(7)\n" + ",impressions                        decimal(18, 4)\n" + ",publisher_clicks                   decimal(18, 4)\n" + ",publisher_cost                     decimal(18, 4)\n" + ",conversions                        decimal(18, 4)\n" + ",revenue                            decimal(18, 4)\n" + "    constraint perf_fact_pk primary key (client_id, time_id, conversion_type_id, device_type, keyword_id, creative_id, placement_id, product_target_id))SALT_BUCKETS=10";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    StringBuilder ddlBuf = new StringBuilder(ddl + " SPLIT ON (");
    for (int i = 0; i < regionBoundaries.length; i++) {
        ddlBuf.append("?,");
    }
    ddlBuf.setCharAt(ddlBuf.length() - 1, ')');
    ;
    PreparedStatement stmt = conn.prepareStatement(ddlBuf.toString());
    int i = 1;
    for (byte[] boundary : regionBoundaries) {
        stmt.setBytes(i++, boundary);
    }
    stmt.execute();
    final PTable table = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, "PERF.BIG_OLAP_DOC"));
    GuidePostsInfoBuilder gpWriter = new GuidePostsInfoBuilder();
    for (byte[] gp : guidePosts) {
        gpWriter.trackGuidePost(new ImmutableBytesWritable(gp), 1000, 0, 0);
    }
    GuidePostsInfo info = gpWriter.build();
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    pConn.addTable(table, System.currentTimeMillis());
    ((ConnectionlessQueryServicesImpl) pConn.getQueryServices()).addTableStats(new GuidePostsKey(table.getName().getBytes(), QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES), info);
    String query = "SELECT count(1) cnt,\n" + "       coalesce(SUM(impressions), 0.0) AS \"impressions\",\n" + "       coalesce(SUM(publisher_clicks), 0.0) AS \"pub_clicks\",\n" + "       coalesce(SUM(publisher_cost), 0.0) AS \"pub_cost\",\n" + "       coalesce(SUM(conversions), 0.0) AS \"conversions\",\n" + "       coalesce(SUM(revenue), 0.0) AS \"revenue\" \n" + "  FROM perf.big_olap_doc\n" + " WHERE time_id between 3000 and 3700\n" + "   AND network in ('SEARCH')\n" + "   AND conversion_type_id = 1\n" + "   AND client_id = 10724\n" + "   AND device_type in ('MOBILE','DESKTOP','OTHER','TABLET')\n" + "   AND keyword_id in (\n" + "613214369, 613217307, 613247509, 613248897, 613250382, 613250387, 613252322, 613260252, 613261753, 613261754, 613261759, \n" + "613261770, 613261873, 613261884, 613261885, 613261888, 613261889, 613261892, 613261897, 613261913, 613261919, 613261927, \n" + "614496021, 843606367, 843606967, 843607021, 843607033, 843607089, 1038731600, 1038731672, 1038731673, 1038731675, \n" + "1038731684, 1038731693, 1046990487, 1046990488, 1046990499, 1046990505, 1046990506, 1049724722, 1051109548, 1051311275, \n" + "1051311904, 1060574377, 1060574395, 1060574506, 1060574562, 1115915938, 1115915939, 1115915941, 1116310571, 1367495544, \n" + "1367495545, 1367497297, 1367497298, 1367497299, 1367497300, 1367497303, 1367497313, 1367497813, 1367497816, 1367497818, \n" + "1367497821, 1367497822, 1367497823, 1624976423, 1624976451, 1624976457, 3275636061, 3275640505, 3275645765, 3275645807, \n" + "3275649138, 3275651456, 3275651460, 3275651478, 3275651479, 3275654566, 3275654568, 3275654570, 3275654575, 3275659612, \n" + "3275659616, 3275659620, 3275668880, 3275669693, 3275675627, 3275675634, 3275677479, 3275677504, 3275678855, 3275679524, \n" + "3275679532, 3275680014, 3275682307, 3275682308, 3275682309, 3275682310, 3275682420, 3275682423, 3275682436, 3275682448, \n" + "3275682460, 3275682462, 3275682474, 3275684831, 3275688903, 3275694023, 3275694025, 3275694027, 3275695054, 3275695056,\n" + "3275695062, 3275699512, 3275699514, 3275699518, 3275701682, 3275701683, 3275701685, 3275701688, 3275703633, 3275703634, \n" + "3275703635, 3275703636, 3275703638, 3275703639, 3275704860, 3275704861, 3275764577, 3275797149, 3275798566, 3275798567, \n" + "3275798568, 3275798592, 3275931147, 3275942728, 3275945337, 3275945338, 3275945339, 3275945340, 3275945342, 3275945344, \n" + "3275946319, 3275946322, 3275946324, 3275946643, 3275949495, 3275949498, 3275949500, 3275950250, 3275955128, 3275955129, \n" + "3275955130, 3427017435, 3427017450, 3438304254, 3438304257, 3447068169, 3505227849, 3505227890, 3505556908, 3506351285, \n" + "3506351389, 3506351398, 3506351468, 3510037138, 3510038610, 3545590644, 3545594378, 3545595073, 3545595318, 3545595506, \n" + "3545597841, 3545598818, 3545599658, 3545599663, 3545601215, 3556080898, 3556080980, 3556080999, 3556081323, 3565122663, \n" + "3565122679, 3565122801, 3565122858, 3565122908, 3565122929, 3565122952, 3565122984, 3565123028, 3565123047, 3565123048, \n" + "3565123203, 3565123230, 3949988054, 3949988056, 3949988070, 3972992248, 3972992252, 3972992254, 3972992257, 3972992263, \n" + "3972992267, 3972992268, 3972992269, 3972992270, 3972992274, 3972992275, 3972992277, 3972992281, 3972992293, 3972992298, \n" + "3972992299, 3972992305, 3972992307, 3972992313, 3972992316, 3972992322, 3972992338, 3978471261, 3978471272, 4266318185, \n" + "4298107404, 4308853119, 4308853123, 4308853500, 4451174646, 4451174656, 4451174701, 4569827278, 4569827284, 4569827287, \n" + "4569827379, 4569827523, 4569827524, 4896589676, 4979049725, 5054587609, 5136433884, 5362640372, 5393109964, 5393405364, \n" + "5393405365, 5393405620, 5393405625, 5393405675, 5393405677, 5393405858, 5393405970)";
    QueryPlan plan = conn.createStatement().unwrap(PhoenixStatement.class).compileQuery(query);
    plan.iterator();
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) GuidePostsInfoBuilder(org.apache.phoenix.schema.stats.GuidePostsInfoBuilder) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) GuidePostsKey(org.apache.phoenix.schema.stats.GuidePostsKey) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) GuidePostsInfo(org.apache.phoenix.schema.stats.GuidePostsInfo) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) QueryPlan(org.apache.phoenix.compile.QueryPlan) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) PTable(org.apache.phoenix.schema.PTable) ConnectionlessQueryServicesImpl(org.apache.phoenix.query.ConnectionlessQueryServicesImpl) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 83 with PTableKey

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

the class AlterTableIT method testAddingColumnsToTablesAndViews.

@Test
public void testAddingColumnsToTablesAndViews() throws Exception {
    String schemaName = generateUniqueName();
    String baseTableName = generateUniqueName();
    String viewName = generateUniqueName();
    String fullTableName = schemaName + "." + baseTableName;
    String fullViewName = schemaName + "." + viewName;
    Properties props = new Properties();
    props.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(true));
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute("CREATE SCHEMA " + schemaName);
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        conn.createStatement().execute("CREATE TABLE " + fullTableName + " (" + " ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)" + " ) " + tableDDLOptions);
        PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        long initBaseTableSeqNumber = baseTable.getSequenceNumber();
        // Add a column to the base table and see if the client and server metadata is updated correctly
        String alterDDL = "ALTER TABLE " + fullTableName + " ADD COL3 VARCHAR PRIMARY KEY, COL4 INTEGER, COL5 VARCHAR, B.COL6 DECIMAL (10, 2)";
        conn.createStatement().execute(alterDDL);
        // assert that the client side cache is updated.
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        EncodedCQCounter encodedCqCounter = baseTable.getEncodedCQCounter();
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 3) : null, encodedCqCounter.getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata is updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
        // assert that the server side metadata for columns is updated correctly.
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL4", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL5", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue("B", "COL6", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2);
        long baseTableSeqNumBeforeAddingChildCols = initBaseTableSeqNumber + 1;
        assertSequenceNumber(schemaName, baseTableName, baseTableSeqNumBeforeAddingChildCols);
        // Create a view
        String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName;
        conn.createStatement().execute(viewDDL);
        // assert that the server side metadata is updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 5);
        // assert that the server side metadata for columns is updated correctly.
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
        assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 4);
        // for encoded columns creating a view that adds its own columns should increment the base table's sequence number too.
        assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 2 : baseTableSeqNumBeforeAddingChildCols);
        // Add column to the view
        viewDDL = "ALTER VIEW " + fullViewName + " ADD VIEW_COL3 DECIMAL(10, 2), A.VIEW_COL4 VARCHAR, B.VIEW_COL5 INTEGER";
        conn.createStatement().execute(viewDDL);
        // assert that the client cache for the base table is updated
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        encodedCqCounter = baseTable.getEncodedCQCounter();
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 8) : null, encodedCqCounter.getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert client cache for view
        PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));
        encodedCqCounter = view.getEncodedCQCounter();
        assertNull("A view should always have the column qualifier counter as null", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata for the base table and the view is also updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 8);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
        assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 4);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL3", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 5);
        assertEncodedCQValue("A", "VIEW_COL4", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 6);
        assertEncodedCQValue("B", "VIEW_COL5", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 7);
        // adding a column to the should increment the base table's sequence number too since we update the cq counters for column families.
        assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 3 : baseTableSeqNumBeforeAddingChildCols);
        assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 1);
        // Add column to the base table which doesn't already exist in the view.
        alterDDL = "ALTER TABLE " + fullTableName + " ADD COL10 VARCHAR, A.COL11 INTEGER";
        conn.createStatement().execute(alterDDL);
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        // assert that the client cache for the base table is updated
        encodedCqCounter = baseTable.getEncodedCQCounter();
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 10) : null, encodedCqCounter.getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert client cache for view
        view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));
        encodedCqCounter = view.getEncodedCQCounter();
        assertNull("A view should always have the column qualifier counter as null", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata for the base table and the view is also updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 10));
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL10", schemaName, viewName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 8));
        assertEncodedCQValue("A", "COL11", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 9);
        assertSequenceNumber(schemaName, baseTableName, columnEncoded ? initBaseTableSeqNumber + 4 : initBaseTableSeqNumber + 2);
        assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 2);
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) EncodedCQCounter(org.apache.phoenix.schema.PTable.EncodedCQCounter) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 84 with PTableKey

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

the class AlterTableIT method testDeclaringColumnAsRowTimestamp.

@Test
public void testDeclaringColumnAsRowTimestamp() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute("CREATE TABLE " + dataTableFullName + " (PK1 DATE NOT NULL, PK2 VARCHAR NOT NULL, KV1 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1 ROW_TIMESTAMP, PK2)) " + tableDDLOptions);
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable table = phxConn.getTable(new PTableKey(phxConn.getTenantId(), dataTableFullName));
        // Assert that the column shows up as row time stamp in the cache.
        assertTrue(table.getColumnForColumnName("PK1").isRowTimestamp());
        assertFalse(table.getColumnForColumnName("PK2").isRowTimestamp());
        assertIsRowTimestampSet(schemaName, dataTableName, "PK1");
        String dataTableName2 = BaseTest.generateUniqueName();
        String dataTableFullName2 = SchemaUtil.getTableName(schemaName, dataTableName2);
        conn.createStatement().execute("CREATE IMMUTABLE TABLE " + dataTableFullName2 + " (PK1 VARCHAR, PK2 DATE PRIMARY KEY ROW_TIMESTAMP, KV1 VARCHAR, KV2 INTEGER)");
        table = phxConn.getTable(new PTableKey(phxConn.getTenantId(), dataTableFullName2));
        // Assert that the column shows up as row time stamp in the cache.
        assertFalse(table.getColumnForColumnName("PK1").isRowTimestamp());
        assertTrue(table.getColumnForColumnName("PK2").isRowTimestamp());
        assertIsRowTimestampSet(schemaName, dataTableName2, "PK2");
        // Create an index on a table has a row time stamp pk column. The column should show up as a row time stamp column for the index too.
        conn.createStatement().execute("CREATE INDEX " + indexTableName + "  ON " + dataTableFullName2 + " (KV1) include (KV2)");
        PTable indexTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), indexTableFullName));
        String indexColName = IndexUtil.getIndexColumnName(table.getColumnForColumnName("PK2"));
        // Assert that the column shows up as row time stamp in the cache.
        assertTrue(indexTable.getColumnForColumnName(indexColName).isRowTimestamp());
        assertIsRowTimestampSet(schemaName, indexTableName, indexColName);
        String viewTableName2 = dataTableName2 + "_VIEW";
        String viewTableFullName2 = SchemaUtil.getTableName(schemaName, viewTableName2);
        // Creating a view with a row_timestamp column in its pk constraint is not allowed
        try {
            conn.createStatement().execute("CREATE VIEW " + viewTableFullName2 + " (KV3 VARCHAR, KV4 DATE, KV5 INTEGER, CONSTRAINT PK PRIMARY KEY (KV3, KV4 ROW_TIMESTAMP) ) AS SELECT * FROM " + dataTableFullName2);
            fail("Creating a view with a row_timestamp column in its pk constraint is not allowed");
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.ROWTIMESTAMP_NOT_ALLOWED_ON_VIEW.getErrorCode(), e.getErrorCode());
        }
        // Make sure that the base table column declared as row_timestamp is also row_timestamp for view
        conn.createStatement().execute("CREATE VIEW " + viewTableFullName2 + " (KV3 VARCHAR, KV4 VARCHAR, KV5 INTEGER, CONSTRAINT PK PRIMARY KEY (KV3, KV4) ) AS SELECT * FROM " + dataTableFullName2);
        PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), viewTableFullName2));
        assertNotNull(view.getPKColumn("PK2"));
        assertTrue(view.getPKColumn("PK2").isRowTimestamp());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 85 with PTableKey

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

the class SetPropertyIT method testSetForSomePhoenixTablePropertiesOnViewsAllowed.

@Test
public void testSetForSomePhoenixTablePropertiesOnViewsAllowed() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String ddl = "CREATE TABLE " + dataTableFullName + " (\n" + "ID1 VARCHAR(15) NOT NULL,\n" + "ID2 VARCHAR(15) NOT NULL,\n" + "CREATED_DATE DATE,\n" + "CREATION_TIME BIGINT,\n" + "LAST_USED DATE,\n" + "CONSTRAINT PK PRIMARY KEY (ID1, ID2)) " + generateDDLOptions("SALT_BUCKETS = 8");
    Connection conn1 = DriverManager.getConnection(getUrl(), props);
    conn1.createStatement().execute(ddl);
    String viewFullName = SchemaUtil.getTableName(schemaName, generateUniqueName());
    ddl = "CREATE VIEW " + viewFullName + " AS SELECT * FROM " + dataTableFullName + " WHERE CREATION_TIME = 1";
    conn1.createStatement().execute(ddl);
    ddl = "ALTER VIEW " + viewFullName + " SET UPDATE_CACHE_FREQUENCY = 10";
    conn1.createStatement().execute(ddl);
    conn1.createStatement().execute("SELECT * FROM " + viewFullName);
    PhoenixConnection pconn = conn1.unwrap(PhoenixConnection.class);
    assertEquals(10, pconn.getTable(new PTableKey(pconn.getTenantId(), viewFullName)).getUpdateCacheFrequency());
    ddl = "ALTER VIEW " + viewFullName + " SET UPDATE_CACHE_FREQUENCY = 20";
    conn1.createStatement().execute(ddl);
    conn1.createStatement().execute("SELECT * FROM " + viewFullName);
    pconn = conn1.unwrap(PhoenixConnection.class);
    assertEquals(20, pconn.getTable(new PTableKey(pconn.getTenantId(), viewFullName)).getUpdateCacheFrequency());
    assertImmutableRows(conn1, viewFullName, false);
    ddl = "ALTER VIEW " + viewFullName + " SET DISABLE_WAL = TRUE";
    try {
        conn1.createStatement().execute(ddl);
        fail();
    } catch (SQLException e) {
        assertEquals(SQLExceptionCode.VIEW_WITH_PROPERTIES.getErrorCode(), e.getErrorCode());
    }
    ddl = "ALTER VIEW " + viewFullName + " SET THROW_INDEX_WRITE_FAILURE = FALSE";
    try {
        conn1.createStatement().execute(ddl);
        fail();
    } catch (SQLException e) {
        assertEquals(SQLExceptionCode.VIEW_WITH_PROPERTIES.getErrorCode(), e.getErrorCode());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test)

Aggregations

PTableKey (org.apache.phoenix.schema.PTableKey)89 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)77 PTable (org.apache.phoenix.schema.PTable)55 Connection (java.sql.Connection)48 Test (org.junit.Test)40 Properties (java.util.Properties)23 ResultSet (java.sql.ResultSet)14 PColumn (org.apache.phoenix.schema.PColumn)14 PreparedStatement (java.sql.PreparedStatement)13 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)12 SQLException (java.sql.SQLException)11 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)11 PMetaData (org.apache.phoenix.schema.PMetaData)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)9 PName (org.apache.phoenix.schema.PName)9 BaseTest (org.apache.phoenix.query.BaseTest)8 Result (org.apache.hadoop.hbase.client.Result)7 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)7 Scan (org.apache.hadoop.hbase.client.Scan)7 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)6