Skip to content

Commit

Permalink
support for WindowSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
kanha-gupta committed Jun 6, 2023
1 parent fa60dcf commit 81a0d39
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.shardingsphere.sqlfederation.optimizer.converter.segment.window;

import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlWindow;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.ExpressionConverter;

import java.util.Collections;
import java.util.Optional;

/**
* Window converter.
*/
public final class WindowConverter implements SQLSegmentConverter<WindowSegment, SqlNodeList> {

@Override
public Optional<SqlNodeList> convert(final WindowSegment segment) {
SqlIdentifier sqlIdentifier = new SqlIdentifier(segment.getIdentifierValue().getValue(), SqlParserPos.ZERO);
SqlNodeList partitionList = new SqlNodeList(Collections.singletonList(new ExpressionConverter().convert(segment.getSegments().iterator().next()).get()), SqlParserPos.ZERO);
SqlNodeList orderList = new SqlNodeList(SqlParserPos.ZERO);
SqlWindow sqlWindow = new SqlWindow(SqlParserPos.ZERO, sqlIdentifier, null, partitionList, orderList, SqlLiteral.createBoolean(false, SqlParserPos.ZERO), null, null, null);
SqlNodeList result = new SqlNodeList(Collections.singletonList(sqlWindow), SqlParserPos.ZERO);
return Optional.of(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.combine.CombineSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.window.WindowConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.from.TableConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.groupby.GroupByConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.groupby.HavingConverter;
Expand Down Expand Up @@ -67,7 +69,9 @@ private SqlSelect convertSelect(final SelectStatement selectStatement) {
SqlNode where = selectStatement.getWhere().flatMap(optional -> new WhereConverter().convert(optional)).orElse(null);
SqlNodeList groupBy = selectStatement.getGroupBy().flatMap(optional -> new GroupByConverter().convert(optional)).orElse(null);
SqlNode having = selectStatement.getHaving().flatMap(optional -> new HavingConverter().convert(optional)).orElse(null);
return new SqlSelect(SqlParserPos.ZERO, distinct, projection, from, where, groupBy, having, SqlNodeList.EMPTY, null, null, null, SqlNodeList.EMPTY);
Optional<WindowSegment> windowSegment = SelectStatementHandler.getWindowSegment(selectStatement);
SqlNodeList window = windowSegment.map(new WindowConverter()::convert).map(Optional::get).orElse(SqlNodeList.EMPTY);
return new SqlSelect(SqlParserPos.ZERO, distinct, projection, from, where, groupBy, having, window, null, null, null, SqlNodeList.EMPTY);
}

private SqlNode convertCombine(final SqlNode sqlNode, final SelectStatement selectStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WhereClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowItemContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowSpecificationContext;
import org.apache.shardingsphere.sql.parser.sql.common.enums.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.enums.CombineType;
import org.apache.shardingsphere.sql.parser.sql.common.enums.JoinType;
Expand Down Expand Up @@ -768,9 +770,25 @@ public ASTNode visitTableStatement(final TableStatementContext ctx) {

@Override
public ASTNode visitWindowClause(final WindowClauseContext ctx) {
if (null != ctx.windowItem()) {
return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getWindowItem(ctx.windowItem(0)),
getWindowSpecification(ctx.windowItem(0).windowSpecification()));
}
return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
}

private IdentifierValue getWindowItem(final WindowItemContext ctx) {
return new IdentifierValue(ctx.identifier().getText());
}

private Collection<ExpressionSegment> getWindowSpecification(final WindowSpecificationContext ctx) {
Collection<ExpressionSegment> segments = new LinkedList<>();
for (ExprContext each : ctx.expr()) {
segments.add((ExpressionSegment) visit(each));
}
return segments;
}

@Override
public ASTNode visitHavingClause(final HavingClauseContext ctx) {
ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WhereClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WhereOrCurrentClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowDefinitionContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowSpecificationContext;
import org.apache.shardingsphere.sql.parser.sql.common.enums.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.enums.CombineType;
import org.apache.shardingsphere.sql.parser.sql.common.enums.JoinType;
Expand Down Expand Up @@ -1016,9 +1018,22 @@ public ASTNode visitHavingClause(final HavingClauseContext ctx) {

@Override
public ASTNode visitWindowClause(final WindowClauseContext ctx) {
if (null != ctx.windowDefinitionList()) {
return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getWindowItem(ctx.windowDefinitionList().windowDefinition()),
getWindowSpecification(ctx.windowDefinitionList().windowDefinition().windowSpecification()));
}
return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
}

private IdentifierValue getWindowItem(final WindowDefinitionContext ctx) {
return new IdentifierValue(ctx.colId().identifier().getText());
}

private Collection<ExpressionSegment> getWindowSpecification(final WindowSpecificationContext ctx) {
Collection<ExpressionSegment> result = createInsertValuesSegments(ctx.partitionClause().exprList());
return result;
}

@Override
public ASTNode visitGroupClause(final GroupClauseContext ctx) {
Collection<OrderByItemSegment> items = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WhereClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WhereOrCurrentClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WindowClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WindowDefinitionContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WindowSpecificationContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParserBaseVisitor;
import org.apache.shardingsphere.sql.parser.sql.common.enums.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.enums.CombineType;
Expand Down Expand Up @@ -980,9 +982,22 @@ public ASTNode visitHavingClause(final HavingClauseContext ctx) {

@Override
public ASTNode visitWindowClause(final WindowClauseContext ctx) {
if (null != ctx.windowDefinitionList()) {
return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getWindowItem(ctx.windowDefinitionList().windowDefinition()),
getWindowSpecification(ctx.windowDefinitionList().windowDefinition().windowSpecification()));
}
return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
}

private IdentifierValue getWindowItem(final WindowDefinitionContext ctx) {
return new IdentifierValue(ctx.colId().identifier().getText());
}

private Collection<ExpressionSegment> getWindowSpecification(final WindowSpecificationContext ctx) {
Collection<ExpressionSegment> result = createInsertValuesSegments(ctx.partitionClause().exprList());
return result;
}

@Override
public ASTNode visitGroupClause(final GroupClauseContext ctx) {
Collection<OrderByItemSegment> items = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;

import java.util.Collection;

/**
* Window segment.
Expand All @@ -31,4 +35,15 @@ public final class WindowSegment implements SQLSegment {
private final int startIndex;

private final int stopIndex;

private IdentifierValue identifierValue;

private Collection<ExpressionSegment> segments;

public WindowSegment(final int startIndex, final int stopIndex, final IdentifierValue identifierValue, final Collection<ExpressionSegment> segments) {
this.startIndex = startIndex;
this.stopIndex = stopIndex;
this.identifierValue = identifierValue;
this.segments = segments;
}
}
22 changes: 22 additions & 0 deletions test/it/optimizer/src/test/resources/converter/select-group-by.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<sql-node-converter-test-cases>
<test-cases sql-case-id="select_group_by_with_having_and_window" expected-sql="SELECT COUNT(`order_id`) AS `orders_count`, `user_id` FROM `t_order` GROUP BY `user_id` HAVING `orders_count` > 0 WINDOW `w` AS (PARTITION BY `user_id`)" db-types="MySQL,PostgreSQL,openGauss" />
<test-cases sql-case-id="select_group_by_with_having_and_window" expected-sql="SELECT COUNT(&quot;order_id&quot;) AS &quot;orders_count&quot;, &quot;user_id&quot; FROM &quot;t_order&quot; GROUP BY &quot;user_id&quot; HAVING &quot;orders_count&quot; &gt; 0 WINDOW &quot;w&quot; AS (PARTITION BY &quot;user_id&quot;)" db-types="PostgreSQL,openGauss" />
</sql-node-converter-test-cases>

0 comments on commit 81a0d39

Please sign in to comment.