-
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 |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
import io.cdap.cdap.api.data.schema.Schema; | ||
import io.cdap.plugin.common.db.DBUtils; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
@@ -29,7 +31,6 @@ | |
* Common schema reader for mapping non specific DB types. | ||
*/ | ||
public class CommonSchemaReader implements SchemaReader { | ||
|
||
@Override | ||
public List<Schema.Field> getSchemaFields(ResultSet resultSet) throws SQLException { | ||
List<Schema.Field> schemaFields = Lists.newArrayList(); | ||
|
@@ -61,4 +62,50 @@ public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLExcepti | |
public boolean shouldIgnoreColumn(ResultSetMetaData metadata, int index) throws SQLException { | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns the schema fields for the specified table using JDBC metadata. | ||
* Supports schema-qualified table names (e.g. "schema.table"). | ||
* Throws SQLException if the table has no columns. | ||
* | ||
* @param connection JDBC connection | ||
* @param tableName table name, optionally schema-qualified | ||
* @return list of schema fields | ||
* @throws SQLException if no columns found or on database error | ||
*/ | ||
@Override | ||
public List<Schema.Field> getSchemaFields(Connection connection, String tableName) throws SQLException { | ||
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. add javadoc whenever adding a public method 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. added javdoc |
||
DatabaseMetaData dbMetaData = connection.getMetaData(); | ||
String schema = null; | ||
String table = tableName; | ||
// Support schema-qualified table names like "schema.table" | ||
if (tableName != null && tableName.contains(".")) { | ||
String[] parts = tableName.split("\\.", 2); | ||
schema = parts[0]; | ||
table = parts[1]; | ||
} | ||
try (ResultSet columns = dbMetaData.getColumns(null, schema, table, null)) { | ||
List<Schema.Field> schemaFields = Lists.newArrayList(); | ||
while (columns.next()) { | ||
String columnName = columns.getString("COLUMN_NAME"); | ||
String typeName = columns.getString("TYPE_NAME"); | ||
int columnType = columns.getInt("DATA_TYPE"); | ||
int precision = columns.getInt("COLUMN_SIZE"); | ||
int scale = columns.getInt("DECIMAL_DIGITS"); | ||
int nullable = columns.getInt("NULLABLE"); | ||
|
||
Schema columnSchema = DBUtils.getSchema(typeName, columnType, precision, scale, columnName, true, true); | ||
if (nullable == DatabaseMetaData.columnNullable) { | ||
columnSchema = Schema.nullableOf(columnSchema); | ||
} | ||
Schema.Field field = Schema.Field.of(columnName, columnSchema); | ||
schemaFields.add(field); | ||
} | ||
if (schemaFields.isEmpty()) { | ||
throw new SQLException("No columns found for table: " + | ||
(schema != null ? schema + "." : "") + table); | ||
} | ||
return schemaFields; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,9 @@ | |
* Abstract Config for DB Specific Source plugin | ||
*/ | ||
public abstract class AbstractDBSpecificSourceConfig extends PluginConfig implements DatabaseSourceConfig { | ||
|
||
public static final String TABLE_NAME = "tableName"; | ||
public static final String IMPORT_QUERY = "importQuery"; | ||
public static final String PROPERTY_IMPORT_QUERY_TYPE = "importQueryType"; | ||
public static final String BOUNDING_QUERY = "boundingQuery"; | ||
public static final String SPLIT_BY = "splitBy"; | ||
public static final String NUM_SPLITS = "numSplits"; | ||
|
@@ -54,6 +55,19 @@ public abstract class AbstractDBSpecificSourceConfig extends PluginConfig implem | |
@Description(Constants.Reference.REFERENCE_NAME_DESCRIPTION) | ||
public String referenceName; | ||
|
||
@Name(PROPERTY_IMPORT_QUERY_TYPE) | ||
@Description("Whether to select Table Name or Import Query to extract the data.") | ||
@Macro | ||
@Nullable | ||
public String importQueryType; | ||
|
||
@Nullable | ||
@Name(TABLE_NAME) | ||
@Description("The name of the table to import data from. This can be used instead of specifying an import query.") | ||
@Macro | ||
protected String tableName; | ||
|
||
@Nullable | ||
@Name(IMPORT_QUERY) | ||
@Description("The SELECT query to use to import data from the specified table. " + | ||
"You can specify an arbitrary number of columns to import, or import all columns using *. " + | ||
|
@@ -103,10 +117,15 @@ public String getImportQuery() { | |
return cleanQuery(importQuery); | ||
} | ||
|
||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
public String getBoundingQuery() { | ||
return cleanQuery(boundingQuery); | ||
} | ||
|
||
|
||
public void validate(FailureCollector collector) { | ||
boolean hasOneSplit = false; | ||
if (!containsMacro(NUM_SPLITS) && numSplits != null) { | ||
|
@@ -125,16 +144,19 @@ public void validate(FailureCollector collector) { | |
TransactionIsolationLevel.validate(getTransactionIsolationLevel(), collector); | ||
} | ||
|
||
if (!containsMacro(IMPORT_QUERY) && Strings.isNullOrEmpty(importQuery)) { | ||
collector.addFailure("Import Query is empty.", "Specify the Import Query.") | ||
.withConfigProperty(IMPORT_QUERY); | ||
if ((!containsMacro(TABLE_NAME) && !containsMacro(IMPORT_QUERY)) && | ||
(Strings.isNullOrEmpty(tableName) && Strings.isNullOrEmpty(importQuery))) { | ||
collector.addFailure(" Import Query must be specified.", | ||
" Import Query, Can not be empty.") | ||
.withConfigProperty(IMPORT_QUERY); | ||
} | ||
|
||
if (!hasOneSplit && !containsMacro(IMPORT_QUERY) && !getImportQuery().contains("$CONDITIONS")) { | ||
collector.addFailure(String.format( | ||
"Import Query %s must contain the string '$CONDITIONS'. if Number of Splits is not set to 1.", importQuery), | ||
"Include '$CONDITIONS' in the Import Query") | ||
.withConfigProperty(IMPORT_QUERY); | ||
if (!Strings.isNullOrEmpty(importQuery) && | ||
(!hasOneSplit && !containsMacro(IMPORT_QUERY) && !getImportQuery().contains("$CONDITIONS"))) { | ||
collector.addFailure(String.format( | ||
"Import Query %s must contain the string '$CONDITIONS'. " + | ||
"if Number of Splits is not set to 1.", importQuery), | ||
"Include '$CONDITIONS' in the Import Query") | ||
.withConfigProperty(IMPORT_QUERY); | ||
} | ||
|
||
if (!hasOneSplit && !containsMacro(SPLIT_BY) && (splitBy == null || splitBy.isEmpty())) { | ||
|
@@ -177,8 +199,7 @@ public void validateSchema(Schema actualSchema, FailureCollector collector) { | |
actualField.getSchema().getNonNullable() : actualField.getSchema(); | ||
Schema expectedFieldSchema = field.getSchema().isNullable() ? | ||
field.getSchema().getNonNullable() : field.getSchema(); | ||
|
||
validateField(collector, field, actualFieldSchema, expectedFieldSchema); | ||
validateField(collector, field, actualFieldSchema, expectedFieldSchema); | ||
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. remove space 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. removed |
||
} | ||
} | ||
|
||
|
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.
remove empty line
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.
removed