@Test public void testCastingWithLengthInWhere() throws Exception { String query = "SELECT b_string FROM aTable WHERE CAST (b_string AS VARCHAR(10)) = 'b'"; List<Object> binds = Collections.emptyList(); compileQuery(query, binds); }
@Test public void testCastingIntegerToDecimalInSelect() throws Exception { String query = "SELECT CAST (a_integer AS DECIMAL)/2 FROM aTable WHERE 5=a_integer"; List<Object> binds = Collections.emptyList(); compileQuery(query, binds); }
@Test public void testCastingWithLengthInSelect() throws Exception { String query = "SELECT CAST (b_string AS VARCHAR(10)) FROM aTable"; List<Object> binds = Collections.emptyList(); compileQuery(query, binds); }
public void testWrongTimeUnitInRoundDateFunction() throws Exception { String query = "SELECT ROUND(a_date, 'dayss', 1) FROM aTable"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since dayss is not a valid time unit type"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains(TimeUnit.VALID_VALUES)); } }
@Test public void testIsNullOnNotNullable() throws Exception { // Select columns in PK String query = "select a_string from atable where entity_id is null"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); assertDegenerate(scan); }
@Test public void testPercentileWrongQuery1() throws Exception { String query = "select PERCENTILE_CONT('*') WITHIN GROUP (ORDER BY a_integer ASC) from ATABLE"; try { compileQuery(query, Collections.emptyList()); fail(); } catch (SQLException e) { assertEquals( "ERROR 203 (22005): Type mismatch. expected: [DECIMAL] but was: VARCHAR at PERCENTILE_CONT argument 3", e.getMessage()); } }
@Test public void testPercentileWrongQueryWithMixOfAggrAndNonAggrExps() throws Exception { String query = "select a_integer, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY a_integer ASC) from ATABLE"; try { compileQuery(query, Collections.emptyList()); fail(); } catch (SQLException e) { assertEquals("ERROR 1018 (42Y27): Aggregate may not contain columns not in GROUP BY. A_INTEGER", e.getMessage()); } }
@Test public void testPercentileWrongQuery3() throws Exception { String query = "select PERCENTILE_CONT(-1) WITHIN GROUP (ORDER BY a_integer ASC) from ATABLE"; try { compileQuery(query, Collections.emptyList()); fail(); } catch (Exception e) { assertEquals( "ERROR 213 (22003): Value outside range. expected: [0 , 1] but was: -1 at PERCENTILE_CONT argument 3", e.getMessage()); } }
@Test public void testOnlyNullInScanKey() throws Exception { // Select columns in PK String query = "select val from ptsdb where inst is null"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); // Projects column family with not null column assertEquals(1,scan.getFamilyMap().keySet().size()); assertArrayEquals(Bytes.toBytes(SchemaUtil.normalizeIdentifier(QueryConstants.DEFAULT_COLUMN_FAMILY)), scan.getFamilyMap().keySet().iterator().next()); }
@Test public void testNoCachingHint() throws Exception { List<Object> binds = Collections.emptyList(); Scan scan = compileQuery("select val from ptsdb", binds); assertTrue(scan.getCacheBlocks()); scan = compileQuery("select /*+ NO_CACHE */ val from ptsdb", binds); assertFalse(scan.getCacheBlocks()); scan = compileQuery("select /*+ NO_CACHE */ p1.val from ptsdb p1 inner join ptsdb p2 on p1.inst = p2.inst", binds); assertFalse(scan.getCacheBlocks()); }
@Test public void testNonConstantInList() throws Exception { String query = "SELECT a_integer, x_integer FROM aTable WHERE a_integer IN (x_integer)"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since non constants in IN is not valid"); } catch (SQLException e) { assertTrue(e.getErrorCode() == SQLExceptionCode.VALUE_IN_LIST_NOT_CONSTANT.getErrorCode()); } }
@Test public void testWrongMultiplierInRoundDateFunction() throws Exception { String query = "SELECT ROUND(a_date, 'days', 1.23) FROM aTable"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since multiplier can be an INTEGER only"); } catch (SQLException e) { assertEquals(SQLExceptionCode.TYPE_MISMATCH.getErrorCode(), e.getErrorCode()); } }
@Test public void testTypeMismatchForArrayElem() throws Exception { String query = "SELECT (a_string,a_date)[1] FROM aTable"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since a row value constructor is not an array"); } catch (SQLException e) { assertEquals(SQLExceptionCode.TYPE_MISMATCH.getErrorCode(), e.getErrorCode()); } }
@Test public void testNullInScanKey() throws Exception { // Select columns in PK String query = "select val from ptsdb where inst is null and host='a'"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); // Projects column family with not null column assertNull(scan.getFilter()); assertEquals(1,scan.getFamilyMap().keySet().size()); assertArrayEquals(Bytes.toBytes(SchemaUtil.normalizeIdentifier(QueryConstants.DEFAULT_COLUMN_FAMILY)), scan.getFamilyMap().keySet().iterator().next()); }
@Test public void testCastingStringToDecimalInSelect() throws Exception { String query = "SELECT CAST (b_string AS DECIMAL)/2 FROM aTable WHERE 5=a_integer"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since casting a string to decimal isn't supported"); } catch (SQLException e) { assertTrue(e.getErrorCode() == SQLExceptionCode.TYPE_MISMATCH.getErrorCode()); } }
@Test public void testUsingNonComparableDataTypesOfLiteralOnRHSAndRowValueConstructorFails() throws Exception { String query = "SELECT a_integer, x_integer FROM aTable WHERE (a_integer, x_integer) < 'abc'"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since casting a integer to string isn't supported"); } catch (SQLException e) { assertTrue(e.getErrorCode() == SQLExceptionCode.TYPE_MISMATCH.getErrorCode()); } }
@Test public void testCastingStringToDecimalInWhere() throws Exception { String query = "SELECT a_integer FROM aTable WHERE 2.5=CAST (b_string AS DECIMAL)/2 "; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since casting a string to decimal isn't supported"); } catch (SQLException e) { assertTrue(e.getErrorCode() == SQLExceptionCode.TYPE_MISMATCH.getErrorCode()); } }
@Test public void testTypeMismatch2ForArrayElem() throws Exception { String query = "SELECT ROUND(a_date, 'days', 1.23)[1] FROM aTable"; List<Object> binds = Collections.emptyList(); try { compileQuery(query, binds); fail("Compilation should have failed since ROUND does not return an array"); } catch (SQLException e) { assertEquals(SQLExceptionCode.TYPE_MISMATCH.getErrorCode(), e.getErrorCode()); } }
@Test public void testRTrimSetScanKey() throws Exception { String query = "SELECT inst FROM ptsdb WHERE rtrim(inst) = 'abc'"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); assertArrayEquals(Bytes.toBytes("abc"), scan.getStartRow()); assertArrayEquals(ByteUtil.nextKey(Bytes.toBytes("abc ")), scan.getStopRow()); assertNotNull(scan.getFilter()); }
@Test public void testIsNotNullOnNotNullable() throws Exception { // Select columns in PK String query = "select a_string from atable where entity_id is not null"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); assertNull(scan.getFilter()); assertTrue(scan.getStartRow().length == 0); assertTrue(scan.getStopRow().length == 0); }