Search in sources :

Example 1 with SequenceNotFoundException

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

the class ConnectionlessQueryServicesImpl method incrementSequences.

@Override
public void incrementSequences(List<SequenceAllocation> sequenceAllocations, long timestamp, long[] values, SQLException[] exceptions) throws SQLException {
    int i = 0;
    for (SequenceAllocation sequenceAllocation : sequenceAllocations) {
        SequenceKey key = sequenceAllocation.getSequenceKey();
        SequenceInfo info = sequenceMap.get(key);
        if (info == null) {
            exceptions[i] = new SequenceNotFoundException(key.getSchemaName(), key.getSequenceName());
        } else {
            boolean increaseSeq = info.incrementBy > 0;
            if (info.limitReached) {
                SQLExceptionCode code = increaseSeq ? SQLExceptionCode.SEQUENCE_VAL_REACHED_MAX_VALUE : SQLExceptionCode.SEQUENCE_VAL_REACHED_MIN_VALUE;
                exceptions[i] = new SQLExceptionInfo.Builder(code).build().buildException();
            } else {
                values[i] = info.sequenceValue;
                info.sequenceValue += info.incrementBy * info.cacheSize;
                info.limitReached = SequenceUtil.checkIfLimitReached(info);
                if (info.limitReached && info.cycle) {
                    info.sequenceValue = increaseSeq ? info.minValue : info.maxValue;
                    info.limitReached = false;
                }
            }
        }
        i++;
    }
    i = 0;
    for (SQLException e : exceptions) {
        if (e != null) {
            sequenceMap.remove(sequenceAllocations.get(i).getSequenceKey());
        }
        i++;
    }
}
Also used : SQLExceptionCode(org.apache.phoenix.exception.SQLExceptionCode) SequenceKey(org.apache.phoenix.schema.SequenceKey) SequenceInfo(org.apache.phoenix.schema.SequenceInfo) SQLException(java.sql.SQLException) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) SequenceAllocation(org.apache.phoenix.schema.SequenceAllocation) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 2 with SequenceNotFoundException

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

the class SequenceIT method testDropCachedSeq.

private void testDropCachedSeq(boolean detectDeleteSeqInEval) throws Exception {
    String sequenceName = generateSequenceNameWithSchema();
    String alternateSequenceName = generateSequenceNameWithSchema();
    String tableName = generateTableNameWithSchema();
    conn.createStatement().execute("CREATE SEQUENCE " + sequenceName);
    conn.createStatement().execute("CREATE SEQUENCE " + alternateSequenceName + " START WITH 101");
    conn.createStatement().execute("CREATE TABLE " + tableName + " (k BIGINT NOT NULL PRIMARY KEY)");
    String stmtStr1a = "UPSERT INTO " + tableName + " VALUES(NEXT VALUE FOR  " + sequenceName + " )";
    PreparedStatement stmt1a = conn.prepareStatement(stmtStr1a);
    stmt1a.execute();
    stmt1a.execute();
    String stmtStr1b = "UPSERT INTO " + tableName + " VALUES(NEXT VALUE FOR " + alternateSequenceName + ")";
    PreparedStatement stmt1b = conn.prepareStatement(stmtStr1b);
    stmt1b.execute();
    stmt1b.execute();
    stmt1b.execute();
    conn.commit();
    Connection conn2 = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES));
    ;
    PreparedStatement stmt2 = conn2.prepareStatement("UPSERT INTO " + tableName + " VALUES(NEXT VALUE FOR " + alternateSequenceName + ")");
    stmt2.execute();
    conn2.commit();
    ResultSet rs = conn.createStatement().executeQuery("SELECT k FROM " + tableName + "");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertTrue(rs.next());
    assertEquals(2, rs.getInt(1));
    assertTrue(rs.next());
    assertEquals(101, rs.getInt(1));
    assertTrue(rs.next());
    assertEquals(102, rs.getInt(1));
    assertTrue(rs.next());
    assertEquals(103, rs.getInt(1));
    assertTrue(rs.next());
    assertEquals(104, rs.getInt(1));
    assertFalse(rs.next());
    conn.createStatement().execute("DROP SEQUENCE " + alternateSequenceName);
    stmt1a = conn.prepareStatement(stmtStr1a);
    stmt1a.execute();
    if (!detectDeleteSeqInEval) {
        // Will allocate new batch for " + sequenceName + " and get error for bar.bas, but ignore it
        stmt1a.execute();
    }
    stmt1b = conn.prepareStatement(stmtStr1b);
    try {
        // Will try to get new batch, but fail b/c sequence has been dropped
        stmt1b.execute();
        fail();
    } catch (SequenceNotFoundException e) {
    }
    conn2.close();
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) PreparedStatement(java.sql.PreparedStatement)

