From 25128fcc1a75cae79afb8b2008540cb787ab9a38 Mon Sep 17 00:00:00 2001 From: Liao Lanyu <1435078631@qq.com> Date: Sat, 25 Jan 2025 12:31:52 +0800 Subject: [PATCH] Remove duplicate code in column transformer --- .../relational/ColumnTransformerBuilder.java | 234 ++++++------------ 1 file changed, 80 insertions(+), 154 deletions(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/relational/ColumnTransformerBuilder.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/relational/ColumnTransformerBuilder.java index 55de53919385..ce69b23c3bcf 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/relational/ColumnTransformerBuilder.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/relational/ColumnTransformerBuilder.java @@ -237,16 +237,8 @@ protected ColumnTransformer visitArithmeticBinary( throw new UnsupportedOperationException( String.format(UNSUPPORTED_EXPRESSION, node.getOperator())); } - TSDataType tsDataType = InternalTypeManager.getTSDataType(type); - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - type, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(tsDataType); - context.cache.put(node, identity); + appendIdentityColumnTransformer( + node, type, InternalTypeManager.getTSDataType(type), context); } else { ZoneId zoneId = context.sessionInfo.getZoneId(); ColumnTransformer left = process(node.getLeft(), context); @@ -276,9 +268,7 @@ protected ColumnTransformer visitArithmeticBinary( context.cache.put(node, child); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override @@ -290,15 +280,7 @@ protected ColumnTransformer visitArithmeticUnary( case MINUS: if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - DOUBLE, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.DOUBLE); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, DOUBLE, TSDataType.DOUBLE, context); } else { ColumnTransformer childColumnTransformer = process(node.getValue(), context); context.cache.put( @@ -306,9 +288,7 @@ protected ColumnTransformer visitArithmeticUnary( ArithmeticColumnTransformerApi.getNegationTransformer(childColumnTransformer)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); default: throw new UnsupportedOperationException("Unknown sign: " + node.getSign()); } @@ -318,15 +298,7 @@ protected ColumnTransformer visitArithmeticUnary( protected ColumnTransformer visitBetweenPredicate(BetweenPredicate node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - BOOLEAN, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.BOOLEAN); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, BOOLEAN, TSDataType.BOOLEAN, context); } else { ColumnTransformer value = this.process(node.getValue(), context); ColumnTransformer min = this.process(node.getMin(), context); @@ -334,9 +306,7 @@ protected ColumnTransformer visitBetweenPredicate(BetweenPredicate node, Context context.cache.put(node, new BetweenColumnTransformer(BOOLEAN, value, min, max, false)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override @@ -345,15 +315,12 @@ protected ColumnTransformer visitCast(Cast node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { ColumnTransformer columnTransformer = context.hasSeen.get(node); - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - columnTransformer.getType(), - context.originSize + context.commonTransformerList.size()); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(getTSDataType(columnTransformer.getType())); - context.cache.put(node, identity); + appendIdentityColumnTransformer( + node, + columnTransformer.getType(), + getTSDataType(columnTransformer.getType()), + context, + columnTransformer); } else { ColumnTransformer child = this.process(node.getExpression(), context); Type type; @@ -369,9 +336,7 @@ protected ColumnTransformer visitCast(Cast node, Context context) { : new CastFunctionColumnTransformer(type, child, context.sessionInfo.getZoneId())); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override @@ -642,24 +607,19 @@ protected ColumnTransformer visitFunctionCall(FunctionCall node, Context context if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { ColumnTransformer columnTransformer = context.hasSeen.get(node); - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - columnTransformer.getType(), - context.originSize + context.commonTransformerList.size()); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(getTSDataType(columnTransformer.getType())); - context.cache.put(node, identity); + appendIdentityColumnTransformer( + node, + columnTransformer.getType(), + getTSDataType(columnTransformer.getType()), + context, + columnTransformer); } else { context.cache.put( node, getFunctionColumnTransformer(node.getName().getSuffix(), node.getArguments(), context)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } private ColumnTransformer getFunctionColumnTransformer( @@ -1047,15 +1007,7 @@ private ColumnTransformer getFunctionColumnTransformer( protected ColumnTransformer visitInPredicate(InPredicate node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - BOOLEAN, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.BOOLEAN); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, BOOLEAN, TSDataType.BOOLEAN, context); } else { ColumnTransformer childColumnTransformer = process(node.getValue(), context); TypeEnum childTypeEnum = childColumnTransformer.getType().getTypeEnum(); @@ -1076,9 +1028,7 @@ protected ColumnTransformer visitInPredicate(InPredicate node, Context context) } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } private static InMultiColumnTransformer constructInColumnTransformer( @@ -1181,38 +1131,20 @@ private static InMultiColumnTransformer constructInColumnTransformer( protected ColumnTransformer visitNotExpression(NotExpression node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - BOOLEAN, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.BOOLEAN); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, BOOLEAN, TSDataType.BOOLEAN, context); } else { ColumnTransformer childColumnTransformer = process(node.getValue(), context); context.cache.put(node, new LogicNotColumnTransformer(BOOLEAN, childColumnTransformer)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override protected ColumnTransformer visitLikePredicate(LikePredicate node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - BOOLEAN, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.BOOLEAN); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, BOOLEAN, TSDataType.BOOLEAN, context); } else { ColumnTransformer likeColumnTransformer = null; ColumnTransformer childColumnTransformer = process(node.getValue(), context); @@ -1247,56 +1179,34 @@ protected ColumnTransformer visitLikePredicate(LikePredicate node, Context conte context.cache.put(node, likeColumnTransformer); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override protected ColumnTransformer visitIsNotNullPredicate(IsNotNullPredicate node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - BOOLEAN, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.BOOLEAN); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, BOOLEAN, TSDataType.BOOLEAN, context); } else { ColumnTransformer childColumnTransformer = process(node.getValue(), context); context.cache.put(node, new IsNullColumnTransformer(BOOLEAN, childColumnTransformer, true)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override protected ColumnTransformer visitIsNullPredicate(IsNullPredicate node, Context context) { if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - BOOLEAN, context.originSize + context.commonTransformerList.size()); - ColumnTransformer columnTransformer = context.hasSeen.get(node); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(TSDataType.BOOLEAN); - context.cache.put(node, identity); + appendIdentityColumnTransformer(node, BOOLEAN, TSDataType.BOOLEAN, context); } else { ColumnTransformer childColumnTransformer = process(node.getValue(), context); context.cache.put( node, new IsNullColumnTransformer(BOOLEAN, childColumnTransformer, false)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override @@ -1366,15 +1276,12 @@ protected ColumnTransformer visitCoalesceExpression(CoalesceExpression node, Con if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { ColumnTransformer columnTransformer = context.hasSeen.get(node); - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - columnTransformer.getType(), - context.originSize + context.commonTransformerList.size()); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(getTSDataType(columnTransformer.getType())); - context.cache.put(node, identity); + appendIdentityColumnTransformer( + node, + columnTransformer.getType(), + getTSDataType(columnTransformer.getType()), + context, + columnTransformer); } else { List children = node.getChildren().stream().map(c -> process(c, context)).collect(Collectors.toList()); @@ -1392,15 +1299,12 @@ protected ColumnTransformer visitSimpleCaseExpression( if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { ColumnTransformer columnTransformer = context.hasSeen.get(node); - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - columnTransformer.getType(), - context.originSize + context.commonTransformerList.size()); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(InternalTypeManager.getTSDataType(columnTransformer.getType())); - context.cache.put(node, identity); + appendIdentityColumnTransformer( + node, + columnTransformer.getType(), + getTSDataType(columnTransformer.getType()), + context, + columnTransformer); } else { List whenList = new ArrayList<>(); List thenList = new ArrayList<>(); @@ -1423,9 +1327,7 @@ protected ColumnTransformer visitSimpleCaseExpression( thenList.get(0).getType(), whenList, thenList, elseColumnTransformer)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override @@ -1434,15 +1336,12 @@ protected ColumnTransformer visitSearchedCaseExpression( if (!context.cache.containsKey(node)) { if (context.hasSeen.containsKey(node)) { ColumnTransformer columnTransformer = context.hasSeen.get(node); - IdentityColumnTransformer identity = - new IdentityColumnTransformer( - columnTransformer.getType(), - context.originSize + context.commonTransformerList.size()); - columnTransformer.addReferenceCount(); - context.commonTransformerList.add(columnTransformer); - context.leafList.add(identity); - context.inputDataTypes.add(InternalTypeManager.getTSDataType(columnTransformer.getType())); - context.cache.put(node, identity); + appendIdentityColumnTransformer( + node, + columnTransformer.getType(), + InternalTypeManager.getTSDataType(columnTransformer.getType()), + context, + columnTransformer); } else { List whenList = new ArrayList<>(); List thenList = new ArrayList<>(); @@ -1460,9 +1359,7 @@ protected ColumnTransformer visitSearchedCaseExpression( thenList.get(0).getType(), whenList, thenList, elseColumnTransformer)); } } - ColumnTransformer res = context.cache.get(node); - res.addReferenceCount(); - return res; + return getColumnTransformerFromCacheAndAddReferenceCount(node, context); } @Override @@ -1480,6 +1377,35 @@ protected ColumnTransformer visitNullIfExpression(NullIfExpression node, Context throw new UnsupportedOperationException(String.format(UNSUPPORTED_EXPRESSION, node)); } + private void appendIdentityColumnTransformer( + Expression expression, Type identityReturnType, TSDataType inputType, Context context) { + appendIdentityColumnTransformer( + expression, identityReturnType, inputType, context, context.hasSeen.get(expression)); + } + + private void appendIdentityColumnTransformer( + Expression expression, + Type identityReturnType, + TSDataType inputType, + Context context, + ColumnTransformer columnTransformer) { + IdentityColumnTransformer identity = + new IdentityColumnTransformer( + identityReturnType, context.originSize + context.commonTransformerList.size()); + columnTransformer.addReferenceCount(); + context.commonTransformerList.add(columnTransformer); + context.leafList.add(identity); + context.inputDataTypes.add(inputType); + context.cache.put(expression, identity); + } + + private ColumnTransformer getColumnTransformerFromCacheAndAddReferenceCount( + Expression expression, Context context) { + ColumnTransformer columnTransformer = context.cache.get(expression); + columnTransformer.addReferenceCount(); + return columnTransformer; + } + public static boolean isLongLiteral(Expression expression) { return expression instanceof LongLiteral; }