Skip to content

Commit

Permalink
Merge branch 'future-migrate' into future-migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzihaogithub committed Nov 6, 2020
2 parents 82e4449 + 7b9a66d commit 7053497
Show file tree
Hide file tree
Showing 143 changed files with 2,081 additions and 522 deletions.
2 changes: 1 addition & 1 deletion calcite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
<dependency>
<groupId>io.mycat</groupId>
<artifactId>common</artifactId>
<!-- <version>1.12-SNAPSHOT</version>-->
<version>1.12-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
Expand Down
24 changes: 23 additions & 1 deletion calcite/src/main/java/org/apache/calcite/MycatContext.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package org.apache.calcite;

import io.mycat.Authenticator;
import io.mycat.MetaClusterCurrent;
import io.mycat.MycatDataContext;
import io.mycat.MycatUser;

public class MycatContext {
public Object[] values;
public static final ThreadLocal<MycatDataContext> CONTEXT = ThreadLocal.withInitial(() -> null);
public static Object getVariable(String name){
public Object getVariable(String name){
return CONTEXT.get().getVariable(name);
}
public String getDatabase(){
return CONTEXT.get().getDefaultSchema();
}
public Long getLastInsertId(){
return CONTEXT.get().getLastInsertId();
}
public Long getConnectionId(){
return CONTEXT.get().getSessionId();
}
public String getCurrentUser(){
MycatUser user = CONTEXT.get().getUser();
Authenticator authenticator = MetaClusterCurrent.wrapper(Authenticator.class);
return user.getUserName()+"@"+authenticator.getUserInfo(user.getUserName()).getIp();
}

public String getUser(){
MycatUser user = CONTEXT.get().getUser();
return user.getUserName()+"@"+user.getHost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import org.apache.calcite.MycatContext;
import org.apache.calcite.linq4j.tree.*;
import org.apache.calcite.mycat.MycatBuiltInMethod;
import org.apache.calcite.mycat.MycatBuiltInMethodImpl;
import org.apache.calcite.mycat.MycatSqlDefinedFunction;
import org.apache.calcite.mycat.*;
import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.avatica.util.ByteString;
import org.apache.calcite.avatica.util.DateTimeUtils;
Expand Down Expand Up @@ -742,6 +740,24 @@ private static RexCallImplementor wrapAsRexCallImplementor(
}

public RexCallImplementor get(final SqlOperator operator) {
if (operator == MycatSessionValueFunction.INSTANCE){
return RexImpTable.MycatSessionValueImplementor.INSTANCE;
}
if (operator == MycatDatabaseFunction.INSTANCE){
return RexImpTable.MycatDatabaseImplementor.INSTANCE;
}
if (operator == MycatLastInsertIdFunction.INSTANCE){
return MycatLastInsertIdImplementor.INSTANCE;
}
if (operator == MycatConnectionIdFunction .INSTANCE){
return MycatConnectionIdImplementor.INSTANCE;
}
if (operator == MycatCurrentUserFunction .INSTANCE){
return MycatCurrrentUserImplementor.INSTANCE;
}
if (operator == MycatUserFunction .INSTANCE){
return MycatUserImplementor.INSTANCE;
}
if (operator instanceof MycatSqlDefinedFunction ){
CallImplementor implementor = ((MycatSqlDefinedFunction) operator);
return wrapAsRexCallImplementor(implementor);
Expand All @@ -759,9 +775,6 @@ public RexCallImplementor get(final SqlOperator operator) {
} else if (operator instanceof SqlTypeConstructorFunction) {
return map.get(SqlStdOperatorTable.ROW);
}
if (operator.getName().equalsIgnoreCase("MYCATSESSIONVALUE")){
return RexImpTable.MycatSessionValueImplementor.INSTANCE;
}
return map.get(operator);
}

Expand Down Expand Up @@ -2431,7 +2444,93 @@ Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expre
,argValueList.get(0));
}
}
public static class MycatDatabaseImplementor extends AbstractRexCallImplementor{
public static final MycatDatabaseImplementor INSTANCE = new MycatDatabaseImplementor();
MycatDatabaseImplementor() {
super(NullPolicy.STRICT, false);
}

@Override
String getVariableName() {
return "mycatDatabase";
}

@Override
Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) {
return Expressions.call(Expressions.variable(org.apache.calcite.MycatContext.class,"context"),"getDatabase"
);
}
}

