private static Expression createEstimateRepresentation(Estimate estimate) { if (estimate.isUnknown()) { return NULL_DOUBLE; } return new DoubleLiteral(Double.toString(estimate.getValue())); }
private static DoubleLiteral doubleLiteral(double value) { return new DoubleLiteral(Double.toString(value)); }
@Override public Node visitDoubleLiteral(SqlBaseParser.DoubleLiteralContext context) { return new DoubleLiteral(getLocation(context), context.getText()); }
private static Expression toExpression(Object value) throws PrestoException { if (value instanceof String) { return new StringLiteral(value.toString()); } if (value instanceof Boolean) { return new BooleanLiteral(value.toString()); } if (value instanceof Long || value instanceof Integer) { return new LongLiteral(value.toString()); } if (value instanceof Double) { return new DoubleLiteral(value.toString()); } if (value instanceof List) { List<?> list = (List<?>) value; return new ArrayConstructor(list.stream() .map(Visitor::toExpression) .collect(toList())); } throw new PrestoException(INVALID_TABLE_PROPERTY, format("Failed to convert object of type %s to expression: %s", value.getClass().getName(), value)); }
@Override public Result apply(SampleNode sample, Captures captures, Context context) { return Result.ofPlanNode(new FilterNode( sample.getId(), sample.getSource(), new ComparisonExpression( ComparisonExpression.Operator.LESS_THAN, new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()), new DoubleLiteral(Double.toString(sample.getSampleRatio()))))); } }
@Test public void testDouble() { assertExpression("123E7", new DoubleLiteral("123E7")); assertExpression("123.E7", new DoubleLiteral("123E7")); assertExpression("123.0E7", new DoubleLiteral("123E7")); assertExpression("123E+7", new DoubleLiteral("123E7")); assertExpression("123E-7", new DoubleLiteral("123E-7")); assertExpression("123.456E7", new DoubleLiteral("123.456E7")); assertExpression("123.456E+7", new DoubleLiteral("123.456E7")); assertExpression("123.456E-7", new DoubleLiteral("123.456E-7")); assertExpression(".4E42", new DoubleLiteral(".4E42")); assertExpression(".4E+42", new DoubleLiteral(".4E42")); assertExpression(".4E-42", new DoubleLiteral(".4E-42")); }
@Override public Node visitDecimalLiteral(SqlBaseParser.DecimalLiteralContext context) { switch (parsingOptions.getDecimalLiteralTreatment()) { case AS_DOUBLE: return new DoubleLiteral(getLocation(context), context.getText()); case AS_DECIMAL: return new DecimalLiteral(getLocation(context), context.getText()); case REJECT: throw new ParsingException("Unexpected decimal literal: " + context.getText()); } throw new AssertionError("Unreachable"); }
@Test public void testArrayConstructor() { assertExpression("ARRAY []", new ArrayConstructor(ImmutableList.of())); assertExpression("ARRAY [1, 2]", new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2")))); assertExpression("ARRAY [1e0, 2.5e0]", new ArrayConstructor(ImmutableList.of(new DoubleLiteral("1.0"), new DoubleLiteral("2.5")))); assertExpression("ARRAY ['hi']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi")))); assertExpression("ARRAY ['hi', 'hello']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi"), new StringLiteral("hello")))); }
@Test public void testDoubleInQuery() { assertStatement("SELECT 123.456E7 FROM DUAL", simpleQuery( selectList(new DoubleLiteral("123.456E7")), table(QualifiedName.of("DUAL")))); }
return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()); return new DoubleLiteral(object.toString());
@Test public void testValues() { Query valuesQuery = query(values( row(new StringLiteral("a"), new LongLiteral("1"), new DoubleLiteral("2.2")), row(new StringLiteral("b"), new LongLiteral("2"), new DoubleLiteral("3.3")))); assertStatement("VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0)", valuesQuery); assertStatement("SELECT * FROM (VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0))", simpleQuery( selectList(new AllColumns()), subquery(valuesQuery))); }
assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("y"), new DoubleLiteral("2.5"))) .outputRowsCount(250.0) // all rows minus nulls times range coverage (50%) .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("x"), new DoubleLiteral("10.0"))) .outputRowsCount(750.0) // all rows minus nulls times range coverage (100%) .symbolStats("x", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("x"), new DoubleLiteral("-10.0"))) .outputRowsCount(18.75) // all rows minus nulls divided by NDV (one value from edge is included as approximation) .symbolStats("x", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("y"), new DoubleLiteral("-10.0"))) .outputRowsCount(0.0) // all rows minus nulls times range coverage (0%) .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("leftOpen"), new DoubleLiteral("0.0"))) .outputRowsCount(450.0) // all rows minus nulls times range coverage (50% - heuristic) .symbolStats("leftOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("rightOpen"), new DoubleLiteral("0.0"))) .outputRowsCount(225.0) // all rows minus nulls times range coverage (25% - heuristic) .symbolStats("rightOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))) .outputRowsCount(450.0) // all rows minus nulls times range coverage (50% - heuristic) .symbolStats("unknownRange", symbolAssert -> { assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("emptyRange"), new DoubleLiteral("0.0")))
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("y"), new DoubleLiteral("2.5"))) .outputRowsCount(250.0) // all rows minus nulls times range coverage (50%) .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("x"), new DoubleLiteral("-10.0"))) .outputRowsCount(750.0) // all rows minus nulls times range coverage (100%) .symbolStats("x", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("x"), new DoubleLiteral("10.0"))) .outputRowsCount(18.75) // all rows minus nulls divided by NDV (one value from edge is included as approximation) .symbolStats("x", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("y"), new DoubleLiteral("10.0"))) .outputRowsCount(0.0) // all rows minus nulls times range coverage (0%) .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("leftOpen"), new DoubleLiteral("0.0"))) .outputRowsCount(225.0) // all rows minus nulls times range coverage (25% - heuristic) .symbolStats("leftOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("rightOpen"), new DoubleLiteral("0.0"))) .outputRowsCount(450.0) // all rows minus nulls times range coverage (50% - heuristic) .symbolStats("rightOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))) .outputRowsCount(450.0) // all rows minus nulls times range coverage (50% - heuristic) .symbolStats("unknownRange", symbolAssert -> { assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("emptyRange"), new DoubleLiteral("0.0")))
assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))) .outputRowsCount(475.0) // all rows minus nulls multiplied by ((distinct values - 1) / distinct values) .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))) .outputRowsCount(731.25) // all rows minus nulls multiplied by ((distinct values - 1) / distinct values) .symbolStats("x", symbolAssert -> { assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))) .outputRowsCount(500.0) // all rows minus nulls .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))) .outputRowsCount(882.0) // all rows minus nulls multiplied by ((distinct values - 1) / distinct values) .symbolStats("leftOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))) .outputRowsCount(882.0) // all rows minus nulls divided by distinct values count .symbolStats("rightOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))) .outputRowsCount(882.0) // all rows minus nulls divided by distinct values count .symbolStats("unknownRange", symbolAssert -> { assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))) .outputRowsCount(0.0) .symbolStats("emptyRange", equalTo(emptyRangeStats));
assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))) .outputRowsCount(25.0) // all rows minus nulls divided by distinct values count .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))) .outputRowsCount(18.75) // all rows minus nulls divided by distinct values count .symbolStats("x", symbolAssert -> { assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))) .outputRowsCount(0.0) // all rows minus nulls divided by distinct values count .symbolStats("y", symbolAssert -> { assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))) .outputRowsCount(18.0) // all rows minus nulls divided by distinct values count .symbolStats("leftOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))) .outputRowsCount(18.0) // all rows minus nulls divided by distinct values count .symbolStats("rightOpen", symbolAssert -> { assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))) .outputRowsCount(18.0) // all rows minus nulls divided by distinct values count .symbolStats("unknownRange", symbolAssert -> { assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))) .outputRowsCount(0.0) .symbolStats("emptyRange", equalTo(emptyRangeStats));
.nullsFraction(0.0); assertCalculate(new DoubleLiteral("7.5")) .distinctValuesCount(1.0) .lowValue(7.5)
@Override public Node visitDecimalLiteral(SqlBaseParser.DecimalLiteralContext context) { return new DoubleLiteral(getLocation(context), context.getText()); }
@Override public Node visitDoubleLiteral(SqlBaseParser.DoubleLiteralContext context) { return new DoubleLiteral(getLocation(context), context.getText()); }
@Test public void testDoubleInQuery() { assertStatement("SELECT 123.456E7 FROM DUAL", simpleQuery( selectList(new DoubleLiteral("123.456E7")), table(QualifiedName.of("DUAL")))); }
@Test public void testValues() { Query valuesQuery = query(values( row(new StringLiteral("a"), new LongLiteral("1"), new DoubleLiteral("2.2")), row(new StringLiteral("b"), new LongLiteral("2"), new DoubleLiteral("3.3")))); assertStatement("VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0)", valuesQuery); assertStatement("SELECT * FROM (VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0))", simpleQuery( selectList(new AllColumns()), subquery(valuesQuery))); }