Search in sources :

Example 1 with CobarHint

use of com.alibaba.cobar.route.hint.CobarHint in project cobar by alibaba.

the class CobarHintTest method testHint1.

public void testHint1() throws Exception {
    String sql = "  /*!cobar: $dataNodeId =2.1, $table='offer'*/ select * ";
    CobarHint hint = CobarHint.parserCobarHint(sql, 2);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals(1, hint.getDataNodes().size());
    Assert.assertEquals(new Pair<Integer, Integer>(2, 1), hint.getDataNodes().get(0));
    sql = "  /*!cobar: $dataNodeId=0.0, $table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals(1, hint.getDataNodes().size());
    Assert.assertEquals(new Pair<Integer, Integer>(0, 0), hint.getDataNodes().get(0));
    sql = "  /*!cobar: $dataNodeId=0, $table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals(1, hint.getDataNodes().size());
    Assert.assertEquals(new Pair<Integer, Integer>(0, null), hint.getDataNodes().get(0));
    sql = "/*!cobar: $dataNodeId   = [ 1,2,5.2]  , $table =  'offer'   */ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals(3, hint.getDataNodes().size());
    sql = "/*!cobar: $partitionOperand=( 'member_id' = 'm1'), $table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Pair<String[], Object[][]> pair = hint.getPartitionOperand();
    Assert.assertEquals(1, pair.getKey().length);
    Assert.assertEquals("MEMBER_ID", pair.getKey()[0]);
    Assert.assertEquals(1, pair.getValue().length);
    Assert.assertEquals(1, pair.getValue()[0].length);
    Assert.assertEquals("m1", pair.getValue()[0][0]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand =   ( 'member_id' = ['m1'  ,   'm2' ]  ), $table='offer'  , $replica=  2*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals(2, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(1, pair.getKey().length);
    Assert.assertEquals("MEMBER_ID", pair.getKey()[0]);
    Assert.assertEquals(2, pair.getValue().length);
    Assert.assertEquals(1, pair.getValue()[0].length);
    Assert.assertEquals("m1", pair.getValue()[0][0]);
    Assert.assertEquals("m2", pair.getValue()[1][0]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand=('member_id'=['m1', 'm2']),$table='offer',$replica=2*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals(2, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(1, pair.getKey().length);
    Assert.assertEquals("MEMBER_ID", pair.getKey()[0]);
    Assert.assertEquals(2, pair.getValue().length);
    Assert.assertEquals(1, pair.getValue()[0].length);
    Assert.assertEquals("m1", pair.getValue()[0][0]);
    Assert.assertEquals("m2", pair.getValue()[1][0]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand = ( ['offer_id','group_id'] = [123,'3c']), $table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(2, pair.getKey().length);
    Assert.assertEquals("OFFER_ID", pair.getKey()[0]);
    Assert.assertEquals("GROUP_ID", pair.getKey()[1]);
    Assert.assertEquals(1, pair.getValue().length);
    Assert.assertEquals(2, pair.getValue()[0].length);
    Assert.assertEquals(123L, pair.getValue()[0][0]);
    Assert.assertEquals("3c", pair.getValue()[0][1]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand=(['offer_id' , 'group_iD' ]=[ 123 , '3c' ]) ,$table = 'offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(2, pair.getKey().length);
    Assert.assertEquals("OFFER_ID", pair.getKey()[0]);
    Assert.assertEquals("GROUP_ID", pair.getKey()[1]);
    Assert.assertEquals(1, pair.getValue().length);
    Assert.assertEquals(2, pair.getValue()[0].length);
    Assert.assertEquals(123L, pair.getValue()[0][0]);
    Assert.assertEquals("3c", pair.getValue()[0][1]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand=(['offer_id','group_id']=[123,'3c']),$table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(2, pair.getKey().length);
    Assert.assertEquals("OFFER_ID", pair.getKey()[0]);
    Assert.assertEquals("GROUP_ID", pair.getKey()[1]);
    Assert.assertEquals(1, pair.getValue().length);
    Assert.assertEquals(2, pair.getValue()[0].length);
    Assert.assertEquals(123L, pair.getValue()[0][0]);
    Assert.assertEquals("3c", pair.getValue()[0][1]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand=(['offer_id','group_id']=[[123,'3c'],[234,'food']]), $table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(2, pair.getKey().length);
    Assert.assertEquals("OFFER_ID", pair.getKey()[0]);
    Assert.assertEquals("GROUP_ID", pair.getKey()[1]);
    Assert.assertEquals(2, pair.getValue().length);
    Assert.assertEquals(2, pair.getValue()[0].length);
    Assert.assertEquals(2, pair.getValue()[1].length);
    Assert.assertEquals(123L, pair.getValue()[0][0]);
    Assert.assertEquals("3c", pair.getValue()[0][1]);
    Assert.assertEquals(234L, pair.getValue()[1][0]);
    Assert.assertEquals("food", pair.getValue()[1][1]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand= ( [ 'ofFER_id','groUp_id' ]= [ [123,'3c'],[ 234,'food']]  ), $table='offer'*/select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals("select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(2, pair.getKey().length);
    Assert.assertEquals("OFFER_ID", pair.getKey()[0]);
    Assert.assertEquals("GROUP_ID", pair.getKey()[1]);
    Assert.assertEquals(2, pair.getValue().length);
    Assert.assertEquals(2, pair.getValue()[0].length);
    Assert.assertEquals(2, pair.getValue()[1].length);
    Assert.assertEquals(123L, pair.getValue()[0][0]);
    Assert.assertEquals("3c", pair.getValue()[0][1]);
    Assert.assertEquals(234L, pair.getValue()[1][0]);
    Assert.assertEquals("food", pair.getValue()[1][1]);
    Assert.assertNull(hint.getDataNodes());
    sql = "/*!cobar:$partitionOperand=(['offer_id']=[123,234]), $table='offer'*/ select * ";
    hint = CobarHint.parserCobarHint(sql, 0);
    Assert.assertEquals(" select * ", hint.getOutputSql());
    Assert.assertEquals("OFFER", hint.getTable());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, hint.getReplica());
    pair = hint.getPartitionOperand();
    Assert.assertEquals(1, pair.getKey().length);
    Assert.assertEquals("OFFER_ID", pair.getKey()[0]);
    Assert.assertEquals(2, pair.getValue().length);
    Assert.assertEquals(1, pair.getValue()[0].length);
    Assert.assertEquals(1, pair.getValue()[1].length);
    Assert.assertEquals(123L, pair.getValue()[0][0]);
    Assert.assertEquals(234L, pair.getValue()[1][0]);
    Assert.assertNull(hint.getDataNodes());
}
Also used : CobarHint(com.alibaba.cobar.route.hint.CobarHint)

Aggregations

CobarHint (com.alibaba.cobar.route.hint.CobarHint)1