Skip to content
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

SQL: Add filtering to SYS TYPES #35852

Merged
merged 4 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ statement
| SYS COLUMNS (CATALOG cluster=string)?
(TABLE tableLike=likePattern | tableIdent=tableIdentifier)?
(columnPattern=likePattern)? #sysColumns
| SYS TYPES #sysTypes
| SYS TYPES ((PLUS | MINUS)? type=number)? #sysTypes
| SYS TABLE TYPES #sysTableTypes
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.antlr.v4.runtime.Token;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.xpack.sql.analysis.index.IndexResolver.IndexType;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.DebugContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ExplainContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ShowColumnsContext;
Expand Down Expand Up @@ -190,7 +191,13 @@ public Object visitSysColumns(SysColumnsContext ctx) {

@Override
public SysTypes visitSysTypes(SysTypesContext ctx) {
return new SysTypes(source(ctx));
int type = 0;
if (ctx.type != null) {
Literal value = (Literal) visit(ctx.type);
type = ((Number) value.fold()).intValue();
}

return new SysTypes(source(ctx), Integer.valueOf(type));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
import org.elasticsearch.xpack.sql.expression.literal.Interval;
import org.elasticsearch.xpack.sql.expression.literal.IntervalDayTime;
import org.elasticsearch.xpack.sql.expression.literal.IntervalYearMonth;
import org.elasticsearch.xpack.sql.expression.literal.Intervals;
import org.elasticsearch.xpack.sql.expression.literal.Intervals.TimeUnit;
import org.elasticsearch.xpack.sql.expression.literal.IntervalYearMonth;
import org.elasticsearch.xpack.sql.expression.predicate.Range;
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.MatchQueryPredicate;
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.MultiMatchQueryPredicate;
Expand Down Expand Up @@ -97,6 +97,7 @@
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StringLiteralContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StringQueryContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SubqueryExpressionContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysTypesContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.TimeEscapedLiteralContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.TimestampEscapedLiteralContext;
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
Expand Down Expand Up @@ -653,12 +654,14 @@ public Literal visitIntegerLiteral(IntegerLiteralContext ctx) {
throw new ParsingException(source(ctx), siae.getMessage());
}

Object val = Long.valueOf(value);
DataType type = DataType.LONG;
// try to downsize to int if possible (since that's the most common type)
if ((int) value == value) {
type = DataType.INTEGER;
val = Integer.valueOf((int) value);
}
return new Literal(source(ctx), value, type);
return new Literal(source(ctx), val, type);
}

@Override
Expand Down Expand Up @@ -836,6 +839,8 @@ private boolean hasMinusFromParent(SqlBaseParser.NumberContext ctx) {
} else if (parentCtx instanceof SqlBaseParser.IntervalContext) {
IntervalContext ic = (IntervalContext) parentCtx;
return ic.sign != null && ic.sign.getType() == SqlBaseParser.MINUS;
} else if (parentCtx instanceof SqlBaseParser.SysTypesContext) {
return ((SysTypesContext) parentCtx).MINUS() != null;
}
}
return false;
Expand Down
Loading