forked from elastic/elasticsearch
-
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.
SQL: Adds MONTHNAME, DAYNAME and QUARTER functions (elastic#33411)
* Added monthname, dayname and quarter functions * Updated docs tests with the new functions
- Loading branch information
Showing
30 changed files
with
838 additions
and
110 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
70 changes: 70 additions & 0 deletions
70
...org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeFunction.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,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime; | ||
|
||
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.expression.Expressions; | ||
import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunctionAttribute; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate; | ||
import org.elasticsearch.xpack.sql.tree.Location; | ||
import org.elasticsearch.xpack.sql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.sql.type.DataType; | ||
|
||
import java.util.TimeZone; | ||
|
||
abstract class BaseDateTimeFunction extends UnaryScalarFunction { | ||
|
||
private final TimeZone timeZone; | ||
private final String name; | ||
|
||
BaseDateTimeFunction(Location location, Expression field, TimeZone timeZone) { | ||
super(location, field); | ||
this.timeZone = timeZone; | ||
|
||
StringBuilder sb = new StringBuilder(super.name()); | ||
// add timezone as last argument | ||
sb.insert(sb.length() - 1, " [" + timeZone.getID() + "]"); | ||
|
||
this.name = sb.toString(); | ||
} | ||
|
||
@Override | ||
protected final NodeInfo<BaseDateTimeFunction> info() { | ||
return NodeInfo.create(this, ctorForInfo(), field(), timeZone()); | ||
} | ||
|
||
protected abstract NodeInfo.NodeCtor2<Expression, TimeZone, BaseDateTimeFunction> ctorForInfo(); | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (field().dataType() == DataType.DATE) { | ||
return TypeResolution.TYPE_RESOLVED; | ||
} | ||
return new TypeResolution("Function [" + functionName() + "] cannot be applied on a non-date expression ([" | ||
+ Expressions.name(field()) + "] of type [" + field().dataType().esType + "])"); | ||
} | ||
|
||
public TimeZone timeZone() { | ||
return timeZone; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean foldable() { | ||
return field().foldable(); | ||
} | ||
|
||
@Override | ||
protected ScriptTemplate asScriptFrom(AggregateFunctionAttribute aggregate) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...rg/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeProcessor.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,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor; | ||
import org.joda.time.ReadableInstant; | ||
|
||
import java.io.IOException; | ||
import java.util.TimeZone; | ||
|
||
public abstract class BaseDateTimeProcessor implements Processor { | ||
|
||
private final TimeZone timeZone; | ||
|
||
BaseDateTimeProcessor(TimeZone timeZone) { | ||
this.timeZone = timeZone; | ||
} | ||
|
||
BaseDateTimeProcessor(StreamInput in) throws IOException { | ||
timeZone = TimeZone.getTimeZone(in.readString()); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(timeZone.getID()); | ||
} | ||
|
||
TimeZone timeZone() { | ||
return timeZone; | ||
} | ||
|
||
@Override | ||
public Object process(Object l) { | ||
if (l == null) { | ||
return null; | ||
} | ||
long millis; | ||
if (l instanceof String) { | ||
// 6.4+ | ||
millis = Long.parseLong(l.toString()); | ||
} else if (l instanceof ReadableInstant) { | ||
// 6.3- | ||
millis = ((ReadableInstant) l).getMillis(); | ||
} else { | ||
throw new SqlIllegalArgumentException("A string or a date is required; received {}", l); | ||
} | ||
|
||
return doProcess(millis); | ||
} | ||
|
||
abstract Object doProcess(long millis); | ||
} |
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.