[
https://issues.apache.org/jira/browse/BEAM-9198?focusedWorklogId=475685&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-475685
]
ASF GitHub Bot logged work on BEAM-9198:
----------------------------------------
Author: ASF GitHub Bot
Created on: 28/Aug/20 05:25
Start Date: 28/Aug/20 05:25
Worklog Time Spent: 10m
Work Description: amaliujia commented on a change in pull request #12595:
URL: https://github.com/apache/beam/pull/12595#discussion_r478837113
##########
File path:
sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/AnalyticScanConverter.java
##########
@@ -0,0 +1,291 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+package org.apache.beam.sdk.extensions.sql.zetasql.translation;
+
+import com.google.zetasql.resolvedast.ResolvedNode;
+import com.google.zetasql.resolvedast.ResolvedNodes;
+import
com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAnalyticFunctionCall;
+import com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAnalyticScan;
+import com.google.zetasql.resolvedast.ResolvedNodes.ResolvedComputedColumn;
+import com.google.zetasql.resolvedast.ResolvedNodes.ResolvedLiteral;
+import com.google.zetasql.resolvedast.ResolvedNodes.ResolvedWindowFrameExpr;
+import com.google.zetasql.resolvedast.ResolvedOrderByItemEnums;
+import com.google.zetasql.resolvedast.ResolvedWindowFrameEnums;
+import com.google.zetasql.resolvedast.ResolvedWindowFrameExprEnums;
+import java.math.BigDecimal;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.repackaged.core.org.apache.commons.compress.utils.Lists;
+import org.apache.beam.sdk.extensions.sql.impl.rel.BeamWindowRel;
+import
org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlCalciteTranslationUtils;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelCollation;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelCollations;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelFieldCollation;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelNode;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.Window;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.Window.Group;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.Window.RexWinAggCall;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.type.RelDataType;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexInputRef;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLiteral;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexNode;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexWindowBound;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlAggFunction;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlLiteral;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlNode;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlRankFunction;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlWindow;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.parser.SqlParserPos;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.type.SqlTypeName;
+import
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.util.ImmutableBitSet;
+
+/** Converts AnalyticScan into BeamWindowRel. */
+public class AnalyticScanConverter extends RelConverter<ResolvedAnalyticScan> {
+
+ AnalyticScanConverter(ConversionContext context) {
+ super(context);
+ }
+
+ @Override
+ public List<ResolvedNode> getInputs(ResolvedAnalyticScan zetaNode) {
+ return Collections.singletonList(zetaNode.getInputScan());
+ }
+
+ @Override
+ public RelNode convert(ResolvedAnalyticScan zetaNode, List<RelNode> inputs) {
+
+ RelNode inputTable = inputs.get(0);
+
+ RelDataType expectedRowType =
+
getExpressionConverter().rexBuilder().getTypeFactory().copyType(inputTable.getRowType());
+
+ List<RexLiteral> constants = Lists.newArrayList();
+ List<Group> groups = Lists.newArrayList();
+
+ int ordinal = 0;
+ for (ResolvedNodes.ResolvedAnalyticFunctionGroup analyticGroup :
+ zetaNode.getFunctionGroupList()) {
+ ImmutableBitSet partitionKeys =
+ ImmutableBitSet.of(
+ analyticGroup.getPartitionBy() != null
+ ?
analyticGroup.getPartitionBy().getPartitionByList().stream()
+ .map(
+ keyColumn -> {
+ return getExpressionConverter()
+ .indexOfProjectionColumnRef(
+ keyColumn.getColumn().getId(),
zetaNode.getColumnList());
+ })
+ .collect(Collectors.toList())
+ : Lists.newArrayList());
+ RelCollation relCollation =
+ RelCollations.of(
+ analyticGroup.getOrderBy().getOrderByItemList().stream()
+ .map(
+ orderColumn -> {
+ int columnIndex =
+ getExpressionConverter()
+ .indexOfProjectionColumnRef(
+
orderColumn.getColumnRef().getColumn().getId(),
+ zetaNode.getColumnList());
+ RelFieldCollation.Direction columnDirection =
+ orderColumn.getIsDescending()
+ ? RelFieldCollation.Direction.DESCENDING
+ : RelFieldCollation.Direction.ASCENDING;
+ RelFieldCollation.NullDirection columnNull =
+ orderColumn.getNullOrder()
+ ==
ResolvedOrderByItemEnums.NullOrderMode.NULLS_FIRST
+ ? RelFieldCollation.NullDirection.FIRST
+ : orderColumn.getNullOrder()
+ ==
ResolvedOrderByItemEnums.NullOrderMode.NULLS_LAST
+ ? RelFieldCollation.NullDirection.LAST
+ :
RelFieldCollation.NullDirection.UNSPECIFIED;
+ return new RelFieldCollation(columnIndex,
columnDirection, columnNull);
+ })
+ .collect(Collectors.toList()));
+
+ for (ResolvedComputedColumn aggColumn :
analyticGroup.getAnalyticFunctionList()) {
+ ResolvedAnalyticFunctionCall aggCall = (ResolvedAnalyticFunctionCall)
aggColumn.getExpr();
+
+ if (aggCall.getDistinct()) {
+ throw new UnsupportedOperationException("Does not support DISTINTC");
+ }
+
+ SqlAggFunction sqlAggFunction =
+ (SqlAggFunction)
+
SqlOperatorMappingTable.ZETASQL_FUNCTION_TO_CALCITE_SQL_OPERATOR.get(
+ aggCall.getFunction().getName());
+ if (sqlAggFunction == null) {
+ throw new UnsupportedOperationException(
+ "Does not support ZetaSQL aggregate function: " +
aggCall.getFunction().getName());
+ }
+
+ RelDataType columnReturnType =
+ ZetaSqlCalciteTranslationUtils.toCalciteType(
+ aggColumn.getColumn().getType(), true,
getCluster().getRexBuilder());
+
+ List<RexNode> argsColumns =
+ aggCall.getArgumentList().stream()
+ .map(
+ argument -> {
+ return
getExpressionConverter().convertRexNodeFromResolvedExpr(argument);
+ })
+ .collect(Collectors.toList());
+
+ RexWinAggCall rexWinAggCall =
+ new RexWinAggCall(
+ sqlAggFunction,
+ columnReturnType,
+ argsColumns,
+ ordinal++,
+ aggCall.getDistinct(),
+ false);
+
+ RexNode rexPreceding = null;
+ RexNode rexFollowing = null;
+
+ if (aggCall.getWindowFrame() != null
+ && (aggCall.getWindowFrame().getStartExpr().getBoundaryType()
+ ==
ResolvedWindowFrameExprEnums.BoundaryType.OFFSET_PRECEDING
+ || aggCall.getWindowFrame().getStartExpr().getBoundaryType()
+ ==
ResolvedWindowFrameExprEnums.BoundaryType.OFFSET_FOLLOWING)) {
+ constants.add(
+ RexLiteral.fromJdbcString(
+ SqlOperators.BIGINT,
+ SqlTypeName.DECIMAL,
+
obtainValue(aggCall.getWindowFrame().getStartExpr()).toString()));
+
+ rexPreceding =
+ getExpressionConverter()
+ .rexBuilder()
Review comment:
you can get RexBuilder by `getCluster().getRexBuilder()`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 475685)
Time Spent: 8h 20m (was: 8h 10m)
> BeamSQL aggregation analytics functionality
> --------------------------------------------
>
> Key: BEAM-9198
> URL: https://issues.apache.org/jira/browse/BEAM-9198
> Project: Beam
> Issue Type: New Feature
> Components: dsl-sql
> Reporter: Rui Wang
> Priority: P2
> Labels: gsoc, gsoc2020, mentor
> Time Spent: 8h 20m
> Remaining Estimate: 0h
>
> Mentor email: [email protected]. Feel free to send emails for your questions.
> Project Information
> ---------------------
> BeamSQL has a long list of of aggregation/aggregation analytics
> functionalities to support.
> To begin with, you will need to support this syntax:
> {code:sql}
> analytic_function_name ( [ argument_list ] )
> OVER (
> [ PARTITION BY partition_expression_list ]
> [ ORDER BY expression [{ ASC | DESC }] [, ...] ]
> [ window_frame_clause ]
> )
> {code}
> As there is a long list of analytics functions, a good start point is support
> rank() first.
> This will requires touch core components of BeamSQL:
> 1. SQL parser to support the syntax above.
> 2. SQL core to implement physical relational operator.
> 3. Distributed algorithms to implement a list of functions in a distributed
> manner.
> 4. Enable in ZetaSQL dialect.
> To understand what SQL analytics functionality is, you could check this great
> explanation doc:
> https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts.
> To know about Beam's programming model, check:
> https://beam.apache.org/documentation/programming-guide/#overview
--
This message was sent by Atlassian Jira
(v8.3.4#803005)