@Test public final void testTypeInferring() { QueryBlock block = (QueryBlock) analyzer.parse("select 1 from alltype where char_col = 'a'").getParseTree(); assertEquals(DataType.CHAR, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where short_col = 1").getParseTree(); assertEquals(DataType.SHORT, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where int_col = 1").getParseTree(); assertEquals(DataType.INT, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where long_col = 1").getParseTree(); assertEquals(DataType.LONG, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where float_col = 1").getParseTree(); assertEquals(DataType.INT, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where float_col = 1.0").getParseTree(); assertEquals(DataType.FLOAT, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where int_col = 1.0").getParseTree(); assertEquals(DataType.DOUBLE, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where double_col = 1.0").getParseTree(); assertEquals(DataType.DOUBLE, block.getWhereCondition().getRightExpr().getValueType()[0]); block = (QueryBlock) analyzer.parse("select 1 from alltype where string_col = 'a'").getParseTree(); assertEquals(DataType.STRING, block.getWhereCondition().getRightExpr().getValueType()[0]); }
@Test public final void testSelectStatementWithAlias() { QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[4]).getParseTree(); assertEquals(2, block.getFromTables().length); assertEquals("people", block.getFromTables()[0].getTableName()); assertEquals("student", block.getFromTables()[1].getTableName()); }
@Test public final void testEmptyGroupSetStatement() { ParseTree tree = analyzer.parse(GROUP_BY[4]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertTrue(block.getGroupByClause().isEmptyGroupSet()); }
@Test public final void testCrossJoinClause() { QueryBlock block = (QueryBlock) analyzer.parse(JOINS[3]).getParseTree(); JoinClause join = block.getJoinClause(); assertEquals(JoinType.CROSS_JOIN, join.getJoinType()); assertEquals("branch", join.getRight().getTableName()); assertTrue(join.hasLeftJoin()); assertEquals("people", join.getLeftJoin().getLeft().getTableName()); assertEquals("student", join.getLeftJoin().getRight().getTableName()); }
@Test public final void testRightJoinClause() { QueryBlock block = (QueryBlock) analyzer.parse(JOINS[8]).getParseTree(); JoinClause join = block.getJoinClause(); assertEquals(JoinType.RIGHT_OUTER, join.getJoinType()); assertEquals("people", join.getLeft().getTableName()); assertEquals("p", join.getLeft().getAlias()); assertEquals("student", join.getRight().getTableName()); assertEquals("s", join.getRight().getAlias()); assertTrue(join.hasJoinQual()); assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType()); }
@Test /** join / \ join branch / \ people student */ public final void testNaturalJoinClause() { QueryBlock block = (QueryBlock) analyzer.parse(JOINS[0]).getParseTree(); JoinClause join = block.getJoinClause(); assertEquals(JoinType.INNER, join.getJoinType()); assertTrue(join.isNatural()); assertEquals("branch", join.getRight().getTableName()); assertTrue(join.hasLeftJoin()); assertEquals("people", join.getLeftJoin().getLeft().getTableName()); assertEquals("student", join.getLeftJoin().getRight().getTableName()); }
@Test public final void testRollUpStatement() { ParseTree tree = analyzer.parse(GROUP_BY[2]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertEquals(1, block.getGroupByClause().getGroupSet().size()); assertEquals(GroupType.ROLLUP, block.getGroupByClause().getGroupSet().get(0).getType()); List<GroupElement> groups = block.getGroupByClause().getGroupSet(); assertEquals("people.name", groups.get(0).getColumns()[0].getQualifiedName()); assertEquals("people.age", groups.get(0).getColumns()[1].getQualifiedName()); }
@Test public void testCaseWhen() { ParseTree tree = analyzer .parse( "select case when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) " + "when p_type = 'MOCC' then l_extendedprice - 100 else 0 end as cond from lineitem, part") .getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.getTargetList()[0].hasAlias()); assertEquals("cond", block.getTargetList()[0].getAlias()); assertEquals(DataType.DOUBLE, block.getTargetList()[0].getEvalTree().getValueType()[0]); }
@Test public final void testSetQulaifier() { ParseTree tree = analyzer.parse(setQualifier[0]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertFalse(block.isDistinct()); tree = analyzer.parse(setQualifier[1]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); block = (QueryBlock) tree; assertTrue(block.isDistinct()); tree = analyzer.parse(setQualifier[2]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); block = (QueryBlock) tree; assertFalse(block.isDistinct()); }
@Test public final void testUnionClause() { ParseTree tree = analyzer.parse(setClauses[0]).getParseTree(); assertEquals(StatementType.UNION, tree.getType()); SetStmt union = (SetStmt) tree; assertEquals(StatementType.SELECT, union.getLeftTree().getType()); assertEquals(StatementType.SELECT, union.getRightTree().getType()); QueryBlock left = (QueryBlock) union.getLeftTree(); assertEquals("student", left.getFromTables()[0].getTableName()); QueryBlock right = (QueryBlock) union.getRightTree(); assertEquals("branch", right.getFromTables()[0].getTableName()); // multiple set statements tree = analyzer.parse(setClauses[1]).getParseTree(); assertEquals(StatementType.UNION, tree.getType()); union = (SetStmt) tree; assertEquals(StatementType.SELECT, union.getLeftTree().getType()); assertEquals(StatementType.INTERSECT, union.getRightTree().getType()); left = (QueryBlock) union.getLeftTree(); assertEquals("student", left.getFromTables()[0].getTableName()); SetStmt rightSet = (SetStmt) union.getRightTree(); left = (QueryBlock) rightSet.getLeftTree(); assertEquals("branch", left.getFromTables()[0].getTableName()); right = (QueryBlock) rightSet.getRightTree(); assertEquals("b", right.getFromTables()[0].getAlias()); }
@Test public final void testMixedGroupByStatement() { ParseTree tree = analyzer.parse(GROUP_BY[3]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertEquals(3, block.getGroupByClause().getGroupSet().size()); Iterator<GroupElement> it = block.getGroupByClause().getGroupSet().iterator(); GroupElement group = it.next(); assertEquals(GroupType.CUBE, group.getType()); assertEquals("people.name", group.getColumns()[0].getQualifiedName()); group = it.next(); assertEquals(GroupType.ROLLUP, group.getType()); assertEquals("people.age", group.getColumns()[0].getQualifiedName()); group = it.next(); assertEquals(GroupType.GROUPBY, group.getType()); assertEquals("people.id", group.getColumns()[0].getQualifiedName()); }
private static void testOrderByCluse(QueryBlock block) { assertEquals(2, block.getSortKeys().length); assertEquals("people.score", block.getSortKeys()[0].getSortKey().getQualifiedName()); assertEquals(true, block.getSortKeys()[0].isAscending()); assertEquals(false, block.getSortKeys()[0].isNullFirst()); assertEquals("people.age", block.getSortKeys()[1].getSortKey().getQualifiedName()); assertEquals(false, block.getSortKeys()[1].isAscending()); assertEquals(true, block.getSortKeys()[1].isNullFirst()); }
@Test /** join / \ people student */ public final void testInnerJoinClause() { QueryBlock block = (QueryBlock) analyzer.parse(JOINS[1]).getParseTree(); JoinClause join = block.getJoinClause(); assertEquals(JoinType.INNER, join.getJoinType()); assertEquals("people", join.getLeft().getTableName()); assertEquals("p", join.getLeft().getAlias()); assertEquals("student", join.getRight().getTableName()); assertEquals("s", join.getRight().getAlias()); assertTrue(join.hasJoinQual()); assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType()); block = (QueryBlock) analyzer.parse(JOINS[2]).getParseTree(); join = block.getJoinClause(); assertEquals(JoinType.INNER, join.getJoinType()); assertEquals("people", join.getLeft().getTableName()); assertEquals("p", join.getLeft().getAlias()); assertEquals("student", join.getRight().getTableName()); assertEquals("s", join.getRight().getAlias()); assertTrue(join.hasJoinColumns()); assertEquals("id", join.getJoinColumns()[0].getColumnName()); }
@Test public final void testGroupByStatement() { ParseTree tree = analyzer.parse(GROUP_BY[0]).getParseTree(); assertEquals(StatementType.SELECT, tree.getType()); QueryBlock block = (QueryBlock) tree; assertTrue(block.hasGroupbyClause()); assertEquals(1, block.getGroupByClause().getGroupSet().size()); assertEquals( "age", block.getGroupByClause().getGroupSet().get(0).getColumns()[0].getColumnName()); assertTrue(block.hasHavingCond()); assertEquals(Type.GTH, block.getHavingCond().getType()); }
@Test public void testLimit() { QueryBlock queryBlock = (QueryBlock) analyzer.parse(QUERIES[7]).getParseTree(); assertEquals(3, queryBlock.getLimitClause().getLimitRow()); }
@Test(expected = InvalidQueryException.class) public final void testInvalidGroupFields() { QueryBlock block = (QueryBlock) analyzer.parse(INVALID_QUERIES[2]).getParseTree(); assertEquals( "age", block.getGroupByClause().getGroupSet().get(0).getColumns()[0].getQualifiedName()); }
@Test public final void testOnlyExpr() { QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[6]).getParseTree(); EvalNode node = block.getTargetList()[0].getEvalTree(); assertEquals(Type.PLUS, node.getType()); }