Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions core/trino-grammar/src/main/antlr4/io/trino/grammar/sql/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,36 @@ joinCriteria
;

sampledRelation
: patternRecognition (
: pivot (
TABLESAMPLE sampleType '(' percentage=expression ')'
)?
;

pivot
: patternRecognition (
PIVOT '('
pivotAggregation (',' pivotAggregation)*
FOR pivotColumns IN '(' pivotValueGroup (',' pivotValueGroup)* ')'
(GROUP BY groupBy)?
')'
(AS? identifier columnAliases?)?
)?
;

pivotAggregation
: expression (AS? identifier)?
;

pivotColumns
: qualifiedName
| '(' qualifiedName (',' qualifiedName)* ')'
;

pivotValueGroup
: '(' expression (',' expression)+ ')' (AS? identifier)?
| expression (AS? identifier)?
;

sampleType
: BERNOULLI
| SYSTEM
Expand Down Expand Up @@ -1059,7 +1084,7 @@ nonReserved
| MAP | MATCH | MATCHED | MATCHES | MATCH_RECOGNIZE | MATERIALIZED | MEASURES | MERGE | MINUTE | MONTH
| NEAREST | NESTED | NEXT | NFC | NFD | NFKC | NFKD | NO | NONE | NULLIF | NULLS
| OBJECT | OF | OFFSET | OMIT | ONE | ONLY | OPTION | ORDINALITY | OUTPUT | OVER | OVERFLOW
| PARTITION | PARTITIONS | PASSING | PAST | PATH | PATTERN | PER | PERIOD | PERMUTE | PLAN | POSITION | PRECEDING | PRECISION | PRIVILEGES | PROPERTIES | PRUNE
| PARTITION | PARTITIONS | PASSING | PAST | PATH | PATTERN | PER | PERIOD | PERMUTE | PIVOT | PLAN | POSITION | PRECEDING | PRECISION | PRIVILEGES | PROPERTIES | PRUNE
| QUOTES
| RANGE | READ | REFRESH | RENAME | REPEAT | REPEATABLE | REPLACE | RESET | RESPECT | RESTRICT | RETURN | RETURNING | RETURNS | REVOKE | ROLE | ROLES | ROLLBACK | ROW | ROWS | RUNNING
| SCALAR | SCHEMA | SCHEMAS | SECOND | SECURITY | SEEK | SERIALIZABLE | SESSION | SET | SETS
Expand Down Expand Up @@ -1273,6 +1298,7 @@ PATTERN: 'PATTERN';
PER: 'PER';
PERIOD: 'PERIOD';
PERMUTE: 'PERMUTE';
PIVOT: 'PIVOT';
PLAN : 'PLAN';
POSITION: 'POSITION';
PRECEDING: 'PRECEDING';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public void test()
"PER",
"PERIOD",
"PERMUTE",
"PIVOT",
"PLAN",
"POSITION",
"PRECEDING",
Expand Down
47 changes: 44 additions & 3 deletions core/trino-main/src/main/java/io/trino/sql/analyzer/Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import io.trino.sql.tree.Offset;
import io.trino.sql.tree.OrderBy;
import io.trino.sql.tree.Parameter;
import io.trino.sql.tree.Pivot;
import io.trino.sql.tree.QualifiedName;
import io.trino.sql.tree.QuantifiedComparisonExpression;
import io.trino.sql.tree.Query;
Expand Down Expand Up @@ -146,6 +147,8 @@ public class Analysis

private final Map<NodeRef<Table>, Query> namedQueries = new LinkedHashMap<>();

private final Map<NodeRef<Pivot>, PivotAnalysis> pivotAnalyses = new LinkedHashMap<>();

// map expandable query to the node being the inner recursive reference
private final Map<NodeRef<Query>, Node> expandableNamedQueries = new LinkedHashMap<>();

Expand Down Expand Up @@ -188,7 +191,7 @@ public class Analysis

private final Map<NodeRef<QuerySpecification>, List<FunctionCall>> aggregates = new LinkedHashMap<>();
private final Map<NodeRef<OrderBy>, List<Expression>> orderByAggregates = new LinkedHashMap<>();
private final Map<NodeRef<QuerySpecification>, GroupingSetAnalysis> groupingSets = new LinkedHashMap<>();
private final Map<NodeRef<Node>, GroupingSetAnalysis> groupingSets = new LinkedHashMap<>();

private final Map<NodeRef<Node>, Expression> where = new LinkedHashMap<>();
private final Map<NodeRef<QuerySpecification>, Expression> having = new LinkedHashMap<>();
Expand Down Expand Up @@ -438,7 +441,7 @@ public Map<NodeRef<Identifier>, LambdaArgumentDeclaration> getLambdaArgumentRefe
return unmodifiableMap(lambdaArgumentReferences);
}

public void setGroupingSets(QuerySpecification node, GroupingSetAnalysis groupingSets)
public void setGroupingSets(Node node, GroupingSetAnalysis groupingSets)
{
this.groupingSets.put(NodeRef.of(node), groupingSets);
}
Expand All @@ -448,7 +451,7 @@ public boolean isAggregation(QuerySpecification node)
return groupingSets.containsKey(NodeRef.of(node));
}

public GroupingSetAnalysis getGroupingSets(QuerySpecification node)
public GroupingSetAnalysis getGroupingSets(Node node)
{
return groupingSets.get(NodeRef.of(node));
}
Expand Down Expand Up @@ -895,6 +898,21 @@ public void registerNamedQuery(Table tableReference, Query query)
namedQueries.put(NodeRef.of(tableReference), query);
}

public void registerPivotAnalysis(Pivot pivot, PivotAnalysis analysis)
{
requireNonNull(pivot, "pivot is null");
requireNonNull(analysis, "analysis is null");

pivotAnalyses.put(NodeRef.of(pivot), analysis);
}

public PivotAnalysis getPivotAnalysis(Pivot pivot)
{
PivotAnalysis analysis = pivotAnalyses.get(NodeRef.of(pivot));
checkArgument(analysis != null, "pivot has no analysis registered: %s", pivot);
return analysis;
}

public void registerExpandableQuery(Query query, Node recursiveReference)
{
requireNonNull(query, "query is null");
Expand Down Expand Up @@ -1743,6 +1761,29 @@ public Set<FieldId> getAllFields()
}
}

public record PivotAnalysis(
GroupingSetAnalysis groupingSetAnalysis,
boolean distinctGroupingSets,
List<PivotOutputColumn> outputColumns,
List<FunctionCall> aggregates)
{
public PivotAnalysis
{
requireNonNull(groupingSetAnalysis, "groupingSetAnalysis is null");
outputColumns = ImmutableList.copyOf(outputColumns);
aggregates = ImmutableList.copyOf(aggregates);
}
}

public record PivotOutputColumn(String name, Type type)
{
public PivotOutputColumn
{
requireNonNull(name, "name is null");
requireNonNull(type, "type is null");
}
}

public static class UnnestAnalysis
{
private final Map<NodeRef<Expression>, List<Field>> mappings;
Expand Down
Loading
Loading