private Expression createEmptyColumnStatsRow(String columnName) { ImmutableList.Builder<Expression> rowValues = ImmutableList.builder(); rowValues.add(new StringLiteral(columnName)); rowValues.add(NULL_DOUBLE); rowValues.add(NULL_DOUBLE); rowValues.add(NULL_DOUBLE); rowValues.add(NULL_DOUBLE); rowValues.add(NULL_VARCHAR); rowValues.add(NULL_VARCHAR); return new Row(rowValues.build()); }
@Override public Node visitUnicodeStringLiteral(SqlBaseParser.UnicodeStringLiteralContext context) { return new StringLiteral(getLocation(context), decodeUnicodeLiteral(context)); }
@Override public Node visitNormalize(SqlBaseParser.NormalizeContext context) { Expression str = (Expression) visit(context.valueExpression()); String normalForm = Optional.ofNullable(context.normalForm()).map(ParserRuleContext::getText).orElse("NFC"); return new FunctionCall(getLocation(context), QualifiedName.of("normalize"), ImmutableList.of(str, new StringLiteral(getLocation(context), normalForm))); }
@Test public void testPosition() { assertExpression("position('a' in 'b')", new FunctionCall(QualifiedName.of("strpos"), ImmutableList.of( new StringLiteral("b"), new StringLiteral("a")))); assertExpression("position('a' in ('b'))", new FunctionCall(QualifiedName.of("strpos"), ImmutableList.of( new StringLiteral("b"), new StringLiteral("a")))); }
private Row createColumnStatsRow(String columnName, Type type, ColumnStatistics columnStatistics) { ImmutableList.Builder<Expression> rowValues = ImmutableList.builder(); rowValues.add(new StringLiteral(columnName)); rowValues.add(createEstimateRepresentation(columnStatistics.getDataSize())); rowValues.add(createEstimateRepresentation(columnStatistics.getDistinctValuesCount())); rowValues.add(createEstimateRepresentation(columnStatistics.getNullsFraction())); rowValues.add(NULL_DOUBLE); rowValues.add(toStringLiteral(type, columnStatistics.getRange().map(DoubleRange::getMin))); rowValues.add(toStringLiteral(type, columnStatistics.getRange().map(DoubleRange::getMax))); return new Row(rowValues.build()); }
@Test public void testSetSession() { testSetSession(new StringLiteral("baz"), "baz"); testSetSession(new FunctionCall(QualifiedName.of("concat"), ImmutableList.of( new StringLiteral("ban"), new StringLiteral("ana"))), "banana"); }
private static StringLiteral rawStringLiteral(final Slice slice) { return new StringLiteral(slice.toStringUtf8()) { @Override public Slice getSlice() { return slice; } }; }
@Test public void testGetChildren() { StringLiteral value = new StringLiteral("a"); StringLiteral pattern = new StringLiteral("b"); StringLiteral escape = new StringLiteral("c"); assertEquals(new LikePredicate(value, pattern, escape).getChildren(), ImmutableList.of(value, pattern, escape)); assertEquals(new LikePredicate(value, pattern, Optional.empty()).getChildren(), ImmutableList.of(value, pattern)); } }
@Test public void testSetSession() { assertStatement("SET SESSION foo = 'bar'", new SetSession(QualifiedName.of("foo"), new StringLiteral("bar"))); assertStatement("SET SESSION foo.bar = 'baz'", new SetSession(QualifiedName.of("foo", "bar"), new StringLiteral("baz"))); assertStatement("SET SESSION foo.bar.boo = 'baz'", new SetSession(QualifiedName.of("foo", "bar", "boo"), new StringLiteral("baz"))); assertStatement("SET SESSION foo.bar = 'ban' || 'ana'", new SetSession( QualifiedName.of("foo", "bar"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of( new StringLiteral("ban"), new StringLiteral("ana"))))); }
@Test public void testExecuteWithUsing() { assertStatement("EXECUTE myquery USING 1, 'abc', ARRAY ['hello']", new Execute(identifier("myquery"), ImmutableList.of(new LongLiteral("1"), new StringLiteral("abc"), new ArrayConstructor(ImmutableList.of(new StringLiteral("hello")))))); }
private static Expression createFailureFunction(RuntimeException exception, Type type) { requireNonNull(exception, "Exception is null"); String failureInfo = JsonCodec.jsonCodec(FailureInfo.class).toJson(Failures.toFailure(exception).toFailureInfo()); FunctionCall jsonParse = new FunctionCall(QualifiedName.of("json_parse"), ImmutableList.of(new StringLiteral(failureInfo))); FunctionCall failureFunction = new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(jsonParse)); return new Cast(failureFunction, type.getTypeSignature().toString()); }
@Test public void testSetSessionWithParameters() { List<Expression> expressionList = new ArrayList<>(); expressionList.add(new StringLiteral("ban")); expressionList.add(new Parameter(0)); testSetSessionWithParameters("bar", new FunctionCall(QualifiedName.of("concat"), expressionList), "banana", ImmutableList.of(new StringLiteral("ana"))); }
@Override public Node visitBasicStringLiteral(SqlBaseParser.BasicStringLiteralContext context) { return new StringLiteral(getLocation(context), unquote(context.STRING().getText())); }
public static Query singleValueQuery(String columnName, String value) { Relation values = values(row(new StringLiteral((value)))); return simpleQuery( selectList(new AllColumns()), aliased(values, "t", ImmutableList.of(columnName))); }
private static void assertLike(byte[] value, String pattern, boolean expected) { Expression predicate = new LikePredicate( rawStringLiteral(Slices.wrappedBuffer(value)), new StringLiteral(pattern), Optional.empty()); assertEquals(evaluate(predicate), expected); }
private static Row createDescribeInputRow(Parameter parameter, Analysis queryAnalysis) { Type type = queryAnalysis.getCoercion(parameter); if (type == null) { type = UNKNOWN; } return row( new LongLiteral(Integer.toString(parameter.getPosition())), new StringLiteral(type.getTypeSignature().getBase())); }
@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")))); }
private static Expression ensureScalarSubquery() { return new SimpleCaseExpression( new SymbolReference("is_distinct"), ImmutableList.of(new WhenClause(TRUE_LITERAL, TRUE_LITERAL)), Optional.of(new Cast(new FunctionCall( QualifiedName.of("fail"), ImmutableList.of( new LongLiteral(Long.toString(StandardErrorCode.SUBQUERY_MULTIPLE_ROWS.ordinal())), new StringLiteral("Scalar sub-query has returned multiple rows"))), StandardTypes.BOOLEAN))); } }
@Test public void testCall() { assertStatement("CALL foo()", new Call(QualifiedName.of("foo"), ImmutableList.of())); assertStatement("CALL foo(123, a => 1, b => 'go', 456)", new Call(QualifiedName.of("foo"), ImmutableList.of( new CallArgument(new LongLiteral("123")), new CallArgument("a", new LongLiteral("1")), new CallArgument("b", new StringLiteral("go")), new CallArgument(new LongLiteral("456"))))); }
@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))); }