@Override protected R visitLikePredicate(LikePredicate node, C context) { process(node.getValue(), context); process(node.getPattern(), context); node.getEscape().ifPresent(value -> process(value, context)); return null; }
@Override protected Object visitLikePredicate(LikePredicate node, Object context) Object value = process(node.getValue(), context); node.getPattern() instanceof StringLiteral && (!node.getEscape().isPresent() || node.getEscape().get() instanceof StringLiteral)) { Object pattern = process(node.getPattern(), context); if (node.getEscape().isPresent()) { escape = process(node.getEscape().get(), context); Type valueType = type(node.getValue()); Type patternType = createVarcharType(unescapedPattern.length()); TypeManager typeManager = metadata.getTypeManager(); if (node.getEscape().isPresent()) { optimizedEscape = Optional.of(toExpression(escape, type(node.getEscape().get()))); return new LikePredicate( toExpression(value, type(node.getValue())), toExpression(pattern, type(node.getPattern())), optimizedEscape);
@Override protected Boolean visitLikePredicate(LikePredicate node, Void context) { return process(node.getValue(), context) && process(node.getPattern(), context); }
private Regex getConstantPattern(LikePredicate node) { Regex result = likePatternCache.get(node); if (result == null) { StringLiteral pattern = (StringLiteral) node.getPattern(); if (node.getEscape().isPresent()) { Slice escape = ((StringLiteral) node.getEscape().get()).getSlice(); result = LikeFunctions.likePattern(pattern.getSlice(), escape); } else { result = LikeFunctions.likePattern(pattern.getSlice()); } likePatternCache.put(node, result); } return result; }
@Override protected Node visitShowCatalogs(ShowCatalogs node, Void context) { List<Expression> rows = listCatalogs(session, metadata, accessControl).keySet().stream() .map(name -> row(new StringLiteral(name))) .collect(toList()); Optional<Expression> predicate = Optional.empty(); Optional<String> likePattern = node.getLikePattern(); if (likePattern.isPresent()) { predicate = Optional.of(new LikePredicate(identifier("Catalog"), new StringLiteral(likePattern.get()), Optional.empty())); } return simpleQuery( selectList(new AllColumns()), aliased(new Values(rows), "catalogs", ImmutableList.of("Catalog")), predicate, Optional.of(ordering(ascending("Catalog")))); }
private boolean evaluateLikePredicate(LikePredicate node, Slice value, Regex regex) { if (type(node.getValue()) instanceof VarcharType) { return LikeFunctions.likeVarchar(value, regex); } Type type = type(node.getValue()); checkState(type instanceof CharType, "LIKE value is neither VARCHAR or CHAR"); return LikeFunctions.likeChar((long) ((CharType) type).getLength(), value, regex); }
@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)); } }
@Override protected String visitLikePredicate(LikePredicate node, Boolean negate) { return "SELECT date, dimension, _user_set FROM " + process(node.getValue(), negate) + " WHERE dimension LIKE " + process(node.getPattern(), negate); }
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 Node visitShowSchemas(ShowSchemas node, Void context) { if (!node.getCatalog().isPresent() && !session.getCatalog().isPresent()) { throw new SemanticException(CATALOG_NOT_SPECIFIED, node, "Catalog must be specified when session catalog is not set"); } String catalog = node.getCatalog().map(Identifier::getValue).orElseGet(() -> session.getCatalog().get()); accessControl.checkCanShowSchemas(session.getRequiredTransactionId(), session.getIdentity(), catalog); Optional<Expression> predicate = Optional.empty(); Optional<String> likePattern = node.getLikePattern(); if (likePattern.isPresent()) { predicate = Optional.of(new LikePredicate( identifier("schema_name"), new StringLiteral(likePattern.get()), node.getEscape().map(StringLiteral::new))); } return simpleQuery( selectList(aliasedName("schema_name", "Schema")), from(catalog, TABLE_SCHEMATA), predicate, Optional.of(ordering(ascending("schema_name")))); }
@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)); } }
@Override protected String visitLikePredicate(LikePredicate node, Void context) { StringBuilder builder = new StringBuilder(); builder.append('(') .append(process(node.getValue(), context)) .append(" LIKE ") .append(process(node.getPattern(), context)); node.getEscape().ifPresent(escape -> { builder.append(" ESCAPE ") .append(process(escape, context)); }); builder.append(')'); return builder.toString(); }
@Override protected Object visitLikePredicate(LikePredicate node, Object context) Object value = process(node.getValue(), context); node.getPattern() instanceof StringLiteral && (node.getEscape() instanceof StringLiteral || node.getEscape() == null)) { Object pattern = process(node.getPattern(), context); if (node.getEscape() != null) { escape = process(node.getEscape(), context); if (!stringPattern.contains("%") && !stringPattern.contains("_")) { return new ComparisonExpression(ComparisonExpression.Type.EQUAL, toExpression(value, expressionTypes.get(node.getValue())), toExpression(pattern, expressionTypes.get(node.getPattern()))); 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); }
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); }
@Override protected Type visitLikePredicate(LikePredicate node, StackableAstVisitorContext<Context> context) { Type valueType = process(node.getValue(), context); if (!(valueType instanceof CharType) && !(valueType instanceof VarcharType)) { coerceType(context, node.getValue(), VARCHAR, "Left side of LIKE expression"); } Type patternType = getVarcharType(node.getPattern(), context); coerceType(context, node.getPattern(), patternType, "Pattern for LIKE expression"); if (node.getEscape().isPresent()) { Expression escape = node.getEscape().get(); Type escapeType = getVarcharType(escape, context); coerceType(context, escape, escapeType, "Escape for LIKE expression"); } return setExpressionType(node, BOOLEAN); }
return new QueryWrapper(QueryBuilders.boolQuery().mustNot(qw.getQuery()), qw.getNestField()); }else if (node instanceof LikePredicate){ String field = getVariableName(((LikePredicate)node).getValue()); FieldAndType fat = getFieldAndType(field, state); field = fat.getFieldName(); return null; String query = ((StringLiteral)((LikePredicate)node).getPattern()).getValue(); if(fat.getFieldType() == Types.REF) return new QueryWrapper(queryForString(field, query), field.split("\\.")[0]);
@Override public Node visitLike(SqlBaseParser.LikeContext context) { Expression result = new LikePredicate( getLocation(context), (Expression) visit(context.value), (Expression) visit(context.pattern), visitIfPresent(context.escape, Expression.class)); if (context.NOT() != null) { result = new NotExpression(getLocation(context), result); } return result; }
@Override protected RowExpression visitLikePredicate(LikePredicate node, Void context) { RowExpression value = process(node.getValue(), context); RowExpression pattern = process(node.getPattern(), context); if (node.getEscape().isPresent()) { RowExpression escape = process(node.getEscape().get(), context); return likeFunctionCall(value, call(likePatternSignature(), LIKE_PATTERN, pattern, escape)); } return likeFunctionCall(value, call(castSignature(LIKE_PATTERN, VARCHAR), LIKE_PATTERN, pattern)); }
@Override protected Node visitShowTables(ShowTables showTables, Void context) { CatalogSchemaName schema = createCatalogSchemaName(session, showTables, showTables.getSchema()); accessControl.checkCanShowTablesMetadata(session.getRequiredTransactionId(), session.getIdentity(), schema); if (!metadata.catalogExists(session, schema.getCatalogName())) { throw new SemanticException(MISSING_CATALOG, showTables, "Catalog '%s' does not exist", schema.getCatalogName()); } if (!metadata.schemaExists(session, schema)) { throw new SemanticException(MISSING_SCHEMA, showTables, "Schema '%s' does not exist", schema.getSchemaName()); } Expression predicate = equal(identifier("table_schema"), new StringLiteral(schema.getSchemaName())); Optional<String> likePattern = showTables.getLikePattern(); if (likePattern.isPresent()) { Expression likePredicate = new LikePredicate( identifier("table_name"), new StringLiteral(likePattern.get()), showTables.getEscape().map(StringLiteral::new)); predicate = logicalAnd(predicate, likePredicate); } return simpleQuery( selectList(aliasedName("table_name", "Table")), from(schema.getCatalogName(), TABLE_TABLES), predicate, ordering(ascending("table_name"))); }