Skip to content

Commit

Permalink
SQL: Add filtering to SYS TYPES
Browse files Browse the repository at this point in the history
  • Loading branch information
costin committed Nov 23, 2018
1 parent 43d6ec8 commit ce28627
Show file tree
Hide file tree
Showing 7 changed files with 877 additions and 709 deletions.
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 @@ -652,12 +653,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 @@ -835,6 +838,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

0 comments on commit ce28627

Please sign in to comment.