Exemplo n.º 1
0
 /** Should success without exception */
 @Test
 public void testQueryParseAndBuildFilterSuccess() {
   String[] queries =
       new String[] {
         "@cluster = \"cluster1\" and @datacenter = \"dc1\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID = \"job_1234\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID = \"PigLatin: \\\"quoted_pig_job_name_value\\\"\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID in (\"job_1234\",\"job_4567\")",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID in (1234,\"job_4567\")",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID in (1234,\"sample job name: \\\"quoted_job_name_value\\\"\")",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID CONTAINS \"job_1234\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID CONTAINS job_1234",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID NOT CONTAINS \"job_456\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID is \"job_789\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID is not \"job_789\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID is null",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID is not null",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID is NULL",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID is not NULL",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID = NULL",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID != null",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID =~ \".*job_1234.*\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID !=~ \".*job_1234.*\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID !=~ \"\\\\|_\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 ",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 and @field3 = 100000",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 and @field5 = 1.56",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 and @field5 > 1.56",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 and @field5 >= 1.56",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 and @field5 < 1.56",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field1 = 1 and @field5 <= 1.56",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 < 100000)\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 in (\"100000\",\"1\"))\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 in (\"100000\",\"1\"))\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field7 in (\"\\\"value1-part1,value1-part2\\\"\",\"value2\"))\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 not in (\"100000\",\"1\"))\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 NOT IN (\"100000\",\"1\"))\"",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field7 NOT IN (\"\\\"value1-part1,value1-part2\\\"\",\"value2\"))\"",
         // expression filter
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and EXP{field3/field7 - field2} > 12",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field5 > EXP{field3/field7 - field2}",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and EXP{field3/field7 - field2} > EXP{field1 * field2}",
         "@cluster = \"cluster1\" and @datacenter = \"dc1\" and EXP{field3/field7 - field2} > EXP{field1 * field2}",
       };
   for (String query : queries) {
     try {
       Filter filter = buildFilter(query);
       Assert.assertNotNull(filter);
     } catch (EagleQueryParseException e) {
       Assert.fail(e.getMessage());
     } catch (Exception ex) {
       Assert.fail(ex.getMessage());
     }
   }
 }
Exemplo n.º 2
0
  @Test
  public void testParsedFilter() {
    String q1 = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field3 = 100000";
    try {
      FilterList filterList = (FilterList) buildFilter(q1);
      Assert.assertEquals(FilterList.Operator.MUST_PASS_ONE, filterList.getOperator());
      Assert.assertEquals(1, filterList.getFilters().size());
      Assert.assertEquals(2, ((FilterList) filterList.getFilters().get(0)).getFilters().size());
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    }

    String q2 =
        "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 < 100000)";
    try {
      FilterList filterList = (FilterList) buildFilter(q2);
      Assert.assertEquals(FilterList.Operator.MUST_PASS_ONE, filterList.getOperator());
      Assert.assertEquals(2, filterList.getFilters().size());
      Assert.assertEquals(2, ((FilterList) filterList.getFilters().get(0)).getFilters().size());
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    }

    // Test parse success but bad type of value
    String q3 =
        "@cluster = \"cluster1\" and @datacenter = \"dc1\" and ( @field3 = 100000 or @field3 < \"bad_int_100000\")";
    boolean q3Ex = false;
    try {
      Assert.assertNull(buildFilter(q3));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (IllegalArgumentException e) {
      LOG.debug("Expect: ", e);
      Assert.assertTrue(e.getCause() instanceof NumberFormatException);
      q3Ex = true;
    }
    Assert.assertTrue(q3Ex);
  }
Exemplo n.º 3
0
  @Test
  public void testWithUnescapedString() {
    ///////////////////////////////////
    // Tag filter with IN or EQUAL
    // Should use RowKeyFilter only
    ///////////////////////////////////
    String query = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID = \"job.1234\"";
    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertFalse(
          "Should use rowkey filter only", filter.toString().matches(".*job.1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }

    query = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID in (\"job_1234\")";
    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertFalse(
          "Should use rowkey filter only", filter.toString().matches(".*job_1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }

    query = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID in (\"job.1234\")";
    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertFalse(
          "Should use rowkey filter only", filter.toString().matches(".*job.*1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }

    ///////////////////////////////
    // Tag with other operators
    ///////////////////////////////
    query = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID =~ \"job_1234\"";

    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertTrue(filter.toString().matches(".*job_1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }

    query = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @jobID =~ \"job.1234\"";

    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertTrue(filter.toString().matches(".*job.1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }

    ///////////////////////////////
    // Tag with IN
    // Should escape regexp chars
    ///////////////////////////////
    query = "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field7 = \"job_1234\"";

    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertTrue(filter.toString().matches(".*job_1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      ex.printStackTrace();
      Assert.fail(ex.getMessage());
    }

    query =
        "@cluster = \"cluster1\" and @datacenter = \"dc1\" and @field7 in (\"job.1234\",\"others\")";

    try {
      FilterList filter = (FilterList) buildFilter(query);
      Assert.assertEquals(
          RowFilter.class,
          ((FilterList) filter.getFilters().get(0)).getFilters().get(0).getClass());
      Assert.assertTrue(filter.toString().matches(".*job\\.1234.*"));
    } catch (EagleQueryParseException e) {
      Assert.fail(e.getMessage());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }
  }