GetSqlFieldUdf implements ScalarUdf { @SamzaSqlUdfMethod(params = {SamzaSqlFieldType.ANY, SamzaSqlFieldType.STRING}, returns = SamzaSqlFieldType.STRING) public String execute(Object currentFieldOrValue, String fieldName) { currentFieldOrValue = getSqlField(currentFieldOrValue, fieldName); if (currentFieldOrValue != null) { return currentFieldOrValue.toString(); } return null; }  @Override void init(Config udfConfig, Context context); @SamzaSqlUdfMethod(params = {SamzaSqlFieldType.ANY, SamzaSqlFieldType.STRING}, returns = SamzaSqlFieldType.STRING) String execute(Object currentFieldOrValue, String fieldName); Object getSqlField(Object currentFieldOrValue, String fieldName);  }
@Test public void testSingleLevel() { SamzaSqlRelRecord record = createRecord("foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "foo"), "bar"); }
@Test public void testMultiLevel() { SamzaSqlRelRecord record = createRecord("bar.baz.baf.foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz.baf.foo"), "bar"); }
@Test public void testNullRecord() { GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(null, "bar.baz.baf.foo"), null); }
@Test (expected = NullPointerException.class) public void testNullFields() { SamzaSqlRelRecord record = createRecord("bar.baz.baf.foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); getSqlFieldUdf.execute(record, null); }
@Test(expected = IllegalArgumentException.class) public void testSingleLevelInvalidField() { SamzaSqlRelRecord record = createRecord("foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); getSqlFieldUdf.execute(record, "bar"); }
@Test(expected = IllegalArgumentException.class) public void testMultiLevelInvalidIntermediateField() { SamzaSqlRelRecord record = createRecord("bar.baz.baf.foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); getSqlFieldUdf.execute(record, "bar.baz.bacon"); }
@Test(expected = IllegalArgumentException.class) public void testMultiLevelInvalidFinalField() { SamzaSqlRelRecord record = createRecord("bar.baz.baf.foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); getSqlFieldUdf.execute(record, "bar.baz.baf.funny"); }
@Test(expected = IllegalArgumentException.class) public void testPathTooDeep() { SamzaSqlRelRecord record = createRecord("bar.foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); getSqlFieldUdf.execute(record, "bar.baz.baf.funny"); }
@Test public void testMapAtLastField() { SamzaSqlRelRecord record = createRecord("bar.baz.baf.map:foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz.baf.foo"), "bar"); }
@Test public void testMapAtIntermediateFields() { SamzaSqlRelRecord record = createRecord("bar.map:baz.map:baf.foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz.baf.foo"), "bar"); }
@Test public void testMapAtAllIntermediateFields() { SamzaSqlRelRecord record = createRecord("bar.map:baz.map:baf.map:foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz.baf.foo"), "bar"); }
@Test public void testArrayAtLastField() { SamzaSqlRelRecord record = createRecord("bar.baz.baf.foo[3]"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz.baf.foo[3]"), "bar"); }
@Test public void testArrayAtIntermediateFields() { SamzaSqlRelRecord record = createRecord("bar.baz[3].baf[2].foo"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz[3].baf[2].foo"), "bar"); }
@Test public void testArrayAtAllIntermediateFields() { SamzaSqlRelRecord record = createRecord("bar.baz[2].baf[3].foo[5]"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz[2].baf[3].foo[5]"), "bar"); }
@Test public void testAllFieldTypes() { SamzaSqlRelRecord record = createRecord("bar.map:baz.baf.foo[3].fun"); GetSqlFieldUdf getSqlFieldUdf = new GetSqlFieldUdf(); Assert.assertEquals(getSqlFieldUdf.execute(record, "bar.baz.baf.foo[3].fun"), "bar"); }