Skip to content

Commit

Permalink
5.4.19-20240625_17192948
Browse files Browse the repository at this point in the history
  • Loading branch information
pzxpolar committed Jun 25, 2024
1 parent db4d3a3 commit 2d74a9c
Show file tree
Hide file tree
Showing 553 changed files with 62,659 additions and 4,759 deletions.
6 changes: 6 additions & 0 deletions polardbx-calcite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ limitations under the License.
<artifactId>polardbx-rule</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.verison}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@
import org.apache.calcite.runtime.CalciteContextException;
import org.apache.calcite.runtime.PredicateImpl;
import org.apache.calcite.schema.ModifiableView;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlExplainFormat;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.MultisetSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
Expand Down Expand Up @@ -4354,6 +4356,26 @@ public boolean test(RelNode relNode) {
return traits.getCollation().isTop() && traits.getDistribution().isTop();
}
};

public static boolean isUnion(SqlNode node) {
return node instanceof SqlBasicCall && node.getKind() == SqlKind.UNION;
}

public static int getColumnCount(SqlNode node) {
if (isUnion(node)) {
final SqlNode[] children = ((SqlBasicCall) node).getOperands();
for (int i = 0; i < children.length; i++) {
final int columnCount = getColumnCount(children[i]);
if (columnCount > 0) {
return columnCount;
}
}
return -1;
} else if (node instanceof SqlSelect) {
return ((SqlSelect) node).getSelectList().size();
}
return -1;
}
}

// End RelOptUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ public RelNode onRegister(RelOptPlanner planner) {
r = copy(getTraitSet(), inputs);
}
r.recomputeDigest();
assert r.isValid(Litmus.THROW, null);
return r;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
*/
package org.apache.calcite.rel.core;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.externalize.RexExplainVisitor;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.util.Optionality;
import org.apache.calcite.util.mapping.Mapping;
import org.apache.calcite.util.mapping.Mappings;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;

