Skip to content

Commit 1d633cf

Browse files
committed
SQL: Add filtering to SYS TYPES (#35852)
Fix #35342 (cherry picked from commit cd822b7) (cherry picked from commit 633a562) (cherry picked from commit 971299b)
1 parent abd7288 commit 1d633cf

File tree

7 files changed

+878
-710
lines changed

7 files changed

+878
-710
lines changed

x-pack/plugin/sql/src/main/antlr/SqlBase.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ statement
6262
| SYS COLUMNS (CATALOG cluster=string)?
6363
(TABLE tableLike=likePattern | tableIdent=tableIdentifier)?
6464
(columnPattern=likePattern)? #sysColumns
65-
| SYS TYPES #sysTypes
65+
| SYS TYPES ((PLUS | MINUS)? type=number)? #sysTypes
6666
| SYS TABLE TYPES #sysTableTypes
6767
;
6868

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/CommandBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.antlr.v4.runtime.Token;
99
import org.elasticsearch.common.Booleans;
1010
import org.elasticsearch.xpack.sql.analysis.index.IndexResolver.IndexType;
11+
import org.elasticsearch.xpack.sql.expression.Literal;
1112
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.DebugContext;
1213
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ExplainContext;
1314
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ShowColumnsContext;
@@ -190,7 +191,13 @@ public Object visitSysColumns(SysColumnsContext ctx) {
190191

191192
@Override
192193
public SysTypes visitSysTypes(SysTypesContext ctx) {
193-
return new SysTypes(source(ctx));
194+
int type = 0;
195+
if (ctx.type != null) {
196+
Literal value = (Literal) visit(ctx.type);
197+
type = ((Number) value.fold()).intValue();
198+
}
199+
200+
return new SysTypes(source(ctx), Integer.valueOf(type));
194201
}
195202

196203
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
2727
import org.elasticsearch.xpack.sql.expression.literal.Interval;
2828
import org.elasticsearch.xpack.sql.expression.literal.IntervalDayTime;
29+
import org.elasticsearch.xpack.sql.expression.literal.IntervalYearMonth;
2930
import org.elasticsearch.xpack.sql.expression.literal.Intervals;
3031
import org.elasticsearch.xpack.sql.expression.literal.Intervals.TimeUnit;
31-
import org.elasticsearch.xpack.sql.expression.literal.IntervalYearMonth;
3232
import org.elasticsearch.xpack.sql.expression.predicate.Range;
3333
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.MatchQueryPredicate;
3434
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.MultiMatchQueryPredicate;
@@ -97,6 +97,7 @@
9797
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StringLiteralContext;
9898
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StringQueryContext;
9999
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SubqueryExpressionContext;
100+
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysTypesContext;
100101
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.TimeEscapedLiteralContext;
101102
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.TimestampEscapedLiteralContext;
102103
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
@@ -653,12 +654,14 @@ public Literal visitIntegerLiteral(IntegerLiteralContext ctx) {
653654
throw new ParsingException(source(ctx), siae.getMessage());
654655
}
655656

657+
Object val = Long.valueOf(value);
656658
DataType type = DataType.LONG;
657659
// try to downsize to int if possible (since that's the most common type)
658660
if ((int) value == value) {
659661
type = DataType.INTEGER;
662+
val = Integer.valueOf((int) value);
660663
}
661-
return new Literal(source(ctx), value, type);
664+
return new Literal(source(ctx), val, type);
662665
}
663666

664667
@Override
@@ -836,6 +839,8 @@ private boolean hasMinusFromParent(SqlBaseParser.NumberContext ctx) {
836839
} else if (parentCtx instanceof SqlBaseParser.IntervalContext) {
837840
IntervalContext ic = (IntervalContext) parentCtx;
838841
return ic.sign != null && ic.sign.getType() == SqlBaseParser.MINUS;
842+
} else if (parentCtx instanceof SqlBaseParser.SysTypesContext) {
843+
return ((SysTypesContext) parentCtx).MINUS() != null;
839844
}
840845
}
841846
return false;

0 commit comments

Comments
 (0)