From 055db0da27122724438d50be568a800d66618193 Mon Sep 17 00:00:00 2001 From: Alan Cai Date: Thu, 24 Oct 2024 10:51:23 -0700 Subject: [PATCH] [v1] Add defaults for AstVisitor; port partiql-ast normalization passes to partiql-planner --- partiql-ast/api/partiql-ast.api | 195 ++++----- .../java/org/partiql/ast/v1/AstVisitor.java | 396 +++++++++++++----- .../partiql/ast/v1/expr/SessionAttribute.java | 8 + .../org/partiql/ast/helpers/ToBinder.kt | 2 + .../org/partiql/ast/normalize/AstPass.kt | 2 + .../org/partiql/ast/normalize/Normalize.kt | 2 + .../ast/normalize/NormalizeFromSource.kt | 2 + .../partiql/ast/normalize/NormalizeGroupBy.kt | 2 + .../planner/internal/helpers/ToBinder.kt | 82 ++++ .../planner/internal/normalize/AstPass.kt | 24 ++ .../planner/internal/normalize/Normalize.kt | 29 ++ .../internal/normalize/NormalizeFromSource.kt | 78 ++++ .../internal/normalize/NormalizeGroupBy.kt | 57 +++ 13 files changed, 684 insertions(+), 195 deletions(-) create mode 100644 partiql-planner/src/main/kotlin/org/partiql/planner/internal/helpers/ToBinder.kt create mode 100644 partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/AstPass.kt create mode 100644 partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/Normalize.kt create mode 100644 partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeFromSource.kt create mode 100644 partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeGroupBy.kt diff --git a/partiql-ast/api/partiql-ast.api b/partiql-ast/api/partiql-ast.api index 6d59f7895..804582189 100644 --- a/partiql-ast/api/partiql-ast.api +++ b/partiql-ast/api/partiql-ast.api @@ -5639,102 +5639,104 @@ public abstract class org/partiql/ast/v1/AstNode { } public abstract interface class org/partiql/ast/v1/AstVisitor { - public abstract fun visit (Lorg/partiql/ast/v1/AstNode;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExclude (Lorg/partiql/ast/v1/Exclude;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExcludePath (Lorg/partiql/ast/v1/ExcludePath;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExcludeStep (Lorg/partiql/ast/v1/ExcludeStep;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExcludeStepCollIndex (Lorg/partiql/ast/v1/ExcludeStep$CollIndex;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExcludeStepCollWildcard (Lorg/partiql/ast/v1/ExcludeStep$CollWildcard;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExcludeStepStructField (Lorg/partiql/ast/v1/ExcludeStep$StructField;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExcludeStepStructWildcard (Lorg/partiql/ast/v1/ExcludeStep$StructWildcard;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExplain (Lorg/partiql/ast/v1/Explain;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExpr (Lorg/partiql/ast/v1/expr/Expr;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprAnd (Lorg/partiql/ast/v1/expr/ExprAnd;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprArray (Lorg/partiql/ast/v1/expr/ExprArray;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprBag (Lorg/partiql/ast/v1/expr/ExprBag;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprBetween (Lorg/partiql/ast/v1/expr/ExprBetween;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprCall (Lorg/partiql/ast/v1/expr/ExprCall;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprCase (Lorg/partiql/ast/v1/expr/ExprCase;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprCaseBranch (Lorg/partiql/ast/v1/expr/ExprCase$Branch;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprCast (Lorg/partiql/ast/v1/expr/ExprCast;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprCoalesce (Lorg/partiql/ast/v1/expr/ExprCoalesce;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprExtract (Lorg/partiql/ast/v1/expr/ExprExtract;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprInCollection (Lorg/partiql/ast/v1/expr/ExprInCollection;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprIsType (Lorg/partiql/ast/v1/expr/ExprIsType;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprLike (Lorg/partiql/ast/v1/expr/ExprLike;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprLit (Lorg/partiql/ast/v1/expr/ExprLit;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprMatch (Lorg/partiql/ast/v1/expr/ExprMatch;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprNot (Lorg/partiql/ast/v1/expr/ExprNot;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprNullIf (Lorg/partiql/ast/v1/expr/ExprNullIf;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprOperator (Lorg/partiql/ast/v1/expr/ExprOperator;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprOr (Lorg/partiql/ast/v1/expr/ExprOr;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprOverlay (Lorg/partiql/ast/v1/expr/ExprOverlay;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprParameter (Lorg/partiql/ast/v1/expr/ExprParameter;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprPath (Lorg/partiql/ast/v1/expr/ExprPath;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprPosition (Lorg/partiql/ast/v1/expr/ExprPosition;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprQuerySet (Lorg/partiql/ast/v1/expr/ExprQuerySet;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprSessionAttribute (Lorg/partiql/ast/v1/expr/ExprSessionAttribute;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprStruct (Lorg/partiql/ast/v1/expr/ExprStruct;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprStructField (Lorg/partiql/ast/v1/expr/ExprStruct$Field;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprSubstring (Lorg/partiql/ast/v1/expr/ExprSubstring;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprTrim (Lorg/partiql/ast/v1/expr/ExprTrim;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprValues (Lorg/partiql/ast/v1/expr/ExprValues;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprValuesRow (Lorg/partiql/ast/v1/expr/ExprValues$Row;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprVarRef (Lorg/partiql/ast/v1/expr/ExprVarRef;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprVariant (Lorg/partiql/ast/v1/expr/ExprVariant;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprWindow (Lorg/partiql/ast/v1/expr/ExprWindow;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitExprWindowOver (Lorg/partiql/ast/v1/expr/ExprWindow$Over;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitFrom (Lorg/partiql/ast/v1/From;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitFromExpr (Lorg/partiql/ast/v1/FromExpr;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitFromJoin (Lorg/partiql/ast/v1/FromJoin;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphLabel (Lorg/partiql/ast/v1/graph/GraphLabel;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphLabelConj (Lorg/partiql/ast/v1/graph/GraphLabel$Conj;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphLabelDisj (Lorg/partiql/ast/v1/graph/GraphLabel$Disj;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphLabelName (Lorg/partiql/ast/v1/graph/GraphLabel$Name;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphLabelNegation (Lorg/partiql/ast/v1/graph/GraphLabel$Negation;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphLabelWildcard (Lorg/partiql/ast/v1/graph/GraphLabel$Wildcard;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphMatch (Lorg/partiql/ast/v1/graph/GraphMatch;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphMatchPattern (Lorg/partiql/ast/v1/graph/GraphPattern;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphPart (Lorg/partiql/ast/v1/graph/GraphPart;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphPartEdge (Lorg/partiql/ast/v1/graph/GraphPart$Edge;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphPartNode (Lorg/partiql/ast/v1/graph/GraphPart$Node;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphPartPattern (Lorg/partiql/ast/v1/graph/GraphPart$Pattern;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphQuantifier (Lorg/partiql/ast/v1/graph/GraphQuantifier;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelector (Lorg/partiql/ast/v1/graph/GraphSelector;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelectorAllShortest (Lorg/partiql/ast/v1/graph/GraphSelector$AllShortest;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelectorAny (Lorg/partiql/ast/v1/graph/GraphSelector$Any;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelectorAnyK (Lorg/partiql/ast/v1/graph/GraphSelector$AnyK;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelectorAnyShortest (Lorg/partiql/ast/v1/graph/GraphSelector$AnyShortest;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelectorShortestK (Lorg/partiql/ast/v1/graph/GraphSelector$ShortestK;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGraphSelectorShortestKGroup (Lorg/partiql/ast/v1/graph/GraphSelector$ShortestKGroup;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGroupBy (Lorg/partiql/ast/v1/GroupBy;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitGroupByKey (Lorg/partiql/ast/v1/GroupBy$Key;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitIdentifier (Lorg/partiql/ast/v1/Identifier;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitIdentifierChain (Lorg/partiql/ast/v1/IdentifierChain;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitLet (Lorg/partiql/ast/v1/Let;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitLetBinding (Lorg/partiql/ast/v1/Let$Binding;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitOrderBy (Lorg/partiql/ast/v1/OrderBy;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitPathStep (Lorg/partiql/ast/v1/expr/PathStep;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitPathStepAllElements (Lorg/partiql/ast/v1/expr/PathStep$AllElements;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitPathStepAllFields (Lorg/partiql/ast/v1/expr/PathStep$AllFields;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitPathStepElement (Lorg/partiql/ast/v1/expr/PathStep$Element;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitPathStepField (Lorg/partiql/ast/v1/expr/PathStep$Field;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitQuery (Lorg/partiql/ast/v1/Query;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitQueryBody (Lorg/partiql/ast/v1/QueryBody;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitQueryBodySFW (Lorg/partiql/ast/v1/QueryBody$SFW;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitQueryBodySetOp (Lorg/partiql/ast/v1/QueryBody$SetOp;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelect (Lorg/partiql/ast/v1/Select;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectItem (Lorg/partiql/ast/v1/SelectItem;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectItemExpr (Lorg/partiql/ast/v1/SelectItem$Expr;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectItemStar (Lorg/partiql/ast/v1/SelectItem$Star;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectList (Lorg/partiql/ast/v1/SelectList;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectPivot (Lorg/partiql/ast/v1/SelectPivot;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectStar (Lorg/partiql/ast/v1/SelectStar;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSelectValue (Lorg/partiql/ast/v1/SelectValue;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSetOp (Lorg/partiql/ast/v1/SetOp;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitSort (Lorg/partiql/ast/v1/Sort;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitStatement (Lorg/partiql/ast/v1/Statement;Ljava/lang/Object;)Ljava/lang/Object; - public abstract fun visitTableRef (Lorg/partiql/ast/v1/FromTableRef;Ljava/lang/Object;)Ljava/lang/Object; + public abstract fun defaultReturn (Lorg/partiql/ast/v1/AstNode;Ljava/lang/Object;)Ljava/lang/Object; + public fun defaultVisit (Lorg/partiql/ast/v1/AstNode;Ljava/lang/Object;)Ljava/lang/Object; + public fun visit (Lorg/partiql/ast/v1/AstNode;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExclude (Lorg/partiql/ast/v1/Exclude;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExcludePath (Lorg/partiql/ast/v1/ExcludePath;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExcludeStep (Lorg/partiql/ast/v1/ExcludeStep;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExcludeStepCollIndex (Lorg/partiql/ast/v1/ExcludeStep$CollIndex;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExcludeStepCollWildcard (Lorg/partiql/ast/v1/ExcludeStep$CollWildcard;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExcludeStepStructField (Lorg/partiql/ast/v1/ExcludeStep$StructField;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExcludeStepStructWildcard (Lorg/partiql/ast/v1/ExcludeStep$StructWildcard;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExplain (Lorg/partiql/ast/v1/Explain;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExpr (Lorg/partiql/ast/v1/expr/Expr;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprAnd (Lorg/partiql/ast/v1/expr/ExprAnd;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprArray (Lorg/partiql/ast/v1/expr/ExprArray;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprBag (Lorg/partiql/ast/v1/expr/ExprBag;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprBetween (Lorg/partiql/ast/v1/expr/ExprBetween;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprCall (Lorg/partiql/ast/v1/expr/ExprCall;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprCase (Lorg/partiql/ast/v1/expr/ExprCase;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprCaseBranch (Lorg/partiql/ast/v1/expr/ExprCase$Branch;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprCast (Lorg/partiql/ast/v1/expr/ExprCast;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprCoalesce (Lorg/partiql/ast/v1/expr/ExprCoalesce;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprExtract (Lorg/partiql/ast/v1/expr/ExprExtract;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprInCollection (Lorg/partiql/ast/v1/expr/ExprInCollection;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprIsType (Lorg/partiql/ast/v1/expr/ExprIsType;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprLike (Lorg/partiql/ast/v1/expr/ExprLike;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprLit (Lorg/partiql/ast/v1/expr/ExprLit;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprMatch (Lorg/partiql/ast/v1/expr/ExprMatch;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprNot (Lorg/partiql/ast/v1/expr/ExprNot;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprNullIf (Lorg/partiql/ast/v1/expr/ExprNullIf;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprOperator (Lorg/partiql/ast/v1/expr/ExprOperator;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprOr (Lorg/partiql/ast/v1/expr/ExprOr;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprOverlay (Lorg/partiql/ast/v1/expr/ExprOverlay;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprParameter (Lorg/partiql/ast/v1/expr/ExprParameter;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprPath (Lorg/partiql/ast/v1/expr/ExprPath;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprPosition (Lorg/partiql/ast/v1/expr/ExprPosition;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprQuerySet (Lorg/partiql/ast/v1/expr/ExprQuerySet;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprSessionAttribute (Lorg/partiql/ast/v1/expr/ExprSessionAttribute;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprStruct (Lorg/partiql/ast/v1/expr/ExprStruct;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprStructField (Lorg/partiql/ast/v1/expr/ExprStruct$Field;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprSubstring (Lorg/partiql/ast/v1/expr/ExprSubstring;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprTrim (Lorg/partiql/ast/v1/expr/ExprTrim;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprValues (Lorg/partiql/ast/v1/expr/ExprValues;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprValuesRow (Lorg/partiql/ast/v1/expr/ExprValues$Row;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprVarRef (Lorg/partiql/ast/v1/expr/ExprVarRef;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprVariant (Lorg/partiql/ast/v1/expr/ExprVariant;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprWindow (Lorg/partiql/ast/v1/expr/ExprWindow;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitExprWindowOver (Lorg/partiql/ast/v1/expr/ExprWindow$Over;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitFrom (Lorg/partiql/ast/v1/From;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitFromExpr (Lorg/partiql/ast/v1/FromExpr;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitFromJoin (Lorg/partiql/ast/v1/FromJoin;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphLabel (Lorg/partiql/ast/v1/graph/GraphLabel;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphLabelConj (Lorg/partiql/ast/v1/graph/GraphLabel$Conj;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphLabelDisj (Lorg/partiql/ast/v1/graph/GraphLabel$Disj;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphLabelName (Lorg/partiql/ast/v1/graph/GraphLabel$Name;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphLabelNegation (Lorg/partiql/ast/v1/graph/GraphLabel$Negation;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphLabelWildcard (Lorg/partiql/ast/v1/graph/GraphLabel$Wildcard;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphMatch (Lorg/partiql/ast/v1/graph/GraphMatch;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphMatchPattern (Lorg/partiql/ast/v1/graph/GraphPattern;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphPart (Lorg/partiql/ast/v1/graph/GraphPart;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphPartEdge (Lorg/partiql/ast/v1/graph/GraphPart$Edge;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphPartNode (Lorg/partiql/ast/v1/graph/GraphPart$Node;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphPartPattern (Lorg/partiql/ast/v1/graph/GraphPart$Pattern;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphQuantifier (Lorg/partiql/ast/v1/graph/GraphQuantifier;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelector (Lorg/partiql/ast/v1/graph/GraphSelector;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelectorAllShortest (Lorg/partiql/ast/v1/graph/GraphSelector$AllShortest;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelectorAny (Lorg/partiql/ast/v1/graph/GraphSelector$Any;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelectorAnyK (Lorg/partiql/ast/v1/graph/GraphSelector$AnyK;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelectorAnyShortest (Lorg/partiql/ast/v1/graph/GraphSelector$AnyShortest;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelectorShortestK (Lorg/partiql/ast/v1/graph/GraphSelector$ShortestK;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGraphSelectorShortestKGroup (Lorg/partiql/ast/v1/graph/GraphSelector$ShortestKGroup;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGroupBy (Lorg/partiql/ast/v1/GroupBy;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitGroupByKey (Lorg/partiql/ast/v1/GroupBy$Key;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitIdentifier (Lorg/partiql/ast/v1/Identifier;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitIdentifierChain (Lorg/partiql/ast/v1/IdentifierChain;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitLet (Lorg/partiql/ast/v1/Let;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitLetBinding (Lorg/partiql/ast/v1/Let$Binding;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitOrderBy (Lorg/partiql/ast/v1/OrderBy;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitPathStep (Lorg/partiql/ast/v1/expr/PathStep;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitPathStepAllElements (Lorg/partiql/ast/v1/expr/PathStep$AllElements;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitPathStepAllFields (Lorg/partiql/ast/v1/expr/PathStep$AllFields;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitPathStepElement (Lorg/partiql/ast/v1/expr/PathStep$Element;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitPathStepField (Lorg/partiql/ast/v1/expr/PathStep$Field;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitQuery (Lorg/partiql/ast/v1/Query;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitQueryBody (Lorg/partiql/ast/v1/QueryBody;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitQueryBodySFW (Lorg/partiql/ast/v1/QueryBody$SFW;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitQueryBodySetOp (Lorg/partiql/ast/v1/QueryBody$SetOp;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelect (Lorg/partiql/ast/v1/Select;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectItem (Lorg/partiql/ast/v1/SelectItem;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectItemExpr (Lorg/partiql/ast/v1/SelectItem$Expr;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectItemStar (Lorg/partiql/ast/v1/SelectItem$Star;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectList (Lorg/partiql/ast/v1/SelectList;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectPivot (Lorg/partiql/ast/v1/SelectPivot;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectStar (Lorg/partiql/ast/v1/SelectStar;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSelectValue (Lorg/partiql/ast/v1/SelectValue;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSetOp (Lorg/partiql/ast/v1/SetOp;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitSort (Lorg/partiql/ast/v1/Sort;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitStatement (Lorg/partiql/ast/v1/Statement;Ljava/lang/Object;)Ljava/lang/Object; + public fun visitTableRef (Lorg/partiql/ast/v1/FromTableRef;Ljava/lang/Object;)Ljava/lang/Object; } public class org/partiql/ast/v1/DataType : org/partiql/ast/v1/AstNode, org/partiql/ast/v1/Enum { @@ -7316,6 +7318,7 @@ public class org/partiql/ast/v1/expr/SessionAttribute : org/partiql/ast/v1/Enum public fun code ()I public fun equals (Ljava/lang/Object;)Z public fun hashCode ()I + public fun name ()Ljava/lang/String; public static fun valueOf (Ljava/lang/String;)Lorg/partiql/ast/v1/expr/SessionAttribute; public static fun values ()[Lorg/partiql/ast/v1/expr/SessionAttribute; } diff --git a/partiql-ast/src/main/java/org/partiql/ast/v1/AstVisitor.java b/partiql-ast/src/main/java/org/partiql/ast/v1/AstVisitor.java index 6ac7c5f85..099132ebd 100644 --- a/partiql-ast/src/main/java/org/partiql/ast/v1/AstVisitor.java +++ b/partiql-ast/src/main/java/org/partiql/ast/v1/AstVisitor.java @@ -41,198 +41,396 @@ import org.partiql.ast.v1.graph.GraphSelector; public interface AstVisitor { - R visit(AstNode node, C ctx); + default R defaultVisit(AstNode node, C ctx) { + for (AstNode child : node.children()) { + child.accept(this, ctx); + } + return defaultReturn(node, ctx); + } - R visitStatement(Statement node, C ctx); + R defaultReturn(AstNode node, C ctx); - R visitQuery(Query node, C ctx); + default R visit(AstNode node, C ctx) { + return node.accept(this, ctx); + } - R visitExplain(Explain node, C ctx); + default R visitStatement(Statement node, C ctx) { + return defaultVisit(node, ctx); + } - R visitIdentifier(Identifier node, C ctx); + default R visitQuery(Query node, C ctx) { + return defaultVisit(node, ctx); + } - R visitIdentifierChain(IdentifierChain node, C ctx); + default R visitExplain(Explain node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExpr(Expr node, C ctx); + default R visitIdentifier(Identifier node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprLit(ExprLit node, C ctx); + default R visitIdentifierChain(IdentifierChain node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprVariant(ExprVariant node, C ctx); + default R visitExpr(Expr node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprVarRef(ExprVarRef node, C ctx); + default R visitExprLit(ExprLit node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprSessionAttribute(ExprSessionAttribute node, C ctx); + default R visitExprVariant(ExprVariant node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprPath(ExprPath node, C ctx); + default R visitExprVarRef(ExprVarRef node, C ctx) { + return defaultVisit(node, ctx); + } - R visitPathStep(PathStep node, C ctx); + default R visitExprSessionAttribute(ExprSessionAttribute node, C ctx) { + return defaultVisit(node, ctx); + } - R visitPathStepField(PathStep.Field node, C ctx); + default R visitExprPath(ExprPath node, C ctx) { + return defaultVisit(node, ctx); + } - R visitPathStepElement(PathStep.Element node, C ctx); + default R visitPathStep(PathStep node, C ctx) { + return defaultVisit(node, ctx); + } - R visitPathStepAllElements(PathStep.AllElements node, C ctx); + default R visitPathStepField(PathStep.Field node, C ctx) { + return defaultVisit(node, ctx); + } - R visitPathStepAllFields(PathStep.AllFields node, C ctx); + default R visitPathStepElement(PathStep.Element node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprCall(ExprCall node, C ctx); + default R visitPathStepAllElements(PathStep.AllElements node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprParameter(ExprParameter node, C ctx); + default R visitPathStepAllFields(PathStep.AllFields node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprOperator(ExprOperator node, C ctx); + default R visitExprCall(ExprCall node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprNot(ExprNot node, C ctx); + default R visitExprParameter(ExprParameter node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprAnd(ExprAnd node, C ctx); + default R visitExprOperator(ExprOperator node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprOr(ExprOr node, C ctx); + default R visitExprNot(ExprNot node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprValues(ExprValues node, C ctx); + default R visitExprAnd(ExprAnd node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprValuesRow(ExprValues.Row node, C ctx); + default R visitExprOr(ExprOr node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprArray(ExprArray node, C ctx); + default R visitExprValues(ExprValues node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprBag(ExprBag node, C ctx); + default R visitExprValuesRow(ExprValues.Row node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprStruct(ExprStruct node, C ctx); + default R visitExprArray(ExprArray node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprStructField(ExprStruct.Field node, C ctx); + default R visitExprBag(ExprBag node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprLike(ExprLike node, C ctx); + default R visitExprStruct(ExprStruct node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprBetween(ExprBetween node, C ctx); + default R visitExprStructField(ExprStruct.Field node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprInCollection(ExprInCollection node, C ctx); + default R visitExprLike(ExprLike node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprIsType(ExprIsType node, C ctx); + default R visitExprBetween(ExprBetween node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprCase(ExprCase node, C ctx); + default R visitExprInCollection(ExprInCollection node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprCaseBranch(ExprCase.Branch node, C ctx); + default R visitExprIsType(ExprIsType node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprCoalesce(ExprCoalesce node, C ctx); + default R visitExprCase(ExprCase node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprNullIf(ExprNullIf node, C ctx); + default R visitExprCaseBranch(ExprCase.Branch node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprSubstring(ExprSubstring node, C ctx); + default R visitExprCoalesce(ExprCoalesce node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprPosition(ExprPosition node, C ctx); + default R visitExprNullIf(ExprNullIf node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprTrim(ExprTrim node, C ctx); + default R visitExprSubstring(ExprSubstring node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprOverlay(ExprOverlay node, C ctx); + default R visitExprPosition(ExprPosition node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprExtract(ExprExtract node, C ctx); + default R visitExprTrim(ExprTrim node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprCast(ExprCast node, C ctx); + default R visitExprOverlay(ExprOverlay node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprQuerySet(ExprQuerySet node, C ctx); + default R visitExprExtract(ExprExtract node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprMatch(ExprMatch node, C ctx); + default R visitExprCast(ExprCast node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprWindow(ExprWindow node, C ctx); + default R visitExprQuerySet(ExprQuerySet node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExprWindowOver(ExprWindow.Over node, C ctx); + default R visitExprMatch(ExprMatch node, C ctx) { + return defaultVisit(node, ctx); + } - R visitQueryBody(QueryBody node, C ctx); + default R visitExprWindow(ExprWindow node, C ctx) { + return defaultVisit(node, ctx); + } - R visitQueryBodySFW(QueryBody.SFW node, C ctx); + default R visitExprWindowOver(ExprWindow.Over node, C ctx) { + return defaultVisit(node, ctx); + } - R visitQueryBodySetOp(QueryBody.SetOp node, C ctx); + default R visitQueryBody(QueryBody node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelect(Select node, C ctx); + default R visitQueryBodySFW(QueryBody.SFW node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectStar(SelectStar node, C ctx); + default R visitQueryBodySetOp(QueryBody.SetOp node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectList(SelectList node, C ctx); + default R visitSelect(Select node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectItem(SelectItem node, C ctx); + default R visitSelectStar(SelectStar node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectItemStar(SelectItem.Star node, C ctx); + default R visitSelectList(SelectList node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectItemExpr(SelectItem.Expr node, C ctx); + default R visitSelectItem(SelectItem node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectPivot(SelectPivot node, C ctx); + default R visitSelectItemStar(SelectItem.Star node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSelectValue(SelectValue node, C ctx); + default R visitSelectItemExpr(SelectItem.Expr node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExclude(Exclude node, C ctx); + default R visitSelectPivot(SelectPivot node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExcludePath(ExcludePath node, C ctx); + default R visitSelectValue(SelectValue node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExcludeStep(ExcludeStep node, C ctx); + default R visitExclude(Exclude node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExcludeStepStructField(ExcludeStep.StructField node, C ctx); + default R visitExcludePath(ExcludePath node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExcludeStepCollIndex(ExcludeStep.CollIndex node, C ctx); + default R visitExcludeStep(ExcludeStep node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExcludeStepStructWildcard(ExcludeStep.StructWildcard node, C ctx); + default R visitExcludeStepStructField(ExcludeStep.StructField node, C ctx) { + return defaultVisit(node, ctx); + } - R visitExcludeStepCollWildcard(ExcludeStep.CollWildcard node, C ctx); + default R visitExcludeStepCollIndex(ExcludeStep.CollIndex node, C ctx) { + return defaultVisit(node, ctx); + } - R visitFrom(From node, C ctx); + default R visitExcludeStepStructWildcard(ExcludeStep.StructWildcard node, C ctx) { + return defaultVisit(node, ctx); + } - R visitTableRef(FromTableRef node, C ctx); + default R visitExcludeStepCollWildcard(ExcludeStep.CollWildcard node, C ctx) { + return defaultVisit(node, ctx); + } - R visitFromExpr(FromExpr node, C ctx); + default R visitFrom(From node, C ctx) { + return defaultVisit(node, ctx); + } - R visitFromJoin(FromJoin node, C ctx); + default R visitTableRef(FromTableRef node, C ctx) { + return defaultVisit(node, ctx); + } - R visitLet(Let node, C ctx); + default R visitFromExpr(FromExpr node, C ctx) { + return defaultVisit(node, ctx); + } - R visitLetBinding(Let.Binding node, C ctx); + default R visitFromJoin(FromJoin node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGroupBy(GroupBy node, C ctx); + default R visitLet(Let node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGroupByKey(GroupBy.Key node, C ctx); + default R visitLetBinding(Let.Binding node, C ctx) { + return defaultVisit(node, ctx); + } - R visitOrderBy(OrderBy node, C ctx); + default R visitGroupBy(GroupBy node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSort(Sort node, C ctx); + default R visitGroupByKey(GroupBy.Key node, C ctx) { + return defaultVisit(node, ctx); + } - R visitSetOp(SetOp node, C ctx); + default R visitOrderBy(OrderBy node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphMatch(GraphMatch node, C ctx); + default R visitSort(Sort node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphMatchPattern(GraphPattern node, C ctx); + default R visitSetOp(SetOp node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphPart(GraphPart node, C ctx); + default R visitGraphMatch(GraphMatch node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphPartNode(GraphPart.Node node, C ctx); + default R visitGraphMatchPattern(GraphPattern node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphPartEdge(GraphPart.Edge node, C ctx); + default R visitGraphPart(GraphPart node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphPartPattern(GraphPart.Pattern node, C ctx); + default R visitGraphPartNode(GraphPart.Node node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphQuantifier(GraphQuantifier node, C ctx); + default R visitGraphPartEdge(GraphPart.Edge node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelector(GraphSelector node, C ctx); + default R visitGraphPartPattern(GraphPart.Pattern node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelectorAnyShortest(GraphSelector.AnyShortest node, C ctx); + default R visitGraphQuantifier(GraphQuantifier node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelectorAllShortest(GraphSelector.AllShortest node, C ctx); + default R visitGraphSelector(GraphSelector node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelectorAny(GraphSelector.Any node, C ctx); + default R visitGraphSelectorAnyShortest(GraphSelector.AnyShortest node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelectorAnyK(GraphSelector.AnyK node, C ctx); + default R visitGraphSelectorAllShortest(GraphSelector.AllShortest node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelectorShortestK(GraphSelector.ShortestK node, C ctx); + default R visitGraphSelectorAny(GraphSelector.Any node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphSelectorShortestKGroup( - GraphSelector.ShortestKGroup node, - C ctx - ); + default R visitGraphSelectorAnyK(GraphSelector.AnyK node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphLabel(GraphLabel node, C ctx); + default R visitGraphSelectorShortestK(GraphSelector.ShortestK node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphLabelName(GraphLabel.Name node, C ctx); + default R visitGraphSelectorShortestKGroup(GraphSelector.ShortestKGroup node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphLabelWildcard(GraphLabel.Wildcard node, C ctx); + default R visitGraphLabel(GraphLabel node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphLabelNegation(GraphLabel.Negation node, C ctx); + default R visitGraphLabelName(GraphLabel.Name node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphLabelConj(GraphLabel.Conj node, C ctx); + default R visitGraphLabelWildcard(GraphLabel.Wildcard node, C ctx) { + return defaultVisit(node, ctx); + } - R visitGraphLabelDisj(GraphLabel.Disj node, C ctx); + default R visitGraphLabelNegation(GraphLabel.Negation node, C ctx) { + return defaultVisit(node, ctx); + } + + default R visitGraphLabelConj(GraphLabel.Conj node, C ctx) { + return defaultVisit(node, ctx); + } + + default R visitGraphLabelDisj(GraphLabel.Disj node, C ctx) { + return defaultVisit(node, ctx); + } } diff --git a/partiql-ast/src/main/java/org/partiql/ast/v1/expr/SessionAttribute.java b/partiql-ast/src/main/java/org/partiql/ast/v1/expr/SessionAttribute.java index 56a348da3..24ce876a1 100644 --- a/partiql-ast/src/main/java/org/partiql/ast/v1/expr/SessionAttribute.java +++ b/partiql-ast/src/main/java/org/partiql/ast/v1/expr/SessionAttribute.java @@ -49,4 +49,12 @@ public static SessionAttribute[] values() { CURRENT_DATE() }; } + + public String name() { + switch (this.code) { + case CURRENT_USER: return "CURRENT_USER"; + case CURRENT_DATE: return "CURRENT_DATE"; + default: return "UNKNOWN"; + } + } } diff --git a/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToBinder.kt b/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToBinder.kt index d0bd97ca1..fbc2a78bc 100644 --- a/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToBinder.kt +++ b/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToBinder.kt @@ -6,6 +6,8 @@ import org.partiql.ast.builder.ast import org.partiql.value.PartiQLValueExperimental import org.partiql.value.StringValue +// TODO DELETE FILE + private val col = { index: () -> Int -> "_${index()}" } /** diff --git a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/AstPass.kt b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/AstPass.kt index 6513c4466..57faa804b 100644 --- a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/AstPass.kt +++ b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/AstPass.kt @@ -16,6 +16,8 @@ package org.partiql.ast.normalize import org.partiql.ast.Statement +// TODO DELETE FILE + /** * Wraps a rewriter with a default entry point. */ diff --git a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/Normalize.kt b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/Normalize.kt index 13d01ad5b..d4bce86b0 100644 --- a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/Normalize.kt +++ b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/Normalize.kt @@ -17,6 +17,8 @@ package org.partiql.ast.normalize import org.partiql.ast.Statement +// TODO DELETE FILE + /** * AST normalization */ diff --git a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt index 9fa0bd963..29531daa6 100644 --- a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt +++ b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt @@ -23,6 +23,8 @@ import org.partiql.ast.fromJoin import org.partiql.ast.helpers.toBinder import org.partiql.ast.util.AstRewriter +// TODO DELETE FILE + /** * Assign aliases to any FROM source which does not have one. */ diff --git a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeGroupBy.kt b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeGroupBy.kt index 85fcad17d..66228fe23 100644 --- a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeGroupBy.kt +++ b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeGroupBy.kt @@ -22,6 +22,8 @@ import org.partiql.ast.groupByKey import org.partiql.ast.helpers.toBinder import org.partiql.ast.util.AstRewriter +// TODO DELETE FILE + /** * Adds a unique binder to each group key. */ diff --git a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/helpers/ToBinder.kt b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/helpers/ToBinder.kt new file mode 100644 index 000000000..45f95146a --- /dev/null +++ b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/helpers/ToBinder.kt @@ -0,0 +1,82 @@ +package org.partiql.planner.internal.helpers + +import org.partiql.ast.v1.Ast.identifier +import org.partiql.ast.v1.Identifier +import org.partiql.ast.v1.IdentifierChain +import org.partiql.ast.v1.expr.Expr +import org.partiql.ast.v1.expr.ExprCast +import org.partiql.ast.v1.expr.ExprLit +import org.partiql.ast.v1.expr.ExprPath +import org.partiql.ast.v1.expr.ExprSessionAttribute +import org.partiql.ast.v1.expr.ExprVarRef +import org.partiql.ast.v1.expr.PathStep +import org.partiql.value.PartiQLValueExperimental +import org.partiql.value.StringValue + +private val col = { index: () -> Int -> "_${index()}" } + +/** + * Produces a "binder" (AS alias) for an expression following the given rules: + * + * 1. If item is an id, use the last symbol + * 2. If item is a path with a final symbol step, use the symbol — else 4 + * 3. If item is a cast, use the value name + * 4. Else, use item index with prefix _ + * + * See https://github.com/partiql/partiql-lang-kotlin/issues/1122 + */ +internal fun Expr.toBinder(index: () -> Int): Identifier = when (this) { + is ExprVarRef -> this.identifierChain.toBinder() + is ExprPath -> this.toBinder(index) + is ExprCast -> this.value.toBinder(index) + is ExprSessionAttribute -> this.sessionAttribute.name().uppercase().toBinder() + else -> col(index).toBinder() +} + +/** + * Simple toBinder that uses an int literal rather than a closure. + * + * @param index + * @return + */ +internal fun Expr.toBinder(index: Int): Identifier = toBinder { index } + +private fun String.toBinder(): Identifier = + // Every binder preserves case + identifier(this@toBinder, true) + +private fun IdentifierChain.toBinder(): Identifier { + if (next == null) return root.symbol.toBinder() + var cur = next + var prev = cur + while (cur != null) { + prev = cur + cur = cur.next + } + return prev!!.root.symbol.toBinder() +} + +private fun Identifier.toBinder(): Identifier = symbol.toBinder() + +@OptIn(PartiQLValueExperimental::class) +private fun ExprPath.toBinder(index: () -> Int): Identifier { + if (next == null) return root.toBinder(index) + var cur = next + var prev = next + while (cur != null) { + prev = cur + cur = cur.next + } + return when (prev) { + is PathStep.Field -> prev.field.toBinder() + is PathStep.Element -> { + val k = prev.element + if (k is ExprLit && k.value is StringValue) { + (k.value as StringValue).value!!.toBinder() + } else { + col(index).toBinder() + } + } + else -> col(index).toBinder() + } +} diff --git a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/AstPass.kt b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/AstPass.kt new file mode 100644 index 000000000..882ff16c0 --- /dev/null +++ b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/AstPass.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at: + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file 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 org.partiql.planner.internal.normalize + +import org.partiql.ast.v1.Statement + +/** + * Wraps a rewriter with a default entry point. + */ +internal interface AstPass { + fun apply(statement: Statement): Statement +} diff --git a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/Normalize.kt b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/Normalize.kt new file mode 100644 index 000000000..c5ef7fe47 --- /dev/null +++ b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/Normalize.kt @@ -0,0 +1,29 @@ +@file:JvmName("Normalize") +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at: + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file 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 org.partiql.planner.internal.normalize + +import org.partiql.ast.v1.Statement + +/** + * AST normalization + */ +internal fun Statement.normalize(): Statement { + // could be a fold, but this is nice for setting breakpoints + var ast = this + ast = NormalizeFromSource.apply(ast) + ast = NormalizeGroupBy.apply(ast) + return ast +} diff --git a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeFromSource.kt b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeFromSource.kt new file mode 100644 index 000000000..41c9e24b9 --- /dev/null +++ b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeFromSource.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at: + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file 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 org.partiql.planner.internal.normalize + +import org.partiql.ast.v1.Ast.fromExpr +import org.partiql.ast.v1.Ast.fromJoin +import org.partiql.ast.v1.AstNode +import org.partiql.ast.v1.AstVisitor +import org.partiql.ast.v1.From +import org.partiql.ast.v1.FromExpr +import org.partiql.ast.v1.FromJoin +import org.partiql.ast.v1.FromTableRef +import org.partiql.ast.v1.FromType +import org.partiql.ast.v1.QueryBody +import org.partiql.ast.v1.Statement +import org.partiql.ast.v1.expr.Expr +import org.partiql.planner.internal.helpers.toBinder + +/** + * Assign aliases to any FROM source which does not have one. + */ +internal object NormalizeFromSource : AstPass { + + override fun apply(statement: Statement): Statement = Visitor.visitStatement(statement, 0) as Statement + + private object Visitor : AstVisitor { + + // Each SFW starts the ctx count again. + override fun visitQueryBodySFW(node: QueryBody.SFW, ctx: Int): AstNode = super.visitQueryBodySFW(node, 0) + + override fun visitFrom(node: From, ctx: Int) = super.visitFrom(node, ctx) as From + + override fun visitFromJoin(node: FromJoin, ctx: Int): FromJoin { + val lhs = visitTableRef(node.lhs, ctx) as FromTableRef + val rhs = visitTableRef(node.rhs, ctx + 1) as FromTableRef + val condition = node.condition?.let { visitExpr(it, ctx) as Expr } + return if (lhs !== node.lhs || rhs !== node.rhs || condition !== node.condition) { + fromJoin(lhs, rhs, node.joinType, condition) + } else { + node + } + } + + override fun visitFromExpr(node: FromExpr, ctx: Int): FromExpr { + val expr = visitExpr(node.expr, ctx) as Expr + var i = ctx + var asAlias = node.asAlias + var atAlias = node.atAlias + // derive AS alias + if (asAlias == null) { + asAlias = expr.toBinder(i++) + } + // derive AT binder + if (atAlias == null && node.fromType == FromType.UNPIVOT()) { + atAlias = expr.toBinder(i++) + } + return if (expr !== node.expr || asAlias !== node.asAlias || atAlias !== node.atAlias) { + fromExpr(expr = expr, fromType = node.fromType, asAlias = asAlias, atAlias = atAlias) + } else { + node + } + } + + override fun defaultReturn(node: AstNode, ctx: Int) = node + } +} diff --git a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeGroupBy.kt b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeGroupBy.kt new file mode 100644 index 000000000..593127253 --- /dev/null +++ b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/normalize/NormalizeGroupBy.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at: + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file 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 org.partiql.planner.internal.normalize + +import org.partiql.ast.v1.Ast.groupBy +import org.partiql.ast.v1.Ast.groupByKey +import org.partiql.ast.v1.AstNode +import org.partiql.ast.v1.AstVisitor +import org.partiql.ast.v1.GroupBy +import org.partiql.ast.v1.Statement +import org.partiql.ast.v1.expr.Expr +import org.partiql.planner.internal.helpers.toBinder + +/** + * Adds a unique binder to each group key. + */ +internal object NormalizeGroupBy : AstPass { + + override fun apply(statement: Statement) = Visitor.visitStatement(statement, 0) as Statement + + private object Visitor : AstVisitor { + + override fun visitGroupBy(node: GroupBy, ctx: Int): AstNode { + val keys = node.keys.mapIndexed { index, key -> + visitGroupByKey(key, index + 1) + } + return groupBy(strategy = node.strategy, keys = keys, asAlias = node.asAlias) + } + + override fun visitGroupByKey(node: GroupBy.Key, ctx: Int): GroupBy.Key { + val expr = visitExpr(node.expr, 0) as Expr + val alias = when (node.asAlias) { + null -> expr.toBinder(ctx) + else -> node.asAlias + } + return if (expr !== node.expr || alias !== node.asAlias) { + groupByKey(expr, alias) + } else { + node + } + } + + override fun defaultReturn(node: AstNode, ctx: Int) = node + } +}