-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7667] Improve string-literal encoding in pushdown translators #5117
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
Open
rubenada
wants to merge
2
commits into
apache:main
Choose a base branch
from
rubenada:CALCITE-7667
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...dra/src/test/java/org/apache/calcite/adapter/cassandra/CassandraFilterTranslatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * 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.calcite.adapter.cassandra; | ||
|
|
||
| import org.apache.calcite.jdbc.JavaTypeFactoryImpl; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.rex.RexInputRef; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Collections; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.containsString; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| /** | ||
| * Unit tests for {@link CassandraFilter.Translator} covering CQL literal serialization. | ||
| * | ||
| * <p>The tests exercise the translator directly against a synthetic row | ||
| * type; they do not require a running Cassandra cluster. | ||
| */ | ||
| class CassandraFilterTranslatorTest { | ||
|
|
||
| private static final RelDataTypeFactory TYPE_FACTORY = new JavaTypeFactoryImpl(); | ||
| private static final RexBuilder REX_BUILDER = new RexBuilder(TYPE_FACTORY); | ||
|
|
||
| /** Two-column row type mirroring how CqlToSqlTypeConversionRules | ||
| * maps Cassandra types: a CHAR column stands in for `uuid`/`timeuuid` | ||
| * and a VARCHAR column stands in for `text`. */ | ||
| private static RelDataType rowType() { | ||
| return TYPE_FACTORY.builder() | ||
| .add("id", SqlTypeName.CHAR, 36) | ||
| .add("name", SqlTypeName.VARCHAR) | ||
| .build(); | ||
| } | ||
|
|
||
| /** Invokes the (private) translator's translateMatch entry point via reflection. | ||
| * Any exception is unwrapped from InvocationTargetException so callers can | ||
| * see the real cause. */ | ||
| private static String translate(RexNode condition) throws Throwable { | ||
| CassandraFilter.Translator t = | ||
| new CassandraFilter.Translator(rowType(), | ||
| Collections.singletonList("id"), | ||
| Collections.emptyList(), | ||
| Collections.emptyList()); | ||
| Method m = CassandraFilter.Translator.class | ||
| .getDeclaredMethod("translateMatch", RexNode.class); | ||
| m.setAccessible(true); | ||
| try { | ||
| return (String) m.invoke(t, condition); | ||
| } catch (InvocationTargetException e) { | ||
| throw e.getCause(); | ||
| } | ||
| } | ||
|
|
||
| private static RexNode eqLit(int fieldIndex, SqlTypeName fieldType, | ||
| int precision, String value) { | ||
| RelDataType t = precision > 0 | ||
| ? TYPE_FACTORY.createSqlType(fieldType, precision) | ||
| : TYPE_FACTORY.createSqlType(fieldType); | ||
| RexInputRef ref = REX_BUILDER.makeInputRef(t, fieldIndex); | ||
| RexNode lit = REX_BUILDER.makeLiteral(value, t, false); | ||
| return REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, ref, lit); | ||
| } | ||
|
|
||
| @Test void uuidColumnValidUuidEmittedUnquoted() throws Throwable { | ||
| String cql = | ||
| translate(eqLit(0, SqlTypeName.CHAR, 36, "037f7c30-abcd-11ee-8000-000000000001")); | ||
| assertThat(cql, is("id = 037f7c30-abcd-11ee-8000-000000000001")); | ||
| } | ||
|
|
||
| @Test void uuidColumnNonUuidValueRejected() { | ||
| IllegalArgumentException e = | ||
| assertThrows( | ||
| IllegalArgumentException.class, () -> translate( | ||
| eqLit(0, SqlTypeName.CHAR, 36, "not-a-uuid"))); | ||
| assertThat(e.getMessage(), containsString("not a well-formed UUID")); | ||
| } | ||
|
|
||
| @Test void uuidColumnEmptyRejected() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> translate(eqLit(0, SqlTypeName.CHAR, 36, ""))); | ||
| } | ||
|
|
||
| @Test void uuidColumnAlmostUuidRejected() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> translate( | ||
| eqLit(0, SqlTypeName.CHAR, 36, "037f7c30-abcd-11ee-8000-000000000001x"))); | ||
| } | ||
|
|
||
| @Test void varcharColumnPlainValueQuoted() throws Throwable { | ||
| String cql = translate(eqLit(1, SqlTypeName.VARCHAR, -1, "alice")); | ||
| assertThat(cql, is("name = 'alice'")); | ||
| } | ||
|
|
||
| @Test void varcharColumnValueWithApostrophe() throws Throwable { | ||
| String cql = translate(eqLit(1, SqlTypeName.VARCHAR, -1, "O'Brien")); | ||
| assertThat(cql, is("name = 'O''Brien'")); | ||
| } | ||
|
|
||
| @Test void varcharColumnValueWithMultipleApostrophes() throws Throwable { | ||
| String cql = translate(eqLit(1, SqlTypeName.VARCHAR, -1, "a''b")); | ||
| assertThat(cql, is("name = 'a''''b'")); | ||
| } | ||
|
|
||
| @Test void varcharColumnValueWithApostropheAtTheStart() throws Throwable { | ||
| String cql = translate(eqLit(1, SqlTypeName.VARCHAR, -1, "'a")); | ||
| assertThat(cql, is("name = '''a'")); | ||
| } | ||
|
|
||
| @Test void varcharColumnValueWithApostropheAtTheEnd() throws Throwable { | ||
| String cql = translate(eqLit(1, SqlTypeName.VARCHAR, -1, "a'")); | ||
| assertThat(cql, is("name = 'a'''")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
geode/src/test/java/org/apache/calcite/adapter/geode/rel/GeodeFilterTranslatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * 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.calcite.adapter.geode.rel; | ||
|
|
||
| import org.apache.calcite.jdbc.JavaTypeFactoryImpl; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.rex.RexInputRef; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| /** | ||
| * Unit tests for {@link GeodeFilter.Translator} covering OQL literal serialization. | ||
| * | ||
| * <p>The tests exercise the translator directly against a synthetic | ||
| * row type; they do not require a running Geode cluster. | ||
| */ | ||
| class GeodeFilterTranslatorTest { | ||
|
|
||
| private static final RelDataTypeFactory TYPE_FACTORY = new JavaTypeFactoryImpl(); | ||
| private static final RexBuilder REX_BUILDER = new RexBuilder(TYPE_FACTORY); | ||
|
|
||
| private static RelDataType rowType() { | ||
| return TYPE_FACTORY.builder() | ||
| .add("code", SqlTypeName.CHAR, 32) | ||
| .build(); | ||
| } | ||
|
|
||
| /** Invokes the (private) translator's translateMatch entry | ||
| * point via reflection so this test can live in the same package | ||
| * without widening the Translator's visibility. Any exception the | ||
| * target throws is unwrapped from InvocationTargetException. */ | ||
| private static String translate(RexNode condition) throws Throwable { | ||
| GeodeFilter.Translator t = | ||
| new GeodeFilter.Translator(rowType(), REX_BUILDER); | ||
| Method m = GeodeFilter.Translator.class | ||
| .getDeclaredMethod("translateMatch", RexNode.class); | ||
| m.setAccessible(true); | ||
| try { | ||
| return (String) m.invoke(t, condition); | ||
| } catch (InvocationTargetException e) { | ||
| throw e.getCause(); | ||
| } | ||
| } | ||
|
|
||
| /** Builds a {@code code = <value>} predicate where the literal is | ||
| * typed as {@code CHAR(N)} matching {@code value.length()} exactly. */ | ||
| private static RexNode eqLit(String value) { | ||
| RelDataType t = TYPE_FACTORY.createSqlType(SqlTypeName.CHAR, value.length()); | ||
| RexInputRef ref = REX_BUILDER.makeInputRef(t, 0); | ||
| RexNode lit = REX_BUILDER.makeLiteral(value, t, false); | ||
| return REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, ref, lit); | ||
| } | ||
|
|
||
| @Test void charColumnPlainValueQuoted() throws Throwable { | ||
| assertThat(translate(eqLit("alpha")), is("code = 'alpha'")); | ||
| } | ||
|
|
||
| @Test void charColumnValueWithApostrophe() throws Throwable { | ||
| assertThat(translate(eqLit("O'Brien")), is("code = 'O''Brien'")); | ||
| } | ||
|
|
||
| @Test void charColumnValueWithMultipleApostrophes() throws Throwable { | ||
| assertThat(translate(eqLit("a''b")), is("code = 'a''''b'")); | ||
| } | ||
|
|
||
| @Test void charColumnValueWithApostropheAtTheStart() throws Throwable { | ||
| assertThat(translate(eqLit("'a")), is("code = '''a'")); | ||
| } | ||
|
|
||
| @Test void charColumnValueWithApostropheAtTheEnd() throws Throwable { | ||
| assertThat(translate(eqLit("a'")), is("code = 'a'''")); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this test was wrong?
I hope that this one was validated somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems so. The test was "correct" for the "TRIM BOTH into REGEXP_REPLACE" part, but it was lacking the backslash escape.