@Override protected String visitLikePredicate(LikePredicate node, Void context) { StringBuilder builder = new StringBuilder(); builder.append('(') .append(process(node.getValue(), null)) .append(" LIKE ") .append(process(node.getPattern(), null)); if (node.getEscape() != null) { builder.append(" ESCAPE ") .append(process(node.getEscape(), null)); } builder.append(')'); return builder.toString(); }
@Override protected String visitLikePredicate(LikePredicate node, StackableAstVisitorContext<Integer> indent) { StringBuilder builder = new StringBuilder(); builder.append('(') .append(process(node.getValue(), indent)) .append(" LIKE ") .append(process(node.getPattern(), indent)); if (node.getEscape() != null) { builder.append(" ESCAPE ") .append(process(node.getEscape(), indent)); } builder.append(')'); return builder.toString(); }
private Regex getConstantPattern(LikePredicate node) { Regex result = likePatternCache.get(node); if (result == null) { StringLiteral pattern = (StringLiteral) node.getPattern(); StringLiteral escape = (StringLiteral) node.getEscape(); if (escape == null) { result = LikeFunctions.likePattern(pattern.getSlice()); } else { result = LikeFunctions.likePattern(pattern.getSlice(), escape.getSlice()); } likePatternCache.put(node, result); } return result; }
@Override protected String visitLikePredicate(LikePredicate node, Boolean unmangleNames) { StringBuilder builder = new StringBuilder(); builder.append('(') .append(process(node.getValue(), unmangleNames)) .append(" LIKE ") .append(process(node.getPattern(), unmangleNames)); if (node.getEscape() != null) { builder.append(" ESCAPE ") .append(process(node.getEscape(), unmangleNames)); } builder.append(')'); return builder.toString(); }
@Override protected RowExpression visitLikePredicate(LikePredicate node, Void context) { RowExpression value = process(node.getValue(), context); RowExpression pattern = process(node.getPattern(), context); if (node.getEscape() != null) { RowExpression escape = process(node.getEscape(), context); return call(likeSignature(), BOOLEAN, value, call(likePatternSignature(), LIKE_PATTERN, pattern, escape)); } return call(likeSignature(), BOOLEAN, value, call(castSignature(LIKE_PATTERN, VARCHAR), LIKE_PATTERN, pattern)); }
@Override protected Type visitLikePredicate(LikePredicate node, StackableAstVisitorContext<AnalysisContext> context) { coerceType(context, node.getValue(), VARCHAR, "Left side of LIKE expression"); coerceType(context, node.getPattern(), VARCHAR, "Pattern for LIKE expression"); if (node.getEscape() != null) { coerceType(context, node.getEscape(), VARCHAR, "Escape for LIKE expression"); } expressionTypes.put(node, BOOLEAN); return BOOLEAN; }
private static void assertLike(byte[] value, String pattern, boolean expected) { Expression predicate = new LikePredicate( rawStringLiteral(Slices.wrappedBuffer(value)), new StringLiteral(pattern), null); assertEquals(evaluate(predicate), expected); }
@Override protected Object visitLikePredicate(LikePredicate node, Object context) { Object value = process(node.getValue(), context); if (value == null) { return null; } if (value instanceof Slice && node.getPattern() instanceof StringLiteral && (node.getEscape() instanceof StringLiteral || node.getEscape() == null)) { // fast path when we know the pattern and escape are constant return LikeFunctions.like((Slice) value, getConstantPattern(node)); } Object pattern = process(node.getPattern(), context); if (pattern == null) { return null; } Object escape = null; if (node.getEscape() != null) { escape = process(node.getEscape(), context); if (escape == null) { return null; } } if (value instanceof Slice && pattern instanceof Slice && (escape == null || escape instanceof Slice)) { Regex regex; if (escape == null) { regex = LikeFunctions.likePattern((Slice) pattern); } else { regex = LikeFunctions.likePattern((Slice) pattern, (Slice) escape); } return LikeFunctions.like((Slice) value, regex); } // if pattern is a constant without % or _ replace with a comparison if (pattern instanceof Slice && escape == null) { String stringPattern = ((Slice) pattern).toStringUtf8(); if (!stringPattern.contains("%") && !stringPattern.contains("_")) { return new ComparisonExpression(ComparisonExpression.Type.EQUAL, toExpression(value, expressionTypes.get(node.getValue())), toExpression(pattern, expressionTypes.get(node.getPattern()))); } } Expression optimizedEscape = null; if (node.getEscape() != null) { optimizedEscape = toExpression(escape, expressionTypes.get(node.getEscape())); } return new LikePredicate( toExpression(value, expressionTypes.get(node.getValue())), toExpression(pattern, expressionTypes.get(node.getPattern())), optimizedEscape); }
@Override protected Boolean visitLikePredicate(LikePredicate node, Void context) { return process(node.getValue(), context) && process(node.getPattern(), context); }
@Override protected RelationType visitShowTables(ShowTables showTables, AnalysisContext context) { String catalogName = session.getCatalog().orElse(null); String schemaName = session.getSchema().orElse(null); Optional<QualifiedName> schema = showTables.getSchema(); if (schema.isPresent()) { List<String> parts = schema.get().getParts(); if (parts.size() > 2) { throw new SemanticException(INVALID_SCHEMA_NAME, showTables, "Too many parts in schema name: %s", schema); } if (parts.size() == 2) { catalogName = parts.get(0); } schemaName = schema.get().getSuffix(); } if (catalogName == null) { throw new SemanticException(CATALOG_NOT_SPECIFIED, showTables, "Catalog must be specified when session catalog is not set"); } if (schemaName == null) { throw new SemanticException(SCHEMA_NOT_SPECIFIED, showTables, "Schema must be specified when session schema is not set"); } if (!metadata.listSchemaNames(session, catalogName).contains(schemaName)) { throw new SemanticException(MISSING_SCHEMA, showTables, "Schema '%s' does not exist", schemaName); } Expression predicate = equal(nameReference("table_schema"), new StringLiteral(schemaName)); Optional<String> likePattern = showTables.getLikePattern(); if (likePattern.isPresent()) { Expression likePredicate = new LikePredicate(nameReference("table_name"), new StringLiteral(likePattern.get()), null); predicate = logicalAnd(predicate, likePredicate); } Query query = simpleQuery( selectList(aliasedName("table_name", "Table")), from(catalogName, TABLE_TABLES), predicate, ordering(ascending("table_name"))); return process(query, context); }