-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial version with PinotJsonContainsPredicate
resolved style and deferred dependency issue
- Loading branch information
1 parent
7006352
commit 5fcc0fd
Showing
13 changed files
with
331 additions
and
40 deletions.
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
plugin/trino-pinot/src/main/java/io/trino/plugin/pinot/PinotJsonContainsPredicate.java
This file contains 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,186 @@ | ||
/* | ||
* 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.pinot; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.airlift.slice.Slice; | ||
import io.trino.spi.block.IntArrayBlock; | ||
import io.trino.spi.block.VariableWidthBlock; | ||
import io.trino.spi.expression.Call; | ||
import io.trino.spi.expression.ConnectorExpression; | ||
import io.trino.spi.expression.Constant; | ||
import io.trino.spi.expression.FunctionName; | ||
import io.trino.spi.expression.Variable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class PinotJsonContainsPredicate | ||
implements PinotJsonPredicate | ||
{ | ||
private final String columnName; | ||
private final String jsonPath; | ||
private final List<String> values; | ||
private final boolean valuesContainsStrings; | ||
private final String type; | ||
|
||
@JsonCreator | ||
public PinotJsonContainsPredicate( | ||
@JsonProperty("columnName") String columnName, | ||
@JsonProperty("jsonPath") String jsonPath, | ||
@JsonProperty("values") List<String> values, | ||
@JsonProperty("valuesContainsStrings") boolean valuesContainsStrings, | ||
@JsonProperty("type") String type) | ||
{ | ||
this.columnName = requireNonNull(columnName, "columnName is null"); | ||
this.jsonPath = requireNonNull(jsonPath, "jsonPath is null"); | ||
this.values = requireNonNull(values, "values is null"); | ||
this.valuesContainsStrings = valuesContainsStrings; | ||
this.type = "contains"; | ||
} | ||
|
||
@JsonProperty | ||
public String getColumnName() | ||
{ | ||
return columnName; | ||
} | ||
|
||
@JsonProperty | ||
public String getJsonPath() | ||
{ | ||
return jsonPath; | ||
} | ||
|
||
@JsonProperty | ||
public List<String> getValues() | ||
{ | ||
return values; | ||
} | ||
|
||
@JsonProperty | ||
public boolean getValuesContainsStrings() | ||
{ | ||
return valuesContainsStrings; | ||
} | ||
|
||
@JsonProperty | ||
public String getType() | ||
{ | ||
return type; | ||
} | ||
|
||
public PinotJsonContainsPredicate(Call call) | ||
{ | ||
List<ConnectorExpression> containsCallArgs = call.getArguments(); | ||
Constant arrayArg = (Constant) containsCallArgs.getFirst(); | ||
values = new ArrayList<>(); | ||
if (arrayArg.getValue() instanceof VariableWidthBlock stringArray) { | ||
valuesContainsStrings = true; | ||
for (int index = 0; index < stringArray.getPositionCount(); index++) { | ||
values.add(stringArray.getSlice(index).toStringUtf8()); | ||
} | ||
} | ||
else if (arrayArg.getValue() instanceof IntArrayBlock intArray) { | ||
valuesContainsStrings = false; | ||
for (int index = 0; index < intArray.getPositionCount(); index++) { | ||
values.add(String.valueOf(intArray.getInt(index))); | ||
} | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unsupported array argument type: " + arrayArg.getValue()); | ||
} | ||
|
||
Call innerCall = (Call) containsCallArgs.get(1); | ||
Call jsonExtractScalarCall = innerCall; | ||
if (new FunctionName("$cast").equals(innerCall.getFunctionName())) { | ||
jsonExtractScalarCall = (Call) innerCall.getArguments().getFirst(); | ||
} | ||
|
||
List<ConnectorExpression> args = jsonExtractScalarCall.getArguments(); | ||
columnName = ((Variable) args.get(0)).getName(); | ||
jsonPath = ((Slice) ((Constant) args.get(1)).getValue()).toStringUtf8(); | ||
type = "contains"; | ||
} | ||
|
||
public static boolean supportsCall(Call call) | ||
{ | ||
if (!new FunctionName("contains").equals(call.getFunctionName())) { | ||
return false; | ||
} | ||
|
||
List<ConnectorExpression> arguments = call.getArguments(); | ||
ConnectorExpression arrayArg = arguments.get(0); | ||
if (!(arrayArg instanceof Constant) || !(arguments.get(1) instanceof Call innerCall)) { | ||
return false; | ||
} | ||
|
||
Constant constant = (Constant) arrayArg; | ||
if (!(constant.getValue() instanceof VariableWidthBlock || constant.getValue() instanceof IntArrayBlock)) { | ||
return false; | ||
} | ||
|
||
if (new FunctionName("$cast").equals(innerCall.getFunctionName())) { | ||
List<ConnectorExpression> castArguments = innerCall.getArguments(); | ||
if (!(castArguments.getFirst() instanceof Call jsonExtracatScalarCall)) { | ||
return false; | ||
} | ||
return isSupportedJsonExtractScalarCall(jsonExtracatScalarCall); | ||
} | ||
else { | ||
return isSupportedJsonExtractScalarCall(innerCall); | ||
} | ||
} | ||
|
||
private static boolean isSupportedJsonExtractScalarCall(Call call) | ||
{ | ||
if (!new FunctionName("json_extract_scalar").equals(call.getFunctionName())) { | ||
return false; | ||
} | ||
|
||
List<ConnectorExpression> arguments = call.getArguments(); | ||
if (!(arguments.get(0) instanceof Variable) || !(arguments.get(1) instanceof Constant)) { | ||
return false; | ||
} | ||
|
||
// TODO: resolve dependency issues to allow usage of io.trino.type.JsonType | ||
// return arguments.get(0).getType() instanceof JsonType; | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String toPQL() | ||
{ | ||
String escape = valuesContainsStrings ? "''" : ""; | ||
String values = String.join(String.format("%s,%s", escape, escape), this.values); | ||
return String.format("JSON_MATCH(%s, '\"%s\" in (%s%s%s)')", | ||
columnName, jsonPath, escape, values, escape); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return toStringHelper(this) | ||
.add("columnName", columnName) | ||
.add("jsonPath", jsonPath) | ||
.add("values", values) | ||
.add("valuesContainsStrings", valuesContainsStrings) | ||
.add("type", type) | ||
.toString(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
plugin/trino-pinot/src/main/java/io/trino/plugin/pinot/PinotJsonPredicate.java
This file contains 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,34 @@ | ||
/* | ||
* 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.pinot; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import io.trino.spi.expression.Call; | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
property = "type") | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = PinotJsonContainsPredicate.class, name = "contains") | ||
}) | ||
public interface PinotJsonPredicate | ||
{ | ||
static boolean supportsCall(Call call) | ||
{ | ||
return false; | ||
} | ||
|
||
String toPQL(); | ||
} |
This file contains 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 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 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
Oops, something went wrong.