Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds DML (INSERT, DELETE, UPDATE, UPSERT, REPLACE) #1666

Merged
merged 9 commits into from
Dec 9, 2024
441 changes: 441 additions & 0 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/AstVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
import org.partiql.ast.ddl.KeyValue;
import org.partiql.ast.ddl.PartitionBy;
import org.partiql.ast.ddl.TableConstraint;
import org.partiql.ast.dml.ConflictAction;
import org.partiql.ast.dml.ConflictTarget;
import org.partiql.ast.dml.Delete;
import org.partiql.ast.dml.DoReplaceAction;
import org.partiql.ast.dml.DoUpdateAction;
import org.partiql.ast.dml.Insert;
import org.partiql.ast.dml.InsertSource;
import org.partiql.ast.dml.OnConflict;
import org.partiql.ast.dml.Replace;
import org.partiql.ast.dml.SetClause;
import org.partiql.ast.dml.Update;
import org.partiql.ast.dml.UpdateTarget;
import org.partiql.ast.dml.UpdateTargetStep;
import org.partiql.ast.dml.Upsert;
import org.partiql.ast.expr.Expr;
import org.partiql.ast.expr.ExprAnd;
import org.partiql.ast.expr.ExprArray;
Expand Down Expand Up @@ -116,6 +130,106 @@ public R visitQuery(Query node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitInsert(Insert node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpsert(Upsert node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitReplace(Replace node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdate(Update node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdateTarget(UpdateTarget node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdateTargetStep(UpdateTargetStep node, C ctx) {
return node.accept(this, ctx);
}

public R visitUpdateTargetStepElement(UpdateTargetStep.Element node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdateTargetStepField(UpdateTargetStep.Field node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitDelete(Delete node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitSetClause(SetClause node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitInsertSource(InsertSource node, C ctx) {
return node.accept(this, ctx);
}

public R visitInsertSourceFromExpr(InsertSource.FromExpr node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitInsertSourceFromDefault(InsertSource.FromDefault node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitOnConflict(OnConflict node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitConflictTarget(ConflictTarget node, C ctx) {
return node.accept(this, ctx);
}

public R visitConflictTargetIndex(ConflictTarget.Index node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitConflictTargetConstraint(ConflictTarget.Constraint node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitConflictAction(ConflictAction node, C ctx) {
return node.accept(this, ctx);
}

public R visitConflictActionDoNothing(ConflictAction.DoNothing node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitConflictActionDoReplace(ConflictAction.DoReplace node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitConflictActionDoUpdate(ConflictAction.DoUpdate node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitDoReplaceAction(DoReplaceAction node, C ctx) {
return node.accept(this, ctx);
}

public R visitDoReplaceActionExcluded(DoReplaceAction.Excluded node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitDoUpdateAction(DoUpdateAction node, C ctx) {
return node.accept(this, ctx);
}

public R visitDoUpdateActionExcluded(DoUpdateAction.Excluded node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitExplain(Explain node, C ctx) {
return defaultVisit(node, ctx);
}
Expand Down
144 changes: 144 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/dml/ConflictAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.partiql.ast.dml;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.partiql.ast.AstNode;
import org.partiql.ast.AstVisitor;
import org.partiql.ast.expr.Expr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* This is the mandatory action of the ON CONFLICT clause.
* @see Insert#onConflict
* @see OnConflict#action
*/
public abstract class ConflictAction extends AstNode {

/**
* This is the DO NOTHING variant of the conflict action.
* @see ConflictAction
* @see OnConflict#action
* @see Insert#onConflict
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class DoNothing extends ConflictAction {

/**
* TODO
*/
public DoNothing() {}

@NotNull
@Override
public Collection<AstNode> children() {
return new ArrayList<>();
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitConflictActionDoNothing(this, ctx);
}
}

/**
* This is the DO REPLACE variant of the conflict action.
* @see ConflictAction
* @see OnConflict#action
* @see Insert#onConflict
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class DoReplace extends ConflictAction {
/**
* TODO
*/
@NotNull
public final DoReplaceAction action;

/**
* TODO
*/
@Nullable
public final Expr condition;

/**
* TODO
* @param action TODO
* @param condition TODO
*/
public DoReplace(@NotNull DoReplaceAction action, @Nullable Expr condition) {
this.action = action;
this.condition = condition;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> children = new ArrayList<>();
children.add(action);
if (condition != null) {
children.add(condition);
}
return children;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitConflictActionDoReplace(this, ctx);
}
}

/**
* This is the DO UPDATE variant of the conflict action.
* @see ConflictAction
* @see OnConflict#action
* @see Insert#onConflict
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class DoUpdate extends ConflictAction {
/**
* TODO
*/
@NotNull
public final DoUpdateAction action;

/**
* TODO
*/
@Nullable
public final Expr condition;

/**
* TODO
* @param action TODO
* @param condition TODO
*/
public DoUpdate(@NotNull DoUpdateAction action, @Nullable Expr condition) {
this.action = action;
this.condition = condition;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> children = new ArrayList<>();
children.add(action);
if (condition != null) {
children.add(condition);
}
return children;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitConflictActionDoUpdate(this, ctx);
}
}
}
93 changes: 93 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/dml/ConflictTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.partiql.ast.dml;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.partiql.ast.AstNode;
import org.partiql.ast.AstVisitor;
import org.partiql.ast.Identifier;
import org.partiql.ast.IdentifierChain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* This represents the potential targets for the ON CONFLICT clause.
* @see OnConflict
* @see OnConflict#target
*/
public abstract class ConflictTarget extends AstNode {

/**
* This is the index variant of the conflict target.
* @see OnConflict
* @see ConflictTarget
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class Index extends ConflictTarget {
/**
* TODO
*/
// TODO: Should this be a list of identifiers? Or paths? Expressions?
@NotNull
public final List<Identifier> indexes;

/**
* TODO
* @param indexes TODO
*/
public Index(@NotNull List<Identifier> indexes) {
this.indexes = indexes;
}

@NotNull
@Override
public Collection<AstNode> children() {
return new ArrayList<>(indexes);
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitConflictTargetIndex(this, ctx);
}
}

/**
* This is the ON CONSTRAINT variant of the conflict target.
* @see OnConflict
* @see ConflictTarget
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class Constraint extends ConflictTarget {
/**
* TODO
*/
// TODO: Should this be a qualified identifier?
@NotNull
public final IdentifierChain constraintName;
johnedquinn marked this conversation as resolved.
Show resolved Hide resolved

/**
* TODO
* @param constraintName TODO
*/
public Constraint(@NotNull IdentifierChain constraintName) {
this.constraintName = constraintName;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> children = new ArrayList<>();
children.add(constraintName);
return children;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitConflictTargetConstraint(this, ctx);
}
}
}
Loading
Loading