Add ExtractionFn to LookupExtractor bridge

This commit is contained in:
Charles Allen 2016-03-18 22:16:26 -07:00
parent e2cabc93ca
commit 0ee861d0da
3 changed files with 425 additions and 0 deletions

View File

@ -22,6 +22,7 @@ package io.druid.query.extraction;
import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.druid.query.lookup.LookupExtractionFn; import io.druid.query.lookup.LookupExtractionFn;
import io.druid.query.lookup.RegisteredLookupExtractionFn;
/** /**
*/ */
@ -35,6 +36,7 @@ import io.druid.query.lookup.LookupExtractionFn;
@JsonSubTypes.Type(name = "timeFormat", value = TimeFormatExtractionFn.class), @JsonSubTypes.Type(name = "timeFormat", value = TimeFormatExtractionFn.class),
@JsonSubTypes.Type(name = "identity", value = IdentityExtractionFn.class), @JsonSubTypes.Type(name = "identity", value = IdentityExtractionFn.class),
@JsonSubTypes.Type(name = "lookup", value = LookupExtractionFn.class), @JsonSubTypes.Type(name = "lookup", value = LookupExtractionFn.class),
@JsonSubTypes.Type(name = "registeredLookup", value = RegisteredLookupExtractionFn.class),
@JsonSubTypes.Type(name = "substring", value = SubstringDimExtractionFn.class), @JsonSubTypes.Type(name = "substring", value = SubstringDimExtractionFn.class),
@JsonSubTypes.Type(name = "cascade", value = CascadeExtractionFn.class), @JsonSubTypes.Type(name = "cascade", value = CascadeExtractionFn.class),
@JsonSubTypes.Type(name = "stringFormat", value = StringFormatExtractionFn.class), @JsonSubTypes.Type(name = "stringFormat", value = StringFormatExtractionFn.class),

View File

@ -0,0 +1,167 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.query.lookup;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import io.druid.query.extraction.ExtractionFn;
import javax.annotation.Nullable;
public class RegisteredLookupExtractionFn implements ExtractionFn
{
private final LookupExtractionFn delegate;
private final String name;
RegisteredLookupExtractionFn(LookupExtractionFn delegate, String name)
{
this.delegate = delegate;
this.name = name;
}
@JsonCreator
public static RegisteredLookupExtractionFn create(
@JacksonInject LookupReferencesManager manager,
@JsonProperty("lookup") String lookup,
@JsonProperty("retainMissingValue") final boolean retainMissingValue,
@Nullable @JsonProperty("replaceMissingValueWith") final String replaceMissingValueWith,
@JsonProperty("injective") final boolean injective,
@JsonProperty("optimize") Boolean optimize
)
{
Preconditions.checkArgument(lookup != null, "`lookup` required");
final LookupExtractorFactory factory = manager.get(lookup);
Preconditions.checkNotNull(factory, "lookup [%s] not found", lookup);
return new RegisteredLookupExtractionFn(
new LookupExtractionFn(
factory.get(),
retainMissingValue,
replaceMissingValueWith,
injective,
optimize
),
lookup
);
}
@JsonProperty("lookup")
public String getLookup()
{
return name;
}
@JsonProperty("retainMissingValue")
public boolean isRetainMissingValue()
{
return delegate.isRetainMissingValue();
}
@JsonProperty("replaceMissingValueWith")
public String getReplaceMissingValueWith()
{
return delegate.getReplaceMissingValueWith();
}
@JsonProperty("injective")
public boolean isInjective()
{
return delegate.isInjective();
}
@JsonProperty("optimize")
public boolean isOptimize()
{
return delegate.isOptimize();
}
@Override
public byte[] getCacheKey()
{
return delegate.getCacheKey();
}
@Override
public String apply(Object value)
{
return delegate.apply(value);
}
@Override
public String apply(String value)
{
return delegate.apply(value);
}
@Override
public String apply(long value)
{
return delegate.apply(value);
}
@Override
public boolean preservesOrdering()
{
return delegate.preservesOrdering();
}
@Override
public ExtractionType getExtractionType()
{
return delegate.getExtractionType();
}
@Override
public String toString()
{
return "RegisteredLookupExtractionFn{" +
"delegate=" + delegate +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RegisteredLookupExtractionFn that = (RegisteredLookupExtractionFn) o;
if (!delegate.equals(that.delegate)) {
return false;
}
return name.equals(that.name);
}
@Override
public int hashCode()
{
int result = delegate.hashCode();
result = 31 * result + name.hashCode();
return result;
}
}

