Skip to content

Commit

Permalink
fixup! Support inserting into table having CHECK constraint in Delta …
Browse files Browse the repository at this point in the history
…Lake
  • Loading branch information
ebyhr committed Jan 25, 2023
1 parent 7fc8d72 commit 596b07d
Show file tree
Hide file tree
Showing 17 changed files with 408 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ booleanExpression
| booleanExpression AND booleanExpression #and
;

// TODO: Support LIKE clause and function calls
// workaround for https://github.com/antlr/antlr4/issues/780
predicate[ParserRuleContext value]
: comparisonOperator right=valueExpression #comparison
Expand All @@ -47,8 +48,9 @@ primaryExpression
| identifier #columnReference
;

// TODO: Support raw string literal
string
: STRING #basicStringLiteral
: STRING #unicodeStringLiteral
;

comparisonOperator
Expand All @@ -59,11 +61,13 @@ booleanValue
: TRUE | FALSE
;

// "..." is a varchar literal in Spark SQL, not a quoted identifier
identifier
: IDENTIFIER #unquotedIdentifier
| BACKQUOTED_IDENTIFIER #backQuotedIdentifier
;

// TODO: Support decimals and scientific notation
number
: MINUS? INTEGER_VALUE #integerLiteral
;
Expand Down Expand Up @@ -109,10 +113,3 @@ fragment LETTER
WS
: [ \r\n\t]+ -> channel(HIDDEN)
;

// Catch-all for anything we can't recognize.
// We use this to be able to ignore and recover all the text
// when splitting statements with DelimiterLexer
UNRECOGNIZED
: .
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed 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.
*/
package io.trino.plugin.deltalake.expression;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

public class BooleanLiteral
extends Literal
{
private final boolean value;

public BooleanLiteral(String value)
{
requireNonNull(value, "value is null");
checkArgument(value.toLowerCase(ENGLISH).equals("true") || value.toLowerCase(ENGLISH).equals("false"));

this.value = value.toLowerCase(ENGLISH).equals("true");
}

public boolean getValue()
{
return value;
}

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

@Override
public int hashCode()
{
return Objects.hash(value);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BooleanLiteral other = (BooleanLiteral) obj;
return Objects.equals(this.value, other.value);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("value", value)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
import org.antlr.v4.runtime.IntStream;
import org.antlr.v4.runtime.misc.Interval;

import static java.util.Objects.requireNonNull;

public class CaseInsensitiveStream
implements CharStream
{
private final CharStream stream;

public CaseInsensitiveStream(CharStream stream)
{
this.stream = stream;
this.stream = requireNonNull(stream, "stream is null");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public class ComparisonExpression
extends Expression
extends SparkExpression
{
private final Operator operator;
private final Expression left;
private final Expression right;
private final SparkExpression left;
private final SparkExpression right;

public ComparisonExpression(Operator operator, Expression left, Expression right)
public ComparisonExpression(Operator operator, SparkExpression left, SparkExpression right)
{
requireNonNull(operator, "operator is null");
requireNonNull(left, "left is null");
Expand All @@ -40,18 +41,18 @@ public Operator getOperator()
return operator;
}

public Expression getLeft()
public SparkExpression getLeft()
{
return left;
}

public Expression getRight()
public SparkExpression getRight()
{
return right;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
public <R, C> R accept(SparkExpressionTreeVisitor<R, C> visitor, C context)
{
return visitor.visitComparisonExpression(this, context);
}
Expand All @@ -67,7 +68,7 @@ public boolean equals(Object o)
}

ComparisonExpression that = (ComparisonExpression) o;
return (operator == that.operator) &&
return operator == that.operator &&
Objects.equals(left, that.left) &&
Objects.equals(right, that.right);
}
Expand All @@ -78,6 +79,16 @@ public int hashCode()
return Objects.hash(operator, left, right);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("operator", operator)
.add("left", left)
.add("right", right)
.toString();
}

public enum Operator
{
EQUAL("="),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
*/
package io.trino.plugin.deltalake.expression;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

public class Identifier
extends Expression
extends SparkExpression
{
private final String value;

Expand All @@ -33,8 +34,16 @@ public String getValue()
}

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

@Override
public String toString()
{
return toStringHelper(this)
.add("value", value)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,11 @@
*/
package io.trino.plugin.deltalake.expression;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

public class Literal
extends Expression
public abstract class Literal
extends SparkExpression
{
private final String value;

public Literal(String value)
{
checkArgument(!isNullOrEmpty(value), "value is null or empty");
this.value = value;
}

public String getValue()
{
return value;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
public <R, C> R accept(SparkExpressionTreeVisitor<R, C> visitor, C context)
{
return visitor.visitLiteral(this, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,49 @@
*/
package io.trino.plugin.deltalake.expression;

import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public class LogicalExpression
extends Expression
extends SparkExpression
{
public enum Operator
{
AND;
}

private final Operator operator;
private final List<Expression> terms;
private final SparkExpression left;
private final SparkExpression right;

public LogicalExpression(Operator operator, List<Expression> terms)
public LogicalExpression(Operator operator, SparkExpression left, SparkExpression right)
{
requireNonNull(operator, "operator is null");
checkArgument(terms.size() >= 2, "Expected at least 2 terms");

this.operator = operator;
this.terms = ImmutableList.copyOf(terms);
this.left = requireNonNull(left, "left is null");
this.right = requireNonNull(right, "right is null");
}

public Operator getOperator()
{
return operator;
}

public List<Expression> getTerms()
public SparkExpression getLeft()
{
return left;
}

public SparkExpression getRight()
{
return terms;
return right;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
public <R, C> R accept(SparkExpressionTreeVisitor<R, C> visitor, C context)
{
return visitor.visitLogicalExpression(this, context);
}
Expand All @@ -67,12 +70,24 @@ public boolean equals(Object o)
return false;
}
LogicalExpression that = (LogicalExpression) o;
return operator == that.operator && Objects.equals(terms, that.terms);
return operator == that.operator &&
Objects.equals(left, that.left) &&
Objects.equals(right, that.right);
}

@Override
public int hashCode()
{
return Objects.hash(operator, terms);
return Objects.hash(operator, left, right);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("operator", operator)
.add("left", left)
.add("right", right)
.toString();
}
}
Loading

0 comments on commit 596b07d

Please sign in to comment.