forked from apache/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "strlen" extractionFn. (apache#3731)
- Loading branch information
Showing
5 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
processing/src/main/java/io/druid/query/extraction/StrlenExtractionFn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.JsonCreator; | ||
|
||
public class StrlenExtractionFn extends DimExtractionFn | ||
{ | ||
private static final StrlenExtractionFn INSTANCE = new StrlenExtractionFn(); | ||
|
||
private StrlenExtractionFn() | ||
{ | ||
} | ||
|
||
@JsonCreator | ||
public static StrlenExtractionFn instance() | ||
{ | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public String apply(String value) | ||
{ | ||
return String.valueOf(value == null ? 0 : value.length()); | ||
} | ||
|
||
@Override | ||
public boolean preservesOrdering() | ||
{ | ||
return false; | ||
} | ||
|
||
@Override | ||
public ExtractionType getExtractionType() | ||
{ | ||
return ExtractionType.MANY_TO_ONE; | ||
} | ||
|
||
@Override | ||
public byte[] getCacheKey() | ||
{ | ||
return new byte[]{ExtractionCacheHelper.CACHE_TYPE_ID_STRLEN}; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
processing/src/test/java/io/druid/query/extraction/StrlenExtractionFnTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.databind.ObjectMapper; | ||
import io.druid.jackson.DefaultObjectMapper; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class StrlenExtractionFnTest | ||
{ | ||
@Test | ||
public void testApply() | ||
{ | ||
Assert.assertEquals("0", StrlenExtractionFn.instance().apply(null)); | ||
Assert.assertEquals("0", StrlenExtractionFn.instance().apply("")); | ||
Assert.assertEquals("1", StrlenExtractionFn.instance().apply("x")); | ||
Assert.assertEquals("3", StrlenExtractionFn.instance().apply("foo")); | ||
Assert.assertEquals("3", StrlenExtractionFn.instance().apply("föo")); | ||
Assert.assertEquals("2", StrlenExtractionFn.instance().apply("\uD83D\uDE02")); | ||
Assert.assertEquals("1", StrlenExtractionFn.instance().apply(1)); | ||
Assert.assertEquals("2", StrlenExtractionFn.instance().apply(-1)); | ||
} | ||
|
||
@Test | ||
public void testGetCacheKey() | ||
{ | ||
Assert.assertArrayEquals(StrlenExtractionFn.instance().getCacheKey(), StrlenExtractionFn.instance().getCacheKey()); | ||
} | ||
|
||
@Test | ||
public void testSerde() throws Exception | ||
{ | ||
final ObjectMapper objectMapper = new DefaultObjectMapper(); | ||
|
||
final String json = "{ \"type\" : \"strlen\" }"; | ||
|
||
StrlenExtractionFn extractionFn = (StrlenExtractionFn) objectMapper.readValue(json, ExtractionFn.class); | ||
StrlenExtractionFn extractionFnRoundTrip = (StrlenExtractionFn) objectMapper.readValue( | ||
objectMapper.writeValueAsString(extractionFn), | ||
ExtractionFn.class | ||
); | ||
|
||
// Should all actually be the same instance. | ||
Assert.assertTrue(extractionFn == extractionFnRoundTrip); | ||
Assert.assertTrue(extractionFn == StrlenExtractionFn.instance()); | ||
} | ||
} |