-
Notifications
You must be signed in to change notification settings - Fork 25.7k
SQL: Make Literal a NamedExpression #33583
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,17 +12,28 @@ | |
| import org.elasticsearch.xpack.sql.type.DataTypeConversion; | ||
| import org.elasticsearch.xpack.sql.type.DataTypes; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class Literal extends LeafExpression { | ||
| import static java.util.Collections.emptyList; | ||
|
|
||
| /** | ||
| * SQL Literal or constant. | ||
| */ | ||
| public class Literal extends NamedExpression { | ||
|
|
||
| public static final Literal TRUE = Literal.of(Location.EMPTY, Boolean.TRUE); | ||
| public static final Literal FALSE = Literal.of(Location.EMPTY, Boolean.FALSE); | ||
|
|
||
| private final Object value; | ||
| private final DataType dataType; | ||
|
|
||
| public Literal(Location location, Object value, DataType dataType) { | ||
| super(location); | ||
| this(location, null, value, dataType); | ||
| } | ||
|
|
||
| public Literal(Location location, String name, Object value, DataType dataType) { | ||
| super(location, name == null ? String.valueOf(value) : name, emptyList(), null); | ||
| this.dataType = dataType; | ||
| this.value = DataTypeConversion.convert(value, dataType); | ||
| } | ||
|
|
@@ -61,48 +72,83 @@ public Object fold() { | |
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public Attribute toAttribute() { | ||
| return new LiteralAttribute(location(), name(), null, false, id(), false, dataType, this); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression replaceChildren(List<Expression> newChildren) { | ||
| throw new UnsupportedOperationException("this type of node doesn't have any children to replace"); | ||
| } | ||
|
|
||
| @Override | ||
| public AttributeSet references() { | ||
| return AttributeSet.EMPTY; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value, dataType); | ||
| return Objects.hash(name(), value, dataType); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
|
|
||
| if (obj == null || getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| Literal other = (Literal) obj; | ||
| return Objects.equals(value, other.value) | ||
| return Objects.equals(name(), other.name()) | ||
| && Objects.equals(value, other.value) | ||
| && Objects.equals(dataType, other.dataType); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return Objects.toString(value); | ||
| String s = String.valueOf(value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to store the String value to a class attribute (minor perf improv if multiple calls on
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return name().equals(s) ? s : name() + "=" + value; | ||
| } | ||
|
|
||
| /** | ||
| * Utility method for creating 'in-line' Literals (out of values instead of expressions). | ||
| */ | ||
| public static Literal of(Location loc, Object value) { | ||
| if (value instanceof Literal) { | ||
| return (Literal) value; | ||
| } | ||
| return new Literal(loc, value, DataTypes.fromJava(value)); | ||
| } | ||
|
|
||
| /** | ||
| * Utility method for creating a literal out of a foldable expression. | ||
| * Throws an exception if the expression is not foldable. | ||
| */ | ||
| public static Literal of(Expression foldable) { | ||
| if (foldable instanceof Literal) { | ||
| return (Literal) foldable; | ||
| } | ||
| return of((String) null, foldable); | ||
| } | ||
|
|
||
| public static Literal of(String name, Expression foldable) { | ||
| if (!foldable.foldable()) { | ||
| throw new SqlIllegalArgumentException("Foldable expression required for Literal creation; received unfoldable " + foldable); | ||
| } | ||
|
|
||
| return new Literal(foldable.location(), foldable.fold(), foldable.dataType()); | ||
| if (foldable instanceof Literal) { | ||
| Literal l = (Literal) foldable; | ||
| if (name == null || l.name().equals(name)) { | ||
| return l; | ||
| } | ||
| } | ||
|
|
||
| Object fold = foldable.fold(); | ||
|
|
||
| if (name == null) { | ||
| name = foldable instanceof NamedExpression ? ((NamedExpression) foldable).name() : String.valueOf(fold); | ||
| } | ||
|
|
||
| return new Literal(foldable.location(), name, foldable.fold(), foldable.dataType()); | ||
|
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.