-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,501 additions
and
6 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
...ava/org/apache/beam/dsls/sql/interpreter/operator/string/BeamSqlCharLengthExpression.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,40 @@ | ||
/* | ||
* 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.dsls.sql.interpreter.operator.string; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; | ||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; | ||
import org.apache.beam.dsls.sql.schema.BeamSQLRow; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
|
||
/** | ||
* 'CHAR_LENGTH' operator. | ||
*/ | ||
public class BeamSqlCharLengthExpression extends BeamSqlStringUnaryExpression { | ||
public BeamSqlCharLengthExpression(List<BeamSqlExpression> operands) { | ||
super(operands, SqlTypeName.INTEGER); | ||
} | ||
|
||
@Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { | ||
String str = opValueEvaluated(0, inputRecord); | ||
return BeamSqlPrimitive.of(SqlTypeName.INTEGER, str.length()); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...in/java/org/apache/beam/dsls/sql/interpreter/operator/string/BeamSqlConcatExpression.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,63 @@ | ||
/* | ||
* 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.dsls.sql.interpreter.operator.string; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; | ||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; | ||
import org.apache.beam.dsls.sql.schema.BeamSQLRow; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
|
||
/** | ||
* String concat operator. | ||
*/ | ||
public class BeamSqlConcatExpression extends BeamSqlExpression { | ||
|
||
protected BeamSqlConcatExpression(List<BeamSqlExpression> operands, SqlTypeName outputType) { | ||
super(operands, outputType); | ||
} | ||
|
||
public BeamSqlConcatExpression(List<BeamSqlExpression> operands) { | ||
super(operands, SqlTypeName.VARCHAR); | ||
} | ||
|
||
@Override public boolean accept() { | ||
if (operands.size() != 2) { | ||
return false; | ||
} | ||
|
||
for (BeamSqlExpression exp : getOperands()) { | ||
if (!SqlTypeName.CHAR_TYPES.contains(exp.getOutputType())) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { | ||
String left = opValueEvaluated(0, inputRecord); | ||
String right = opValueEvaluated(1, inputRecord); | ||
|
||
return BeamSqlPrimitive.of(SqlTypeName.VARCHAR, | ||
new StringBuilder(left.length() + right.length()) | ||
.append(left).append(right).toString()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...n/java/org/apache/beam/dsls/sql/interpreter/operator/string/BeamSqlInitCapExpression.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,56 @@ | ||
/* | ||
* 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.dsls.sql.interpreter.operator.string; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; | ||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; | ||
import org.apache.beam.dsls.sql.schema.BeamSQLRow; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
|
||
/** | ||
* 'INITCAP' operator. | ||
*/ | ||
public class BeamSqlInitCapExpression extends BeamSqlStringUnaryExpression { | ||
public BeamSqlInitCapExpression(List<BeamSqlExpression> operands) { | ||
super(operands, SqlTypeName.VARCHAR); | ||
} | ||
|
||
@Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { | ||
String str = opValueEvaluated(0, inputRecord); | ||
|
||
StringBuilder ret = new StringBuilder(str); | ||
boolean isInit = true; | ||
for (int i = 0; i < str.length(); i++) { | ||
if (Character.isWhitespace(str.charAt(i))) { | ||
isInit = true; | ||
continue; | ||
} | ||
|
||
if (isInit) { | ||
ret.setCharAt(i, Character.toUpperCase(str.charAt(i))); | ||
isInit = false; | ||
} else { | ||
ret.setCharAt(i, Character.toLowerCase(str.charAt(i))); | ||
} | ||
} | ||
return BeamSqlPrimitive.of(SqlTypeName.VARCHAR, ret.toString()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ain/java/org/apache/beam/dsls/sql/interpreter/operator/string/BeamSqlLowerExpression.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,40 @@ | ||
/* | ||
* 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.dsls.sql.interpreter.operator.string; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; | ||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; | ||
import org.apache.beam.dsls.sql.schema.BeamSQLRow; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
|
||
/** | ||
* 'LOWER' operator. | ||
*/ | ||
public class BeamSqlLowerExpression extends BeamSqlStringUnaryExpression { | ||
public BeamSqlLowerExpression(List<BeamSqlExpression> operands) { | ||
super(operands, SqlTypeName.VARCHAR); | ||
} | ||
|
||
@Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { | ||
String str = opValueEvaluated(0, inputRecord); | ||
return BeamSqlPrimitive.of(SqlTypeName.VARCHAR, str.toLowerCase()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...n/java/org/apache/beam/dsls/sql/interpreter/operator/string/BeamSqlOverlayExpression.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,77 @@ | ||
/* | ||
* 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.dsls.sql.interpreter.operator.string; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; | ||
import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; | ||
import org.apache.beam.dsls.sql.schema.BeamSQLRow; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
|
||
/** | ||
* 'OVERLAY' operator. | ||
* | ||
* <p> | ||
* OVERLAY(string1 PLACING string2 FROM integer [ FOR integer2 ]) | ||
* </p> | ||
*/ | ||
public class BeamSqlOverlayExpression extends BeamSqlExpression { | ||
public BeamSqlOverlayExpression(List<BeamSqlExpression> operands) { | ||
super(operands, SqlTypeName.VARCHAR); | ||
} | ||
|
||
@Override public boolean accept() { | ||
if (operands.size() < 3 || operands.size() > 4) { | ||
return false; | ||
} | ||
|
||
if (!SqlTypeName.CHAR_TYPES.contains(opType(0)) | ||
|| !SqlTypeName.CHAR_TYPES.contains(opType(1)) | ||
|| !SqlTypeName.INT_TYPES.contains(opType(2))) { | ||
return false; | ||
} | ||
|
||
if (operands.size() == 4 && !SqlTypeName.INT_TYPES.contains(opType(3))) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { | ||
String str = opValueEvaluated(0, inputRecord); | ||
String replaceStr = opValueEvaluated(1, inputRecord); | ||
int idx = opValueEvaluated(2, inputRecord); | ||
// the index is 1 based. | ||
idx -= 1; | ||
int length = replaceStr.length(); | ||
if (operands.size() == 4) { | ||
length = opValueEvaluated(3, inputRecord); | ||
} | ||
|
||
StringBuilder result = new StringBuilder( | ||
str.length() + replaceStr.length() - length); | ||
result.append(str.substring(0, idx)) | ||
.append(replaceStr) | ||
.append(str.substring(idx + length)); | ||
|
||
return BeamSqlPrimitive.of(SqlTypeName.VARCHAR, result.toString()); | ||
} | ||
} |
Oops, something went wrong.