Example 3 with SequenceNotFoundException

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

the class TenantIdTypeIT method testMultiTenantTables.

@Test
public void testMultiTenantTables() throws Exception {
    // Verify we can create the table
    try (Connection conn = regularConnection(getUrl())) {
        conn.setAutoCommit(true);
        conn.createStatement().execute(ddl);
        try {
            conn.createStatement().execute(ddl);
            fail("Table with " + dataType + " tenantId not created correctly");
        } catch (TableAlreadyExistsException e) {
        // expected
        }
    }
    // Insert test data
    try (Connection conn = regularConnection(getUrl())) {
        conn.setAutoCommit(true);
        String query = "upsert into " + table + " values (" + tenantId + ", 1 , 'valid')";
        conn.createStatement().execute("upsert into " + table + " values (" + tenantId + ", 1 , 'valid')");
        conn.createStatement().execute("upsert into " + table + " values (" + otherTenantId + ", 2 , 'invalid')");
    }
    // Make sure access is properly restricted and add some tenant-specific schema
    try (Connection conn = tenantConnection(getUrl())) {
        conn.setAutoCommit(true);
        ResultSet rs = conn.createStatement().executeQuery("select * from " + table);
        assertTrue("Expected 1 row in result set", rs.next());
        assertEquals("valid", rs.getString(2));
        assertFalse("Expected 1 row in result set", rs.next());
        try {
            conn.createStatement().executeQuery("select * from " + table + " where tenantId = 2");
            fail("TenantId column not hidden on multi-tenant connection");
        } catch (SQLException ex) {
            assertEquals(SQLExceptionCode.COLUMN_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        conn.createStatement().execute("create view " + view + " as select * from " + table);
        conn.createStatement().execute("create sequence " + sequence + " start with 100");
    }
    // Try inserting data to the view
    try (Connection conn = tenantConnection(getUrl())) {
        conn.setAutoCommit(true);
        conn.createStatement().execute("upsert into " + view + " values ( next value for " + sequence + ", 'valid')");
    }
    // Try reading data from the view
    try (Connection conn = tenantConnection(getUrl())) {
        ResultSet rs = conn.createStatement().executeQuery("select * from " + view);
        assertTrue("Expected 2 rows in result set", rs.next());
        assertEquals("valid", rs.getString(2));
        assertTrue("Expected 2 rows in result set", rs.next());
        assertEquals("valid", rs.getString(2));
        assertFalse("Expected 2 rows in result set", rs.next());
    }
    // Make sure the tenant-specific schema is specific to that tenant
    try (Connection conn = regularConnection(getUrl())) {
        try {
            conn.createStatement().execute("upsert into " + table + " values (" + tenantId + ", next value for " + sequence + ", 'valid')");
            fail();
        } catch (SequenceNotFoundException ex) {
        }
        try {
            ResultSet rs = conn.createStatement().executeQuery("select * from " + view);
            fail();
        } catch (SQLException ex) {
            assertEquals(SQLExceptionCode.TABLE_UNDEFINED.getErrorCode(), ex.getErrorCode());
        }
    }
    if (dataType != "VARCHAR" && dataType != "CHAR(10)") {
        // Try setting up an invalid tenant-specific view
        try (Connection conn = inconvertibleConnection(getUrl())) {
            conn.setAutoCommit(true);
            conn.createStatement().execute("create view " + view + " as select * from " + table);
        }
        // Try inserting data to the invalid tenant-specific view
        try (Connection conn = inconvertibleConnection(getUrl())) {
            conn.setAutoCommit(true);
            try {
                conn.createStatement().execute("upsert into " + view + " values ( 3 , 'invalid')");
                fail();
            } catch (SQLException ex) {
                assertEquals(SQLExceptionCode.TENANTID_IS_OF_WRONG_TYPE.getErrorCode(), ex.getErrorCode());
            }
        }
        // Try reading data from the invalid tenant-specific view
        try (Connection conn = inconvertibleConnection(getUrl())) {
            try {
                ResultSet rs = conn.createStatement().executeQuery("select * from " + view);
                fail();
            } catch (SQLException ex) {
                assertEquals(SQLExceptionCode.TENANTID_IS_OF_WRONG_TYPE.getErrorCode(), ex.getErrorCode());
            }
        }
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) Test(org.junit.Test)

Example 4 with SequenceNotFoundException

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

the class AutoPartitionViewsIT method testValidateAttributes.

@Test
public void testValidateAttributes() throws SQLException {
    try (Connection conn = DriverManager.getConnection(getUrl());
        Connection viewConn1 = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : DriverManager.getConnection(getUrl());
        Connection viewConn2 = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : DriverManager.getConnection(getUrl())) {
        String tableName = generateUniqueName();
        String autoSeqName = generateUniqueName();
        try {
            String ddl = String.format("CREATE TABLE " + tableName + " (%s metricId VARCHAR, val1 DOUBLE, val2 DOUBLE CONSTRAINT PK PRIMARY KEY( %s metricId)) %s", isMultiTenant ? "tenantId VARCHAR, " : "", isMultiTenant ? "tenantId, " : "", String.format(tableDDLOptions, autoSeqName));
            conn.createStatement().execute(ddl);
            fail("Sequence value must be castable to the auto partition id column data type");
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.SEQUENCE_NOT_CASTABLE_TO_AUTO_PARTITION_ID_COLUMN.getErrorCode(), e.getErrorCode());
        }
        String ddl = String.format("CREATE TABLE " + tableName + " (%s metricId INTEGER NOT NULL, val1 DOUBLE, val2 DOUBLE CONSTRAINT PK PRIMARY KEY( %s metricId)) %s", isMultiTenant ? "tenantId VARCHAR NOT NULL, " : "", isMultiTenant ? "tenantId, " : "", String.format(tableDDLOptions, autoSeqName));
        conn.createStatement().execute(ddl);
        String baseViewName = generateUniqueName();
        String metricView1 = baseViewName + "_VIEW1";
        String metricView2 = baseViewName + "_VIEW2";
        String metricView3 = baseViewName + "_VIEW3";
        String metricView4 = baseViewName + "_VIEW4";
        try {
            viewConn1.createStatement().execute("CREATE VIEW " + metricView1 + "  AS SELECT * FROM " + tableName);
            fail("Auto-partition sequence must be created before view is created");
        } catch (SequenceNotFoundException e) {
        }
        conn.createStatement().execute("CREATE SEQUENCE " + autoSeqName + " start with " + (Integer.MAX_VALUE - 2) + " cache 1");
        viewConn1.createStatement().execute("CREATE VIEW " + metricView1 + " AS SELECT * FROM " + tableName + " WHERE val2=1.2");
        // create a view without a where clause
        viewConn1.createStatement().execute("CREATE VIEW " + metricView2 + " AS SELECT * FROM " + tableName);
        // create a view with a complex where clause
        viewConn1.createStatement().execute("CREATE VIEW " + metricView3 + " AS SELECT * FROM " + tableName + " WHERE val1=1.0 OR val2=2.0");
        try {
            viewConn1.createStatement().execute("CREATE VIEW " + metricView4 + " AS SELECT * FROM " + tableName);
            fail("Creating a view with a partition id that is too large should fail");
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.CANNOT_COERCE_AUTO_PARTITION_ID.getErrorCode(), e.getErrorCode());
        }
        if (isMultiTenant) {
            // load tables into cache
            viewConn1.createStatement().execute("SELECT * FROM " + metricView1);
            viewConn1.createStatement().execute("SELECT * FROM " + metricView2);
            viewConn1.createStatement().execute("SELECT * FROM " + metricView3);
        }
        PhoenixConnection pconn = viewConn1.unwrap(PhoenixConnection.class);
        PTable view1 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView1));
        PTable view2 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView2));
        PTable view3 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView3));
        // verify the view statement was set correctly
        String expectedViewStatement1 = "SELECT * FROM \"" + tableName + "\" WHERE VAL2 = 1.2 AND METRICID = " + (Integer.MAX_VALUE - 2);
        String expectedViewStatement2 = "SELECT * FROM \"" + tableName + "\" WHERE METRICID = " + (Integer.MAX_VALUE - 1);
        String expectedViewStatement3 = "SELECT * FROM \"" + tableName + "\" WHERE (VAL1 = 1.0 OR VAL2 = 2.0) AND METRICID = " + Integer.MAX_VALUE;
        assertEquals("Unexpected view statement", expectedViewStatement1, view1.getViewStatement());
        assertEquals("Unexpected view statement", expectedViewStatement2, view2.getViewStatement());
        assertEquals("Unexpected view statement", expectedViewStatement3, view3.getViewStatement());
        // verify isViewReferenced was set correctly
        int expectedParitionColIndex = isMultiTenant ? 1 : 0;
        PColumn partitionCol1 = view1.getColumns().get(expectedParitionColIndex);
        PColumn partitionCol2 = view2.getColumns().get(expectedParitionColIndex);
        PColumn partitionCol3 = view3.getColumns().get(expectedParitionColIndex);
        assertTrue("Partition column view referenced attribute should be true ", partitionCol1.isViewReferenced());
        assertTrue("Partition column view referenced attribute should be true ", partitionCol2.isViewReferenced());
        assertTrue("Partition column view referenced attribute should be true ", partitionCol3.isViewReferenced());
        // verify viewConstant was set correctly
        byte[] expectedPartition1 = new byte[Bytes.SIZEOF_INT + 1];
        PInteger.INSTANCE.toBytes(Integer.MAX_VALUE - 2, expectedPartition1, 0);
        byte[] expectedPartition2 = new byte[Bytes.SIZEOF_INT + 1];
        PInteger.INSTANCE.toBytes(Integer.MAX_VALUE - 1, expectedPartition2, 0);
        byte[] expectedPartition3 = new byte[Bytes.SIZEOF_INT + 1];
        PInteger.INSTANCE.toBytes(Integer.MAX_VALUE, expectedPartition3, 0);
        assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition1, partitionCol1.getViewConstant());
        assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition2, partitionCol2.getViewConstant());
        assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition3, partitionCol3.getViewConstant());
        // verify that the table was created correctly on the server
        viewConn2.createStatement().execute("SELECT * FROM " + metricView1);
        viewConn2.createStatement().execute("SELECT * FROM " + metricView2);
        viewConn2.createStatement().execute("SELECT * FROM " + metricView3);
        pconn = viewConn2.unwrap(PhoenixConnection.class);
        view1 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView1));
        view2 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView2));
        view3 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView3));
        // verify the view statement was set correctly
        assertEquals("Unexpected view statement", expectedViewStatement1, view1.getViewStatement());
        assertEquals("Unexpected view statement", expectedViewStatement2, view2.getViewStatement());
        assertEquals("Unexpected view statement", expectedViewStatement3, view3.getViewStatement());
        // verify isViewReferenced was set correctly
        partitionCol1 = view1.getColumns().get(expectedParitionColIndex);
        partitionCol2 = view2.getColumns().get(expectedParitionColIndex);
        partitionCol3 = view3.getColumns().get(expectedParitionColIndex);
        assertTrue("Partition column view referenced attribute should be true ", partitionCol1.isViewReferenced());
        assertTrue("Partition column view referenced attribute should be true ", partitionCol2.isViewReferenced());
        assertTrue("Partition column view referenced attribute should be true ", partitionCol3.isViewReferenced());
        // verify viewConstant was set correctly
        assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition1, partitionCol1.getViewConstant());
        assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition2, partitionCol2.getViewConstant());
        assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition3, partitionCol3.getViewConstant());
    }
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Example 5 with SequenceNotFoundException

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

