Skip to content

Commit

Permalink
Initial implementation that seems to be working.
Browse files Browse the repository at this point in the history
Added sql parsing and execution classes for the DISTINCT predicate.
  • Loading branch information
marstein committed Jul 29, 2024
1 parent fc1af56 commit ce2bf60
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 52,7 @@
import io.crate.sql.tree.ColumnType;
import io.crate.sql.tree.ComparisonExpression;
import io.crate.sql.tree.CurrentTime;
import io.crate.sql.tree.DistinctFromPredicate;
import io.crate.sql.tree.DoubleLiteral;
import io.crate.sql.tree.EscapedCharStringLiteral;
import io.crate.sql.tree.ExistsPredicate;
Expand Down Expand Up @@ -675,6 676,12 @@ protected String visitBetweenPredicate(BetweenPredicate node, @Nullable List<Exp
node.getMax().accept(this, parameters) ")";
}

@Override
protected String visitDistinctFrom(DistinctFromPredicate node, @Nullable List<Expression> parameters) {
return "(" node.getLeft().accept(this, parameters) " IS DISTINCT FROM "
node.getRight().accept(this, parameters) ")";
}

@Override
protected String visitInPredicate(InPredicate node, @Nullable List<Expression> parameters) {
return "(" node.getValue().accept(this, parameters) " IN "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 138,7 @@
import io.crate.sql.tree.Delete;
import io.crate.sql.tree.DenyPrivilege;
import io.crate.sql.tree.DiscardStatement;
import io.crate.sql.tree.DistinctFromPredicate;
import io.crate.sql.tree.DoubleLiteral;
import io.crate.sql.tree.DropAnalyzer;
import io.crate.sql.tree.DropBlobTable;
Expand Down Expand Up @@ -1841,11 1842,9 @@ public Node visitComparison(SqlBaseParser.ComparisonContext context) {

@Override
public Node visitDistinctFrom(SqlBaseParser.DistinctFromContext context) {
Expression expression = new ComparisonExpression(
ComparisonExpression.Type.IS_DISTINCT_FROM,
Expression expression = new DistinctFromPredicate(
(Expression) visit(context.value),
(Expression) visit(context.right));

if (context.NOT() != null) {
expression = new NotExpression(expression);
}
Expand Down Expand Up @@ -2089,8 2088,8 @@ public Node visitSubstring(SqlBaseParser.SubstringContext context) {

@Override
public Node visitPosition(SqlBaseParser.PositionContext context) {
Expression substr = (Expression) visit(context.expr(0));
Expression str = (Expression) visit(context.expr(1));
Expression substr = (Expression) visit(context.expr(0));
Expression str = (Expression) visit(context.expr(1));
return new FunctionCall(QualifiedName.of("strpos"), List.of(str, substr));
}

Expand Down Expand Up @@ -2544,6 2543,7 @@ private static ComparisonExpression.Type getComparisonOperator(Token symbol) {
case SqlBaseLexer.GT -> ComparisonExpression.Type.GREATER_THAN;
case SqlBaseLexer.GTE -> ComparisonExpression.Type.GREATER_THAN_OR_EQUAL;
case SqlBaseLexer.LLT -> ComparisonExpression.Type.CONTAINED_WITHIN;
case SqlBaseLexer.DISTINCT -> ComparisonExpression.Type.IS_DISTINCT_FROM;
case SqlBaseLexer.REGEX_MATCH -> ComparisonExpression.Type.REGEX_MATCH;
case SqlBaseLexer.REGEX_NO_MATCH -> ComparisonExpression.Type.REGEX_NO_MATCH;
case SqlBaseLexer.REGEX_MATCH_CI -> ComparisonExpression.Type.REGEX_MATCH_CI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 52,10 @@ protected R visitBetweenPredicate(BetweenPredicate node, C context) {
return visitExpression(node, context);
}

protected R visitDistinctFrom(DistinctFromPredicate node, C context) {
return visitExpression(node, context);
}

protected R visitComparisonExpression(ComparisonExpression node, C context) {
return visitExpression(node, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 55,14 @@ protected R visitBetweenPredicate(BetweenPredicate node, C context) {
return null;
}

@Override
protected R visitDistinctFrom(DistinctFromPredicate node, C context) {
node.getLeft().accept(this, context);
node.getRight().accept(this, context);

return null;
}

@Override
protected R visitComparisonExpression(ComparisonExpression node, C context) {
node.getLeft().accept(this, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,65 @@
/*
* Licensed to Crate.io GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.sql.tree;

import java.util.Objects;

public class DistinctFromPredicate extends Expression {
private final Expression left;
private final Expression right;

public DistinctFromPredicate(Expression left, Expression right) {
this.left = left;
this.right = right;
}

public Expression getLeft() {
return left;
}

public Expression getRight() {
return right;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitDistinctFrom(this, context);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DistinctFromPredicate that = (DistinctFromPredicate) o;
return Objects.equals(left, that.left) &&
Objects.equals(right, that.right);
}

@Override
public int hashCode() {
return Objects.hash(left, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 123,7 @@
import io.crate.sql.tree.ColumnPolicy;
import io.crate.sql.tree.ComparisonExpression;
import io.crate.sql.tree.CurrentTime;
import io.crate.sql.tree.DistinctFromPredicate;
import io.crate.sql.tree.DoubleLiteral;
import io.crate.sql.tree.EscapedCharStringLiteral;
import io.crate.sql.tree.ExistsPredicate;
Expand Down Expand Up @@ -1094,6 1095,18 @@ protected Symbol visitBetweenPredicate(BetweenPredicate node, ExpressionAnalysis
return allocateFunction(AndOperator.NAME, List.of(gteFunc, lteFunc), context);
}

@Override
protected Symbol visitDistinctFrom(DistinctFromPredicate node, ExpressionAnalysisContext context) {
// <left> is distinct from <right>
// -> (<left> != null && <right != null && <left> != <right>) || !(<left> == null && <right> == null) ||
Symbol left = node.getLeft().accept(this, context);
Symbol right = node.getRight().accept(this, context);
return allocateFunction(
io.crate.expression.predicate.DistinctFromPredicate.NAME,
List.of(left, right),
context);
}

@Override
public Symbol visitMatchPredicate(MatchPredicate node, ExpressionAnalysisContext context) {
Map<Symbol, Symbol> identBoostMap = HashMap.newHashMap(node.idents().size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,107 @@
/*
* Licensed to Crate.io GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.expression.predicate;

import static io.crate.lucene.LuceneQueryBuilder.genericFunctionFilter;
import static io.crate.metadata.functions.TypeVariableConstraint.typeVariable;

import java.util.Collections;
import java.util.List;

import org.apache.lucene.search.Query;

import io.crate.data.Input;
import io.crate.expression.symbol.Function;
import io.crate.expression.symbol.Literal;
import io.crate.expression.symbol.Symbol;
import io.crate.lucene.LuceneQueryBuilder.Context;
import io.crate.metadata.FunctionType;
import io.crate.metadata.Functions;
import io.crate.metadata.NodeContext;
import io.crate.metadata.Scalar;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.functions.BoundSignature;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;
import io.crate.types.TypeSignature;

public class DistinctFromPredicate<T> extends Scalar<Boolean, T> {

public static final String NAME = "op_isdistinctfrom";
public static final Signature SIGNATURE = Signature.builder(NAME, FunctionType.SCALAR)
.argumentTypes(TypeSignature.parse("E"), TypeSignature.parse("E"))
.returnType(DataTypes.BOOLEAN.getTypeSignature())
.features(Feature.DETERMINISTIC, Feature.NOTNULL)
.typeVariableConstraints(typeVariable("E"))
.build();

public static void register(Functions.Builder builder) {
builder.add(
SIGNATURE,
DistinctFromPredicate::new
);
}

private DistinctFromPredicate(Signature signature, BoundSignature boundSignature) {
super(signature, boundSignature);
}

@Override
public Symbol normalizeSymbol(Function symbol, TransactionContext txnCtx, NodeContext nodeCtx) {
assert symbol != null : "function must not be null";
assert symbol.arguments().size() == 2 : "function's number of arguments must be 2";
Symbol arg1 = symbol.arguments().getFirst();
Symbol arg2 = symbol.arguments().get(1);

// two ``NULL`` values are not distinct from one other
if (arg1 instanceof Input<?> input1 && input1.value() == null && arg2 instanceof Input<?> input && input.value() == null)
return Literal.BOOLEAN_FALSE;

// Any non-null Literal is distinct from null
if (arg1 instanceof Literal<?> input1 && input1.value() == null && arg2 instanceof Literal<?> input && input.value() != null)
return Literal.BOOLEAN_TRUE;
if (arg1 instanceof Literal<?> input1 && input1.value() != null && arg2 instanceof Literal<?> input && input.value() == null)
return Literal.BOOLEAN_TRUE;

return symbol;
}

@Override
@SafeVarargs
public final Boolean evaluate(TransactionContext txnCtx, NodeContext nodeCtx, Input<T> ... args) {
assert args.length == 2 : "number of args must be 2";
T value1 = args[0].value();
T value2 = args[1].value();
if (value1 == null && value2 == null) return Boolean.FALSE;
if (value1 == null || value2 == null) return Boolean.TRUE;
return !value1.equals(value2);
}

@Override
public Query toQuery(Function function, Context context) {
List<Symbol> arguments = function.arguments();
assert arguments.size() == 2 : "`<expression> IS DISTINCT FROM <expression>` function must have two arguments";
// TODO: create lucene query from this IS DISTINCT expression. See io.crate.expression.operator.EqOperator.toQuery
// when comparing Literal NULL values we can "hardcode" the result.
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 36,6 @@ public void addFunctions(Settings settings,
IsNullPredicate.register(builder);
NotPredicate.register(builder);
MatchPredicate.register(builder);
DistinctFromPredicate.register(builder);
}
}
Loading

0 comments on commit ce2bf60

Please sign in to comment.