-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Handle NaN when pushing filter for JDBC connector #21923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.jdbc; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import io.trino.plugin.jdbc.logging.RemoteQueryModifier; | ||
| import io.trino.spi.connector.ConnectorSession; | ||
| import io.trino.spi.type.Type; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import static io.trino.spi.type.DoubleType.DOUBLE; | ||
| import static io.trino.spi.type.RealType.REAL; | ||
| import static java.lang.String.format; | ||
|
|
||
| public class NaNSpecificQueryBuilder | ||
| extends DefaultQueryBuilder | ||
| { | ||
| @Inject | ||
| public NaNSpecificQueryBuilder(RemoteQueryModifier queryModifier) | ||
| { | ||
| super(queryModifier); | ||
| } | ||
|
|
||
| @Override | ||
| protected String toPredicate(JdbcClient client, ConnectorSession session, JdbcColumnHandle column, JdbcTypeHandle jdbcType, Type type, WriteFunction writeFunction, String operator, Object value, Consumer<QueryParameter> accumulator) | ||
| { | ||
| if ((type == REAL || type == DOUBLE) && (operator.equals(">") || operator.equals(">="))) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need this check of other operators as they work as expected - in case of Postgres
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be a code comment. when you write it down say "in PostgreSQL ..." in a generic class, you'll realize that you're exploiting a (common) implementation detail.
|
||
| accumulator.accept(new QueryParameter(jdbcType, type, Optional.of(value))); | ||
| return format("((%s %s %s) AND (%s <> 'NaN'))", client.quoted(column.getColumnName()), operator, writeFunction.getBindExpression(), client.quoted(column.getColumnName())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'NaN' as a varchar?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the tests 'NaN' is not parsed as a Varchar but considered as a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it is used along with double/real columns I think it is coerced automatically and the tests also proves the same.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may work for PostgreSQL and may also work for some other databases, but I doubt this to be a really portable behavior (for example, Trino does not support comparing double values with |
||
| } | ||
|
|
||
| return super.toPredicate(client, session, column, jdbcType, type, writeFunction, operator, value, accumulator); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import com.google.common.collect.ImmutableMap; | ||
| import io.trino.plugin.jdbc.BaseJdbcConnectorTest; | ||
| import io.trino.sql.planner.plan.AggregationNode; | ||
| import io.trino.sql.planner.plan.FilterNode; | ||
| import io.trino.testing.MaterializedResult; | ||
| import io.trino.testing.QueryRunner; | ||
| import io.trino.testing.TestingConnectorBehavior; | ||
|
|
@@ -161,6 +162,103 @@ public void testRenameColumnName() | |
| { | ||
| } | ||
|
|
||
| @Test | ||
| @Override | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why override? document |
||
| // Clickhouse doesn't support push down on real columns | ||
| public void testSpecialValueOnApproximateNumericColumn() | ||
| { | ||
| try (TestTable table = new TestTable( | ||
| getQueryRunner()::execute, | ||
| "spl_approx_numeric", | ||
| "(c_varchar VARCHAR, c_real REAL, c_real_2 REAL, c_double DOUBLE, c_double_2 DOUBLE)", | ||
| List.of( | ||
| "'1', NaN(), REAL '1', Nan(), DOUBLE '1'", | ||
| "'2', -Infinity(), REAL '1', -Infinity(), DOUBLE '1'", | ||
| "'3', Infinity(), REAL '1', Infinity(), DOUBLE '1'", | ||
| "'4', Nan(), Nan(), NaN(), Nan()"))) { | ||
| String tableName = table.getName(); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_double > 1")) | ||
| .isFullyPushedDown() | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real > 1 AND c_double > 1")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_double < 1")) | ||
| .isFullyPushedDown() | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '2'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real < 1 AND c_double < 1")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '2'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_double < Infinity()")) | ||
| .isFullyPushedDown() | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '2'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real < Infinity() AND c_double < Infinity()")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '2'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real > -Infinity() AND c_double > -Infinity()")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_double > -Infinity()")) | ||
| .isFullyPushedDown() | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real > c_real_2 AND c_double > c_double_2")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real IS DISTINCT FROM c_real_2 AND c_double IS DISTINCT FROM c_double_2")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '1', '2', '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real_2 IS DISTINCT FROM c_real AND c_double_2 IS DISTINCT FROM c_double")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '1', '2', '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real > c_real_2 OR c_double > c_double_2")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real = c_real_2 OR c_double = c_double_2")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .returnsEmptyResult(); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real <> c_real_2 OR c_double <> c_double_2")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '1', '2', '3', '4'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real < Infinity() OR c_double < Infinity()")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '2'"); | ||
|
|
||
| assertThat(query("SELECT c_varchar FROM " + tableName + " WHERE c_real > -Infinity() OR c_double > -Infinity()")) | ||
| .isNotFullyPushedDown(FilterNode.class) | ||
| .skippingTypesCheck() | ||
| .matches("VALUES '3'"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected Optional<String> filterColumnNameTestData(String columnName) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.