Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.extraction;

import io.druid.segment.column.ValueAccessor;

/**
*/
public abstract class AbstractExtractionFn implements ExtractionFn
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making it abstract class is somehow not very user friendly, assume i have my class that already extends another super class related to my own org, by making this abstract class you make it impossible to use it as an extraction function, unless i have to create the object within the class it self. i think we should find a way to make it as an interface.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an utility class for simplicity. We can always implement ExtractionFn directly.

{
protected transient ValueAccessor accessor = ValueAccessor.STRING;

@Override
public void init(ValueAccessor accessor)
{
this.accessor = accessor;
}

@Override
public boolean preservesOrdering()
{
return false;
}

@Override
public ExtractionType getExtractionType()
{
return ExtractionType.MANY_TO_ONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,29 @@
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Bytes;
import io.druid.segment.column.ValueAccessor;

import java.util.Arrays;

public class CascadeExtractionFn implements ExtractionFn
public class CascadeExtractionFn extends AbstractExtractionFn
{
private final ExtractionFn extractionFns[];
private final ChainedExtractionFn chainedExtractionFn;
private final ChainedExtractionFn DEFAULT_CHAINED_EXTRACTION_FN = new ChainedExtractionFn(
new ExtractionFn()
new AbstractExtractionFn()
{
@Override
public byte[] getCacheKey()
{
return new byte[0];
}

@Override
public String apply(Object value)
{
return null;
}

public String apply(String value)
{
return null;
}

public String apply(long value)
{
return null;
}

public boolean preservesOrdering()
{
return false;
}

public ExtractionType getExtractionType()
{
return ExtractionType.MANY_TO_ONE;
}

@Override
public String toString()
{
Expand Down Expand Up @@ -99,27 +82,21 @@ public ExtractionFn[] getExtractionFns()
}

@Override
public byte[] getCacheKey()
public void init(ValueAccessor accessor)
{
byte[] cacheKey = new byte[]{ExtractionCacheHelper.CACHE_TYPE_ID_CASCADE};

return Bytes.concat(cacheKey, chainedExtractionFn.getCacheKey());
chainedExtractionFn.init(accessor);
}

@Override
public String apply(Object value)
public byte[] getCacheKey()
{
return chainedExtractionFn.apply(value);
}
byte[] cacheKey = new byte[]{ExtractionCacheHelper.CACHE_TYPE_ID_CASCADE};

@Override
public String apply(String value)
{
return chainedExtractionFn.apply(value);
return Bytes.concat(cacheKey, chainedExtractionFn.getCacheKey());
}

@Override
public String apply(long value)
public String apply(Object value)
{
return chainedExtractionFn.apply(value);
}
Expand Down Expand Up @@ -171,7 +148,7 @@ public String toString()
"extractionFns=[" + chainedExtractionFn.toString() + "]}";
}

private class ChainedExtractionFn
private class ChainedExtractionFn implements ExtractionFn
{
private final ExtractionFn fn;
private final ChainedExtractionFn child;
Expand All @@ -189,17 +166,16 @@ public byte[] getCacheKey()
return (child != null) ? Bytes.concat(fnCacheKey, child.getCacheKey()) : fnCacheKey;
}

public String apply(Object value)
{
return fn.apply((child != null) ? child.apply(value) : value);
}

public String apply(String value)
@Override
public void init(ValueAccessor accessor)
{
return fn.apply((child != null) ? child.apply(value) : value);
fn.init(accessor);
if (child != null) {
child.init(accessor);
}
}

public String apply(long value)
public String apply(Object value)
{
return fn.apply((child != null) ? child.apply(value) : value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@

package io.druid.query.extraction;

public abstract class DimExtractionFn implements ExtractionFn
public abstract class DimExtractionFn extends AbstractExtractionFn
{
@Override
public String apply(Object value)
{
return apply(value == null ? null : value.toString());
public String apply(Object value) {
return apply(accessor.getString(value));
}

@Override
public String apply(long value)
{
return apply(Long.toString(value));
}
protected abstract String apply(String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.druid.query.lookup.LookupExtractionFn;
import io.druid.query.lookup.RegisteredLookupExtractionFn;
import io.druid.segment.column.ValueAccessor;

/**
*/
Expand Down Expand Up @@ -60,6 +61,8 @@ public interface ExtractionFn
*/
public byte[] getCacheKey();

public void init(ValueAccessor accessor);

/**
* The "extraction" function. This should map an Object into some String value.
* <p>
Expand All @@ -74,33 +77,9 @@ public interface ExtractionFn
*/
public String apply(Object value);

/**
* The "extraction" function. This should map a String value into some other String value.
* <p>
* Like {@link #apply(Object)}, the empty string is considered invalid output for this method and it should
* instead return null.
*
* @param value the original value of the dimension
*
* @return a value that should be used instead of the original
*/
public String apply(String value);

/**
* The "extraction" function. This should map a long value into some String value.
* <p>
* Like {@link #apply(Object)}, the empty string is considered invalid output for this method and it should
* instead return null.
*
* @param value the original value of the dimension
*
* @return a value that should be used instead of the original
*/
public String apply(long value);

/**
* Offers information on whether the extraction will preserve the original ordering of the values.
* <p>
* <p/>
* Some optimizations of queries is possible if ordering is preserved. Null values *do* count towards
* ordering.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ public String apply(String value)
return extractionFunction.apply(value);
}

@Override
public boolean preservesOrdering()
{
return false;
}

@Override
public ExtractionType getExtractionType()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.google.common.base.Strings;

public class IdentityExtractionFn implements ExtractionFn
public class IdentityExtractionFn extends AbstractExtractionFn
{
private static final IdentityExtractionFn instance = new IdentityExtractionFn();

Expand All @@ -42,18 +42,6 @@ public String apply(Object value)
return value == null ? null : Strings.emptyToNull(value.toString());
}

@Override
public String apply(String value)
{
return Strings.emptyToNull(value);
}

@Override
public String apply(long value)
{
return Long.toString(value);
}

@Override
public boolean preservesOrdering()
{
Expand All @@ -78,7 +66,7 @@ public boolean equals(Object o)
return o != null && o instanceof IdentityExtractionFn;
}

public static final IdentityExtractionFn getInstance()
public static IdentityExtractionFn getInstance()
{
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.metamx.common.StringUtils;
import io.druid.segment.column.ValueAccessor;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;

import java.nio.ByteBuffer;

public class JavaScriptExtractionFn implements ExtractionFn
public class JavaScriptExtractionFn extends AbstractExtractionFn
{
private static Function<Object, String> compile(String function)
{
Expand Down Expand Up @@ -103,21 +104,12 @@ public byte[] getCacheKey()
@Override
public String apply(Object value)
{
if (accessor == ValueAccessor.STRING) {
value = Strings.emptyToNull(accessor.getString(value));
}
return Strings.emptyToNull(fn.apply(value));
}

@Override
public String apply(String value)
{
return this.apply((Object) Strings.emptyToNull(value));
}

@Override
public String apply(long value)
{
return this.apply((Long) value);
}

@Override
public boolean preservesOrdering()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Locale;

@JsonTypeName("lower")
public class LowerExtractionFn extends DimExtractionFn
public class LowerExtractionFn extends AbstractExtractionFn
{
private final Locale locale;

Expand All @@ -52,24 +52,10 @@ public LowerExtractionFn(@JsonProperty("locale") String localeString)

@Nullable
@Override
public String apply(String key)
public String apply(Object key)
{
if (Strings.isNullOrEmpty(key)) {
return null;
}
return key.toLowerCase(locale);
}

@Override
public boolean preservesOrdering()
{
return false;
}

@Override
public ExtractionType getExtractionType()
{
return ExtractionType.MANY_TO_ONE;
final String value = accessor.getString(key);
return Strings.isNullOrEmpty(value) ? null : value.toLowerCase(locale);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public byte[] getCacheKey()
}

@Override
public String apply(String dimValue)
protected String apply(String dimValue)
{
if (Strings.isNullOrEmpty(dimValue)) {
// We'd return null whether or not the pattern matched
Expand All @@ -75,18 +75,6 @@ public String getExpr()
return expr;
}

@Override
public boolean preservesOrdering()
{
return false;
}

@Override
public ExtractionType getExtractionType()
{
return ExtractionType.MANY_TO_ONE;
}

@Override
public String toString()
{
Expand Down
Loading