Expand All @@ -38,16 +38,16 @@
public class AggregateCall {
//~ Instance fields --------------------------------------------------------

private final SqlAggFunction aggFunction;
protected final SqlAggFunction aggFunction;

private final boolean distinct;
private final boolean approximate;
protected final boolean distinct;
protected final boolean approximate;
public final RelDataType type;
public final String name;

// We considered using ImmutableIntList but we would not save much memory:
// since all values are small, ImmutableList uses cached Integer values.
private final ImmutableList<Integer> argList;
protected final ImmutableList<Integer> argList;
public final int filterArg;

//~ Constructors -----------------------------------------------------------
Expand Down Expand Up @@ -147,7 +147,9 @@ public static AggregateCall create(SqlAggFunction aggFunction,
public static AggregateCall create(SqlAggFunction aggFunction,
boolean distinct, boolean approximate, List<Integer> argList,
int filterArg, RelDataType type, String name) {
return new AggregateCall(aggFunction, distinct, approximate, argList,
final boolean distinct2 = distinct
&& (aggFunction.getDistinctOptionality() != Optionality.IGNORED);
return new AggregateCall(aggFunction, distinct2, approximate, argList,
filterArg, type, name);
}

Expand Down Expand Up @@ -251,6 +253,10 @@ public boolean hasFilter() {
return filterArg >= 0;
}

public int getFilterArg() {
return filterArg;
}

@Override public boolean equals(Object o) {
if (!(o instanceof AggregateCall)) {
return false;
Expand Down Expand Up @@ -292,19 +298,14 @@ public AggregateCall copy(List<Integer> args, int filterArg) {
filterArg, type, name);
}

public AggregateCall copy(List<Integer> args, int filterArg, boolean isDistinct) {
return new AggregateCall(aggFunction, isDistinct, approximate, args,
filterArg, type, name);
}

public AggregateCall copy(List<Integer> args, int filterArg, boolean isDistinct, String newName) {
return new AggregateCall(aggFunction, isDistinct, approximate, args,
filterArg, type, newName);
}

@Deprecated // to be removed before 2.0
public AggregateCall copy(List<Integer> args) {
return copy(args, filterArg);
public AggregateCall withDistinct(boolean distinct) {
return distinct == this.distinct ? this
: new AggregateCall(aggFunction, distinct, approximate, argList, filterArg, type, name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ public GroupConcatAggregateCall copy(List<Integer> args, int filterArg, List<Int
filterArg, type, name, orderList, separator, ascOrDescList);
}

@Override
public GroupConcatAggregateCall copy(List<Integer> args, int filterArg) {
return new GroupConcatAggregateCall(aggFunction, distinct, approximate, args,
filterArg, type, name, orderList, separator, ascOrDescList);
}

@Override
public GroupConcatAggregateCall copy(List<Integer> args, int filterArg, boolean isDistinct, String newName) {
return new GroupConcatAggregateCall(aggFunction, isDistinct, approximate, args,
filterArg, type, newName, orderList, separator, ascOrDescList);
}

@Override
public AggregateCall withDistinct(boolean distinct) {
return distinct == this.distinct ? this
: new GroupConcatAggregateCall(aggFunction, distinct, approximate, argList, filterArg, type, name,
orderList, separator, ascOrDescList);
}

@Override
public AggregateCall adaptTo(RelNode input, List<Integer> argList, int filterArg, int oldGroupKeyCount,
int newGroupKeyCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ public TableInfoNode(SqlNode table, SqlNode tableWithAlias, List<RelOptTable> re
if (table instanceof SqlIdentifier || table instanceof SqlDynamicParam) {
// Table
this.columnCount = refTables.get(0).getRowType().getFieldCount();
} else if (RelOptUtil.isUnion(table)) {
this.columnCount = RelOptUtil.getColumnCount(table);
} else {
// Subquery
SqlSelect subquery = (SqlSelect) table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.util.ImmutableBitSet;
Expand Down Expand Up @@ -78,6 +79,11 @@ public abstract class TableScan extends AbstractRelNode {
*/
protected RexNode flashback;

/**
* 记录AS OF 种类:AS_OF/AS_OF_80/AS_OF_57
*/
protected SqlOperator flashbackOperator;

/**
* This tableName identifier's partitions of mysql partition selection syntax
* <pre>
Expand Down Expand Up @@ -108,11 +114,11 @@ protected TableScan(RelOptCluster cluster, RelTraitSet traitSet,

protected TableScan(RelOptCluster cluster, RelTraitSet traitSet,
RelOptTable table, SqlNodeList hints, SqlNode indexNode) {
this(cluster, traitSet, table, hints, indexNode, null, null);
this(cluster, traitSet, table, hints, indexNode, null, null, null);
}

protected TableScan(RelOptCluster cluster, RelTraitSet traitSet, RelOptTable table, SqlNodeList hints,
SqlNode indexNode, RexNode flashback, SqlNode partitions) {
SqlNode indexNode, RexNode flashback, SqlOperator flashbackOperator, SqlNode partitions) {
super(cluster, traitSet);
this.table = table;
this.hints = hints;
Expand All @@ -121,6 +127,7 @@ protected TableScan(RelOptCluster cluster, RelTraitSet traitSet, RelOptTable tab
cluster.getPlanner().registerSchema(table.getRelOptSchema());
}
this.flashback = flashback;
this.flashbackOperator = flashbackOperator;
this.partitions = partitions;
}
/**
Expand Down Expand Up @@ -265,6 +272,14 @@ public void setFlashback(RexNode flashback) {
this.flashback = flashback;
}

public SqlOperator getFlashbackOperator() {
return flashbackOperator;
}

public void setFlashbackOperator(SqlOperator flashbackOperator) {
this.flashbackOperator = flashbackOperator;
}

public SqlNode getPartitions() {
return partitions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ public WindowAggregateCall copy(List<Integer> args, int filterArg, List<RexLiter
filterArg, type, name, constants, offset);
}

@Override
public AggregateCall copy(List<Integer> args, int filterArg) {
return new WindowAggregateCall(aggFunction, distinct, approximate, args,
filterArg, type, name, constants, offset);
}

@Override
public AggregateCall copy(List<Integer> args, int filterArg, boolean isDistinct, String newName) {
return new WindowAggregateCall(aggFunction, isDistinct, approximate, args,
filterArg, type, newName, constants, offset);
}

@Override
public AggregateCall withDistinct(boolean distinct) {
return distinct == this.distinct ? this
: new WindowAggregateCall(aggFunction, distinct, approximate, argList, filterArg, type, name,
constants, offset);
}


public AggregateCall create(SqlAggFunction aggFunction,
boolean distinct, boolean approximate, List<Integer> argList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.calcite.schema.Table;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.parser.SqlParserPos;

import java.util.List;
Expand Down Expand Up @@ -81,8 +82,9 @@ public LogicalTableScan(RelOptCluster cluster, RelTraitSet traitSet,

public LogicalTableScan(RelOptCluster cluster, RelTraitSet traitSet,
RelOptTable table, SqlNodeList hints, SqlNode indexNode, RexNode flashback,
SqlOperator flashbackOperator,
SqlNode partitions) {
super(cluster, traitSet, table, hints, indexNode, flashback, partitions);
super(cluster, traitSet, table, hints, indexNode, flashback, flashbackOperator, partitions);
}

@Deprecated // to be removed before 2.0
Expand Down Expand Up @@ -110,12 +112,12 @@ public LogicalTableScan(RelInput input) {
*/
public static LogicalTableScan create(RelOptCluster cluster,
final RelOptTable relOptTable) {
return create(cluster, relOptTable, new SqlNodeList(SqlParserPos.ZERO), null, null, null);
return create(cluster, relOptTable, new SqlNodeList(SqlParserPos.ZERO), null, null, null, null);
}

public static LogicalTableScan create(RelOptCluster cluster,
final RelOptTable relOptTable, SqlNodeList hints, SqlNode indexNode,
RexNode flashback, SqlNode partitions) {
RexNode flashback, SqlOperator flashbackOperator, SqlNode partitions) {

final Table table = relOptTable.unwrap(Table.class);
final RelTraitSet traitSet =
Expand All @@ -127,7 +129,8 @@ public static LogicalTableScan create(RelOptCluster cluster,
}
return ImmutableList.of();
}).simplify();
return new LogicalTableScan(cluster, traitSet, relOptTable, hints, indexNode, flashback, partitions);
return new LogicalTableScan(cluster, traitSet, relOptTable, hints, indexNode, flashback, flashbackOperator,
partitions);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,8 @@ public SqlNode asFrom() {
final SqlIdentifier identifier = (SqlIdentifier) node;
final SqlIdentifier newIdentifier =
new SqlIdentifier(((SqlIdentifier) node).names, ((SqlIdentifier) node).getCollation(),
node.getParserPosition(), null, null, identifier.partitions, identifier.flashback);
node.getParserPosition(), null, null, identifier.partitions, identifier.flashback,
identifier.flashbackOperator);
final SqlIdentifier alias =
new SqlIdentifier(ImmutableList.of(neededAlias), null, POS, null, identifier.indexNode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.apache.calcite.sql.type.SqlReturnTypeInference;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql.validate.SqlValidatorScope;
import org.apache.calcite.util.Optionality;

import javax.annotation.Nonnull;
import java.util.List;

/**
Expand Down Expand Up @@ -131,6 +133,23 @@ public RelDataType getReturnType(RelDataTypeFactory typeFactory) {
public boolean allowsFilter() {
return true;
}

/** Returns whether this aggregate function allows the {@code DISTINCT}
* keyword.
*
* <p>The default implementation returns {@link Optionality#OPTIONAL},
* which is appropriate for most aggregate functions, including {@code SUM}
* and {@code COUNT}.
*
* <p>Some aggregate functions, for example {@code MIN}, produce the same
* result with or without {@code DISTINCT}, and therefore return
* {@link Optionality#IGNORED} to indicate this. For such functions,
* Calcite will probably remove {@code DISTINCT} while optimizing the query.
*/
public @Nonnull
Optionality getDistinctOptionality() {
return Optionality.OPTIONAL;
}
}

// End SqlAggFunction.java
Loading

0 comments on commit 2d74a9c

Please sign in to comment.