forked from elastic/elasticsearch
/
Expression.java
140 lines (111 loc) · 3.93 KB
/
Expression.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* 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;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.capabilities.Resolvable;
import org.elasticsearch.xpack.sql.capabilities.Resolvables;
import org.elasticsearch.xpack.sql.tree.Source;
import org.elasticsearch.xpack.sql.tree.Node;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.StringUtils;
import java.util.List;
import java.util.Locale;
import static java.lang.String.format;
/**
* In a SQL statement, an Expression is whatever a user specifies inside an
* action, so for instance:
*
* {@code SELECT a, b, MAX(c, d) FROM i}
*
* a, b, ABS(c), and i are all Expressions, with ABS(c) being a Function
* (which is a type of expression) with a single child, c.
*/
public abstract class Expression extends Node<Expression> implements Resolvable {
public static class TypeResolution {
private final boolean failed;
private final String message;
public static final TypeResolution TYPE_RESOLVED = new TypeResolution(false, StringUtils.EMPTY);
public TypeResolution(String message) {
this(true, message);
}
TypeResolution(String message, Object... args) {
this(true, format(Locale.ROOT, message, args));
}
private TypeResolution(boolean unresolved, String message) {
this.failed = unresolved;
this.message = message;
}
public boolean unresolved() {
return failed;
}
public boolean resolved() {
return !failed;
}
public String message() {
return message;
}
}
private TypeResolution lazyTypeResolution = null;
private Boolean lazyChildrenResolved = null;
private Expression lazyCanonical = null;
public Expression(Source location, List<Expression> children) {
super(location, children);
}
// whether the expression can be evaluated statically (folded) or not
public boolean foldable() {
return false;
}
public Object fold() {
throw new SqlIllegalArgumentException("Should not fold expression");
}
// whether the expression becomes null if at least one param/input is null
public abstract boolean nullable();
// the references/inputs/leaves of the expression tree
public AttributeSet references() {
return Expressions.references(children());
}
public boolean childrenResolved() {
if (lazyChildrenResolved == null) {
lazyChildrenResolved = Boolean.valueOf(Resolvables.resolved(children()));
}
return lazyChildrenResolved;
}
public final TypeResolution typeResolved() {
if (lazyTypeResolution == null) {
lazyTypeResolution = resolveType();
}
return lazyTypeResolution;
}
protected TypeResolution resolveType() {
return TypeResolution.TYPE_RESOLVED;
}
public final Expression canonical() {
if (lazyCanonical == null) {
lazyCanonical = canonicalize();
}
return lazyCanonical;
}
protected Expression canonicalize() {
return this;
}
public boolean semanticEquals(Expression other) {
return canonical().equals(other.canonical());
}
public int semanticHash() {
return canonical().hashCode();
}
@Override
public boolean resolved() {
return childrenResolved() && typeResolved().resolved();
}
public abstract DataType dataType();
@Override
public abstract int hashCode();
@Override
public String toString() {
return nodeName() + "[" + propertiesToString(false) + "]";
}
}