Skip to content

Commit

Permalink
Merge baa74f4 into c9144c5
Browse files Browse the repository at this point in the history
  • Loading branch information
sunbufu committed Jul 29, 2019
2 parents c9144c5 + baa74f4 commit 9e79d01
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 38 deletions.
Expand Up @@ -55,17 +55,18 @@ public class AggregationSelectItem implements SelectItem {
public final String getExpression() {
return SQLUtil.getExactlyValue(type.name() + innerExpression);
}

@Override
public final Optional<String> getAlias() {
return Optional.fromNullable(alias);
}

/**
* Get column label.
*
* @return column label
*/
@Override
public String getColumnLabel() {
return getAlias().or(getExpression());
}
Expand Down
@@ -0,0 +1,58 @@
/*
* 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.core.optimize.sharding.segment.select.item;

import com.google.common.base.Optional;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Common select item.
*
* @author zhangliang
* @author sunbufu
*/
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class ColumnSelectItem implements SelectItem {

private final String owner;

private final String name;

private final String alias;

@Override
public final String getExpression() {
return null == owner ? name : owner + "." + name;
}

@Override
public final String getColumnLabel() {
return getAlias().or(name);
}

@Override
public final Optional<String> getAlias() {
return Optional.fromNullable(alias);
}
}
Expand Up @@ -24,22 +24,28 @@
import lombok.ToString;

/**
* Common select item.
* Derived common select item.
*
* @author zhangliang
* @author sunbufu
*/
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public class CommonSelectItem implements SelectItem {
public final class DerivedSelectItem implements SelectItem {

private final String expression;

private final String alias;

@Override
public final Optional<String> getAlias() {
return Optional.fromNullable(alias);
}

@Override
public final String getColumnLabel() {
return getAlias().or(getExpression());
}
}
Expand Up @@ -17,19 +17,34 @@

package org.apache.shardingsphere.core.optimize.sharding.segment.select.item;

import com.google.common.base.Optional;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Derived common select item.
* Expression select item.
*
* @author zhangliang
* @author sunbufu
*/
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public final class DerivedCommonSelectItem extends CommonSelectItem {

public DerivedCommonSelectItem(final String expression, final String alias) {
super(expression, alias);
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class ExpressionSelectItem implements SelectItem {

private final String expression;

private final String alias;

@Override
public Optional<String> getAlias() {
return Optional.fromNullable(alias);
}

@Override
public String getColumnLabel() {
return getAlias().or(getExpression());
}
}
Expand Up @@ -39,4 +39,11 @@ public interface SelectItem {
* @return alias
*/
Optional<String> getAlias();

/**
* Get columnLabel.
*
* @return columnLabel
*/
String getColumnLabel();
}
Expand Up @@ -117,7 +117,7 @@ public List<String> getColumnLabels() {
for (SelectItem each : items) {
// TODO read * from metadata
if (!(each instanceof ShorthandSelectItem)) {
result.add(each.getAlias().or(each.getExpression()));
result.add(each.getColumnLabel());
}
}
return result;
Expand Down
Expand Up @@ -46,7 +46,12 @@ public String getExpression() {
public Optional<String> getAlias() {
return Optional.absent();
}


@Override
public String getColumnLabel() {
return getAlias().or("*");
}

/**
* Get owner.
*
Expand Down
Expand Up @@ -20,8 +20,9 @@
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.AggregationDistinctSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.AggregationSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.CommonSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.ColumnSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.DerivedColumn;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.ExpressionSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.SelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.ShorthandSelectItem;
import org.apache.shardingsphere.core.parse.core.constant.AggregationType;
Expand Down Expand Up @@ -76,12 +77,13 @@ private ShorthandSelectItem createSelectItem(final ShorthandSelectItemSegment se
return new ShorthandSelectItem(owner.isPresent() ? owner.get().getTableName() : null);
}

private CommonSelectItem createSelectItem(final ColumnSelectItemSegment selectItemSegment) {
return new CommonSelectItem(selectItemSegment.getQualifiedName(), selectItemSegment.getAlias().orNull());
private ColumnSelectItem createSelectItem(final ColumnSelectItemSegment selectItemSegment) {
String owner = selectItemSegment.getOwner().isPresent() ? selectItemSegment.getOwner().get().getTableName() : null;
return new ColumnSelectItem(owner, selectItemSegment.getName(), selectItemSegment.getAlias().orNull());
}

private CommonSelectItem createSelectItem(final ExpressionSelectItemSegment selectItemSegment) {
return new CommonSelectItem(selectItemSegment.getText(), selectItemSegment.getAlias().orNull());
private ExpressionSelectItem createSelectItem(final ExpressionSelectItemSegment selectItemSegment) {
return new ExpressionSelectItem(selectItemSegment.getText(), selectItemSegment.getAlias().orNull());
}

private AggregationDistinctSelectItem createSelectItem(final String sql, final AggregationDistinctSelectItemSegment selectItemSegment) {
Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.core.optimize.api.segment.Tables;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.groupby.GroupBy;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.DerivedColumn;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.DerivedCommonSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.DerivedSelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.SelectItem;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.SelectItems;
import org.apache.shardingsphere.core.optimize.sharding.segment.select.item.ShorthandSelectItem;
Expand Down Expand Up @@ -97,7 +97,7 @@ private Collection<SelectItem> getDerivedOrderColumns(final Tables tables, final
int derivedColumnOffset = 0;
for (OrderByItem each : orderItems) {
if (!containsItem(tables, selectItems, each.getSegment())) {
result.add(new DerivedCommonSelectItem(((TextOrderByItemSegment) each.getSegment()).getText(), derivedColumn.getDerivedColumnAlias(derivedColumnOffset++)));
result.add(new DerivedSelectItem(((TextOrderByItemSegment) each.getSegment()).getText(), derivedColumn.getDerivedColumnAlias(derivedColumnOffset++)));
}
}
return result;
Expand Down

0 comments on commit 9e79d01

Please sign in to comment.