Skip to content

Commit

Permalink
Minor code cleanup, Fix LGTM alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
cgivre committed Jul 7, 2022
1 parent 2817ce2 commit 4f35184
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public static Entry<Class, String> typify(String data) {
* @param data The unknown data string
* @param bool01 True, if you want 0/1 to be marked as boolean, false if not
* @param commonTypes Limit typifier to boolean, double, string, timestamp
* @param postfixFL: Allow Typifier to consider f/F/l/L postfixes for float and longs
* @param parseDates: Attempt to parse timestamps
* @return An Entry consisting of the object class and the original value as a String
* @param postfixFL Allow typifier to consider f/F/l/L postfixes for float and longs
* @param parseDates Attempt to parse timestamps
* @return An {@link Entry} consisting of the object class and the original value as a String
*/
public static Entry<Class, String> typify(String data,
boolean bool01,
Expand Down Expand Up @@ -170,24 +170,24 @@ public static Entry<Class, String> typify(String data,
if (!commonTypes) {
// 1. Check if data is a Byte (1-byte integer with range [-(2e7) = -128, ((2e7)-1) = 127])
try {
Byte b = Byte.parseByte(s);
return new SimpleEntry<>(Byte.class, b.toString());
byte b = Byte.parseByte(s);
return new SimpleEntry<>(Byte.class, Byte.toString(b));
} catch (NumberFormatException ex) {
// Okay, guess it's not a Byte
}

// 2. Check if data is a Short (2-byte integer with range [-(2e15) = -32768, ((2e15)-1) = 32767])
try {
Short h = Short.parseShort(s);
return new SimpleEntry<>(Short.class, h.toString());
short h = Short.parseShort(s);
return new SimpleEntry<>(Short.class, Short.toString(h));
} catch (NumberFormatException ex) {
// Okay, guess it's not a Short
}

// 3. Check if data is an Integer (4-byte integer with range [-(2e31), (2e31)-1])
try {
Integer i = Integer.parseInt(s);
return new SimpleEntry<>(Integer.class, i.toString());
int i = Integer.parseInt(s);
return new SimpleEntry<>(Integer.class, Integer.toString(i));
} catch (NumberFormatException ex) {
// okay, guess it's not an Integer
}
Expand All @@ -201,19 +201,19 @@ public static Entry<Class, String> typify(String data,
}

try {
Long l = Long.parseLong(s_L_trimmed);
return new SimpleEntry<>(Long.class, l.toString());
long l = Long.parseLong(s_L_trimmed);
return new SimpleEntry<>(Long.class, Long.toString(l));
} catch (NumberFormatException ex) {
// okay, guess it's not a Long
}

// 5. Check if data is a Float (32-bit IEEE 754 floating point with approximate extents +/- 3.4028235e38)
if (postfixFL || !lastCharF) {
try {
Float f = Float.parseFloat(s);
float f = Float.parseFloat(s);
// If it's beyond the range of Float, maybe it's not beyond the range of Double
if (!f.isInfinite()) {
return new SimpleEntry<>(Float.class, f.toString());
if (!Float.isInfinite(f)) {
return new SimpleEntry<>(Float.class, Float.toString(f));
}
} catch (NumberFormatException ex) {
// okay, guess it's not a Float
Expand All @@ -224,9 +224,9 @@ public static Entry<Class, String> typify(String data,
// 6. Check if data is a Double (64-bit IEEE 754 floating point with approximate extents +/- 1.797693134862315e308 )
if (postfixFL || !lastCharF) {
try {
Double d = Double.parseDouble(s);
if (!d.isInfinite()) {
return new SimpleEntry<>(Double.class, d.toString());
double d = Double.parseDouble(s);
if (!Double.isInfinite(d)) {
return new SimpleEntry<>(Double.class, Double.toString(d));
} else {
return new SimpleEntry<>(String.class, s);
}
Expand Down
5 changes: 5 additions & 0 deletions contrib/storage-googlesheets/src/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Testing Procedures for Google Sheets Plugin
The GS plugin is a little tricky to test because it makes extensive use of the Google APIs. The plugin is designed to make extensive use of static functions in the `GoogleSheetsUtils` class which can be tested without a live connection to Google Sheets.



Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,41 @@

package org.apache.drill.exec.store.googlesheets;

import org.apache.drill.exec.oauth.PersistentTokenTable;
import org.apache.drill.exec.store.StoragePluginRegistry;
import org.apache.drill.exec.store.StoragePluginRegistry.PluginException;
import org.apache.drill.test.ClusterFixture;
import org.apache.drill.test.ClusterTest;
import org.apache.drill.test.QueryBuilder.QuerySummary;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@Ignore("These tests require a live Google Sheets connection. Please run manually.")
public class TestGoogleSheetsWriter extends ClusterTest {
private static final String CLIENT_ID = "632163729406-qpj19bbcgn95ve7tlgof9sj5rckpjjng.apps.googleusercontent.com";
private static final String CLIENT_SECRET = "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";
private static final String AUTH_URI = "https://accounts.google.com/o/oauth2/auth";
private static final String TOKEN_URI = "https://oauth2.googleapis.com/token";
private static final String ACCESS_TOKEN = "ya29.a0ARrdaM-Q7c5x7pQngU4iXzMwepg48QOiBRd3jbUR6V6MKdZ94bn2drcOKFR0c3Xm6iaUKfE9MevWXIZSNq-a_25uKcY0hAbozypgd_XLzHZ_J__1Jmm5gNDfLYXrY5TqblPHmhk3t88pK8Z5nhHmLSgkEwKu";
private static final String REFRESH_TOKEN = "1//0doUy8GkHiYkxCgYIARAAGA0SNwF-L9IrMk_gaXNddLvtkcc6wNkpF9W_tyKRAkccISo6N7NVjkgulDkir9j8Y9gzAhAZjFO82M0";
private static final List<String> REDIRECT_URI = new ArrayList<>(Arrays.asList("urn:ietf:wg:oauth:2.0:oob", "http://localhost"));

private static StoragePluginRegistry pluginRegistry;


@BeforeClass
public static void init() throws Exception {
startCluster(ClusterFixture.builder(dirTestWatcher));

StoragePluginRegistry pluginRegistry = cluster.drillbit().getContext().getStorage();
pluginRegistry = cluster.drillbit().getContext().getStorage();
GoogleSheetsStoragePluginConfig config = GoogleSheetsStoragePluginConfig.builder()
.clientID(CLIENT_ID)
.clientSecret(CLIENT_SECRET)
Expand All @@ -58,10 +67,30 @@ public static void init() throws Exception {

@Test
public void testBasicCTAS() throws Exception {
try {
initializeTokens();
} catch (PluginException e) {
fail(e.getMessage());
}

String query = "CREATE TABLE googlesheets.`test_sheet`.`test_table` (ID, NAME) AS " +
"SELECT * FROM (VALUES(1,2), (3,4))";
// Create the table and insert the values
QuerySummary insertResults = queryBuilder().sql(query).run();
assertTrue(insertResults.succeeded());
}

/**
* This function is used for testing only. It initializes a {@link PersistentTokenTable} and populates it
* with a valid access and refresh token.
* @throws PluginException If anything goes wrong
*/
private void initializeTokens() throws PluginException {
GoogleSheetsStoragePlugin plugin = (GoogleSheetsStoragePlugin) pluginRegistry.getPlugin("googlesheets");
plugin.initializeTokenTableForTesting();
PersistentTokenTable tokenTable = plugin.getTokenTable();
tokenTable.setAccessToken(ACCESS_TOKEN);
tokenTable.setRefreshToken(REFRESH_TOKEN);
tokenTable.setExpiresIn("50000");
}
}

0 comments on commit 4f35184

Please sign in to comment.