forked from opensearch-project/sql
-
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 OpenSearchDateType as a datatype for matching with Date/Time Open…
…Search types Signed-off-by: Andrew Carbonetto <[email protected]>
- Loading branch information
1 parent
31148da
commit 8b2f65d
Showing
7 changed files
with
265 additions
and
106 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
89 changes: 89 additions & 0 deletions
89
opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDateType.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,89 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.data.type; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.StringTokenizer; | ||
import lombok.EqualsAndHashCode; | ||
import org.joda.time.DateTime; | ||
import org.opensearch.sql.data.type.ExprType; | ||
|
||
/** | ||
* Of type join with relations. See | ||
* <a href="https://opensearch.org/docs/latest/opensearch/supported-field-types/join/">doc</a> | ||
*/ | ||
@EqualsAndHashCode(callSuper = false) | ||
public class OpenSearchDateType extends OpenSearchDataType { | ||
|
||
private static final OpenSearchDateType instance = new OpenSearchDateType(); | ||
|
||
|
||
// a read-only collection of relations | ||
@EqualsAndHashCode.Exclude | ||
DateTimeFormatter format; | ||
|
||
private OpenSearchDateType() { | ||
super(MappingType.Date); | ||
exprCoreType = UNKNOWN; | ||
} | ||
|
||
/** | ||
* Create a Date type which has a LinkedHashMap defining all formats | ||
* @return A new type object. | ||
*/ | ||
public static OpenSearchDateType of(String format) { | ||
var res = new OpenSearchDateType(); | ||
|
||
// Initialize the format based on the given string | ||
try { | ||
if (format.contains("||")) { | ||
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); | ||
for (String token: format.split("\\|\\|")) { | ||
builder.appendPattern(token); | ||
} | ||
res.format = builder.toFormatter(); | ||
} else { | ||
res.format = DateTimeFormatter.ofPattern(format); | ||
} | ||
} catch (IllegalArgumentException iae) { | ||
// invalid format - skipping | ||
// TODO: warn the user that the format is illegal in the mapping | ||
} | ||
return res; | ||
} | ||
|
||
public static OpenSearchDateType of(DateTimeFormatter format) { | ||
var res = new OpenSearchDateType(); | ||
res.format = format; | ||
return res; | ||
} | ||
|
||
public static OpenSearchDateType of() { | ||
return OpenSearchDateType.instance; | ||
} | ||
|
||
@Override | ||
public List<ExprType> getParent() { | ||
return List.of(STRING); | ||
} | ||
|
||
@Override | ||
public boolean shouldCast(ExprType other) { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected OpenSearchDataType cloneEmpty() { | ||
return OpenSearchDateType.of(this.format); | ||
} | ||
} |
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
Oops, something went wrong.