From 0ec7eb3ae5ffc9caf852bd63551d03660d624e04 Mon Sep 17 00:00:00 2001 From: Clint Wylie Date: Tue, 24 Sep 2024 15:21:33 -0700 Subject: [PATCH] use CastToObjectVectorProcessor for cast to string (#17148) --- .../vector/CastToStringVectorProcessor.java | 52 ------------------- .../vector/CastToTypeVectorProcessor.java | 3 -- .../druid/math/expr/VectorExprSanityTest.java | 10 ++++ 3 files changed, 10 insertions(+), 55 deletions(-) delete mode 100644 processing/src/main/java/org/apache/druid/math/expr/vector/CastToStringVectorProcessor.java diff --git a/processing/src/main/java/org/apache/druid/math/expr/vector/CastToStringVectorProcessor.java b/processing/src/main/java/org/apache/druid/math/expr/vector/CastToStringVectorProcessor.java deleted file mode 100644 index e3d5f06f8a1..00000000000 --- a/processing/src/main/java/org/apache/druid/math/expr/vector/CastToStringVectorProcessor.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.druid.math.expr.vector; - -import org.apache.druid.math.expr.Evals; -import org.apache.druid.math.expr.Expr; -import org.apache.druid.math.expr.ExpressionType; - -public final class CastToStringVectorProcessor extends CastToTypeVectorProcessor -{ - private final Object[] output; - - public CastToStringVectorProcessor(ExprVectorProcessor delegate, int maxVectorSize) - { - super(delegate); - this.output = new Object[maxVectorSize]; - } - - @Override - public ExprEvalVector evalVector(Expr.VectorInputBinding bindings) - { - ExprEvalVector result = delegate.evalVector(bindings); - final Object[] objects = result.getObjectVector(); - for (int i = 0; i < bindings.getCurrentVectorSize(); i++) { - output[i] = Evals.asString(objects[i]); - } - return new ExprEvalObjectVector(output, ExpressionType.STRING); - } - - @Override - public ExpressionType getOutputType() - { - return ExpressionType.STRING; - } -} diff --git a/processing/src/main/java/org/apache/druid/math/expr/vector/CastToTypeVectorProcessor.java b/processing/src/main/java/org/apache/druid/math/expr/vector/CastToTypeVectorProcessor.java index c6448b67ed2..62f2cf505ff 100644 --- a/processing/src/main/java/org/apache/druid/math/expr/vector/CastToTypeVectorProcessor.java +++ b/processing/src/main/java/org/apache/druid/math/expr/vector/CastToTypeVectorProcessor.java @@ -42,9 +42,6 @@ public abstract class CastToTypeVectorProcessor implements ExprVectorPr caster = castInput; } else { switch (castToType.getType()) { - case STRING: - caster = new CastToStringVectorProcessor(castInput, maxVectorSize); - break; case LONG: caster = new CastToLongVectorProcessor(castInput); break; diff --git a/processing/src/test/java/org/apache/druid/math/expr/VectorExprSanityTest.java b/processing/src/test/java/org/apache/druid/math/expr/VectorExprSanityTest.java index 5a32e8f505d..cf513b4ffba 100644 --- a/processing/src/test/java/org/apache/druid/math/expr/VectorExprSanityTest.java +++ b/processing/src/test/java/org/apache/druid/math/expr/VectorExprSanityTest.java @@ -271,6 +271,16 @@ public class VectorExprSanityTest extends InitializedNullHandlingTest testExpression("array(s1, l2)", types); } + @Test + public void testCastArraysRoundTrip() + { + testExpression("cast(cast(s1, 'ARRAY'), 'STRING')", types); + testExpression("cast(cast(d1, 'ARRAY'), 'DOUBLE')", types); + testExpression("cast(cast(d1, 'ARRAY'), 'DOUBLE')", types); + testExpression("cast(cast(l1, 'ARRAY'), 'LONG')", types); + testExpression("cast(cast(l1, 'ARRAY'), 'LONG')", types); + } + @Test public void testJsonFns() {