the class ConnectionlessQueryServicesImpl method validateSequences.

@Override
public void validateSequences(List<SequenceAllocation> sequenceAllocations, long timestamp, long[] values, SQLException[] exceptions, Sequence.ValueOp action) throws SQLException {
    int i = 0;
    for (SequenceAllocation sequenceAllocation : sequenceAllocations) {
        SequenceInfo info = sequenceMap.get(sequenceAllocation.getSequenceKey());
        if (info == null) {
            exceptions[i] = new SequenceNotFoundException(sequenceAllocation.getSequenceKey().getSchemaName(), sequenceAllocation.getSequenceKey().getSequenceName());
        } else {
            values[i] = info.sequenceValue;
        }
        i++;
    }
}
Also used : SequenceInfo(org.apache.phoenix.schema.SequenceInfo) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) SequenceAllocation(org.apache.phoenix.schema.SequenceAllocation)

Aggregations

SequenceNotFoundException (org.apache.phoenix.schema.SequenceNotFoundException)10 Connection (java.sql.Connection)6 ResultSet (java.sql.ResultSet)6 Test (org.junit.Test)6 SQLException (java.sql.SQLException)4 SequenceAllocation (org.apache.phoenix.schema.SequenceAllocation)3 Properties (java.util.Properties)2 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)2 PColumn (org.apache.phoenix.schema.PColumn)2 PTable (org.apache.phoenix.schema.PTable)2 SequenceInfo (org.apache.phoenix.schema.SequenceInfo)2 SequenceKey (org.apache.phoenix.schema.SequenceKey)2 ByteString (com.google.protobuf.ByteString)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Cell (org.apache.hadoop.hbase.Cell)1 KeyValue (org.apache.hadoop.hbase.KeyValue)1