This is an automated email from the ASF dual-hosted git repository.
jackie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new b2689bcd80 Add a post-validator visitor that verifies there are no
cast to bytes (#12475)
b2689bcd80 is described below
commit b2689bcd8034fe17a627bcec1d46c850c5290104
Author: Gonzalo Ortiz Jaureguizar <[email protected]>
AuthorDate: Sat Mar 9 05:47:32 2024 +0100
Add a post-validator visitor that verifies there are no cast to bytes
(#12475)
---
.../org/apache/pinot/query/QueryEnvironment.java | 2 +
.../pinot/query/validate/BytesCastVisitor.java | 72 ++++++++++++++++++++++
.../pinot/query/validate/InvalidCastException.java | 25 ++++++++
3 files changed, 99 insertions(+)
diff --git
a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
index 769d6a607f..32a75c4a3d 100644
---
a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
+++
b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
@@ -72,6 +72,7 @@ import
org.apache.pinot.query.planner.physical.DispatchableSubPlan;
import org.apache.pinot.query.planner.physical.PinotDispatchPlanner;
import org.apache.pinot.query.routing.WorkerManager;
import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.query.validate.BytesCastVisitor;
import org.apache.pinot.sql.parsers.CalciteSqlParser;
import org.apache.pinot.sql.parsers.SqlNodeAndOptions;
import org.apache.pinot.sql.parsers.parser.SqlPhysicalExplain;
@@ -294,6 +295,7 @@ public class QueryEnvironment {
throw new IllegalArgumentException(
String.format("unsupported SQL query, cannot validate out a valid
sql from:\n%s", parsed));
}
+ validated.accept(new BytesCastVisitor(plannerContext.getValidator()));
return validated;
}
diff --git
a/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/BytesCastVisitor.java
b/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/BytesCastVisitor.java
new file mode 100644
index 0000000000..afdd58f4f5
--- /dev/null
+++
b/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/BytesCastVisitor.java
@@ -0,0 +1,72 @@
+/**
+ * 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.pinot.query.validate;
+
+import com.google.common.base.Preconditions;
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCharStringLiteral;
+import org.apache.calcite.sql.SqlDataTypeSpec;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlCastFunction;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.calcite.sql.util.SqlBasicVisitor;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.util.Static;
+
+
+public class BytesCastVisitor extends SqlBasicVisitor<Void> {
+
+ private final SqlValidator _originalValidator;
+
+ public BytesCastVisitor(SqlValidator originalValidator) {
+ _originalValidator = originalValidator;
+ }
+
+ @Override
+ public Void visit(SqlCall call) {
+ if (call.getOperator() instanceof SqlCastFunction) {
+ List<SqlNode> operands = call.getOperandList();
+ SqlNode sqlNode = operands.get(1);
+ Preconditions.checkState(sqlNode instanceof SqlDataTypeSpec);
+ RelDataType toType = ((SqlDataTypeSpec)
sqlNode).deriveType(_originalValidator);
+ if (!SqlTypeUtil.isBinary(toType)) {
+ return super.visit(call);
+ }
+ SqlNode srcNode = operands.get(0);
+ RelDataType fromType =
_originalValidator.getValidatedNodeTypeIfKnown(srcNode);
+ if (fromType != null && SqlTypeUtil.isBinary(fromType)) {
+ return super.visit(call);
+ }
+ String message = "Cannot cast " + srcNode + " as " + toType + ".";
+ if (srcNode instanceof SqlCharStringLiteral) {
+ message += " Try to use binary literal instead (like X" + srcNode +
")";
+ } else if (fromType != null && SqlTypeUtil.isCharacter(fromType)) {
+ message += " Try to wrap the expression in hexToBytes (like
hexToBytes(" + srcNode + "))";
+ }
+ SqlParserPos pos = call.getParserPosition();
+ RuntimeException ex = new InvalidCastException(message);
+ throw Static.RESOURCE.validatorContext(pos.getLineNum(),
pos.getColumnNum(), pos.getEndLineNum(),
+ pos.getEndColumnNum()).ex(ex);
+ }
+ return super.visit(call);
+ }
+}
diff --git
a/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/InvalidCastException.java
b/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/InvalidCastException.java
new file mode 100644
index 0000000000..115a48def5
--- /dev/null
+++
b/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/InvalidCastException.java
@@ -0,0 +1,25 @@
+/**
+ * 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.pinot.query.validate;
+
+public class InvalidCastException extends RuntimeException {
+ public InvalidCastException(String message) {
+ super(message);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]