-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation that seems to be working.
Added sql parsing and execution classes for the DISTINCT predicate.
- Loading branch information
Showing
11 changed files
with
496 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
libs/sql-parser/src/main/java/io/crate/sql/tree/DistinctFromPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
server/src/main/java/io/crate/expression/predicate/DistinctFromPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.