diff --git a/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalQueryRewriter.java b/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalQueryRewriter.java index 08cf169..85a55ef 100644 --- a/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalQueryRewriter.java +++ b/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalQueryRewriter.java @@ -37,7 +37,7 @@ class ExasolLocalQueryRewriter implements QueryRewriter { public String rewrite(final SqlStatement statement, final List selectListDataTypes, final ExaMetadata exaMetadata, final AdapterProperties properties) throws AdapterException { final SqlGenerationContext context = new SqlGenerationContext(properties.getCatalogName(), - properties.getSchemaName(), false); + properties.getSchemaName(), true); final SqlGenerator sqlGeneratorVisitor = this.dialect.getSqlGenerator(context); final String selectStatement = sqlGeneratorVisitor.generateSqlFor(statement); LOGGER.finer(() -> "SELECT push-down statement:\n" + selectStatement); diff --git a/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalSqlGenerationVisitor.java b/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalSqlGenerationVisitor.java new file mode 100644 index 0000000..d5cc448 --- /dev/null +++ b/src/main/java/com/exasol/adapter/dialects/exasol/ExasolLocalSqlGenerationVisitor.java @@ -0,0 +1,40 @@ +package com.exasol.adapter.dialects.exasol; + +import com.exasol.adapter.AdapterException; +import com.exasol.adapter.dialects.SqlDialect; +import com.exasol.adapter.dialects.rewriting.SqlGenerationContext; +import com.exasol.adapter.metadata.DataType; +import com.exasol.adapter.sql.SqlColumn; + +public class ExasolLocalSqlGenerationVisitor extends ExasolSqlGenerationVisitor{ + /** + * Creates a new instance of the {@link ExasolSqlGenerationVisitor}. + * + * @param dialect {@link ExasolSqlDialect} dialect + * @param context SQL generation context + */ + ExasolLocalSqlGenerationVisitor(SqlDialect dialect, SqlGenerationContext context) { + super(dialect, context); + } + @Override + public String visit(final SqlColumn column) throws AdapterException { + ExasolSqlDialect exasolSqlDialect = new ExasolSqlDialect(null,null); + String tablePrefix = ""; + if (column.hasTableAlias()) { + tablePrefix = exasolSqlDialect.applyQuote(column.getTableAlias()) + + exasolSqlDialect.getTableCatalogAndSchemaSeparator(); + } else if ((column.getTableName() != null) && !column.getTableName().isEmpty()) { + tablePrefix = exasolSqlDialect.applyQuote(column.getTableName()) + + exasolSqlDialect.getTableCatalogAndSchemaSeparator(); + } + //PROVIDE A CAST TO CORRESPONDING DATATYPE WITH THE SAME LENGTH BUT WITH UTF8 AS CHARSET + if (column.getMetadata().getType().getExaDataType() == DataType.ExaDataType.CHAR || // + column.getMetadata().getType().getExaDataType() == DataType.ExaDataType.VARCHAR) { + int size = column.getMetadata().getType().getSize(); + String dataTypeStr = column.getMetadata().getType().getExaDataType() == DataType.ExaDataType.CHAR ? "CHAR":"VARCHAR"; + return "CAST(" + tablePrefix + exasolSqlDialect.applyQuote(column.getName())+" AS "+ dataTypeStr+"("+size+") UTF8)"; + } else { + return tablePrefix + exasolSqlDialect.applyQuote(column.getName()); + } + } +} diff --git a/src/main/java/com/exasol/adapter/dialects/exasol/ExasolSqlDialect.java b/src/main/java/com/exasol/adapter/dialects/exasol/ExasolSqlDialect.java index 8ebd0a9..78592d5 100644 --- a/src/main/java/com/exasol/adapter/dialects/exasol/ExasolSqlDialect.java +++ b/src/main/java/com/exasol/adapter/dialects/exasol/ExasolSqlDialect.java @@ -155,7 +155,12 @@ public String getStringLiteral(final String value) { @Override public SqlGenerator getSqlGenerator(final SqlGenerationContext context) { - return new ExasolSqlGenerationVisitor(this, context); + if (context.isLocal()){ + return new ExasolLocalSqlGenerationVisitor(this, context); + } else { + return new ExasolSqlGenerationVisitor(this, context); + } + } /**