public static class MycatLastInsertIdImplementor extends AbstractRexCallImplementor{
public static final MycatLastInsertIdImplementor INSTANCE = new MycatLastInsertIdImplementor();
MycatLastInsertIdImplementor() {
super(NullPolicy.STRICT, false);
}

@Override
String getVariableName() {
return "mycatLastInsertId";
}

@Override
Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) {
return Expressions.call(Expressions.variable(org.apache.calcite.MycatContext.class,"context"),"getLastInsertId"
);
}
}

public static class MycatCurrrentUserImplementor extends AbstractRexCallImplementor{
public static final MycatCurrrentUserImplementor INSTANCE = new MycatCurrrentUserImplementor();
MycatCurrrentUserImplementor() {
super(NullPolicy.STRICT, false);
}

@Override
String getVariableName() {
return "mycatCurrentUser";
}

@Override
Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) {
return Expressions.call(Expressions.variable(org.apache.calcite.MycatContext.class,"context"),"getCurrentUser"
);
}
}
public static class MycatUserImplementor extends AbstractRexCallImplementor{
public static final MycatCurrrentUserImplementor INSTANCE = new MycatCurrrentUserImplementor();
MycatUserImplementor() {
super(NullPolicy.STRICT, false);
}

@Override
String getVariableName() {
return "mycatCurrentUser";
}

@Override
Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) {
return Expressions.call(Expressions.variable(org.apache.calcite.MycatContext.class,"context"),"getUser"
);
}
}
public static class MycatConnectionIdImplementor extends AbstractRexCallImplementor{
public static final MycatConnectionIdImplementor INSTANCE = new MycatConnectionIdImplementor();
MycatConnectionIdImplementor() {
super(NullPolicy.STRICT, false);
}

@Override
String getVariableName() {
return "mycatConnectionId";
}

@Override
Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) {
return Expressions.call(Expressions.variable(org.apache.calcite.MycatContext.class,"context"),"getConnectionId"
);
}
}
/** Implementor for the SQL {@code CAST} operator. */
private static class CastImplementor extends AbstractRexCallImplementor {
CastImplementor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.calcite.linq4j.tree.Primitive;
import org.apache.calcite.linq4j.tree.Statement;
import org.apache.calcite.mycat.MycatBuiltInMethod;
import org.apache.calcite.mycat.MycatSessionValueFunction;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexCall;
Expand Down Expand Up @@ -2554,11 +2555,15 @@ private ConstantExpression getTypedNullLiteral(RexLiteral literal) {
return RexUtil.expandSearch(builder, program, call).accept(this);
}

final RexImpTable.RexCallImplementor implementor =
RexImpTable.RexCallImplementor implementor =
RexImpTable.INSTANCE.get(operator);

if (implementor == null) {
throw new RuntimeException("cannot translate call " + call);
if (MycatSessionValueFunction.INSTANCE.getName().equalsIgnoreCase(operator.getName())){
implementor = RexImpTable.MycatSessionValueImplementor.INSTANCE;
}else {
throw new RuntimeException("cannot translate call " + call);
}
}
final List<RexNode> operandList = call.getOperands();
final List<Type> storageTypes = EnumUtils.internalTypes(operandList);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.apache.calcite.mycat;

import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;

public class MycatConnectionIdFunction extends MycatSqlDefinedFunction {
public final static MycatConnectionIdFunction INSTANCE = new MycatConnectionIdFunction();

public MycatConnectionIdFunction() {
super("CONNECTION_ID",
ReturnTypes.BIGINT_NULLABLE,
InferTypes.RETURN_TYPE, OperandTypes.NILADIC, null, SqlFunctionCategory.SYSTEM);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.apache.calcite.mycat;

import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;

public class MycatCurrentUserFunction extends MycatSqlDefinedFunction {
public final static MycatCurrentUserFunction INSTANCE = new MycatCurrentUserFunction();

public MycatCurrentUserFunction() {
super("CURRENT_USER;",
ReturnTypes.VARCHAR_2000,
InferTypes.RETURN_TYPE, OperandTypes.NILADIC, null, SqlFunctionCategory.SYSTEM);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.apache.calcite.mycat;

import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;

public class MycatDatabaseFunction extends MycatSqlDefinedFunction {
public final static MycatDatabaseFunction INSTANCE = new MycatDatabaseFunction();

public MycatDatabaseFunction() {
super("DATABASE",
ReturnTypes.explicit(SqlTypeName.VARCHAR),
InferTypes.RETURN_TYPE, OperandTypes.NILADIC, null, SqlFunctionCategory.SYSTEM);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.apache.calcite.mycat;

import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;

public class MycatLastInsertIdFunction extends MycatSqlDefinedFunction {
public final static MycatLastInsertIdFunction INSTANCE = new MycatLastInsertIdFunction();

public MycatLastInsertIdFunction() {
super("LAST_INSERT_ID",
ReturnTypes.BIGINT_NULLABLE,
InferTypes.RETURN_TYPE, OperandTypes.NILADIC, null, SqlFunctionCategory.SYSTEM);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.mycat.calcite.sqlfunction;
package org.apache.calcite.mycat;

import org.apache.calcite.mycat.MycatSqlDefinedFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.apache.calcite.mycat;

import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;

public class MycatUserFunction extends MycatSqlDefinedFunction {
public final static MycatUserFunction INSTANCE = new MycatUserFunction();

public MycatUserFunction() {
super("USER",
ReturnTypes.VARCHAR_2000,
InferTypes.RETURN_TYPE, OperandTypes.NILADIC, null, SqlFunctionCategory.SYSTEM);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.calcite.mycat;

import org.apache.calcite.schema.impl.ScalarFunctionImpl;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;

public class MycatVersionFunction extends MycatSqlDefinedFunction {
public final static MycatVersionFunction INSTANCE = new MycatVersionFunction();

public MycatVersionFunction() {
super("VERSION",
ReturnTypes.explicit(SqlTypeName.VARCHAR),
InferTypes.RETURN_TYPE, OperandTypes.NILADIC,
ScalarFunctionImpl.create(MycatVersionFunction.class,"version"),
SqlFunctionCategory.SYSTEM);
}

public static String version(){
return "8.19";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mycat.proxy.session;
package io.mycat;

import io.mycat.config.UserConfig;
import lombok.AllArgsConstructor;
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/io/mycat/ConfigOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface ConfigOps extends AutoCloseable{

Object currentConfig();

void commit(Object ops);
void commit(Object ops)throws Exception ;

void close();
}
4 changes: 3 additions & 1 deletion common/src/main/java/io/mycat/MetaClusterCurrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
public class MetaClusterCurrent {
public static final AtomicReference<Map<Class,Object>> context = new AtomicReference<>(new HashMap<>());
public static <T> T wrapper(Class<T> tClass){
return (T)Objects.requireNonNull(context.get().get(tClass));
Map<Class, Object> classObjectMap = context.get();
Object o = classObjectMap.get(tClass);
return (T)Objects.requireNonNull(o);
}

public static void register(Map<Class,Object> newContext) {
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/io/mycat/MetadataStorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public abstract class MetadataStorageManager {

abstract void start();
abstract void start() throws Exception ;

public abstract void reportReplica(String name, Set<String> dsNames);

Expand Down
5 changes: 2 additions & 3 deletions common/src/main/java/io/mycat/MycatDataContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

public interface MycatDataContext extends Wrapper, SessionOpt {

long getSessionId();

TransactionType transactionType();
Expand All @@ -20,9 +21,7 @@ public interface MycatDataContext extends Wrapper, SessionOpt {

void switchTransaction(TransactionType transactionSessionType);

default <T> T getVariable(String target) {
return (T) MySQLVariablesUtil.getVariable(this, target);
}
<T> T getVariable(String target);

<T> T getVariable(MycatDataContextEnum name);

Expand Down
Loading

0 comments on commit 7053497

Please sign in to comment.