-
Notifications
You must be signed in to change notification settings - Fork 34
Patch/abhi tablename : Adding import query type property and modify tableName property for redshift and postgres plugin #607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
import io.cdap.plugin.db.CommonSchemaReader; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
@@ -56,34 +55,12 @@ public RedshiftSchemaReader(String sessionID) { | |
public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLException { | ||
String typeName = metadata.getColumnTypeName(index); | ||
int columnType = metadata.getColumnType(index); | ||
int precision = metadata.getPrecision(index); | ||
String columnName = metadata.getColumnName(index); | ||
int scale = metadata.getScale(index); | ||
boolean isSigned = metadata.isSigned(index); | ||
|
||
if (STRING_MAPPED_REDSHIFT_TYPES_NAMES.contains(typeName)) { | ||
return Schema.of(Schema.Type.STRING); | ||
} | ||
if (typeName.equalsIgnoreCase("INT")) { | ||
return Schema.of(Schema.Type.INT); | ||
} | ||
if (typeName.equalsIgnoreCase("BIGINT")) { | ||
return Schema.of(Schema.Type.LONG); | ||
} | ||
|
||
// If it is a numeric type without precision then use the Schema of String to avoid any precision loss | ||
if (Types.NUMERIC == columnType) { | ||
int precision = metadata.getPrecision(index); | ||
if (precision == 0) { | ||
LOG.warn(String.format("Field '%s' is a %s type without precision and scale, " | ||
+ "converting into STRING type to avoid any precision loss.", | ||
metadata.getColumnName(index), | ||
metadata.getColumnTypeName(index))); | ||
return Schema.of(Schema.Type.STRING); | ||
} | ||
} | ||
|
||
if (typeName.equalsIgnoreCase("timestamp")) { | ||
return Schema.of(Schema.LogicalType.DATETIME); | ||
} | ||
|
||
return super.getSchema(metadata, index); | ||
return getSchema(typeName, columnType, precision, scale, columnName, isSigned, true); | ||
} | ||
|
||
@Override | ||
|
@@ -114,4 +91,45 @@ public List<Schema.Field> getSchemaFields(ResultSet resultSet) throws SQLExcepti | |
return schemaFields; | ||
} | ||
|
||
/** | ||
* Returns the CDAP {@link Schema} for a database column based on JDBC metadata. | ||
* Handles Redshift-specific and common JDBC types: | ||
* Maps Redshift string types to {@link Schema.Type#STRING} | ||
* Maps "INT" to {@link Schema.Type#INT} | ||
* Maps "BIGINT" to {@link Schema.Type#LONG}. | ||
* Maps NUMERIC with zero precision to {@link Schema.Type#STRING} and logs a warning. | ||
* Maps "timestamp" to {@link Schema.LogicalType#DATETIME}. | ||
* Delegates to the parent plugin for all other types. | ||
* @param typeName SQL type name (e.g. "INT", "BIGINT", "timestamp") | ||
* @param columnType JDBC type code (see {@link java.sql.Types}) | ||
* @param precision column precision (for numeric types) | ||
* @param scale column scale (for numeric types) | ||
* @param columnName column name | ||
* @param isSigned whether the column is signed | ||
* @param handleAsDecimal whether to handle as decimal | ||
* @return the mapped {@link Schema} type | ||
*/ | ||
@Override | ||
public Schema getSchema(String typeName, int columnType, int precision, int scale, String columnName, | ||
boolean isSigned, boolean handleAsDecimal) { | ||
if (STRING_MAPPED_REDSHIFT_TYPES_NAMES.contains(typeName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code is getting repeated from public Schema getSchema(ResultSetMetaData metadata, int index) method, extract that common code, do the same in postgres |
||
return Schema.of(Schema.Type.STRING); | ||
} | ||
if ("INT".equalsIgnoreCase(typeName)) { | ||
return Schema.of(Schema.Type.INT); | ||
} | ||
if ("BIGINT".equalsIgnoreCase(typeName)) { | ||
return Schema.of(Schema.Type.LONG); | ||
} | ||
if (Types.NUMERIC == columnType && precision == 0) { | ||
LOG.warn(String.format("Field '%s' is a %s type without precision and scale," + | ||
" converting into STRING type to avoid any precision loss.", | ||
columnName, typeName)); | ||
return Schema.of(Schema.Type.STRING); | ||
} | ||
if ("timestamp".equalsIgnoreCase(typeName)) { | ||
return Schema.of(Schema.LogicalType.DATETIME); | ||
} | ||
return super.getSchema(typeName, columnType, precision, scale, columnName, isSigned, handleAsDecimal); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,16 @@ | |
package io.cdap.plugin.amazon.redshift; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Strings; | ||
import io.cdap.cdap.api.annotation.Description; | ||
import io.cdap.cdap.api.annotation.Macro; | ||
import io.cdap.cdap.api.annotation.Metadata; | ||
import io.cdap.cdap.api.annotation.MetadataProperty; | ||
import io.cdap.cdap.api.annotation.Name; | ||
import io.cdap.cdap.api.annotation.Plugin; | ||
import io.cdap.cdap.etl.api.FailureCollector; | ||
import io.cdap.cdap.etl.api.PipelineConfigurer; | ||
import io.cdap.cdap.etl.api.StageConfigurer; | ||
import io.cdap.cdap.etl.api.batch.BatchSource; | ||
import io.cdap.cdap.etl.api.batch.BatchSourceContext; | ||
import io.cdap.cdap.etl.api.connector.Connector; | ||
|
@@ -34,12 +37,17 @@ | |
import io.cdap.plugin.db.config.AbstractDBSpecificSourceConfig; | ||
import io.cdap.plugin.db.source.AbstractDBSource; | ||
import io.cdap.plugin.util.DBUtils; | ||
import io.cdap.plugin.util.ImportQueryType; | ||
import org.apache.hadoop.mapreduce.lib.db.DBWritable; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
import static io.cdap.plugin.db.config.AbstractDBSpecificSourceConfig.IMPORT_QUERY; | ||
import static io.cdap.plugin.db.config.AbstractDBSpecificSourceConfig.PROPERTY_IMPORT_QUERY_TYPE; | ||
import static io.cdap.plugin.db.config.AbstractDBSpecificSourceConfig.TABLE_NAME; | ||
|
||
/** | ||
* Batch source to read from an Amazon Redshift database. | ||
*/ | ||
|
@@ -59,6 +67,30 @@ public RedshiftSource(RedshiftSourceConfig redshiftSourceConfig) { | |
this.redshiftSourceConfig = redshiftSourceConfig; | ||
} | ||
|
||
@Override | ||
public void configurePipeline(PipelineConfigurer pipelineConfigurer) { | ||
FailureCollector collector = pipelineConfigurer.getStageConfigurer().getFailureCollector(); | ||
StageConfigurer stageConfigurer = pipelineConfigurer.getStageConfigurer(); | ||
if (sourceConfig.containsMacro(TABLE_NAME) || sourceConfig.containsMacro(IMPORT_QUERY)) { | ||
if (sourceConfig.getSchema() != null) { | ||
stageConfigurer.setOutputSchema(sourceConfig.getSchema()); | ||
} | ||
return; | ||
} | ||
validateTableNameAndImportQuery(collector); | ||
super.configurePipeline(pipelineConfigurer); | ||
} | ||
|
||
@Override | ||
public void prepareRun(BatchSourceContext context) throws Exception { | ||
FailureCollector collector = context.getFailureCollector(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this common logic of configurePipeline and prepareRun to its parent class as a separate method and call the same from postgres also |
||
if (sourceConfig.containsMacro(TABLE_NAME) || sourceConfig.containsMacro(IMPORT_QUERY)) { | ||
return; | ||
} | ||
validateTableNameAndImportQuery(collector); | ||
super.prepareRun(context); | ||
} | ||
|
||
@Override | ||
protected SchemaReader getSchemaReader() { | ||
return new RedshiftSchemaReader(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is there in parent class, you can use that one, instead you can call getSchema method of this class inside
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no update on this, also javadoc not updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update javadoc