/** * For an identifier of the form "a.b.c.d", returns "a.b.c" * For an identifier of the form "a", returns absent */ public Optional<QualifiedName> getPrefix() { if (parts.size() == 1) { return Optional.empty(); } List<String> subList = parts.subList(0, parts.size() - 1); return Optional.of(new QualifiedName(subList, subList)); }
public static QualifiedName of(Iterable<String> originalParts) { requireNonNull(originalParts, "originalParts is null"); checkArgument(!isEmpty(originalParts), "originalParts is empty"); List<String> parts = ImmutableList.copyOf(transform(originalParts, part -> part.toLowerCase(ENGLISH))); return new QualifiedName(ImmutableList.copyOf(originalParts), parts); }
public static Expression functionCall(String name, Expression... arguments) { return new FunctionCall(new QualifiedName(name), ImmutableList.copyOf(arguments)); }
/** * For an identifier of the form "a.b.c.d", returns "a.b.c" * For an identifier of the form "a", returns absent */ public Optional<QualifiedName> getPrefix() { if (parts.size() == 1) { return Optional.empty(); } List<String> subList = parts.subList(0, parts.size() - 1); return Optional.of(new QualifiedName(subList, subList)); }
/** * For an identifier of the form "a.b.c.d", returns "a.b.c" * For an identifier of the form "a", returns absent */ public Optional<QualifiedName> getPrefix() { if (parts.size() == 1) { return Optional.empty(); } return Optional.of(new QualifiedName(parts.subList(0, parts.size() - 1))); }
public static QualifiedName of(Iterable<String> originalParts) { requireNonNull(originalParts, "originalParts is null"); checkArgument(!isEmpty(originalParts), "originalParts is empty"); List<String> parts = ImmutableList.copyOf(transform(originalParts, part -> part.toLowerCase(ENGLISH))); return new QualifiedName(ImmutableList.copyOf(originalParts), parts); }
public static QualifiedName of(String first, String... rest) { requireNonNull(first, "first is null"); return new QualifiedName(ImmutableList.copyOf(Lists.asList(first, rest))); }
/** * If this DereferenceExpression looks like a QualifiedName, return QualifiedName. * Otherwise return null */ public static QualifiedName getQualifiedName(DereferenceExpression expression) { List<String> parts = tryParseParts(expression.base, expression.fieldName); return parts == null ? null : new QualifiedName(parts); }
private QualifiedName getQualifiedName(SqlBaseParser.QualifiedNameContext context) { List<String> parts = context .identifier().stream() .map(ParseTree::getText) .collect(toList()); return new QualifiedName(parts, caseSensitive); }
@Override public Expression rewriteCurrentTime(CurrentTime node, Void context, ExpressionTreeRewriter<Void> treeRewriter) { if (node.getPrecision() != null) { throw new UnsupportedOperationException("not yet implemented: non-default precision"); } switch (node.getType()) { case DATE: return new FunctionCall(new QualifiedName("current_date"), ImmutableList.<Expression>of()); case TIME: return new FunctionCall(new QualifiedName("current_time"), ImmutableList.<Expression>of()); case LOCALTIME: return new FunctionCall(new QualifiedName("localtime"), ImmutableList.<Expression>of()); case TIMESTAMP: return new FunctionCall(new QualifiedName("current_timestamp"), ImmutableList.<Expression>of()); case LOCALTIMESTAMP: return new FunctionCall(new QualifiedName("localtimestamp"), ImmutableList.<Expression>of()); default: throw new UnsupportedOperationException("not yet implemented: " + node.getType()); } }
@Override public Node visitColumnReference(SqlBaseParser.ColumnReferenceContext context) { return new QualifiedNameReference(getLocation(context), new QualifiedName(context.getText(), caseSensitive)); }
@Override public Node visitPosition(SqlBaseParser.PositionContext context) { List<Expression> arguments = Lists.reverse(visit(context.valueExpression(), Expression.class)); return new FunctionCall(getLocation(context), new QualifiedName("strpos"), arguments); }
@Override public Node visitSubstring(SqlBaseParser.SubstringContext context) { return new FunctionCall(getLocation(context), new QualifiedName("substr"), visit(context.valueExpression(), Expression.class)); }
@Override protected Void visitFunctionCall(FunctionCall node, AtomicBoolean deterministic) { // TODO: total hack to figure out if a function is deterministic. martint should fix this when he refactors the planning code if (node.getName().equals(new QualifiedName("rand")) || node.getName().equals(new QualifiedName("random"))) { deterministic.set(false); } return super.visitFunctionCall(node, deterministic); } }
@Override public Node visitConcatenation(SqlBaseParser.ConcatenationContext context) { return new FunctionCall( getLocation(context.CONCAT()), new QualifiedName("concat"), ImmutableList.of( (Expression) visit(context.left), (Expression) visit(context.right))); }
private static FunctionCall function(String name, Expression... inputs) { return new FunctionCall(new QualifiedName(name), Arrays.asList(inputs)); }
@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), new QualifiedName("normalize"), ImmutableList.of(str, new StringLiteral(getLocation(context), normalForm))); }
private static Expression randPredicate(Symbol symbol) { return comparison(GREATER_THAN, reference(symbol), new FunctionCall(new QualifiedName("rand"), ImmutableList.<Expression>of())); }
@Test public void testSetSession() throws Exception { testSetSession(new StringLiteral("baz"), "baz"); testSetSession(new FunctionCall(new QualifiedName("concat"), ImmutableList.of( new StringLiteral("ban"), new StringLiteral("ana"))), "banana"); }
private Relation render(List<ForeignKey> keys) { if (keys.isEmpty()) { return QueryUtil.table(new QualifiedName(baseTable)); } ForeignKey key = keys.get(0); if (keys.size() == 1) { return new Join(Join.Type.INNER, QueryUtil.table(new QualifiedName(key.getSourceTable())), QueryUtil.table(new QualifiedName(key.getDestinationTable())), Optional.of(new JoinOn(new ComparisonExpression( ComparisonExpression.Type.EQUAL, new QualifiedNameReference(QualifiedName.of( key.getSourceTable(), key.getSourceColumn())), new QualifiedNameReference(QualifiedName.of( key.getDestinationTable(), key.getDestinationColumn())))))); } return new Join(Join.Type.INNER, render(keys.subList(1, keys.size())), QueryUtil.table(new QualifiedName(key.getDestinationTable())), Optional.of(new JoinOn(new ComparisonExpression( ComparisonExpression.Type.EQUAL, new QualifiedNameReference(QualifiedName.of( key.getSourceTable(), key.getSourceColumn())), new QualifiedNameReference(QualifiedName.of( key.getDestinationTable(), key.getDestinationColumn())))))); }