Skip to content

Commit

Permalink
#94 Added CAST function support (#96)
Browse files Browse the repository at this point in the history
* #94 Added CAST function support

Co-authored-by: Anastasiia Sergienko <anastasiia.sergienko@exasol.com>
  • Loading branch information
jakobbraun and AnastasiiaSergienko authored Oct 22, 2020
1 parent 90cdea0 commit 1e4d903
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/changes/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changes

* [4.3.0](changes_4.3.0.md)
* [4.2.0](changes_4.2.0.md)
* [4.1.0](changes_4.1.0.md)
* [4.0.0](changes_4.0.0.md)
7 changes: 7 additions & 0 deletions doc/changes/changes_4.3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SQL Statement Builder 4.3.0, released 2020-10-22

Code Name: CAST function support

## Features / Enhancements

* #94: Added CAST function support
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.exasol</groupId>
<artifactId>sql-statement-builder</artifactId>
<version>4.2.0</version>
<version>4.3.0</version>
<name>Exasol SQL Statement Builder</name>
<description>This module provides a Builder for SQL statements that helps creating the correct structure and
validates variable parts of the statements.
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/exasol/sql/dql/select/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Select field(final String... names) {
/**
* Add a function.
*
* @param functionName a name of function
* @param functionName name of function
* @param valueExpressions zero or more value expression
* @return <code>this</code> instance for fluent programming
*/
Expand All @@ -79,6 +79,30 @@ public Select function(final FunctionName functionName, final String derivedColu
return this;
}

/**
* Add a function.
*
* @param function function
* @param derivedColumnName name under which you can refer to the derived column
* @return <code>this</code> instance for fluent programming
*/
public Select function(final Function function, final String derivedColumnName) {
final DerivedColumn derivedColumn = new DerivedColumn(this, function, derivedColumnName);
this.derivedColumns.add(derivedColumn);
return this;
}

/**
* Add a function.
*
* @param function function
* @return <code>this</code> instance for fluent programming
*/
public Select function(final Function function) {
function(function, "");
return this;
}

/**
* Add a User Defined Function.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.exasol.sql.expression;

import com.exasol.sql.UnnamedPlaceholder;
import com.exasol.sql.expression.function.exasol.CastExasolFunction;
import com.exasol.sql.expression.function.exasol.ExasolFunction;
import com.exasol.sql.expression.function.exasol.ExasolUdf;

Expand Down Expand Up @@ -41,4 +42,6 @@ public interface ValueExpressionVisitor {
public void visit(NullLiteral nullLiteral);

public void visit(BooleanExpression booleanExpression);

public void visit(CastExasolFunction castFunction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.exasol.sql.expression.function.exasol;

import java.util.List;

import com.exasol.datatype.type.DataType;
import com.exasol.sql.expression.ValueExpression;
import com.exasol.sql.expression.ValueExpressionVisitor;
import com.exasol.sql.expression.function.FunctionName;

/**
* This class represents the Exasol CAST function.
*/
public class CastExasolFunction extends ExasolFunction {
private final DataType type;

private CastExasolFunction(final ValueExpression value, final DataType type) {
super(ExasolScalarFunctionCast.CAST, List.of(value));
this.type = type;
}

/**
* Create a new {@link CastExasolFunction} instance.
*
* @param valueExpression value to cast
* @param type type to cast the value to
* @return new {@link CastExasolFunction}
*/
public static CastExasolFunction of(final ValueExpression valueExpression, final DataType type) {
return new CastExasolFunction(valueExpression, type);
}

/**
* Get the value to cast.
*
* @return value to cast
*/
public ValueExpression getValue() {
return this.valueExpressions.get(0);
}

/**
* Get the type to cast the value to.
*
* @return type to cast the value to
*/
public DataType getType() {
return this.type;
}

@Override
public boolean hasParenthesis() {
return true;
}

@Override
public void accept(final ValueExpressionVisitor visitor) {
visitor.visit(this);
}

private enum ExasolScalarFunctionCast implements FunctionName {
CAST
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* <a href="https://github.com/exasol/sql-statement-builder/issues/72"> github issue # 72</a>.
* </p>
*/
public enum ExasolAnalyticFunctions implements FunctionName {
public enum ExasolAnalyticFunction implements FunctionName {
ANY, EVERY, LISTAGG
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.exasol.sql.expression.function.exasol;

import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.exasol.sql.expression.ValueExpression;
import com.exasol.sql.expression.ValueExpressionVisitor;
Expand All @@ -14,7 +16,7 @@ public class ExasolFunction extends AbstractFunction {
private static final List<String> functionsWithoutParenthesis = Arrays.asList("SYSDATE", "CURRENT_SCHEMA",
"CURRENT_SESSION", "CURRENT_STATEMENT", "CURRENT_USER", "ROWNUM", "ROWID", "SCOPE_USER", "USER");

private ExasolFunction(final FunctionName functionName, final List<ValueExpression> valueExpressions) {
protected ExasolFunction(final FunctionName functionName, final List<ValueExpression> valueExpressions) {
super(functionName.toString(), valueExpressions);
}

Expand All @@ -31,7 +33,7 @@ public static ExasolFunction of(final FunctionName functionName) {
/**
* Create a new {@link ExasolFunction} instance.
*
* @param functionName name of the function
* @param functionName name of the function
* @param valueExpressions zero or more value expressions
* @return new {@link ExasolFunction}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.exasol.sql.expression.rendering;

import com.exasol.datatype.type.DataType;
import com.exasol.sql.ColumnsDefinition;
import com.exasol.sql.UnnamedPlaceholder;
import com.exasol.sql.expression.*;
import com.exasol.sql.expression.function.Function;
import com.exasol.sql.expression.function.exasol.CastExasolFunction;
import com.exasol.sql.expression.function.exasol.ExasolFunction;
import com.exasol.sql.expression.function.exasol.ExasolUdf;
import com.exasol.sql.rendering.ColumnsDefinitionRenderer;
Expand Down Expand Up @@ -55,7 +57,7 @@ public void visit(final FloatLiteral literal) {
}

@Override
public void visit(BigDecimalLiteral literal) {
public void visit(final BigDecimalLiteral literal) {
appendCommaWhenNeeded(literal);
append(literal.toString());
setLastVisited(literal);
Expand Down Expand Up @@ -129,7 +131,7 @@ private void appendEmitsWhenNecessary(final ExasolUdf function) {
appendKeyword(" EMITS");
append(" ");
final ColumnsDefinition columnsDefinition = function.getEmitsColumnsDefinition().get();
final ColumnsDefinitionRenderer columnsDefinitionRenderer = new ColumnsDefinitionRenderer(this.config);
final ColumnsDefinitionRenderer columnsDefinitionRenderer = getColumnsDefinitionRenderer();
columnsDefinition.accept(columnsDefinitionRenderer);
this.builder.append(columnsDefinitionRenderer.render());
}
Expand Down Expand Up @@ -165,4 +167,21 @@ public void visit(final BooleanExpression booleanExpression) {
booleanExpression.accept(expressionRenderer);
append(expressionRenderer.render());
}

@Override
public void visit(final CastExasolFunction castFunction) {
appendKeyword("CAST");
startParenthesis();
castFunction.getValue().accept(this);
appendKeyword(" AS");
final DataType type = castFunction.getType();
final ColumnsDefinitionRenderer columnsDefinitionRenderer = getColumnsDefinitionRenderer();
type.accept(columnsDefinitionRenderer);
append(columnsDefinitionRenderer.render());
endParenthesis();
}

private ColumnsDefinitionRenderer getColumnsDefinitionRenderer() {
return new ColumnsDefinitionRenderer(this.config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.exasol.sql.expression.function.exasol;

import static com.exasol.hamcrest.SqlFragmentRenderResultMatcher.rendersTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

import com.exasol.datatype.type.Varchar;
import com.exasol.sql.StatementFactory;
import com.exasol.sql.dql.select.Select;
import com.exasol.sql.expression.NullLiteral;

class CastExasolFunctionTest {
@Test
void testRendering() {
final Select select = StatementFactory.getInstance().select()
.function(CastExasolFunction.of(NullLiteral.nullLiteral(), new Varchar(254)));
assertThat(select, rendersTo("SELECT CAST(NULL AS VARCHAR(254))"));
}

@Test
void testRenderingWithName() {
final Select select = StatementFactory.getInstance().select()
.function(CastExasolFunction.of(NullLiteral.nullLiteral(), new Varchar(254)), "TEST");
assertThat(select, rendersTo("SELECT CAST(NULL AS VARCHAR(254)) TEST"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ExasolAnalyticFunctionTest {
void testAggregateFunctionCoalesce() {
final Select select = StatementFactory.getInstance().select() //
.field("department") //
.function(ExasolAnalyticFunctions.ANY, " ANY_ ", BooleanTerm.lt(column("age"), integerLiteral(30)));
.function(ExasolAnalyticFunction.ANY, " ANY_ ", BooleanTerm.lt(column("age"), integerLiteral(30)));
select.from().table("employee_table");
select.groupBy(column("department"));
assertThat(select,
Expand Down

0 comments on commit 1e4d903

Please sign in to comment.