-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: Adds MONTHNAME, DAYNAME and QUARTER functions #33411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3641b55
Added monthname, dayname and quarter functions
astefan 52241ae
More tests, minor adjustements
astefan 7e27176
Another case where the marked bug reports needs a fix for
astefan 3dbfaa1
Added monthname, dayname and quarter functions
astefan 6df006a
Updated docs tests with the new functions
astefan 0ec7108
More tests, minor adjustements
astefan 53cf547
Another case where the marked bug reports needs a fix for
astefan 8395375
Minor javadoc adjustement
astefan 010e40c
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
astefan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need alias here and next line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, ignore this, Checked already. It's because
DayName
becomesday_name
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make the class
Dayname
instead then? Do we want to supportDAY_NAME
as well asDAYNAME
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nik9000 I chose it like this because of consistency. There are other date/time functions that have aliases, even though the spec mentions only one name. For example WEEK (WEEK_OF_YEAR), HOUR (HOUR_OF_DAY) etc.