Skip to content

Commit

Permalink
[dingo-common, calcite, exec] Use dingo-expr lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
lasyard committed Nov 29, 2023
1 parent 1a1e81c commit 1b618d3
Show file tree
Hide file tree
Showing 445 changed files with 1,466 additions and 22,096 deletions.
1 change: 1 addition & 0 deletions config/dingo.dic
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ sql
sqlline
sqls
substr
ucase
unixtime
1 change: 0 additions & 1 deletion dingo-calcite/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dependencies {
implementation project(':dingo-partition-api')
implementation project(':dingo-codec-api')
compileOnly group: 'org.immutables', name: 'value', version: 'immutables'.v()
implementation project(':dingo-expr:dingo-expr-parser')
implementation project(':dingo-store-api')
implementation project(':dingo-verify')
implementation group: 'org.apache.commons', name: 'commons-lang3', version: 'commons-lang3'.v()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import io.dingodb.calcite.rel.DingoFunctionScan;
import io.dingodb.calcite.rel.DingoVector;
import io.dingodb.calcite.traits.DingoConvention;
import io.dingodb.calcite.traits.DingoRelStreaming;
import io.dingodb.exec.fun.string.SubstringFun;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.plan.RelTraitSet;
Expand Down Expand Up @@ -79,7 +77,7 @@ class DingoSqlToRelConverter extends SqlToRelConverter {
if (node.getKind() == SqlKind.OTHER_FUNCTION) {
SqlOperator operator = ((SqlCall) node).getOperator();
// Override `substring` function to avoid complicated conversion in Calcite.
if (operator.isName(SubstringFun.NAME, false)) {
if (operator.isName("substring", false)) {
// The same of `this.rexBuilder`.
RexBuilder rb = bb.getRexBuilder();
List<RexNode> operands = ((SqlCall) node).getOperandList().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import java.util.Map;
import java.util.stream.Collectors;

@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
public class DingoTable extends AbstractTable implements TranslatableTable {
@Getter
@EqualsAndHashCode.Include
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,31 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.dingodb.exec.fun.AutoIncrementFun;
import io.dingodb.exec.fun.DingoFunFactory;
import io.dingodb.exec.fun.mysql.GlobalVariableFun;
import io.dingodb.exec.fun.mysql.VersionFun;
import io.dingodb.exec.fun.number.FormatFun;
import io.dingodb.exec.fun.number.PowFun;
import io.dingodb.exec.fun.string.ConcatFun;
import io.dingodb.exec.fun.string.LTrimFun;
import io.dingodb.exec.fun.string.LeftFun;
import io.dingodb.exec.fun.string.LocateFun;
import io.dingodb.exec.fun.string.MidFun;
import io.dingodb.exec.fun.string.RTrimFun;
import io.dingodb.exec.fun.string.RepeatFun;
import io.dingodb.exec.fun.string.ReverseFun;
import io.dingodb.exec.fun.string.RightFun;
import io.dingodb.exec.fun.time.DateDiffFun;
import io.dingodb.exec.fun.special.ThrowFun;
import io.dingodb.exec.fun.vector.VectorCosineDistanceFun;
import io.dingodb.exec.fun.vector.VectorDistanceFun;
import io.dingodb.exec.fun.vector.VectorIPDistanceFun;
import io.dingodb.exec.fun.vector.VectorImageFun;
import io.dingodb.exec.fun.vector.VectorL2DistanceFun;
import io.dingodb.exec.fun.vector.VectorTextFun;
import io.dingodb.expr.runtime.op.mathematical.PowFunFactory;
import io.dingodb.expr.runtime.op.string.ConcatFunFactory;
import io.dingodb.expr.runtime.op.string.LTrim1FunFactory;
import io.dingodb.expr.runtime.op.string.LeftFunFactory;
import io.dingodb.expr.runtime.op.string.Locate2FunFactory;
import io.dingodb.expr.runtime.op.string.Mid2FunFactory;
import io.dingodb.expr.runtime.op.string.NumberFormatFunFactory;
import io.dingodb.expr.runtime.op.string.RTrim1FunFactory;
import io.dingodb.expr.runtime.op.string.RepeatFunFactory;
import io.dingodb.expr.runtime.op.string.ReverseFunFactory;
import io.dingodb.expr.runtime.op.string.RightFunFactory;
import io.dingodb.expr.runtime.op.time.DateDiffFunFactory;
import io.dingodb.expr.runtime.op.time.DateFormat1FunFactory;
import io.dingodb.expr.runtime.op.time.FromUnixTimeFunFactory;
import io.dingodb.expr.runtime.op.time.TimeFormat1FunFactory;
import io.dingodb.expr.runtime.op.time.TimestampFormat1FunFactory;
import io.dingodb.expr.runtime.op.time.UnixTimestamp1FunFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
Expand Down Expand Up @@ -95,51 +99,51 @@ private void init() {

// number
registerFunction(
FormatFun.NAME,
NumberFormatFunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
DingoInferTypes.DECIMAL,
OperandTypes.NUMERIC_NUMERIC,
SqlFunctionCategory.STRING
);
registerFunction(
PowFun.NAME,
DingoReturnTypes.DECIMAL_NULLABLE,
PowFunFactory.NAME,
ReturnTypes.DOUBLE,
DingoInferTypes.DOUBLE,
OperandTypes.NUMERIC_NUMERIC,
SqlFunctionCategory.STRING
);

// string
registerFunction(
ConcatFun.NAME,
ConcatFunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
InferTypes.VARCHAR_1024,
OperandTypes.STRING_STRING,
SqlFunctionCategory.STRING
);
registerFunction(
LeftFun.NAME,
LeftFunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
DingoInferTypes.VARCHAR1024_INTEGER,
family(SqlTypeFamily.STRING, SqlTypeFamily.NUMERIC),
SqlFunctionCategory.STRING
);
registerFunction(
LocateFun.NAME,
Locate2FunFactory.NAME,
ReturnTypes.INTEGER_NULLABLE,
InferTypes.VARCHAR_1024,
OperandTypes.STRING_STRING,
SqlFunctionCategory.NUMERIC
);
registerFunction(
LTrimFun.NAME,
LTrim1FunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
InferTypes.VARCHAR_1024,
OperandTypes.STRING,
SqlFunctionCategory.STRING
);
registerFunction(
MidFun.NAME,
Mid2FunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
DingoInferTypes.VARCHAR1024_INTEGER_INTEGER,
OperandTypes.or(
Expand All @@ -149,28 +153,28 @@ private void init() {
SqlFunctionCategory.STRING
);
registerFunction(
RepeatFun.NAME,
RepeatFunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
DingoInferTypes.VARCHAR1024_INTEGER,
family(SqlTypeFamily.STRING, SqlTypeFamily.NUMERIC),
SqlFunctionCategory.STRING
);
registerFunction(
ReverseFun.NAME,
ReverseFunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
InferTypes.VARCHAR_1024,
OperandTypes.STRING,
SqlFunctionCategory.STRING
);
registerFunction(
RightFun.NAME,
RightFunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
DingoInferTypes.VARCHAR1024_INTEGER,
family(SqlTypeFamily.STRING, SqlTypeFamily.NUMERIC),
SqlFunctionCategory.STRING
);
registerFunction(
RTrimFun.NAME,
RTrim1FunFactory.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
InferTypes.VARCHAR_1024,
OperandTypes.STRING,
Expand All @@ -179,14 +183,14 @@ private void init() {

// time
registerFunction(
DingoFunFactory.FROM_UNIXTIME,
FromUnixTimeFunFactory.NAME,
ReturnTypes.TIMESTAMP,
InferTypes.VARCHAR_1024,
OperandTypes.NUMERIC,
SqlFunctionCategory.STRING
);
registerFunction(
DingoFunFactory.UNIX_TIMESTAMP,
UnixTimestamp1FunFactory.NAME,
ReturnTypes.BIGINT,
DingoInferTypes.TIMESTAMP,
OperandTypes.or(
Expand All @@ -197,7 +201,7 @@ private void init() {
SqlFunctionCategory.NUMERIC
);
registerFunction(
DingoFunFactory.DATE_FORMAT,
DateFormat1FunFactory.NAME,
ReturnTypes.VARCHAR_2000,
DingoInferTypes.DATE_VARCHAR1024,
// Why not `OperandTypes.family(ImmutableList.of(SqlTypeFamily.DATE, SqlTypeFamily.STRING), i -> i == 1)`?
Expand All @@ -209,7 +213,7 @@ private void init() {
SqlFunctionCategory.STRING
);
registerFunction(
DingoFunFactory.TIME_FORMAT,
TimeFormat1FunFactory.NAME,
ReturnTypes.VARCHAR_2000,
DingoInferTypes.TIME_VARCHAR1024,
OperandTypes.or(
Expand All @@ -219,7 +223,7 @@ private void init() {
SqlFunctionCategory.STRING
);
registerFunction(
DingoFunFactory.TIMESTAMP_FORMAT,
TimestampFormat1FunFactory.NAME,
ReturnTypes.VARCHAR_2000,
DingoInferTypes.TIMESTAMP_VARCHAR1024,
OperandTypes.or(
Expand All @@ -231,7 +235,7 @@ private void init() {
SqlFunctionCategory.STRING
);
registerFunction(
DateDiffFun.NAME,
DateDiffFunFactory.NAME,
ReturnTypes.BIGINT,
DingoInferTypes.DATE_DATE,
family(SqlTypeFamily.DATE, SqlTypeFamily.DATE),
Expand All @@ -240,10 +244,10 @@ private void init() {

// special
registerFunction(
DingoFunFactory.THROW,
ThrowFun.NAME,
ReturnTypes.VARCHAR_2000_NULLABLE,
InferTypes.VARCHAR_1024,
OperandTypes.STRING,
null,
OperandTypes.NILADIC,
SqlFunctionCategory.STRING
);
registerFunction(
Expand Down Expand Up @@ -295,13 +299,6 @@ private void init() {
family(SqlTypeFamily.ARRAY, SqlTypeFamily.ARRAY),
SqlFunctionCategory.NUMERIC
);
registerFunction(
VectorDistanceFun.NAME,
FLOAT,
DingoInferTypes.FLOAT,
family(SqlTypeFamily.ARRAY, SqlTypeFamily.ARRAY),
SqlFunctionCategory.NUMERIC
);
registerFunction(
VersionFun.NAME,
ReturnTypes.VARCHAR_2000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import io.dingodb.common.type.DingoType;
import io.dingodb.common.type.DingoTypeFactory;
import io.dingodb.common.util.ArrayUtils;
import io.dingodb.expr.core.TypeCode;
import io.dingodb.expr.parser.exception.ElementNotExists;
import io.dingodb.expr.parser.exception.ExprCompileException;
import io.dingodb.expr.runtime.EvalEnv;
import io.dingodb.expr.runtime.ExprConfig;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.rel.core.JoinRelType;
Expand All @@ -51,27 +48,21 @@ public void onMatch(@NonNull RelOptRuleCall call) {
List<Object[]> tuples = new LinkedList<>();
if (join.getJoinType() == JoinRelType.INNER) {
DingoType type = DefinitionMapper.mapToDingoType(join.getRowType());
EvalEnv env = CalcValueUtils.getEnv(call);
try {
for (Object[] v0 : value0.getTuples()) {
for (Object[] v1 : value1.getTuples()) {
Object[] newTuple = ArrayUtils.concat(v0, v1);
Object v = CalcValueUtils.calcValue(
join.getCondition(),
DingoTypeFactory.scalar(TypeCode.BOOL, false),
newTuple,
type,
env
);
if (v != null && (boolean) v) {
tuples.add(newTuple);
}
ExprConfig config = CalcValueUtils.getConfig(call);
for (Object[] v0 : value0.getTuples()) {
for (Object[] v1 : value1.getTuples()) {
Object[] newTuple = ArrayUtils.concat(v0, v1);
Object v = CalcValueUtils.calcValue(
join.getCondition(),
DingoTypeFactory.INSTANCE.scalar("BOOL", false),
newTuple,
type,
config
);
if (v != null && (boolean) v) {
tuples.add(newTuple);
}
}
} catch (ElementNotExists e) {
return;
} catch (ExprCompileException e) {
throw new RuntimeException(e);
}
call.transformTo(new LogicalDingoValues(
join.getCluster(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import io.dingodb.calcite.utils.CalcValueUtils;
import io.dingodb.common.type.DingoType;
import io.dingodb.common.type.DingoTypeFactory;
import io.dingodb.expr.core.TypeCode;
import io.dingodb.expr.parser.exception.ElementNotExists;
import io.dingodb.expr.parser.exception.ExprCompileException;
import io.dingodb.expr.runtime.EvalEnv;
import io.dingodb.expr.runtime.ExprConfig;
import io.dingodb.expr.runtime.exception.ElementNotExist;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.rel.logical.LogicalFilter;
Expand All @@ -51,15 +49,13 @@ private static void matchProject(
DingoType tupleType = DefinitionMapper.mapToDingoType(values.getRowType());
DingoType rowType = DefinitionMapper.mapToDingoType(project.getRowType());
List<Object[]> tuples = new LinkedList<>();
EvalEnv env = CalcValueUtils.getEnv(call);
ExprConfig config = CalcValueUtils.getConfig(call);
try {
for (Object[] tuple : values.getTuples()) {
tuples.add(CalcValueUtils.calcValues(project.getProjects(), rowType, tuple, tupleType, env));
tuples.add(CalcValueUtils.calcValues(project.getProjects(), rowType, tuple, tupleType, config));
}
} catch (ElementNotExists e) { // Means it is not a constant.
} catch (ElementNotExist e) { // Means not constants.
return;
} catch (ExprCompileException e) {
throw new RuntimeException(e);
}
call.transformTo(new LogicalDingoValues(
project.getCluster(),
Expand All @@ -77,24 +73,17 @@ private static void matchFilter(
LogicalDingoValues values = call.rel(1);
DingoType tupleType = DefinitionMapper.mapToDingoType(values.getRowType());
List<Object[]> tuples = new LinkedList<>();
EvalEnv env = CalcValueUtils.getEnv(call);
try {
for (Object[] tuple : values.getTuples()) {
Object v = CalcValueUtils.calcValue(
filter.getCondition(),
DingoTypeFactory.scalar(TypeCode.BOOL, false),
tuple,
tupleType,
env
);
if (v != null && (boolean) v) {
tuples.add(tuple);
}
for (Object[] tuple : values.getTuples()) {
Object v = CalcValueUtils.calcValue(
filter.getCondition(),
DingoTypeFactory.INSTANCE.scalar("BOOL", false),
tuple,
tupleType,
CalcValueUtils.getConfig(call)
);
if (v != null && (boolean) v) {
tuples.add(tuple);
}
} catch (ElementNotExists e) {
return;
} catch (ExprCompileException e) {
throw new RuntimeException(e);
}
call.transformTo(new LogicalDingoValues(
filter.getCluster(),
Expand Down
Loading

0 comments on commit 1b618d3

Please sign in to comment.