github-advanced-security[bot] commented on code in PR #16288: URL: https://github.com/apache/druid/pull/16288#discussion_r1569686799
########## extensions-contrib/druid-deltalake-extensions/src/main/java/org/apache/druid/delta/filter/DeltaBinaryOperatorFilter.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.delta.filter; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.ImmutableList; +import io.delta.kernel.expressions.Column; +import io.delta.kernel.expressions.Literal; +import io.delta.kernel.expressions.Predicate; +import io.delta.kernel.types.DataType; +import io.delta.kernel.types.DateType; +import io.delta.kernel.types.DoubleType; +import io.delta.kernel.types.IntegerType; +import io.delta.kernel.types.LongType; +import io.delta.kernel.types.ShortType; +import io.delta.kernel.types.StringType; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; +import org.apache.druid.error.InvalidInput; + +import java.sql.Date; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + +public abstract class DeltaBinaryOperatorFilter implements DeltaFilter +{ + @JsonProperty + private final String operator; + @JsonProperty + private final String column; + @JsonProperty + private final String value; + + private DeltaBinaryOperatorFilter( + final String operator, + final String column, + final String value + ) + { + if (operator == null) { + throw InvalidInput.exception("operator is required for Delta filters."); + } + if (column == null) { + throw InvalidInput.exception("column is required for Delta filters."); + } + if (value == null) { + throw InvalidInput.exception("value is required for Delta filters."); + } + this.operator = operator; + this.column = column; + this.value = value; + } + + @Override + public Predicate getFilterPredicate(StructType snapshotSchema) + { + return new Predicate( + operator, + ImmutableList.of( + new Column(column), + dataTypeToLiteral(snapshotSchema, column, value) + ) + ); + } + + private static Literal dataTypeToLiteral( + final StructType snapshotSchema, + final String column, + final String value + ) + { + if (!snapshotSchema.fieldNames().contains(column)) { + throw InvalidInput.exception( + "column[%s] doesn't exist in schema[%s]", column, snapshotSchema + ); + } + + final StructField structField = snapshotSchema.get(column); + final DataType dataType = structField.getDataType(); + try { + if (dataType instanceof StringType) { Review Comment: ## Chain of 'instanceof' tests This if block performs a chain of 6 type tests - consider alternatives, e.g. polymorphism or the visitor pattern. [Show more details](https://github.com/apache/druid/security/code-scanning/7287) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