View File

@ -0,0 +1,256 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.query.lookup;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import io.druid.jackson.DefaultObjectMapper;
import io.druid.query.extraction.MapLookupExtractor;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Map;
public class RegisteredLookupExtractionFnTest
{
private static Map<String, String> MAP = ImmutableMap.of(
"foo", "bar",
"bat", "baz"
);
private static final LookupExtractor LOOKUP_EXTRACTOR = new MapLookupExtractor(MAP, true);
private static final String LOOKUP_NAME = "some lookup";
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testSimpleDelegation()
{
final LookupReferencesManager manager = EasyMock.createStrictMock(LookupReferencesManager.class);
managerReturnsMap(manager);
EasyMock.replay(manager);
final RegisteredLookupExtractionFn fn = RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
true,
null,
true,
false
);
EasyMock.verify(manager);
for (String orig : Arrays.asList("", "foo", "bat")) {
Assert.assertEquals(LOOKUP_EXTRACTOR.apply(orig), fn.apply(orig));
}
Assert.assertEquals("not in the map", fn.apply("not in the map"));
}
@Test
public void testMissingDelegation()
{
final LookupReferencesManager manager = EasyMock.createStrictMock(LookupReferencesManager.class);
EasyMock.expect(manager.get(EasyMock.eq(LOOKUP_NAME))).andReturn(null).once();
EasyMock.replay(manager);
expectedException.expectMessage("lookup [some lookup] not found");
try {
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
true,
null,
true,
false
);
}
finally {
EasyMock.verify(manager);
}
}
@Test
public void testNullLookup()
{
expectedException.expectMessage("`lookup` required");
RegisteredLookupExtractionFn.create(
null,
null,
true,
null,
true,
false
);
}
@Test
public void testSerDe() throws Exception
{
final ObjectMapper mapper = new DefaultObjectMapper();
final LookupReferencesManager manager = EasyMock.createStrictMock(LookupReferencesManager.class);
managerReturnsMap(manager);
EasyMock.replay(manager);
final RegisteredLookupExtractionFn fn = RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
true,
null,
true,
false
);
EasyMock.verify(manager);
final TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>()
{
};
final Map<String, Object> result = mapper.readValue(mapper.writeValueAsString(fn), typeReference);
Assert.assertEquals(mapper.convertValue(fn, typeReference), result);
Assert.assertEquals(LOOKUP_NAME, result.get("lookup"));
Assert.assertEquals(true, result.get("retainMissingValue"));
Assert.assertEquals(true, result.get("injective"));
Assert.assertNull(result.get("replaceMissingValueWith"));
Assert.assertEquals(false, result.get("optimize"));
}
@Test
public void testEquals()
{
final LookupReferencesManager manager = EasyMock.createStrictMock(LookupReferencesManager.class);
managerReturnsMap(manager);
EasyMock.replay(manager);
final RegisteredLookupExtractionFn fn = RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
false,
"something",
true,
false
);
Assert.assertEquals(
fn,
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
false,
"something",
true,
false
)
);
Assert.assertNotEquals(
fn,
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
true,
null,
true,
false
)
);
Assert.assertNotEquals(
fn,
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
false,
"something else",
true,
false
)
);
Assert.assertNotEquals(
fn,
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
false,
"something",
false,
false
)
);
Assert.assertNotEquals(
fn,
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
false,
"something",
true,
true
)
);
Assert.assertNotEquals(
fn,
RegisteredLookupExtractionFn.create(
manager,
LOOKUP_NAME,
false,
null,
true,
false
)
);
EasyMock.verify(manager);
}
private void managerReturnsMap(LookupReferencesManager manager)
{
EasyMock.expect(manager.get(EasyMock.eq(LOOKUP_NAME))).andReturn(new LookupExtractorFactory()
{
@Override
public boolean start()
{
return false;
}
@Override
public boolean close()
{
return false;
}
@Override
public boolean replaces(@Nullable LookupExtractorFactory other)
{
return false;
}
@Override
public LookupExtractor get()
{
return LOOKUP_EXTRACTOR;
}
}).anyTimes();
}
}