[
https://issues.apache.org/jira/browse/BEAM-4365?focusedWorklogId=110274&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-110274
]
ASF GitHub Bot logged work on BEAM-4365:
----------------------------------------
Author: ASF GitHub Bot
Created on: 08/Jun/18 21:02
Start Date: 08/Jun/18 21:02
Worklog Time Spent: 10m
Work Description: akedin commented on a change in pull request #5433:
[BEAM-4365] Make BeamSqlExpression for operators, use it for string operators
URL: https://github.com/apache/beam/pull/5433#discussion_r194180401
##########
File path:
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/StringOperators.java
##########
@@ -0,0 +1,242 @@
+/*
+ * 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.beam.sdk.extensions.sql.impl.interpreter.operator;
+
+import java.util.List;
+import org.apache.calcite.runtime.SqlFunctions;
+import org.apache.calcite.sql.fun.SqlTrimFunction;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+/** String operator implementations. */
+public class StringOperators {
+
+ /** A {@link BeamSqlOperator} that returns a string. */
+ public interface StringOperator extends BeamSqlOperator {
+ default SqlTypeName getOutputType() {
+ return SqlTypeName.VARCHAR;
+ }
+ }
+
+ @FunctionalInterface
+ private interface StringUnaryOperator extends BeamSqlUnaryOperator {
+ default boolean accept(BeamSqlExpression arg) {
+ return SqlTypeName.CHAR_TYPES.contains(arg.getOutputType());
+ }
+
+ default SqlTypeName getOutputType() {
+ return SqlTypeName.VARCHAR;
+ }
+ }
+
+ public static final BeamSqlOperator CHAR_LENGTH =
+ (StringUnaryOperator)
+ (BeamSqlPrimitive arg) ->
+ BeamSqlPrimitive.of(SqlTypeName.INTEGER,
SqlFunctions.charLength(arg.getString()));
+
+ public static final BeamSqlOperator UPPER =
+ (StringUnaryOperator)
+ (BeamSqlPrimitive arg) ->
+ BeamSqlPrimitive.of(SqlTypeName.VARCHAR,
SqlFunctions.upper(arg.getString()));
+
+ public static final BeamSqlOperator LOWER =
+ (StringUnaryOperator)
+ (BeamSqlPrimitive arg) ->
+ BeamSqlPrimitive.of(SqlTypeName.VARCHAR,
SqlFunctions.lower(arg.getString()));
+
+ /** {@code INITCAP}. */
+ public static final BeamSqlOperator INIT_CAP =
+ (StringUnaryOperator)
+ (BeamSqlPrimitive arg) ->
+ BeamSqlPrimitive.of(SqlTypeName.VARCHAR,
SqlFunctions.initcap(arg.getString()));
+
+ public static final BeamSqlBinaryOperator CONCAT =
+ new BeamSqlBinaryOperator() {
+ @Override
+ public SqlTypeName getOutputType() {
+ return SqlTypeName.VARCHAR;
+ }
+
+ @Override
+ public boolean accept(BeamSqlExpression left, BeamSqlExpression right)
{
+ return SqlTypeName.CHAR_TYPES.contains(left.getOutputType())
+ && SqlTypeName.CHAR_TYPES.contains(right.getOutputType());
+ }
+
+ @Override
+ public BeamSqlPrimitive apply(BeamSqlPrimitive left, BeamSqlPrimitive
right) {
+ return BeamSqlPrimitive.of(
+ SqlTypeName.VARCHAR, SqlFunctions.concat(left.getString(),
right.getString()));
+ }
+ };
+
+ public static final BeamSqlOperator POSITION =
+ new StringOperator() {
+ @Override
+ public boolean accept(List<BeamSqlExpression> operands) {
+ if (operands.size() < 2 || operands.size() > 3) {
+ return false;
+ }
+
+ return
SqlTypeName.CHAR_TYPES.contains(operands.get(0).getOutputType())
+ &&
SqlTypeName.CHAR_TYPES.contains(operands.get(1).getOutputType())
+ && ((operands.size() < 3)
+ ||
SqlTypeName.INT_TYPES.contains(operands.get(2).getOutputType()));
+ }
+
+ @Override
+ public BeamSqlPrimitive apply(List<BeamSqlPrimitive> arguments) {
+ String targetStr = arguments.get(0).getString();
+ String containingStr = arguments.get(1).getString();
+ int from = arguments.size() < 3 ? 1 : arguments.get(2).getInteger();
+ return BeamSqlPrimitive.of(
+ SqlTypeName.INTEGER, SqlFunctions.position(targetStr,
containingStr, from));
+ }
+ };
+
+ public static final BeamSqlOperator TRIM =
+ new StringOperator() {
+ @Override
+ public boolean accept(List<BeamSqlExpression> subexpressions) {
+ return (subexpressions.size() == 1
+ &&
SqlTypeName.CHAR_TYPES.contains(subexpressions.get(0).getOutputType()))
+ || (subexpressions.size() == 3
+ &&
SqlTypeName.SYMBOL.equals(subexpressions.get(0).getOutputType())
+ &&
SqlTypeName.CHAR_TYPES.contains(subexpressions.get(1).getOutputType())
+ &&
SqlTypeName.CHAR_TYPES.contains(subexpressions.get(2).getOutputType()));
+ }
+
+ @Override
+ public BeamSqlPrimitive apply(List<BeamSqlPrimitive> operands) {
+ SqlTrimFunction.Flag type;
+ String removeChars;
+ String fromStr;
+
+ if (operands.size() == 1) {
+ type = SqlTrimFunction.Flag.BOTH;
+ removeChars = " ";
+ fromStr = operands.get(0).getString();
+ } else {
+ type = (SqlTrimFunction.Flag) operands.get(0).getValue();
+ removeChars = operands.get(1).getString();
+ fromStr = operands.get(2).getString();
+ }
+
+ return BeamSqlPrimitive.of(
+ SqlTypeName.VARCHAR,
+ trim(
+ type == SqlTrimFunction.Flag.BOTH || type ==
SqlTrimFunction.Flag.LEADING,
+ type == SqlTrimFunction.Flag.BOTH || type ==
SqlTrimFunction.Flag.TRAILING,
+ removeChars,
+ fromStr));
+ }
+ };
+
+ /**
+ * Calcite's implementation of TRIM is incorrect and only trims the first
character in removeStr.
+ *
+ * <p>This implementation deliberately kept compatible with eventual
upstreaming.
+ */
+ private static String trim(
Review comment:
isn't there a Guava trim method?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 110274)
Time Spent: 2h 10m (was: 2h)
> SQL operator argument evaluation should have one place where it is managed
> --------------------------------------------------------------------------
>
> Key: BEAM-4365
> URL: https://issues.apache.org/jira/browse/BEAM-4365
> Project: Beam
> Issue Type: Bug
> Components: dsl-sql
> Reporter: Kenneth Knowles
> Assignee: Kenneth Knowles
> Priority: Major
> Time Spent: 2h 10m
> Remaining Estimate: 0h
>
> The way Beam SQL is factored, each operator has to explicitly ask its
> argument to be evaluated. This should be handled generically at a higher
> level. Since the language is pure and terminating, it is fine for them to
> vary, but given the simplicity of the expression language it makes sense to
> use simple call-by-value.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)