adding lower and upper extraction fn

This commit is contained in:
Slim Bouguerra 2016-01-20 14:39:51 -06:00
parent 0c5f4b947c
commit 78feb3a13e
7 changed files with 310 additions and 1 deletions

View File

@ -371,3 +371,23 @@ Following filtered dimension spec retains only the values matching regex. Note t
```
For more details and examples, see [multi-valued dimensions](multi-valued-dimensions.html).
### Upper and Lower extraction functions.
Returns the dimension values as all upper case or lower case.
Optionally user can specify the language to use in order to perform upper or lower transformation
```json
{
"type" : "upper",
"locale":"fr"
}
```
or without setting "locale" (in this case, the current value of the default locale for this instance of the Java Virtual Machine.)
```json
{
"type" : "lower"
}
```

View File

@ -35,4 +35,6 @@ public class ExtractionCacheHelper
public static final byte CACHE_TYPE_ID_SUBSTRING = 0x8;
public static final byte CACHE_TYPE_ID_CASCADE = 0x9;
public static final byte CACHE_TYPE_ID_STRING_FORMAT = 0xA;
public static final byte CACHE_TYPE_ID_UPPER = 0xB;
public static final byte CACHE_TYPE_ID_LOWER = 0xC;
}

View File

@ -36,7 +36,9 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonSubTypes.Type(name = "lookup", value = LookupExtractionFn.class),
@JsonSubTypes.Type(name = "substring", value = SubstringDimExtractionFn.class),
@JsonSubTypes.Type(name = "cascade", value = CascadeExtractionFn.class),
@JsonSubTypes.Type(name = "stringFormat", value = StringFormatExtractionFn.class)
@JsonSubTypes.Type(name = "stringFormat", value = StringFormatExtractionFn.class),
@JsonSubTypes.Type(name = "upper", value = UpperExtractionFn.class),
@JsonSubTypes.Type(name = "lower", value = LowerExtractionFn.class)
})
/**
* An ExtractionFn is a function that can be used to transform the values of a column (typically a dimension)

View File

@ -0,0 +1,97 @@
/*
*
* 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 com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.Strings;
import com.metamx.common.StringUtils;
import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.Locale;
@JsonTypeName("lower")
public class LowerExtractionFn implements ExtractionFn
{
private final Locale locale;
@JsonProperty
private final String localeString;
public LowerExtractionFn(@JsonProperty("locale") String localeString)
{
this.localeString = localeString;
this.locale = localeString == null ? Locale.getDefault() : Locale.forLanguageTag(localeString);
}
/**
* @param key string input of extraction function
*
* @return new string with all of the characters in {@code key} as an lower case or <tt>null</tt> if {@code key} is empty or null
*/
@Nullable
@Override
public String apply(String key)
{
if (Strings.isNullOrEmpty(key)) {
return null;
}
return key.toLowerCase(locale);
}
@Override
public String apply(long value)
{
return apply(String.valueOf(value));
}
@Override
public boolean preservesOrdering()
{
return false;
}
@Override
public ExtractionType getExtractionType()
{
return ExtractionType.MANY_TO_ONE;
}
@Override
public byte[] getCacheKey()
{
byte[] localeBytes = StringUtils.toUtf8(Strings.nullToEmpty(localeString));
return ByteBuffer.allocate(2 + localeBytes.length)
.put(ExtractionCacheHelper.CACHE_TYPE_ID_LOWER)
.put((byte) 0XFF)
.put(localeBytes)
.array();
}
@Override
public String apply(Object value)
{
return apply(String.valueOf(value));
}
}

View File

@ -0,0 +1,96 @@
/*
*
* 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 com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.Strings;
import com.metamx.common.StringUtils;
import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.Locale;
@JsonTypeName("upper")
public class UpperExtractionFn implements ExtractionFn
{
private final Locale locale;
@JsonProperty
private final String localeString;
public UpperExtractionFn(@JsonProperty("locale") String localeString)
{
this.localeString = localeString;
this.locale = localeString == null ? Locale.getDefault() : Locale.forLanguageTag(localeString);
}
/**
* @param key string input of extraction function
*
* @return new string with all of the characters in {@code key} as an upper case or <tt>null</tt> if {@code key} is empty or null
*/
@Nullable
@Override
public String apply(String key)
{
if (Strings.isNullOrEmpty(key)) {
return null;
}
return key.toUpperCase(locale);
}
@Override
public String apply(long value)
{
return apply(String.valueOf(value));
}
@Override
public boolean preservesOrdering()
{
return false;
}
@Override
public ExtractionType getExtractionType()
{
return ExtractionType.MANY_TO_ONE;
}
@Override
public byte[] getCacheKey()
{
byte[] localeBytes = StringUtils.toUtf8(Strings.nullToEmpty(localeString));
return ByteBuffer.allocate(2 + localeBytes.length)
.put(ExtractionCacheHelper.CACHE_TYPE_ID_UPPER)
.put((byte) 0XFF)
.put(localeBytes)
.array();
}
@Override
public String apply(Object value)
{
return apply(String.valueOf(value));
}
}

View File

@ -0,0 +1,46 @@
/*
*
* 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 org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
public class LowerExtractionFnTest
{
ExtractionFn extractionFn = new LowerExtractionFn(null);
@Test
public void testApply()
{
Assert.assertEquals("lower 1 string", extractionFn.apply("lOwER 1 String"));
Assert.assertEquals(null, extractionFn.apply(""));
Assert.assertEquals(null, extractionFn.apply(null));
}
@Test
public void testGetCacheKey()
{
Assert.assertArrayEquals(extractionFn.getCacheKey(), extractionFn.getCacheKey());
Assert.assertFalse(Arrays.equals(extractionFn.getCacheKey(), new UpperExtractionFn(null).getCacheKey()));
}
}

View File

@ -0,0 +1,46 @@
/*
*
* 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 org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
public class UpperExtractionFnTest
{
ExtractionFn extractionFn = new UpperExtractionFn(null);
@Test
public void testApply()
{
Assert.assertEquals("UPPER", extractionFn.apply("uPpeR"));
Assert.assertEquals(null, extractionFn.apply(""));
Assert.assertEquals(null, extractionFn.apply(null));
}
@Test
public void testGetCacheKey()
{
Assert.assertArrayEquals(extractionFn.getCacheKey(), extractionFn.getCacheKey());
Assert.assertFalse(Arrays.equals(extractionFn.getCacheKey(), new LowerExtractionFn(null).getCacheKey()));
}
}