Search in sources :

Example 1 with FunctionAlreadyExistsException

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

the class UserDefinedFunctionsIT method testSameUDFWithDifferentImplementationsInDifferentTenantConnections.

@Test
public void testSameUDFWithDifferentImplementationsInDifferentTenantConnections() throws Exception {
    Connection nonTenantConn = driver.connect(url, EMPTY_PROPS);
    nonTenantConn.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
    try {
        nonTenantConn.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end.UnknownClass' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
        fail("FunctionAlreadyExistsException should be thrown.");
    } catch (FunctionAlreadyExistsException e) {
    }
    String tenantId1 = "tenId1";
    String tenantId2 = "tenId2";
    nonTenantConn.createStatement().execute("create table t7(tenant_id varchar not null, k integer not null, k1 integer, name varchar constraint pk primary key(tenant_id, k)) multi_tenant=true");
    Connection tenant1Conn = driver.connect(url + ";" + PhoenixRuntime.TENANT_ID_ATTRIB + "=" + tenantId1, EMPTY_PROPS);
    Connection tenant2Conn = driver.connect(url + ";" + PhoenixRuntime.TENANT_ID_ATTRIB + "=" + tenantId2, EMPTY_PROPS);
    tenant1Conn.createStatement().execute("upsert into t7 values(1,1,'jock')");
    tenant1Conn.commit();
    tenant2Conn.createStatement().execute("upsert into t7 values(1,2,'jock')");
    tenant2Conn.commit();
    tenant1Conn.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
    try {
        tenant1Conn.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end.UnknownClass' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
        fail("FunctionAlreadyExistsException should be thrown.");
    } catch (FunctionAlreadyExistsException e) {
    }
    tenant2Conn.createStatement().execute("create function myfunction(INTEGER, INTEGER CONSTANT defaultValue=10 minvalue=1 maxvalue=15 ) returns INTEGER as 'org.apache.phoenix.end2end." + MY_SUM_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar2.jar" + "'");
    try {
        tenant2Conn.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end.UnknownClass' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/unknown.jar" + "'");
        fail("FunctionAlreadyExistsException should be thrown.");
    } catch (FunctionAlreadyExistsException e) {
    }
    ResultSet rs = tenant1Conn.createStatement().executeQuery("select MYFUNCTION(name) from t7");
    assertTrue(rs.next());
    assertEquals("kcoj", rs.getString(1));
    assertFalse(rs.next());
    rs = tenant1Conn.createStatement().executeQuery("select * from t7 where MYFUNCTION(name)='kcoj'");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertEquals(1, rs.getInt(2));
    assertEquals("jock", rs.getString(3));
    assertFalse(rs.next());
    rs = tenant2Conn.createStatement().executeQuery("select MYFUNCTION(k) from t7");
    assertTrue(rs.next());
    assertEquals(11, rs.getInt(1));
    assertFalse(rs.next());
    rs = tenant2Conn.createStatement().executeQuery("select * from t7 where MYFUNCTION(k1)=12");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertEquals(2, rs.getInt(2));
    assertEquals("jock", rs.getString(3));
    assertFalse(rs.next());
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) FunctionAlreadyExistsException(org.apache.phoenix.schema.FunctionAlreadyExistsException) Test(org.junit.Test)

Example 2 with FunctionAlreadyExistsException

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

the class UserDefinedFunctionsIT method testUDFsWithMultipleConnections.

@Test
public void testUDFsWithMultipleConnections() throws Exception {
    Connection conn1 = driver.connect(url, EMPTY_PROPS);
    conn1.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
    Connection conn2 = driver.connect(url, EMPTY_PROPS);
    try {
        conn2.createStatement().execute("create function myfunction(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
        fail("FunctionAlreadyExistsException should be thrown.");
    } catch (FunctionAlreadyExistsException e) {
    }
    conn2.createStatement().execute("create table t8(k integer not null primary key, k1 integer, name varchar)");
    conn2.createStatement().execute("upsert into t8 values(1,1,'jock')");
    conn2.commit();
    ResultSet rs = conn2.createStatement().executeQuery("select MYFUNCTION(name) from t8");
    assertTrue(rs.next());
    assertEquals("kcoj", rs.getString(1));
    assertFalse(rs.next());
    rs = conn2.createStatement().executeQuery("select * from t8 where MYFUNCTION(name)='kcoj'");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertEquals(1, rs.getInt(2));
    assertEquals("jock", rs.getString(3));
    assertFalse(rs.next());
    conn2.createStatement().execute("drop function MYFUNCTION");
    try {
        rs = conn1.createStatement().executeQuery("select MYFUNCTION(name) from t8");
        fail("FunctionNotFoundException should be thrown");
    } catch (FunctionNotFoundException e) {
    }
}
Also used : FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) FunctionAlreadyExistsException(org.apache.phoenix.schema.FunctionAlreadyExistsException) Test(org.junit.Test)

Example 3 with FunctionAlreadyExistsException

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

the class UserDefinedFunctionsIT method testCreateFunction.

@Test
public void testCreateFunction() throws Exception {
    Connection conn = driver.connect(url, EMPTY_PROPS);
    Statement stmt = conn.createStatement();
    conn.createStatement().execute("create table t(k integer primary key, firstname varchar, lastname varchar)");
    stmt.execute("upsert into t values(1,'foo','jock')");
    conn.commit();
    stmt.execute("create function myreverse(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
    ResultSet rs = stmt.executeQuery("select myreverse(firstname) from t");
    assertTrue(rs.next());
    assertEquals("oof", rs.getString(1));
    assertFalse(rs.next());
    rs = stmt.executeQuery("select * from t where myreverse(firstname)='oof'");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertEquals("foo", rs.getString(2));
    assertEquals("jock", rs.getString(3));
    assertFalse(rs.next());
    try {
        stmt.execute("create function myreverse(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
        fail("Duplicate function should not be created.");
    } catch (FunctionAlreadyExistsException e) {
    }
    // without specifying the jar should pick the class from path of hbase.dynamic.jars.dir configuration. 
    stmt.execute("create function myreverse2(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "'");
    rs = stmt.executeQuery("select myreverse2(firstname) from t");
    assertTrue(rs.next());
    assertEquals("oof", rs.getString(1));
    assertFalse(rs.next());
    rs = stmt.executeQuery("select * from t where myreverse2(firstname)='oof'");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertEquals("foo", rs.getString(2));
    assertEquals("jock", rs.getString(3));
    assertFalse(rs.next());
    conn.createStatement().execute("create table t3(tenant_id varchar not null, k integer not null, firstname varchar, lastname varchar constraint pk primary key(tenant_id,k)) MULTI_TENANT=true");
    // Function created with global id should be accessible.
    Connection conn2 = driver.connect(url + ";" + PhoenixRuntime.TENANT_ID_ATTRIB + "=" + TENANT_ID, EMPTY_PROPS);
    try {
        conn2.createStatement().execute("upsert into t3 values(1,'foo','jock')");
        conn2.commit();
        conn2.createStatement().execute("create function myreverse(VARCHAR) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_REVERSE_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar1.jar" + "'");
        rs = conn2.createStatement().executeQuery("select myreverse(firstname) from t3");
        assertTrue(rs.next());
        assertEquals("oof", rs.getString(1));
    } catch (FunctionAlreadyExistsException e) {
        fail("FunctionAlreadyExistsException should not be thrown");
    }
    // calling global udf on tenant specific specific connection.
    rs = conn2.createStatement().executeQuery("select myreverse2(firstname) from t3");
    assertTrue(rs.next());
    assertEquals("oof", rs.getString(1));
    try {
        conn2.createStatement().execute("drop function myreverse2");
        fail("FunctionNotFoundException should be thrown");
    } catch (FunctionNotFoundException e) {
    }
    conn.createStatement().execute("drop function myreverse2");
    try {
        rs = conn2.createStatement().executeQuery("select myreverse2(firstname) from t3");
        fail("FunctionNotFoundException should be thrown.");
    } catch (FunctionNotFoundException e) {
    }
    try {
        rs = conn2.createStatement().executeQuery("select unknownFunction(firstname) from t3");
        fail("FunctionNotFoundException should be thrown.");
    } catch (FunctionNotFoundException e) {
    }
    conn.createStatement().execute("CREATE TABLE TESTTABLE10(ID VARCHAR NOT NULL, NAME VARCHAR ARRAY, CITY VARCHAR ARRAY CONSTRAINT pk PRIMARY KEY (ID) )");
    conn.createStatement().execute("create function UDF_ARRAY_ELEM(VARCHAR ARRAY, INTEGER) returns VARCHAR as 'org.apache.phoenix.end2end." + MY_ARRAY_INDEX_CLASS_NAME + "' using jar " + "'" + util.getConfiguration().get(DYNAMIC_JARS_DIR_KEY) + "/myjar3.jar" + "'");
    conn.createStatement().execute("UPSERT INTO TESTTABLE10(ID,NAME,CITY) VALUES('111', ARRAY['JOHN','MIKE','BOB'], ARRAY['NYC','LA','SF'])");
    conn.createStatement().execute("UPSERT INTO TESTTABLE10(ID,NAME,CITY) VALUES('112', ARRAY['CHEN','CARL','ALICE'], ARRAY['BOSTON','WASHINGTON','PALO ALTO'])");
    conn.commit();
    rs = conn.createStatement().executeQuery("SELECT ID, UDF_ARRAY_ELEM(NAME, 2) FROM TESTTABLE10");
    assertTrue(rs.next());
    assertEquals("111", rs.getString(1));
    assertEquals("MIKE", rs.getString(2));
    assertTrue(rs.next());
    assertEquals("112", rs.getString(1));
    assertEquals("CARL", rs.getString(2));
    assertFalse(rs.next());
    rs = conn2.createStatement().executeQuery("SELECT ID, UDF_ARRAY_ELEM(NAME, 2) FROM TESTTABLE10");
    assertTrue(rs.next());
    assertEquals("111", rs.getString(1));
    assertEquals("MIKE", rs.getString(2));
    assertTrue(rs.next());
    assertEquals("112", rs.getString(1));
    assertEquals("CARL", rs.getString(2));
    assertFalse(rs.next());
}
Also used : FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) FunctionAlreadyExistsException(org.apache.phoenix.schema.FunctionAlreadyExistsException) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 FunctionAlreadyExistsException (org.apache.phoenix.schema.FunctionAlreadyExistsException)3 Test (org.junit.Test)3 FunctionNotFoundException (org.apache.phoenix.schema.FunctionNotFoundException)2 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1